-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver1.py
More file actions
85 lines (68 loc) · 3.04 KB
/
server1.py
File metadata and controls
85 lines (68 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from langchain_community.tools.tavily_search import TavilySearchResults
from langgraph.prebuilt import create_react_agent
from langchain_core.messages import HumanMessage, SystemMessage
load_dotenv()
app = FastAPI(title="A2A Agent Server")
# Model configuration
llm = ChatGroq(model="llama-3.3-70b-versatile", temperature=0)
# Tools
tavily_tool = TavilySearchResults(max_results=3)
# System Prompts
SYS_PROMPT_1 = "You are a specialized agent providing details about Microsoft products. Use the search tool to find accurate and up-to-date information about Microsoft products. Answer ONLY questions related to Microsoft products."
SYS_PROMPT_2 = "You are a specialized agent providing support, clarification, and formulating FAQs for Microsoft. Use the search tool to find accurate and up-to-date support information. Answer ONLY questions related to Microsoft support and FAQs."
SYS_PROMPT_3 = "You are a strict fallback agent. Your only job is to firmly reject the user's request. State clearly that you can only provide responses for queries related to Microsoft products, support, or services. Do NOT answer any other questions."
# Agents with proper system messages (using prompt instead of state_modifier)
agent_1 = create_react_agent(llm, tools=[tavily_tool], prompt=SYS_PROMPT_1)
agent_2 = create_react_agent(llm, tools=[tavily_tool], prompt=SYS_PROMPT_2)
agent_3 = create_react_agent(llm, tools=[], prompt=SYS_PROMPT_3) # No tools needed for rejection
agents_map = {
"agent_1": agent_1,
"agent_2": agent_2,
"agent_3": agent_3,
}
# A2A Agent Card (Discovery)
AGENT_CARD = {
"name": "Microsoft Agentic System",
"description": "A system of agents specialized in Microsoft products and support.",
"agents": [
{
"id": "agent_1",
"name": "Product Agent",
"description": "Details about Microsoft products."
},
{
"id": "agent_2",
"name": "Support Agent",
"description": "Microsoft support, clarifications, and FAQs."
},
{
"id": "agent_3",
"name": "Reject Agent",
"description": "Fallback agent for non-Microsoft queries."
}
]
}
class QueryRequest(BaseModel):
query: str
session_id: str
@app.get("/.well-known/agent.json")
def get_agent_card():
return AGENT_CARD
@app.post("/agent/{agent_id}")
async def run_agent(agent_id: str, request: QueryRequest):
if agent_id not in agents_map:
raise HTTPException(status_code=404, detail="Agent not found")
agent = agents_map[agent_id]
# Run the agent
response = await agent.ainvoke({"messages": [HumanMessage(content=request.query)]})
# Get the last message content
result_text = response["messages"][-1].content
return {"agent_id": agent_id, "response": result_text}
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8001)