mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Updated instructions/system_message logic in GitHub Copilot agent (#3625)
* Updated instructions handling * Small improvement * Included runtime options in session creation logic
This commit is contained in:
committed by
GitHub
Unverified
parent
98cd72839e
commit
73033c300f
@@ -18,7 +18,7 @@ from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import tool
|
||||
from agent_framework.github import GitHubCopilotAgent, GitHubCopilotOptions
|
||||
from agent_framework.github import GitHubCopilotAgent
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -36,8 +36,8 @@ async def non_streaming_example() -> None:
|
||||
"""Example of non-streaming response (get the complete result at once)."""
|
||||
print("=== Non-streaming Response Example ===")
|
||||
|
||||
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
|
||||
default_options={"instructions": "You are a helpful weather agent."},
|
||||
agent = GitHubCopilotAgent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
@@ -52,8 +52,8 @@ async def streaming_example() -> None:
|
||||
"""Example of streaming response (get results as they are generated)."""
|
||||
print("=== Streaming Response Example ===")
|
||||
|
||||
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
|
||||
default_options={"instructions": "You are a helpful weather agent."},
|
||||
agent = GitHubCopilotAgent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
@@ -67,11 +67,46 @@ async def streaming_example() -> None:
|
||||
print("\n")
|
||||
|
||||
|
||||
async def runtime_options_example() -> None:
|
||||
"""Example of overriding system message at runtime."""
|
||||
print("=== Runtime Options Example ===")
|
||||
|
||||
agent = GitHubCopilotAgent(
|
||||
instructions="Always respond in exactly 3 words.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
async with agent:
|
||||
query = "What's the weather like in Paris?"
|
||||
|
||||
# First call uses default instructions (3 words response)
|
||||
print("Using default instructions (3 words):")
|
||||
print(f"User: {query}")
|
||||
result1 = await agent.run(query)
|
||||
print(f"Agent: {result1}\n")
|
||||
|
||||
# Second call overrides with runtime system_message in replace mode
|
||||
print("Using runtime system_message with replace mode (detailed response):")
|
||||
print(f"User: {query}")
|
||||
result2 = await agent.run(
|
||||
query,
|
||||
options={
|
||||
"system_message": {
|
||||
"mode": "replace",
|
||||
"content": "You are a weather expert. Provide detailed weather information "
|
||||
"with temperature, and recommendations.",
|
||||
}
|
||||
},
|
||||
)
|
||||
print(f"Agent: {result2}\n")
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
print("=== Basic GitHub Copilot Agent Example ===")
|
||||
|
||||
await non_streaming_example()
|
||||
await streaming_example()
|
||||
await runtime_options_example()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
+4
-6
@@ -14,7 +14,7 @@ SECURITY NOTE: Only enable file permissions when you trust the agent's actions.
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework.github import GitHubCopilotAgent, GitHubCopilotOptions
|
||||
from agent_framework.github import GitHubCopilotAgent
|
||||
from copilot.types import PermissionRequest, PermissionRequestResult
|
||||
|
||||
|
||||
@@ -35,11 +35,9 @@ def prompt_permission(request: PermissionRequest, context: dict[str, str]) -> Pe
|
||||
async def main() -> None:
|
||||
print("=== GitHub Copilot Agent with File Operation Permissions ===\n")
|
||||
|
||||
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
|
||||
default_options={
|
||||
"instructions": "You are a helpful assistant that can read and write files.",
|
||||
"on_permission_request": prompt_permission,
|
||||
},
|
||||
agent = GitHubCopilotAgent(
|
||||
instructions="You are a helpful assistant that can read and write files.",
|
||||
default_options={"on_permission_request": prompt_permission},
|
||||
)
|
||||
|
||||
async with agent:
|
||||
|
||||
@@ -14,7 +14,7 @@ of MCP-related actions.
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework.github import GitHubCopilotAgent, GitHubCopilotOptions
|
||||
from agent_framework.github import GitHubCopilotAgent
|
||||
from copilot.types import MCPServerConfig, PermissionRequest, PermissionRequestResult
|
||||
|
||||
|
||||
@@ -49,9 +49,9 @@ async def main() -> None:
|
||||
},
|
||||
}
|
||||
|
||||
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
|
||||
agent = GitHubCopilotAgent(
|
||||
instructions="You are a helpful assistant with access to the local filesystem and Microsoft Learn.",
|
||||
default_options={
|
||||
"instructions": "You are a helpful assistant with access to the local filesystem and Microsoft Learn.",
|
||||
"on_permission_request": prompt_permission,
|
||||
"mcp_servers": mcp_servers,
|
||||
},
|
||||
|
||||
+4
-6
@@ -20,7 +20,7 @@ More permissions mean more potential for unintended actions.
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework.github import GitHubCopilotAgent, GitHubCopilotOptions
|
||||
from agent_framework.github import GitHubCopilotAgent
|
||||
from copilot.types import PermissionRequest, PermissionRequestResult
|
||||
|
||||
|
||||
@@ -43,11 +43,9 @@ def prompt_permission(request: PermissionRequest, context: dict[str, str]) -> Pe
|
||||
async def main() -> None:
|
||||
print("=== GitHub Copilot Agent with Multiple Permissions ===\n")
|
||||
|
||||
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
|
||||
default_options={
|
||||
"instructions": "You are a helpful development assistant that can read, write files and run commands.",
|
||||
"on_permission_request": prompt_permission,
|
||||
},
|
||||
agent = GitHubCopilotAgent(
|
||||
instructions="You are a helpful development assistant that can read, write files and run commands.",
|
||||
default_options={"on_permission_request": prompt_permission},
|
||||
)
|
||||
|
||||
async with agent:
|
||||
|
||||
@@ -13,7 +13,7 @@ from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import tool
|
||||
from agent_framework.github import GitHubCopilotAgent, GitHubCopilotOptions
|
||||
from agent_framework.github import GitHubCopilotAgent
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -31,8 +31,8 @@ async def example_with_automatic_session_creation() -> None:
|
||||
"""Each run() without thread creates a new session."""
|
||||
print("=== Automatic Session Creation Example ===")
|
||||
|
||||
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
|
||||
default_options={"instructions": "You are a helpful weather agent."},
|
||||
agent = GitHubCopilotAgent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
@@ -55,8 +55,8 @@ async def example_with_session_persistence() -> None:
|
||||
"""Reuse session via thread object for multi-turn conversations."""
|
||||
print("=== Session Persistence Example ===")
|
||||
|
||||
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
|
||||
default_options={"instructions": "You are a helpful weather agent."},
|
||||
agent = GitHubCopilotAgent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
@@ -91,8 +91,8 @@ async def example_with_existing_session_id() -> None:
|
||||
existing_session_id = None
|
||||
|
||||
# First agent instance - start a conversation
|
||||
agent1: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
|
||||
default_options={"instructions": "You are a helpful weather agent."},
|
||||
agent1 = GitHubCopilotAgent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
@@ -112,8 +112,8 @@ async def example_with_existing_session_id() -> None:
|
||||
print("\n--- Continuing with the same session ID in a new agent instance ---")
|
||||
|
||||
# Second agent instance - resume the conversation
|
||||
agent2: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
|
||||
default_options={"instructions": "You are a helpful weather agent."},
|
||||
agent2 = GitHubCopilotAgent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ Shell commands have full access to your system within the permissions of the run
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework.github import GitHubCopilotAgent, GitHubCopilotOptions
|
||||
from agent_framework.github import GitHubCopilotAgent
|
||||
from copilot.types import PermissionRequest, PermissionRequestResult
|
||||
|
||||
|
||||
@@ -34,11 +34,9 @@ def prompt_permission(request: PermissionRequest, context: dict[str, str]) -> Pe
|
||||
async def main() -> None:
|
||||
print("=== GitHub Copilot Agent with Shell Permissions ===\n")
|
||||
|
||||
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
|
||||
default_options={
|
||||
"instructions": "You are a helpful assistant that can execute shell commands.",
|
||||
"on_permission_request": prompt_permission,
|
||||
},
|
||||
agent = GitHubCopilotAgent(
|
||||
instructions="You are a helpful assistant that can execute shell commands.",
|
||||
default_options={"on_permission_request": prompt_permission},
|
||||
)
|
||||
|
||||
async with agent:
|
||||
|
||||
@@ -13,7 +13,7 @@ URL fetching allows the agent to access any URL accessible from your network.
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework.github import GitHubCopilotAgent, GitHubCopilotOptions
|
||||
from agent_framework.github import GitHubCopilotAgent
|
||||
from copilot.types import PermissionRequest, PermissionRequestResult
|
||||
|
||||
|
||||
@@ -34,11 +34,9 @@ def prompt_permission(request: PermissionRequest, context: dict[str, str]) -> Pe
|
||||
async def main() -> None:
|
||||
print("=== GitHub Copilot Agent with URL Fetching ===\n")
|
||||
|
||||
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
|
||||
default_options={
|
||||
"instructions": "You are a helpful assistant that can fetch and summarize web content.",
|
||||
"on_permission_request": prompt_permission,
|
||||
},
|
||||
agent = GitHubCopilotAgent(
|
||||
instructions="You are a helpful assistant that can fetch and summarize web content.",
|
||||
default_options={"on_permission_request": prompt_permission},
|
||||
)
|
||||
|
||||
async with agent:
|
||||
|
||||
Reference in New Issue
Block a user