mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Fixed Anthropic and GitHub Copilot samples (#4025)
* Fixed Anthropic advanced example * Small improvement * Simplified skills sample * Fixed custom agent sample * Added service_session_id parameter * Added tests * Resolved comments
This commit is contained in:
committed by
GitHub
Unverified
parent
28e3fc308b
commit
f900febb6f
@@ -44,7 +44,7 @@ async def main() -> None:
|
||||
print("Agent: ", end="", flush=True)
|
||||
async for chunk in agent.run(query, stream=True):
|
||||
for content in chunk.contents:
|
||||
if content.type == "text_reasoning":
|
||||
if content.type == "text_reasoning" and content.text:
|
||||
print(f"\033[32m{content.text}\033[0m", end="", flush=True)
|
||||
if content.type == "usage":
|
||||
print(f"\n\033[34m[Usage so far: {content.usage_details}]\033[0m\n", end="", flush=True)
|
||||
|
||||
@@ -123,8 +123,8 @@ async def example_with_existing_session_id() -> None:
|
||||
)
|
||||
|
||||
async with agent2:
|
||||
# Create session with existing session ID
|
||||
session = agent2.create_session(service_session_id=existing_session_id)
|
||||
# Get session with existing session ID
|
||||
session = agent2.get_session(service_session_id=existing_session_id)
|
||||
|
||||
query2 = "What was the last city I asked about?"
|
||||
print(f"User: {query2}")
|
||||
|
||||
@@ -47,7 +47,7 @@ async def main() -> None:
|
||||
)
|
||||
|
||||
async with agent:
|
||||
query = "List the first 3 Python files in the current directory"
|
||||
query = "List the first 3 markdown (.md) files in the current directory"
|
||||
print(f"User: {query}")
|
||||
result = await agent.run(query)
|
||||
print(f"Agent: {result.text}\n")
|
||||
|
||||
@@ -36,8 +36,8 @@ async def main() -> None:
|
||||
instructions="You are a helpful agent for creating powerpoint presentations.",
|
||||
tools=client.get_code_interpreter_tool(),
|
||||
default_options={
|
||||
"max_tokens": 20000,
|
||||
"thinking": {"type": "enabled", "budget_tokens": 10000},
|
||||
"max_tokens": 4096,
|
||||
"thinking": {"type": "enabled", "budget_tokens": 2000},
|
||||
"container": {"skills": [{"type": "anthropic", "skill_id": "pptx", "version": "latest"}]},
|
||||
},
|
||||
)
|
||||
@@ -49,7 +49,7 @@ async def main() -> None:
|
||||
"\033[32mAgent Reasoning: (green)\033[0m\n"
|
||||
"\033[34mUsage: (blue)\033[0m\n"
|
||||
)
|
||||
query = "Create a presentation about renewable energy with 5 slides"
|
||||
query = "Create a simple presentation with 2 slides about Python programming"
|
||||
print(f"User: {query}")
|
||||
print("Agent: ", end="", flush=True)
|
||||
files: list[Content] = []
|
||||
@@ -79,9 +79,9 @@ async def main() -> None:
|
||||
file_content = await client.anthropic_client.beta.files.download(
|
||||
file_id=file.file_id, betas=["files-api-2025-04-14"]
|
||||
)
|
||||
with open(Path(__file__).parent / f"renewable_energy-{idx}.pptx", "wb") as f:
|
||||
with open(Path(__file__).parent / f"python_programming-{idx}.pptx", "wb") as f:
|
||||
await file_content.write_to_file(f.name)
|
||||
print(f"File {idx}: renewable_energy-{idx}.pptx saved to disk.")
|
||||
print(f"File {idx}: python_programming-{idx}.pptx saved to disk.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -12,7 +12,6 @@ from agent_framework import (
|
||||
Content,
|
||||
InMemoryHistoryProvider,
|
||||
Message,
|
||||
Role,
|
||||
normalize_messages,
|
||||
)
|
||||
|
||||
@@ -93,7 +92,7 @@ class EchoAgent(BaseAgent):
|
||||
|
||||
if not normalized_messages:
|
||||
response_message = Message(
|
||||
role=Role.ASSISTANT,
|
||||
role="assistant",
|
||||
contents=[
|
||||
Content.from_text(text="Hello! I'm a custom echo agent. Send me a message and I'll echo it back.")
|
||||
],
|
||||
@@ -106,11 +105,11 @@ class EchoAgent(BaseAgent):
|
||||
else:
|
||||
echo_text = f"{self.echo_prefix}[Non-text message received]"
|
||||
|
||||
response_message = Message(role=Role.ASSISTANT, contents=[Content.from_text(text=echo_text)])
|
||||
response_message = Message(role="assistant", contents=[Content.from_text(text=echo_text)])
|
||||
|
||||
# Store messages in session state if provided
|
||||
if session is not None:
|
||||
stored = session.state.setdefault("memory", {}).setdefault("messages", [])
|
||||
stored = session.state.setdefault(InMemoryHistoryProvider.DEFAULT_SOURCE_ID, {}).setdefault("messages", [])
|
||||
stored.extend(normalized_messages)
|
||||
stored.append(response_message)
|
||||
|
||||
@@ -145,7 +144,7 @@ class EchoAgent(BaseAgent):
|
||||
|
||||
yield AgentResponseUpdate(
|
||||
contents=[Content.from_text(text=chunk_text)],
|
||||
role=Role.ASSISTANT,
|
||||
role="assistant",
|
||||
)
|
||||
|
||||
# Small delay to simulate streaming
|
||||
@@ -153,8 +152,8 @@ class EchoAgent(BaseAgent):
|
||||
|
||||
# Store messages in session state if provided
|
||||
if session is not None:
|
||||
complete_response = Message(role=Role.ASSISTANT, contents=[Content.from_text(text=response_text)])
|
||||
stored = session.state.setdefault("memory", {}).setdefault("messages", [])
|
||||
complete_response = Message(role="assistant", contents=[Content.from_text(text=response_text)])
|
||||
stored = session.state.setdefault(InMemoryHistoryProvider.DEFAULT_SOURCE_ID, {}).setdefault("messages", [])
|
||||
stored.extend(normalized_messages)
|
||||
stored.append(complete_response)
|
||||
|
||||
|
||||
@@ -118,8 +118,8 @@ async def example_with_existing_session_id() -> None:
|
||||
)
|
||||
|
||||
async with agent2:
|
||||
# Create session with existing session ID
|
||||
session = agent2.create_session(service_session_id=existing_session_id)
|
||||
# Get session with existing session ID
|
||||
session = agent2.get_session(service_session_id=existing_session_id)
|
||||
|
||||
query2 = "What was the last city I asked about?"
|
||||
print(f"User: {query2}")
|
||||
|
||||
@@ -32,8 +32,8 @@ from typing import Any
|
||||
from agent_framework import (
|
||||
AgentExecutorRequest,
|
||||
AgentExecutorResponse,
|
||||
Message,
|
||||
Executor,
|
||||
Message,
|
||||
Workflow,
|
||||
WorkflowBuilder,
|
||||
WorkflowContext,
|
||||
|
||||
Reference in New Issue
Block a user