快速上手
简单的对话
python
from agno.agent import Agent
from agno.models.anthropic import Claude
from agno.tools.yfinance import YFinanceTools
agent = Agent(
model=Claude(id="claude-sonnet-4-20250514"),
tools=[YFinanceTools(stock_price=True)],
instructions="Use tables to display data. Don't include any other text.",
markdown=True,
)
agent.print_response("What is the stock price of Apple?", stream=True)
使用自定义模型
python
import os
from dotenv import load_dotenv
load_dotenv()
BASE_URL = os.getenv("OPENAI_API_BASE")
API_KEY = os.getenv("OPENAI_API_KEY")
from agno.models.openai.like import OpenAILike
model = OpenAILike(id="gpt-4o-mini", api_key=API_KEY, base_url=BASE_URL)
def new_model(id: str):
return OpenAILike(id=id, api_key=API_KEY, base_url=BASE_URL)
根据网页搭建知识库
python
from agno.agent import Agent
from agno.embedder.openai import OpenAIEmbedder
from agno.knowledge.url import UrlKnowledge
from agno.models.anthropic import Claude
from agno.storage.sqlite import SqliteStorage
from agno.vectordb.lancedb import LanceDb, SearchType
# 只获取单页面, 不会递归检索里面的a标签
knowledge = UrlKnowledge(
urls=["https://docs.agno.com/introduction.md"],
vector_db=LanceDb(
uri="tmp/lancedb",
table_name="agno_docs",
search_type=SearchType.hybrid,
embedder=OpenAIEmbedder(id="text-embedding-3-small", dimensions=1536),
),
)
storage = SqliteStorage(table_name="agent_sessions", db_file="tmp/agent.db")
agent = Agent(
name="Agno Assist",
model=new_model(id="gpt-4o-mini"),
instructions=[
"Search your knowledge before answering the question.",
"Only include the output in your response. No other text.",
],
knowledge=knowledge,
storage=storage,
add_datetime_to_instructions=True,
# 加到聊天记录里
add_history_to_messages=True,
num_history_runs=3,
markdown=True,
)
if __name__ == "__main__":
agent.knowledge.load(recreate=False)
agent.print_response("What is Agno?", stream=True)
自动管理的记忆
python
from agno.agent import Agent
from agno.memory.v2.db.sqlite import SqliteMemoryDb
from agno.memory.v2.memory import Memory
from agno.models.anthropic import Claude
from agno.tools.reasoning import ReasoningTools
from agno.tools.yfinance import YFinanceTools
memory = Memory(
# Use any model for creating and managing memories
model=new_model(id="gpt-4o-mini"),
# Store memories in a SQLite database
db=SqliteMemoryDb(table_name="user_memories", db_file="tmp/agent.db"),
# We disable deletion by default, enable it if needed
delete_memories=True,
clear_memories=True,
)
agent = Agent(
model=Claude(id="claude-sonnet-4-20250514"),
tools=[
ReasoningTools(add_instructions=True),
YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True, company_news=True),
],
# User ID for storing memories, `default` if not provided
user_id="ava",
instructions=[
"Use tables to display data.",
"Include sources in your response.",
"Only include the report in your response. No other text.",
],
memory=memory,
# Let the Agent manage its memories
enable_agentic_memory=True,
markdown=True,
)
if __name__ == "__main__":
# This will create a memory that "ava's" favorite stocks are NVIDIA and TSLA
agent.print_response(
"My favorite stocks are NVIDIA and TSLA",
stream=True,
show_full_reasoning=True,
stream_intermediate_steps=True,
)
# This will use the memory to answer the question
agent.print_response(
"Can you compare my favorite stocks?",
stream=True,
show_full_reasoning=True,
stream_intermediate_steps=True,
)
这个Memory跟聊天记录不一样, Memory会由llm提取出看上去重要的东西, 然后存在sqlite里. 之后的对话会加载.
多Agent(team)
python
from agno.agent import Agent
from agno.models.openai.like import OpenAILike
from agno.team.team import Team
from agno.tools.duckduckgo import DuckDuckGoTools
from agno.tools.reasoning import ReasoningTools
web_agent = Agent(
name="Web Search Agent",
role="Handle web search requests and general research",
model=new_model("gpt-4o-mini"),
tools=[DuckDuckGoTools()],
instructions="Always include sources",
add_datetime_to_instructions=True,
)
reasoning_team = Team(
name="Reasoning Team",
mode="coordinate",
model=new_model("gpt-4o-mini"),
members=[web_agent],
tools=[ReasoningTools(add_instructions=True)],
instructions=[
"Collaborate with the web search agent to answer the question.",
],
markdown=True,
show_members_responses=True,
enable_agentic_context=True,
add_datetime_to_instructions=True,
)
if __name__ == "__main__":
reasoning_team.print_response("What is the stock price of Apple?", stream=True,show_full_reasoning=True,
stream_intermediate_steps=True,)
workflow
这太奇怪了, 为什么文档里的过不了我的静态检查, 但是跑得起来?