Skip to main content

Generative AI tools from ToolForge-AI organization

Project description

genai-forge

Lightweight utilities to call LLMs and parse their outputs with Pydantic.

Installation

Install from PyPI:

pip install genai-forge

Requirements

  • Python 3.10+
  • An OpenAI API key in your environment:
    • Create a .env file at your project root with:
OPENAI_API_KEY=sk-...

genai_forge will load .env automatically via python-dotenv when using the OpenAI provider.

Quick start

from genai_forge.llm import create_llm
from genai_forge.prompting import PromptTemplate

template = PromptTemplate(
    system="You are a concise expert assistant.",
    template="Generate one actionable tip.\nAudience: {audience}\nTime: {time}",
)

llm = create_llm("openai:gpt-4o-mini", temperature=0.2)

# Chain: query | template | llm
query = "Provide a short productivity tip."
chain = query | template | llm
print(chain({"audience": "Backend Python developer", "time": "30 minutes"}))

Parsing structured outputs with Pydantic

Use PydanticOutputParser to have the LLM return valid JSON that is validated into a Pydantic model. When you add a parser to the chain, the parser’s format instructions are automatically appended to the prompt; you do not need to place {instructions} in your template.

from typing import List
from pydantic import BaseModel
from genai_forge.llm import create_llm
from genai_forge import PydanticOutputParser
from genai_forge.prompting import PromptTemplate

class CityPlan(BaseModel):
    city: str
    attractions: List[str]
    days: int

template = PromptTemplate(
    system="You are a helpful travel planner.",
    template="Create a city plan.\nCity: {city}\nDays: {days}",
)

llm = create_llm("openai:gpt-4o-mini", temperature=0.1)
parser = PydanticOutputParser(CityPlan)

# Chain: query | template | llm | parser
query = "Create a 2-day city plan for Kyoto."
chain = query | template | llm | parser
result = chain({"city": "Kyoto", "days": 2})  # -> CityPlan
print(result)

What chaining does

The | operator builds a Chain:

  • template | llm produces a function that formats the prompt (system + user) and calls the LLM.
  • template | llm | parser also injects the parser's format instructions automatically into the prompt and validates the output.

Output parser: how it works

PydanticOutputParser:

  • Accepts tolerant output formats (e.g., extra text or ```json fences).
  • Extracts JSON from the text (tries direct parse, then finds the first balanced {...} block).
  • Validates the JSON against your Pydantic model.
  • Provides get_format_instructions(); the Chain appends it for you when a parser is present.

API surface:

from genai_forge import PydanticOutputParser, BaseOutputParser, OutputParserException

Key methods:

  • get_format_instructions() -> str: Include in your prompt.
  • parse(text: str) -> BaseModel: Parse and validate output.

Provider configuration

By default, models without an explicit provider use openai. You can specify:

create_llm("openai:gpt-4o-mini")
# or
create_llm("gpt-4.1", provider="openai")

The OpenAI provider reads the key from OPENAI_API_KEY (or api_key argument).

Running the example

An example.py is included at the repo root. It demonstrates:

  • A chain: user input → PromptTemplate (with system prompt) → LLM → PydanticOutputParser
  • Automatic format instructions injection

Ensure you have a .env with OPENAI_API_KEY, then:

python example.py

License

See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

genai_forge-0.1.13.tar.gz (29.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

genai_forge-0.1.13-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file genai_forge-0.1.13.tar.gz.

File metadata

  • Download URL: genai_forge-0.1.13.tar.gz
  • Upload date:
  • Size: 29.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for genai_forge-0.1.13.tar.gz
Algorithm Hash digest
SHA256 573540a9fcd2c1fb83bda4ebf639b3efdc7142090231f18a52a19db1e5290f1f
MD5 4b19566ce6ee43fbad5b93c8098a12a5
BLAKE2b-256 bebc6f9ab4a1cd404ed5ee39ecfa6a02d0806492f43370b1c0c94e6c2be06216

See more details on using hashes here.

Provenance

The following attestation bundles were made for genai_forge-0.1.13.tar.gz:

Publisher: release.yml on ToolForge-AI/genai-forge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file genai_forge-0.1.13-py3-none-any.whl.

File metadata

  • Download URL: genai_forge-0.1.13-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for genai_forge-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 8b0b05d6fa6a73c2bd4a6afcd69b129ee47ee22d1c74fe2f533b5a1c7d929f3d
MD5 0fe47c9bb8d7ff87b21fda151fb637d5
BLAKE2b-256 552a674e03eeeedf0ed4c34e74e63e984f0514ef8d8f22e780c088caaa7d2918

See more details on using hashes here.

Provenance

The following attestation bundles were made for genai_forge-0.1.13-py3-none-any.whl:

Publisher: release.yml on ToolForge-AI/genai-forge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page