Agent
模板字符串输出
python
from utils import new_model
from datetime import datetime
from textwrap import dedent
from agno.agent import Agent
today = datetime.now().strftime("%Y-%m-%d")
agent = Agent(
model=new_model(id="gpt-4o-mini"),
description=dedent(
"""\
You are Professor X-1000, a distinguished AI research scientist with expertise
in analyzing and synthesizing complex information. Your specialty lies in creating
compelling, fact-based reports that combine academic rigor with engaging narrative.
Your writing style is:
- Clear and authoritative
- Engaging but professional
- Fact-focused with proper citations
- Accessible to educated non-specialists\
"""
),
instructions=dedent(
"""\
Begin by running 3 distinct searches to gather comprehensive information.
Analyze and cross-reference sources for accuracy and relevance.
Structure your report following academic standards but maintain readability.
Include only verifiable facts with proper citations.
Create an engaging narrative that guides the reader through complex topics.
End with actionable takeaways and future implications.\
"""
),
expected_output=dedent(
"""\
A professional research report in markdown format:
# {Compelling Title That Captures the Topic's Essence}
## Executive Summary
{Brief overview of key findings and significance}
## Introduction
{Context and importance of the topic}
{Current state of research/discussion}
## Key Findings
{Major discoveries or developments}
{Supporting evidence and analysis}
## Implications
{Impact on field/society}
{Future directions}
## Key Takeaways
- {Bullet point 1}
- {Bullet point 2}
- {Bullet point 3}
## References
- [Source 1](link) - Key finding/quote
- [Source 2](link) - Key finding/quote
- [Source 3](link) - Key finding/quote
---
Report generated by Professor X-1000
Advanced Research Systems Division
Date: {current_date}\
"""
),
markdown=True,
show_tool_calls=True,
add_datetime_to_instructions=True,
)
# Example usage
def test():
# Generate a research report on a cutting-edge topic
agent.print_response(
"Research the latest developments in brain-computer interfaces", stream=True
)
使用{}插值. 并不是强制的, 只是会附加到system_message后面.
多轮对话
python
from utils import new_model
from agno.agent import Agent
def test():
agent = Agent(
model=new_model("gpt-4o-mini"),
add_history_to_messages=True,
num_history_runs=3,
)
session1_id = 'session1'
session2_id = 'session2'
agent.print_response("这是我的第几句话?", session_id=session1_id)
agent.print_response("这是我的第几句话?", session_id=session1_id)
agent.print_response("这是我的第几句话?", session_id=session2_id)
第一句, 第二句, 第一句.
记忆管理
python
from utils import new_model
from agno.agent import Agent
from agno.memory.v2.memory import Memory
from agno.memory.v2.db.sqlite import SqliteMemoryDb
def test():
db_file = "tmp/agent.db"
memory = Memory(
model=new_model("gpt-4o-mini"),
db=SqliteMemoryDb(table_name="user_memories", db_file=db_file),
)
agent = Agent(
model=new_model("gpt-4o-mini"),
memory=memory,
enable_agentic_memory=True,
markdown=True
)
user_id = "user1"
agent.print_response("我是小明", user_id=user_id)
agent.print_response("这是我的第几句话?", user_id=user_id)
agent.print_response("我是谁?", user_id=user_id)
agent.print_response("我是谁?", user_id="user2")
让ai管理记忆, 这个记忆在整个对话有效. ai会自己提取看上去需要记忆的东西. 记忆通过user_id隔离, 存在sqlite里.
跟会话不同, 看着没用的东西不会保存. 会回复这是第一句话.
存储对话
python
from utils import new_model
from agno.agent import Agent
from agno.storage.sqlite import SqliteStorage
def test():
db_file = "tmp/session.db"
storage = SqliteStorage(table_name="agent_sessions", db_file=db_file)
agent = Agent(
model=new_model("gpt-4o-mini"),
storage=storage,
add_history_to_messages=True,
num_history_runs=3,
markdown=True
)
agent.print_response("我是小明", session_id="1")
agent.print_response("我是谁?", session_id="1")
执行之后, 去掉我是小明再执行一次之前的对话也会加载进来. 已经存进数据库里了.
tool
python
from utils import new_model
from agno.agent import Agent
def add(a: int, b: int) -> int:
"""
加法
Args:
a: int
b: int
Returns:
int: 和
"""
return a + b
def test():
agent = Agent(
model=new_model("gpt-4o-mini"),
tools=[add],
show_tool_calls=True,
markdown=True
)
agent.print_response("1 + 2 = ?")
结构化输出
python
from utils import new_model
from agno.agent import Agent
from pydantic import BaseModel, Field
class User(BaseModel):
name: str = Field(..., description="用户名")
email: str = Field(..., description="邮箱")
def test():
json_mode_agent = Agent(
model=new_model("gpt-4o-mini"),
description="你是一个注册机器人, 需要收集用户信息并输出",
response_model=User,
use_json_mode=True,
)
json_mode_agent.print_response("我是小明, 邮箱是[email protected]")
struct_mode_agent = Agent(
model=new_model("gpt-4o-mini"),
description="你是一个注册机器人, 需要收集用户信息并输出",
response_model=User,
use_json_mode=False,
)
struct_mode_agent.print_response("我是小明, 邮箱是[email protected]")
都是输出
json
{
"name": "小明",
"email": "[email protected]"
}