Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import requests
|
|
3 |
from smolagents import CodeAgent
|
4 |
import logging
|
5 |
from tenacity import retry, stop_after_attempt, wait_exponential
|
|
|
6 |
|
7 |
# Configure logging
|
8 |
logging.basicConfig(level=logging.INFO)
|
@@ -12,7 +13,7 @@ logger = logging.getLogger(__name__)
|
|
12 |
HF_SPACE_URL = "https://manavraj-troubleshoot-mcp.hf.space"
|
13 |
|
14 |
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
|
15 |
-
def call_mcp_server(message, tool_type="knowledge_base"):
|
16 |
"""Call MCP server with proper endpoint routing"""
|
17 |
try:
|
18 |
endpoint_map = {
|
@@ -37,7 +38,7 @@ def call_mcp_server(message, tool_type="knowledge_base"):
|
|
37 |
if not isinstance(data, dict) or 'data' not in data:
|
38 |
raise Exception("Invalid MCP response format")
|
39 |
|
40 |
-
return data['data'][0]
|
41 |
|
42 |
except Exception as e:
|
43 |
logger.error(f"MCP call failed: {str(e)}")
|
@@ -45,20 +46,18 @@ def call_mcp_server(message, tool_type="knowledge_base"):
|
|
45 |
|
46 |
# Initialize CodeAgent with proper system prompt including required tags
|
47 |
agent = CodeAgent(
|
48 |
-
tools=[],
|
49 |
model="microsoft/DialoGPT-medium",
|
50 |
system_prompt="""{{authorized_imports}}
|
51 |
- requests for API calls
|
52 |
- standard Python libraries
|
53 |
-
|
54 |
{{managed_agents_descriptions}}
|
55 |
-
|
56 |
You are an advanced Technical Support Assistant with these capabilities:
|
57 |
1. Troubleshooting technical issues (wifi, hardware, software)
|
58 |
2. Finding information through web search
|
59 |
3. Formatting instructions into clear steps
|
60 |
|
61 |
-
Tools
|
62 |
- knowledge_base: For technical issues (wifi, screen, sound problems)
|
63 |
- web_search: For finding latest information or non-technical queries
|
64 |
- formatter: To organize raw steps into numbered lists
|
@@ -72,19 +71,25 @@ Response Guidelines:
|
|
72 |
|
73 |
Example workflow:
|
74 |
THOUGHT: User has wifi issue, should check knowledge base
|
75 |
-
ACTION: knowledge_base
|
76 |
OBSERVATION: Found 4 troubleshooting steps
|
77 |
FINAL RESPONSE: Here's how to fix your wifi: [steps]
|
78 |
|
79 |
Never show THOUGHT/ACTION/OBSERVATION to user - only final response."""
|
80 |
)
|
81 |
|
82 |
-
def chat_interface(message, history):
|
83 |
"""Simplified chat interface"""
|
84 |
try:
|
85 |
# Let agent handle the complete process
|
86 |
response = agent.run(message)
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
except Exception as e:
|
89 |
logger.error(f"Chat error: {str(e)}")
|
90 |
return f"Error processing request: {str(e)}"
|
@@ -97,7 +102,8 @@ demo = gr.ChatInterface(
|
|
97 |
"My wifi keeps disconnecting",
|
98 |
"Find the latest Windows 11 update",
|
99 |
"Format: Restart. Check connections. Update drivers"
|
100 |
-
]
|
|
|
101 |
)
|
102 |
|
103 |
if __name__ == "__main__":
|
|
|
3 |
from smolagents import CodeAgent
|
4 |
import logging
|
5 |
from tenacity import retry, stop_after_attempt, wait_exponential
|
6 |
+
from typing import Optional
|
7 |
|
8 |
# Configure logging
|
9 |
logging.basicConfig(level=logging.INFO)
|
|
|
13 |
HF_SPACE_URL = "https://manavraj-troubleshoot-mcp.hf.space"
|
14 |
|
15 |
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
|
16 |
+
def call_mcp_server(message: str, tool_type: str = "knowledge_base") -> str:
|
17 |
"""Call MCP server with proper endpoint routing"""
|
18 |
try:
|
19 |
endpoint_map = {
|
|
|
38 |
if not isinstance(data, dict) or 'data' not in data:
|
39 |
raise Exception("Invalid MCP response format")
|
40 |
|
41 |
+
return str(data['data'][0])
|
42 |
|
43 |
except Exception as e:
|
44 |
logger.error(f"MCP call failed: {str(e)}")
|
|
|
46 |
|
47 |
# Initialize CodeAgent with proper system prompt including required tags
|
48 |
agent = CodeAgent(
|
49 |
+
tools=[call_mcp_server],
|
50 |
model="microsoft/DialoGPT-medium",
|
51 |
system_prompt="""{{authorized_imports}}
|
52 |
- requests for API calls
|
53 |
- standard Python libraries
|
|
|
54 |
{{managed_agents_descriptions}}
|
|
|
55 |
You are an advanced Technical Support Assistant with these capabilities:
|
56 |
1. Troubleshooting technical issues (wifi, hardware, software)
|
57 |
2. Finding information through web search
|
58 |
3. Formatting instructions into clear steps
|
59 |
|
60 |
+
Available Tools:
|
61 |
- knowledge_base: For technical issues (wifi, screen, sound problems)
|
62 |
- web_search: For finding latest information or non-technical queries
|
63 |
- formatter: To organize raw steps into numbered lists
|
|
|
71 |
|
72 |
Example workflow:
|
73 |
THOUGHT: User has wifi issue, should check knowledge base
|
74 |
+
ACTION: knowledge_base("wifi keeps disconnecting")
|
75 |
OBSERVATION: Found 4 troubleshooting steps
|
76 |
FINAL RESPONSE: Here's how to fix your wifi: [steps]
|
77 |
|
78 |
Never show THOUGHT/ACTION/OBSERVATION to user - only final response."""
|
79 |
)
|
80 |
|
81 |
+
def chat_interface(message: str, history: Optional[list] = None) -> str:
|
82 |
"""Simplified chat interface"""
|
83 |
try:
|
84 |
# Let agent handle the complete process
|
85 |
response = agent.run(message)
|
86 |
+
|
87 |
+
# Extract final response and clean it up
|
88 |
+
if isinstance(response, str):
|
89 |
+
if "FINAL RESPONSE:" in response:
|
90 |
+
return response.split("FINAL RESPONSE:")[-1].strip()
|
91 |
+
return response
|
92 |
+
return str(response)
|
93 |
except Exception as e:
|
94 |
logger.error(f"Chat error: {str(e)}")
|
95 |
return f"Error processing request: {str(e)}"
|
|
|
102 |
"My wifi keeps disconnecting",
|
103 |
"Find the latest Windows 11 update",
|
104 |
"Format: Restart. Check connections. Update drivers"
|
105 |
+
],
|
106 |
+
description="A technical support assistant that can troubleshoot issues, search the web, and format instructions."
|
107 |
)
|
108 |
|
109 |
if __name__ == "__main__":
|