Skip to main content

Library for building prompts for LLMs

Project description

Prompt Builder

Library for building prompts and agents with LLMs.

Installation

From PyPI:

pip install promptbuilder

From source:

git clone https://github.com/kapulkin/promptbuilder.git
cd promptbuilder
pip install -e .

Features

  • Prompt templates with variables and content tags
  • Structured output with TypeScript-like schema definition
  • LLM client with native structured output support and caching option
  • Integration with multiple LLM providers through aisuite
  • Agents with routing based on tools
  • Tools as agent for flexibility and scalability

Quick Start

Basic Prompt Usage

from promptbuilder.llm_client import LLMClient
from promptbuilder.prompt_builder import PromptBuilder

# Build prompt template
prompt_template = PromptBuilder() \
    .text("What is the capital of ").variable("country").text("?") \
    .build()

# Use with LLM
llm_client = LLMClient(model="your-model", api_key="your-api-key")
response = llm_client.from_text(
    prompt_template.render(country="France")
)
print(response)

Using Agents

from typing import List
from pydantic import BaseModel, Field
from promptbuilder.agent.agent import AgentRouter
from promptbuilder.agent.context import Context, InMemoryDialogHistory
from promptbuilder.agent.message import Message
from promptbuilder.llm_client import LLMClient

# Define tool arguments
class AddTodoArgs(BaseModel):
    item: TodoItem = Field(..., description="Todo item to add")

# Create custom context
class TodoItem(BaseModel):
    description: str = Field(..., description="Description of the todo item")

class TodoListContext(Context[InMemoryDialogHistory]):
    todos: List[TodoItem] = []

# Create agent with tools
class TodoListAgent(AgentRouter[InMemoryDialogHistory, TodoListContext]):
    def __init__(self, llm_client: LLMClient, context: TodoListContext):
        super().__init__(llm_client=llm_client, context=context)
    
llm_client = LLMClient(model="your-model", api_key="your-api-key")
agent = TodoListAgent(llm_client=llm_client, context=TodoListContext())

@agent.tool(description="Add a new todo item to the list", args_model=AddTodoArgs)
async def add_todo(message: Message, args: AddTodoArgs, context: TodoListContext) -> str:
    context.todos.append(args.item)
    return f"Added todo item: {args.item.description}"

# Use the agent
async def main():
    response = await agent(Message(role="user", content="Add a todo: Buy groceries"))
    print(response)

See the examples directory for more detailed examples, including a complete todo list manager.

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a pull request.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

promptbuilder-0.4.42.tar.gz (49.4 kB view details)

Uploaded Source

Built Distribution

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

promptbuilder-0.4.42-py3-none-any.whl (50.4 kB view details)

Uploaded Python 3

File details

Details for the file promptbuilder-0.4.42.tar.gz.

File metadata

  • Download URL: promptbuilder-0.4.42.tar.gz
  • Upload date:
  • Size: 49.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for promptbuilder-0.4.42.tar.gz
Algorithm Hash digest
SHA256 a6d9a8ade4e39b684f312a9eb81e4f851baf676f7d56d070350e8a5dd22894cb
MD5 b2170a78b1c96c78df1c38cd40da5449
BLAKE2b-256 4214edd09f01ba95a79e447f4a1674e2f1bc1ddf49654fc7f7b257f674a07a0c

See more details on using hashes here.

File details

Details for the file promptbuilder-0.4.42-py3-none-any.whl.

File metadata

  • Download URL: promptbuilder-0.4.42-py3-none-any.whl
  • Upload date:
  • Size: 50.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for promptbuilder-0.4.42-py3-none-any.whl
Algorithm Hash digest
SHA256 360a7967073dc5886ffcc00ceb0aaf0c533a089f92b3ed95ba5d50c1b2c749f2
MD5 abf3b6cb76f14a444ec5c9b5efc41d0d
BLAKE2b-256 ee20ea6c24c3567c5c8bed5451db3c7460eb5e9605d0c096428e36368e8eadfd

See more details on using hashes here.

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