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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file promptbuilder-0.4.12.tar.gz.
File metadata
- Download URL: promptbuilder-0.4.12.tar.gz
- Upload date:
- Size: 30.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80be4615696646f173bcd165b4c5021da4661d5a12f346beccd7834e211f8178
|
|
| MD5 |
e3fe0bb6b943836f10db78f5c7e0585e
|
|
| BLAKE2b-256 |
99fd789354e14e83fc4f7f5cf769cd7eb3ba279372e0624e3dd5c47cfdca5e75
|
File details
Details for the file promptbuilder-0.4.12-py3-none-any.whl.
File metadata
- Download URL: promptbuilder-0.4.12-py3-none-any.whl
- Upload date:
- Size: 35.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe3a4cdc618a85a5b64d2f7afdcbfda3afa3b983a7646d4cbf93cc82527874f9
|
|
| MD5 |
2cf555790cad9b35b11a7b12ab2f2333
|
|
| BLAKE2b-256 |
af6030c77720f0ed448ef74515f770418f0bd9feccb59924c3f9197c3e565dc9
|