Spaces:
Runtime error
Runtime error
Commit ·
6a8a4fa
1
Parent(s): a3a374d
Improve prompt and handling of tool result
Browse filesSigned-off-by: Aivin V. Solatorio <avsolatorio@gmail.com>
- mcp_client.py +24 -47
mcp_client.py
CHANGED
|
@@ -3,7 +3,7 @@ import os
|
|
| 3 |
import json
|
| 4 |
from typing import List, Dict, Any, Union
|
| 5 |
from contextlib import AsyncExitStack
|
| 6 |
-
|
| 7 |
import gradio as gr
|
| 8 |
from gradio.components.chatbot import ChatMessage
|
| 9 |
from mcp import ClientSession, StdioServerParameters
|
|
@@ -16,7 +16,9 @@ load_dotenv()
|
|
| 16 |
loop = asyncio.new_event_loop()
|
| 17 |
asyncio.set_event_loop(loop)
|
| 18 |
|
| 19 |
-
SYSTEM_PROMPT = """
|
|
|
|
|
|
|
| 20 |
|
| 21 |
You must not provide answers beyond what the tools provide.
|
| 22 |
|
|
@@ -181,53 +183,28 @@ class MCPClientWrapper:
|
|
| 181 |
result_content = result.content
|
| 182 |
print(result_content)
|
| 183 |
if isinstance(result_content, list):
|
| 184 |
-
result_content =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
print("result_content", result_content)
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
"metadata": {
|
| 201 |
-
"parent_id": f"result_{tool_name}",
|
| 202 |
-
"id": f"image_{tool_name}",
|
| 203 |
-
"title": "Generated Image",
|
| 204 |
-
},
|
| 205 |
-
}
|
| 206 |
-
)
|
| 207 |
-
else:
|
| 208 |
-
result_messages.append(
|
| 209 |
-
{
|
| 210 |
-
"role": "assistant",
|
| 211 |
-
"content": "```\n" + result_content + "\n```",
|
| 212 |
-
"metadata": {
|
| 213 |
-
"parent_id": f"result_{tool_name}",
|
| 214 |
-
"id": f"raw_result_{tool_name}",
|
| 215 |
-
"title": "Raw Output",
|
| 216 |
-
},
|
| 217 |
-
}
|
| 218 |
-
)
|
| 219 |
-
except:
|
| 220 |
-
result_messages.append(
|
| 221 |
-
{
|
| 222 |
-
"role": "assistant",
|
| 223 |
-
"content": "```\n" + result_content + "\n```",
|
| 224 |
-
"metadata": {
|
| 225 |
-
"parent_id": f"result_{tool_name}",
|
| 226 |
-
"id": f"raw_result_{tool_name}",
|
| 227 |
-
"title": "Raw Output",
|
| 228 |
-
},
|
| 229 |
-
}
|
| 230 |
-
)
|
| 231 |
|
| 232 |
# claude_messages.append(
|
| 233 |
# {
|
|
|
|
| 3 |
import json
|
| 4 |
from typing import List, Dict, Any, Union
|
| 5 |
from contextlib import AsyncExitStack
|
| 6 |
+
from datetime import datetime
|
| 7 |
import gradio as gr
|
| 8 |
from gradio.components.chatbot import ChatMessage
|
| 9 |
from mcp import ClientSession, StdioServerParameters
|
|
|
|
| 16 |
loop = asyncio.new_event_loop()
|
| 17 |
asyncio.set_event_loop(loop)
|
| 18 |
|
| 19 |
+
SYSTEM_PROMPT = f"""You are a helpful assistant and today is {datetime.now().strftime("%Y-%m-%d")}.
|
| 20 |
+
|
| 21 |
+
You do not have any knowledge of the World Development Indicators (WDI) data. However, you can use the tools provided to answer questions.
|
| 22 |
|
| 23 |
You must not provide answers beyond what the tools provide.
|
| 24 |
|
|
|
|
| 183 |
result_content = result.content
|
| 184 |
print(result_content)
|
| 185 |
if isinstance(result_content, list):
|
| 186 |
+
result_content = [r.model_dump() for r in result_content]
|
| 187 |
+
|
| 188 |
+
for r in result_content:
|
| 189 |
+
# Remove annotations field from each item if it exists
|
| 190 |
+
r.pop("annotations", None)
|
| 191 |
+
|
| 192 |
+
# result_content = "\n".join(str(item) for item in result_content)
|
| 193 |
print("result_content", result_content)
|
| 194 |
|
| 195 |
+
result_messages.append(
|
| 196 |
+
{
|
| 197 |
+
"role": "assistant",
|
| 198 |
+
"content": "```\n"
|
| 199 |
+
+ json.dumps(result_content, indent=2)
|
| 200 |
+
+ "\n```",
|
| 201 |
+
"metadata": {
|
| 202 |
+
"parent_id": f"result_{tool_name}",
|
| 203 |
+
"id": f"raw_result_{tool_name}",
|
| 204 |
+
"title": "Raw Output",
|
| 205 |
+
},
|
| 206 |
+
}
|
| 207 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
# claude_messages.append(
|
| 210 |
# {
|