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
from promptbuilder.agent.message import Message
from promptbuilder.llm_client import LLMClient

# Define tool arguments
class TodoItem(BaseModel):
    description: str = Field(..., description="Description of the todo item")

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

# Create custom context
class TodoListContext(Context):
    todos: List[TodoItem] = []

# Create agent with tools
class TodoListAgent(AgentRouter[TodoListContext]):
    def __init__(self, llm_client: LLMClient, context: TodoListContext):
        super().__init__(llm_client=llm_client, context=context)
        
        # Register tools
        self.tool(
            description="Add a new todo item to the list",
            args_model=AddTodoArgs
        )(self.add_todo)
    
    async def add_todo(self, 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():
    llm_client = LLMClient(model="your-model", api_key="your-api-key")
    agent = TodoListAgent(llm_client=llm_client, context=TodoListContext())
    
    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

This version

0.2.3

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.2.3.tar.gz (9.6 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.2.3-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: promptbuilder-0.2.3.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for promptbuilder-0.2.3.tar.gz
Algorithm Hash digest
SHA256 2fa7442b3d29a8ccd1ab9c852d08fa34d66bd5b82972c81e2a6a6028c01c9c72
MD5 e19f77704678bb28e3a788f46f4fde3a
BLAKE2b-256 02b9181abaaaf260001383433c8a9234a96d9acf0b65e728e0df73f6d2c588b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: promptbuilder-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for promptbuilder-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 62ce830787d6a3893a5608e419e5cea013f3fa6e048f4b89251f1b2d5d33e890
MD5 a46ebca2d52e1de0357b1da801173f8f
BLAKE2b-256 f2a7b4d6c152766e625d066265880ca3c076bedc14d0db65343a7607f910e203

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