|
1 | 1 | import asyncio |
2 | 2 | import logging |
3 | 3 | import os |
| 4 | +from datetime import datetime |
4 | 5 |
|
5 | 6 | import azure.identity |
6 | 7 | from dotenv import load_dotenv |
7 | 8 | from langchain.agents import create_agent |
8 | | -from langchain_core.messages import HumanMessage |
| 9 | +from langchain_core.messages import HumanMessage, SystemMessage |
9 | 10 | from langchain_mcp_adapters.client import MultiServerMCPClient |
10 | 11 | from langchain_openai import AzureChatOpenAI, ChatOpenAI |
11 | 12 | from pydantic import SecretStr |
12 | 13 | from rich.logging import RichHandler |
13 | 14 |
|
14 | | -logging.basicConfig(level=logging.WARNING, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()]) |
| 15 | +# Configure logging |
| 16 | +logging.basicConfig( |
| 17 | + level=logging.WARNING, |
| 18 | + format="%(message)s", |
| 19 | + datefmt="[%X]", |
| 20 | + handlers=[RichHandler()] |
| 21 | +) |
15 | 22 | logger = logging.getLogger("itinerario_lang") |
16 | 23 |
|
| 24 | +# Load environment variables |
17 | 25 | load_dotenv(override=True) |
| 26 | + |
| 27 | +# Constants |
| 28 | +MCP_SERVER_URL = "http://localhost:8000/mcp/" |
| 29 | +AZURE_COGNITIVE_SERVICES_SCOPE = "https://cognitiveservices.azure.com/.default" |
| 30 | + |
| 31 | +# Configure language model based on API_HOST |
18 | 32 | API_HOST = os.getenv("API_HOST", "github") |
19 | 33 |
|
20 | 34 | if API_HOST == "azure": |
21 | 35 | token_provider = azure.identity.get_bearer_token_provider( |
22 | | - azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default" |
| 36 | + azure.identity.DefaultAzureCredential(), |
| 37 | + AZURE_COGNITIVE_SERVICES_SCOPE |
23 | 38 | ) |
24 | 39 | base_model = AzureChatOpenAI( |
25 | 40 | azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"), |
|
43 | 58 | base_model = ChatOpenAI(model=os.getenv("OPENAI_MODEL", "gpt-4o-mini")) |
44 | 59 |
|
45 | 60 |
|
46 | | -async def run_agent(): |
47 | | - from datetime import datetime |
48 | | - from langchain_core.messages import SystemMessage |
49 | | - |
| 61 | +async def run_agent() -> None: |
| 62 | + """ |
| 63 | + Run the agent to process expense-related queries using MCP tools. |
| 64 | + """ |
| 65 | + # Initialize MCP client |
50 | 66 | client = MultiServerMCPClient( |
51 | 67 | { |
52 | | - "itinerary": { |
53 | | - "url": "http://localhost:8000/mcp/", |
| 68 | + "expenses": { |
| 69 | + "url": MCP_SERVER_URL, |
54 | 70 | "transport": "streamable_http", |
55 | 71 | } |
56 | 72 | } |
57 | 73 | ) |
58 | 74 |
|
| 75 | + # Get tools and create agent |
59 | 76 | tools = await client.get_tools() |
60 | 77 | agent = create_agent(base_model, tools) |
61 | 78 |
|
| 79 | + # Prepare query with context |
62 | 80 | today = datetime.now().strftime("%Y-%m-%d") |
63 | | - user_query = ( |
64 | | - "yesterday I bought a laptop for $1200 using my visa. " |
65 | | - ) |
| 81 | + user_query = "yesterday I bought a laptop for $1200 using my visa." |
66 | 82 |
|
| 83 | + # Invoke agent |
67 | 84 | response = await agent.ainvoke({ |
68 | 85 | "messages": [ |
69 | 86 | SystemMessage(content=f"Today's date is {today}."), |
70 | 87 | HumanMessage(content=user_query) |
71 | 88 | ] |
72 | 89 | }) |
73 | | - final = response["messages"][-1].content |
74 | | - print(final) |
| 90 | + |
| 91 | + # Display result |
| 92 | + final_response = response["messages"][-1].content |
| 93 | + print(final_response) |
75 | 94 |
|
76 | 95 |
|
77 | | -def main(): |
| 96 | +def main() -> None: |
| 97 | + """Main entry point for the application.""" |
78 | 98 | asyncio.run(run_agent()) |
79 | 99 |
|
80 | 100 |
|
|
0 commit comments