|
3 | 3 | import os |
4 | 4 | from datetime import datetime |
5 | 5 |
|
6 | | -from agent_framework import ChatAgent, MCPStreamableHTTPTool |
7 | | -from agent_framework.azure import AzureOpenAIChatClient |
| 6 | +from agent_framework import Agent, MCPStreamableHTTPTool |
8 | 7 | from agent_framework.openai import OpenAIChatClient |
9 | | -from azure.identity import DefaultAzureCredential |
| 8 | +from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider |
10 | 9 | from dotenv import load_dotenv |
11 | 10 | from rich import print |
12 | 11 | from rich.logging import RichHandler |
|
26 | 25 |
|
27 | 26 | # Configure chat client based on API_HOST |
28 | 27 | API_HOST = os.getenv("API_HOST", "github") |
| 28 | +async_credential = None |
29 | 29 |
|
30 | 30 | if API_HOST == "azure": |
31 | | - client = AzureOpenAIChatClient( |
32 | | - credential=DefaultAzureCredential(), |
33 | | - deployment_name=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"), |
34 | | - endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"), |
35 | | - api_version=os.environ.get("AZURE_OPENAI_VERSION"), |
| 31 | + async_credential = DefaultAzureCredential() |
| 32 | + token_provider = get_bearer_token_provider(async_credential, "https://cognitiveservices.azure.com/.default") |
| 33 | + client = OpenAIChatClient( |
| 34 | + base_url=f"{os.environ['AZURE_OPENAI_ENDPOINT']}/openai/v1/", |
| 35 | + api_key=token_provider, |
| 36 | + model_id=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"], |
36 | 37 | ) |
37 | 38 | elif API_HOST == "github": |
38 | 39 | client = OpenAIChatClient( |
39 | 40 | base_url="https://models.github.ai/inference", |
40 | 41 | api_key=os.environ["GITHUB_TOKEN"], |
41 | | - model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"), |
| 42 | + model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4.1-mini"), |
42 | 43 | ) |
43 | 44 | elif API_HOST == "ollama": |
44 | 45 | client = OpenAIChatClient( |
|
48 | 49 | ) |
49 | 50 | else: |
50 | 51 | client = OpenAIChatClient( |
51 | | - api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o") |
| 52 | + api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4.1-mini") |
52 | 53 | ) |
53 | 54 |
|
54 | 55 |
|
55 | 56 | # --- Main Agent Logic --- |
56 | 57 | async def http_mcp_example() -> None: |
57 | | - async with ( |
58 | | - MCPStreamableHTTPTool(name="Expenses MCP Server", url=MCP_SERVER_URL) as mcp_server, |
59 | | - ChatAgent( |
60 | | - chat_client=client, |
61 | | - name="Expenses Agent", |
62 | | - instructions=f"You help users to log expenses. Today's date is {datetime.now().strftime('%Y-%m-%d')}.", |
63 | | - ) as agent, |
64 | | - ): |
65 | | - user_query = "yesterday I bought a laptop for $1200 using my visa." |
66 | | - result = await agent.run(user_query, tools=mcp_server) |
67 | | - print(result) |
| 58 | + """Run an agent connected to the local expenses MCP server.""" |
| 59 | + try: |
| 60 | + async with ( |
| 61 | + MCPStreamableHTTPTool(name="Expenses MCP Server", url=MCP_SERVER_URL) as mcp_server, |
| 62 | + Agent( |
| 63 | + client=client, |
| 64 | + name="Expenses Agent", |
| 65 | + instructions=f"You help users to log expenses. Today's date is {datetime.now().strftime('%Y-%m-%d')}.", |
| 66 | + tools=[mcp_server], |
| 67 | + ) as agent, |
| 68 | + ): |
| 69 | + user_query = "yesterday I bought a laptop for $1200 using my visa." |
| 70 | + result = await agent.run(user_query) |
| 71 | + print(result.text) |
68 | 72 |
|
69 | | - # Keep the worker alive in production |
70 | | - while RUNNING_IN_PRODUCTION: |
71 | | - await asyncio.sleep(60) |
72 | | - logger.info("Worker still running...") |
| 73 | + # Keep the worker alive in production |
| 74 | + while RUNNING_IN_PRODUCTION: |
| 75 | + await asyncio.sleep(60) |
| 76 | + logger.info("Worker still running...") |
| 77 | + finally: |
| 78 | + if async_credential: |
| 79 | + await async_credential.close() |
73 | 80 |
|
74 | 81 |
|
75 | 82 | if __name__ == "__main__": |
|
0 commit comments