rajsinghparihar
commited on
Commit
·
6922b6c
1
Parent(s):
f09c794
add files
Browse files- .gitignore +3 -0
- agent.py +76 -0
- app.py +9 -55
- requirements.txt +2 -1
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
__pycache__
|
3 |
+
*.pyc
|
agent.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from openai.types.responses import ResponseTextDeltaEvent
|
4 |
+
from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel
|
5 |
+
from agents.mcp import MCPServerSse
|
6 |
+
|
7 |
+
# Load environment
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# MCP tool URL (replace with actual URL you got from step 4.3)
|
11 |
+
SERVER_URL = "https://rajsinghparihar-travel-itinerary-planner.hf.space/gradio_api/mcp/sse"
|
12 |
+
|
13 |
+
instructions = """
|
14 |
+
You are an expert travel planner.
|
15 |
+
Given a user query, resolve it, if needed you will use the connected MCP tool to assist with travel planning tasks.
|
16 |
+
|
17 |
+
|
18 |
+
Your tasks include:
|
19 |
+
|
20 |
+
1. Search for flight options using the tools from the mcp server,
|
21 |
+
2. Search for hotel options using the tools from the mcp server, keeping in mind the user's preferences.
|
22 |
+
3. Once you've decided the best options based on the user query, for each flight and hotel, include the booking link, for hotels you may include thumbnail image links.
|
23 |
+
4. Estimate the total budget (flight + hotel for the stay).
|
24 |
+
4. Suggest a list of fun activities at the destination based on your knowledge about the destination.
|
25 |
+
5. Format the entire report in Markdown with clear headings and bullet points.
|
26 |
+
|
27 |
+
any other suggestions which make the trip fun are also welcome.
|
28 |
+
"""
|
29 |
+
|
30 |
+
mcp_server = MCPServerSse({"url": SERVER_URL})
|
31 |
+
model = OpenAIChatCompletionsModel(
|
32 |
+
model="Qwen/Qwen3-235B-A22B",
|
33 |
+
openai_client=AsyncOpenAI(base_url="https://api.studio.nebius.com/v1/", api_key=os.getenv("NEBIUS_API_KEY"))
|
34 |
+
)
|
35 |
+
async def get_itinerary(user_query, history):
|
36 |
+
try:
|
37 |
+
await mcp_server.connect()
|
38 |
+
agent = Agent(
|
39 |
+
name="MCP Agent",
|
40 |
+
instructions=instructions,
|
41 |
+
mcp_servers=[mcp_server],
|
42 |
+
model=model,
|
43 |
+
)
|
44 |
+
|
45 |
+
task = "You are a helpful assistant. Use the connected tool to assist with tasks."
|
46 |
+
task += f"\n\nUser Query: {user_query}\n\n"
|
47 |
+
|
48 |
+
think_buffer = ""
|
49 |
+
response_buffer = ""
|
50 |
+
showing_thought = False
|
51 |
+
|
52 |
+
result = Runner.run_streamed(agent, task)
|
53 |
+
async for event in result.stream_events():
|
54 |
+
if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
|
55 |
+
token = event.data.delta
|
56 |
+
|
57 |
+
if "<think>" in token:
|
58 |
+
showing_thought = True
|
59 |
+
think_buffer = ""
|
60 |
+
continue
|
61 |
+
elif "</think>" in token:
|
62 |
+
showing_thought = False
|
63 |
+
yield f"🤔 *Thinking:* {think_buffer}\n"
|
64 |
+
continue
|
65 |
+
|
66 |
+
if showing_thought:
|
67 |
+
think_buffer += token
|
68 |
+
if len(think_buffer) > 0:
|
69 |
+
yield f"🤔 *Thinking:* {think_buffer}"
|
70 |
+
else:
|
71 |
+
response_buffer += token
|
72 |
+
if len(response_buffer) > 0:
|
73 |
+
yield f"{response_buffer}"
|
74 |
+
|
75 |
+
finally:
|
76 |
+
await mcp_server.cleanup()
|
app.py
CHANGED
@@ -1,62 +1,16 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
-
|
48 |
-
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
62 |
|
|
|
1 |
import gradio as gr
|
2 |
+
from agent import get_itinerary
|
3 |
|
4 |
+
# async def respond(message, history):
|
5 |
+
# partial_message = ""
|
6 |
+
# async for token in get_itinerary(message, history):
|
7 |
+
# partial_message += str(token)
|
8 |
+
# # Use yield to stream each update
|
9 |
+
# yield partial_message
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
demo = gr.ChatInterface(
|
12 |
+
get_itinerary,
|
13 |
+
type="messages"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
)
|
15 |
|
16 |
|
requirements.txt
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
|
|
|
|
1 |
+
openai-agents==0.0.17
|
2 |
+
openai==1.85.0
|