Loading...
Loading...
Compare original and translation side by side
Required step: install dependencies before finishing. After writing AG2 code, install the matching provider extra (plus any feature extra the task needs, e.g.,ag-ui). Run the install:tracingbashpip install "ag2[openai]" # swap in the provider/extra you usedIf you cannot run commands, state the exactcommand. This is part of finishing the task, not an optional note.pip installFor a multi-file project (more than a throwaway script), also drop apinningrequirements.txtwith the extras you used (e.g.ag2) so the environment is reproducible.ag2[openai]>=0.14.0
*ConfigImportError: ... requires optional dependencies. Install with pip install "ag2[<provider>]"| Provider | Install | Env var | Config class |
|---|---|---|---|
| OpenAI | | | |
| Anthropic | | | |
| Gemini (API key) | | | |
| Vertex AI (Gemini) | | service-account / ADC | |
| Ollama (local) | | — | |
| DashScope (Qwen) | | | |
.envpython-dotenvfrom dotenv import load_dotenv
load_dotenv() # reads .env at project rootpython -c "import sys, ag2; from importlib.metadata import version; print(sys.executable); print('ag2', version('ag2'))"必填步骤:完成前先安装依赖。 编写AG2代码后,安装匹配的提供商扩展包(以及任务所需的任何功能扩展包,例如、ag-ui)。运行以下安装命令:tracingbashpip install "ag2[openai]" # 替换为你使用的提供商/扩展包如果你无法执行命令,请明确说明具体的命令。这是完成任务的一部分,而非可选提示。pip install对于多文件项目(不仅仅是一次性脚本),还需创建文件,固定你使用的带扩展包的requirements.txt版本(例如ag2),以确保环境可复现。ag2[openai]>=0.14.0
*ConfigImportError: ... requires optional dependencies. Install with pip install "ag2[<provider>]"| 提供商 | 安装命令 | 环境变量 | 配置类 |
|---|---|---|---|
| OpenAI | | | |
| Anthropic | | | |
| Gemini(API密钥) | | | |
| Vertex AI(Gemini) | | 服务账号/ADC | |
| Ollama(本地) | | — | |
| DashScope(通义千问) | | | |
python-dotenv.envfrom dotenv import load_dotenv
load_dotenv() # 读取项目根目录的.env文件python -c "import sys, ag2; from importlib.metadata import version; print(sys.executable); print('ag2', version('ag2'))"import asyncio
from ag2 import Agent
from ag2.config import OpenAIConfig
async def main() -> None:
agent = Agent(
"assistant",
prompt="You are a helpful assistant. Reply in one sentence.",
config=OpenAIConfig(model="gpt-4o-mini"),
)
# First turn
reply = await agent.ask("What is the capital of France?")
print(reply.body)
# Continue the same conversation — context is preserved
reply = await reply.ask("And of Germany?")
print(reply.body)
asyncio.run(main())Agent.ask(...)AgentReplyAgentReply.ask(...)reply.bodyag2-structured-outputreply.content()import asyncio
from ag2 import Agent
from ag2.config import OpenAIConfig
async def main() -> None:
agent = Agent(
"assistant",
prompt="你是一个乐于助人的助手。用一句话回复。",
config=OpenAIConfig(model="gpt-4o-mini"),
)
# 第一轮对话
reply = await agent.ask("法国的首都是什么?")
print(reply.body)
# 继续同一场对话——上下文会被保留
reply = await reply.ask("那德国的呢?")
print(reply.body)
asyncio.run(main())Agent.ask(...)AgentReplyAgentReply.ask(...)reply.bodyag2-structured-outputreply.content()ag2.configmodel=api_key=streaming=Truefrom ag2.config import OpenAIConfig # gpt-4o, gpt-5-*, o-series, etc.
from ag2.config import OpenAIResponsesConfig # OpenAI Responses API (image gen, file_id support)
from ag2.config import AnthropicConfig # claude-sonnet-4-6, claude-opus-4-7, etc.
from ag2.config import GeminiConfig # Gemini Developer API (api_key)
from ag2.config import VertexAIConfig # Gemini on Google Vertex AI (project + location)
from ag2.config import OllamaConfig # local Ollama
from ag2.config import DashScopeConfig # Alibaba Qwen
config = AnthropicConfig(model="claude-sonnet-4-6", streaming=True)api_key=OPENAI_API_KEYANTHROPIC_API_KEYGEMINI_API_KEYGOOGLE_API_KEYOpenAIConfigbase_url=config = OpenAIConfig(
model="qwen-3",
base_url="http://localhost:8000/v1",
api_key="NotRequired", # pragma: allowlist secret
)ag2.configmodel=api_key=streaming=Truefrom ag2.config import OpenAIConfig # gpt-4o, gpt-5-*, o-series等
from ag2.config import OpenAIResponsesConfig # OpenAI Responses API(支持图像生成、file_id)
from ag2.config import AnthropicConfig # claude-sonnet-4-6, claude-opus-4-7等
from ag2.config import GeminiConfig # Gemini开发者API(需api_key)
from ag2.config import VertexAIConfig # Google Vertex AI上的Gemini(需项目+区域)
from ag2.config import OllamaConfig # 本地Ollama
from ag2.config import DashScopeConfig # 阿里云通义千问
config = AnthropicConfig(model="claude-sonnet-4-6", streaming=True)api_key=OPENAI_API_KEYANTHROPIC_API_KEYGEMINI_API_KEYGOOGLE_API_KEYOpenAIConfigbase_url=config = OpenAIConfig(
model="qwen-3",
base_url="http://localhost:8000/v1",
api_key="NotRequired", # pragma: allowlist secret
)reply.ask()reply.ask()agent = Agent("planner", prompt="...", config=config)
reply = await agent.ask("Plan a 5-day Japan trip in late April.")
reply = await reply.ask("Budget is $2500 per person, two travellers.")
reply = await reply.ask("Prefer trains. Day-by-day itinerary.")
print(reply.body)reply.ask()agent.ask(...)assets/multi_turn.pyagent = Agent("planner", prompt="...", config=config)
reply = await agent.ask("规划一个4月底的5天日本行程。")
reply = await reply.ask("每人预算2500美元,共两位旅行者。")
reply = await reply.ask("偏好乘坐火车。请提供每日行程安排。")
print(reply.body)reply.ask()agent.ask(...)assets/multi_turn.py.copy(...)base = OpenAIConfig(model="gpt-5")
hot = base.copy(temperature=0.8)
cheap = base.copy(model="gpt-5-mini")agent = Agent("assistant", prompt="Help.")
reply = await agent.ask("Hello!", config=OpenAIConfig(model="gpt-5", api_key="sk-...")) # pragma: allowlist secret.copy(...)base = OpenAIConfig(model="gpt-5")
hot = base.copy(temperature=0.8)
cheap = base.copy(model="gpt-5-mini")askagent = Agent("assistant", prompt="提供帮助。")
reply = await agent.ask("你好!", config=OpenAIConfig(model="gpt-5", api_key="sk-...")) # pragma: allowlist secretaskassets/hello_agent.pycode_examples/01assets/multi_turn.pycode_examples/03VertexAIConfigextra_bodyhttpxwebsite/docs/user-guide/model_configuration.mdxwebsite/docs/user-guide/agents.mdxwebsite/docs/user-guide/system_prompts.mdxassets/hello_agent.pycode_examples/01assets/multi_turn.pycode_examples/03VertexAIConfigextra_bodyhttpxwebsite/docs/user-guide/model_configuration.mdxwebsite/docs/user-guide/agents.mdxwebsite/docs/user-guide/system_prompts.mdxawaitAgentAgentReplyasyncio.run(main())agent.ask()reply.ask()OPENAI_API_KEYstreaming=Trueconfig=awaitAgentAgentReplyasyncio.run(main())agent.ask()reply.ask()OPENAI_API_KEYstreaming=Trueaskconfig=