Agent runtime with tool calling and state management
Project description
pig-agent-core
Agent runtime with tool calling and state management.
Features
- 🤖 Agent Runtime: Base agent class with lifecycle management
- 🔧 Tool System: Decorator-based tool registration
- 💾 State Management: Conversation history and context
- 🔄 Async Support: Full async/await support
- 📝 Message History: Automatic conversation tracking
- 🎯 Tool Execution: Safe tool calling with validation
Installation
pip install pig-agent-core
Quick Start
Define Tools
from pig_agent_core import Agent, tool
@tool(description="Get current weather for a location")
def get_weather(location: str) -> str:
"""Get weather information."""
return f"Weather in {location}: Sunny, 72°F"
@tool(description="Calculate result of mathematical expression")
def calculate(expression: str) -> float:
"""Safely evaluate math expression."""
return eval(expression) # In production, use ast.literal_eval
Create and Run Agent
from pig_llm import LLM
# Create agent with tools
agent = Agent(
name="WeatherBot",
llm=LLM(provider="openai"),
tools=[get_weather, calculate],
system_prompt="You are a helpful weather assistant."
)
# Run agent
response = agent.run("What's the weather in Paris?")
print(response.content)
# Agent automatically:
# 1. Receives user message
# 2. Calls get_weather("Paris") if needed
# 3. Returns formatted response
Conversation with History
# Multi-turn conversation
agent.run("What's the weather in Tokyo?")
agent.run("What about New York?")
agent.run("Which one is warmer?")
# View history
for msg in agent.history:
print(f"{msg.role}: {msg.content}")
Async Usage
import asyncio
async def main():
response = await agent.arun("Check weather in London")
print(response.content)
asyncio.run(main())
Advanced Usage
Custom Tool Parameters
from pydantic import BaseModel, Field
class WeatherParams(BaseModel):
location: str = Field(description="City name")
units: str = Field(default="fahrenheit", description="Temperature units")
@tool(params_model=WeatherParams)
def get_weather_detailed(location: str, units: str = "fahrenheit") -> str:
"""Get detailed weather with custom units."""
temp = 72 if units == "fahrenheit" else 22
return f"Weather in {location}: Sunny, {temp}°{units[0].upper()}"
State Management
# Save and restore state
state = agent.get_state()
agent.save_state("conversation.json")
# Later...
agent = Agent.from_state("conversation.json")
agent.run("Continue our conversation")
Tool Callbacks
def on_tool_start(tool_name: str, args: dict):
print(f"Starting {tool_name} with {args}")
def on_tool_end(tool_name: str, result: any):
print(f"Finished {tool_name}: {result}")
agent = Agent(
llm=llm,
tools=[get_weather],
on_tool_start=on_tool_start,
on_tool_end=on_tool_end,
)
Architecture
Agent
├── LLM Client (pig-llm)
├── Tool Registry
├── Message History
├── State Manager
└── Execution Loop
Tool System
Tools are Python functions decorated with @tool:
@tool(
name="custom_name", # Optional: defaults to function name
description="What it does", # Required for LLM understanding
params_model=ParamsModel, # Optional: Pydantic model for params
)
def my_tool(arg1: str, arg2: int = 10) -> str:
"""Function that the agent can call."""
return f"Result: {arg1} * {arg2}"
The tool decorator:
- Validates parameters using type hints or Pydantic models
- Generates JSON schema for LLM function calling
- Handles execution and error catching
- Tracks usage statistics
Examples
See examples/ directory:
basic_agent.py- Simple agent usagetools_demo.py- Tool system demonstrationasync_agent.py- Async agent examplestateful_agent.py- State management
License
MIT
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
pig_agent_core-0.0.3.tar.gz
(34.0 kB
view details)
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 pig_agent_core-0.0.3.tar.gz.
File metadata
- Download URL: pig_agent_core-0.0.3.tar.gz
- Upload date:
- Size: 34.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
727c8e014da282863402542671867f5fc04857cfc4d9bad19ecb9a9f898f41fa
|
|
| MD5 |
0541f15969ecfe7c521bcb0b0272350d
|
|
| BLAKE2b-256 |
5aebbf34395b0d5a144fe1e5b7422ec9c882be9b4ff95428efb81a59a5dda366
|
File details
Details for the file pig_agent_core-0.0.3-py3-none-any.whl.
File metadata
- Download URL: pig_agent_core-0.0.3-py3-none-any.whl
- Upload date:
- Size: 31.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
226acfd853bdff1fc56aa43aec821f99a983ff98f1cb85c15317691ca8a06784
|
|
| MD5 |
48674a8514fba8d881ce30a6c2e7b555
|
|
| BLAKE2b-256 |
1ce1f822064a49dc8f770b33a86247e1b9a4f9cb9527e5c9948cdfd92d8e2cf1
|