from praisonaiagents import Agent
agent = Agent(
name="Assistant",
instructions="You are a helpful AI assistant that provides concise, accurate answers."
)
response = agent.start("What is the capital of France?")
print(response)
Agent with Tools
python
from praisonaiagents import Agent, tool
@tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"Weather in {city}: Sunny, 22°C"
agent = Agent(
name="Weather Assistant",
instructions="Help users check the weather",
tools=[get_weather]
)
response = agent.start("What's the weather in Paris?")
Agent with Handoffs
python
from praisonaiagents import Agent
billing_agent = Agent(name="Billing", instructions="Handle billing inquiries")
tech_agent = Agent(name="Tech Support", instructions="Solve technical issues")
main_agent = Agent(
name="Customer Service",
instructions="Route customers to the right department",
handoffs=[billing_agent, tech_agent]
)
response = main_agent.start("I have a billing question")
Agent with Knowledge Base
python
from praisonaiagents import Agent
agent = Agent(
name="Research Assistant",
instructions="Answer questions using the knowledge base",
knowledge=["research_papers/", "data.pdf"]
)
response = agent.start("Summarize the key findings")
Agent with Streaming
Choose the right method based on your use case:
python
from praisonaiagents import Agent
agent = Agent(
name="Story Writer",
instructions="Write creative stories"
)
# Method 1: start() - streams automatically in terminal (TTY)
# Best for interactive/beginner use
for chunk in agent.start("Write a short story"):
print(chunk, end="", flush=True)
# Method 2: iter_stream() - always streams, no display
# Best for app integration
full_response = ""
for chunk in agent.iter_stream("Write a short story"):
full_response += chunk
# Custom processing here
# Method 3: run() - silent, returns result directly
# Best for production/scripted use
result = agent.run("Write a short story")
print(result)
# Method 2: Use any preset + stream=True in start() (per-call control)
agent = Agent(
name="Story Writer",
instructions="Write creative stories",
output="verbose" # Any preset
)
for chunk in agent.start("Write a short story", stream=True):
print(chunk, end="", flush=True)
# Method 3: Silent streaming (no verbose output, clean stream)
agent = Agent(
name="Writer",
instructions="You are concise",
output="silent"
)
for chunk in agent.start("Write a haiku", stream=True):
print(chunk, end="", flush=True)
# Method 4: Collect full response while streaming
chunks = []
for chunk in agent.start("List 3 tips"):
chunks.append(chunk)
print(chunk, end="", flush=True)
full_response = "".join(chunks)
Agent with Custom LLM
python
from praisonaiagents import Agent
# Using Ollama
agent = Agent(
name="Local Assistant",
instructions="You are a helpful assistant",
llm="ollama/llama3",
base_url="http://localhost:11434"
)
# Using Anthropic
agent = Agent(
name="Claude Assistant",
instructions="You are a helpful assistant",
llm="anthropic/claude-3-sonnet-20240229"
)
Agent with Consolidated Config
python
from praisonaiagents import Agent
agent = Agent(
name="Advanced Agent",
instructions="You are an advanced assistant",
output="verbose", # Output preset
execution="thorough", # Execution preset
memory=True, # Enable memory with defaults
planning=True, # Enable planning mode
reflection=True, # Enable self-reflection
)
Async Support
The Agent class provides full async support:
python
import asyncio
from praisonaiagents import Agent
async def main():
agent = Agent(
name="AsyncAgent",
instructions="Handle async operations efficiently"
)
# Async chat
result = await agent.achat("Process this request")
print(result)
# Async start
result = await agent.astart("Another request")
print(result)
asyncio.run(main())
Multi-Agent Safe
Agents are designed to be multi-agent safe. Each agent maintains its own:
Chat history
Memory instance
Knowledge base
Session state
Multiple agents can run concurrently without interference.