ChrisSacrumCor commited on
Commit
21790e7
·
verified ·
1 Parent(s): 164121b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -47
app.py CHANGED
@@ -95,69 +95,63 @@ class MCPClient:
95
 
96
  def analyze_user_request(self, user_input: str) -> Dict[str, Any]:
97
  """Use OpenAI to analyze user request and determine which MCP server to use"""
98
- system_prompt = f"""
99
  You are an intelligent assistant that helps users interact with MCP (Model Context Protocol) servers.
100
 
101
  Available MCP servers (use these EXACT keys in your response):
102
- 1. terraform: {self.mcp_servers['terraform']['description']}
103
- 2. linux: {self.mcp_servers['linux']['description']}
104
- 3. cisco: {self.mcp_servers['cisco']['description']}
105
 
106
  IMPORTANT: For the "recommended_server" field, you MUST use one of these exact keys: "terraform", "linux", or "cisco"
107
 
108
- Analyze the user's request and determine:
109
- 1. Which MCP server(s) would be most appropriate
110
- 2. What specific tools or operations might be needed
111
- 3. Any parameters or arguments that should be passed
112
 
113
  Respond in JSON format with:
114
- {{
115
  "recommended_server": "terraform|linux|cisco",
116
  "reasoning": "explanation of why this server was chosen",
117
- "suggested_action": "what action to take",
118
- "parameters": {{"key": "value"}}
119
- }}
120
  """
121
 
122
  try:
 
 
 
 
 
 
 
 
 
 
 
 
123
  try:
124
- response = self.openai_client.chat.completions.create(
125
- model="gpt-4",
126
- messages=[
127
- {"role": "system", "content": system_prompt},
128
- {"role": "user", "content": user_input}
129
- ],
130
- temperature=0.3
131
- )
132
-
133
- content = response.choices[0].message.content.strip()
 
 
134
 
135
- # Try to parse JSON, handle cases where GPT returns non-JSON
136
- try:
137
- return json.loads(content)
138
- except json.JSONDecodeError:
139
- # If not valid JSON, extract server recommendation manually
140
- content_lower = content.lower()
141
- if "cisco" in content_lower:
142
- recommended_server = "cisco"
143
- elif "linux" in content_lower:
144
- recommended_server = "linux"
145
- elif "terraform" in content_lower:
146
- recommended_server = "terraform"
147
- else:
148
- recommended_server = "cisco" # Default for network questions
149
-
150
- return {
151
- "recommended_server": recommended_server,
152
- "reasoning": "Based on content analysis",
153
- "suggested_action": content,
154
- "parameters": {}
155
- }
156
 
157
  except Exception as e:
158
  return {
159
  "error": f"Failed to analyze request: {str(e)}",
160
- "recommended_server": "cisco" # Default fallback
161
  }
162
 
163
  def generate_response(self, user_input: str, mcp_results: Dict[str, Any] = None) -> str:
@@ -323,7 +317,7 @@ def create_gradio_interface():
323
  history.append([message, error_msg])
324
  return history, ""
325
 
326
- def get_server_status(self):
327
  status_info = "## MCP Servers Status\n\n"
328
  for key, config in (mcp_client.mcp_servers.items() if mcp_client else []):
329
  try:
@@ -472,5 +466,4 @@ if __name__ == "__main__":
472
  server_port=7860,
473
  share=True,
474
  debug=True
475
- )
476
- # Now?
 
95
 
96
  def analyze_user_request(self, user_input: str) -> Dict[str, Any]:
97
  """Use OpenAI to analyze user request and determine which MCP server to use"""
98
+ system_prompt = """
99
  You are an intelligent assistant that helps users interact with MCP (Model Context Protocol) servers.
100
 
101
  Available MCP servers (use these EXACT keys in your response):
102
+ 1. terraform: Terraform infrastructure management
103
+ 2. linux: Linux system operations
104
+ 3. cisco: Cisco network management
105
 
106
  IMPORTANT: For the "recommended_server" field, you MUST use one of these exact keys: "terraform", "linux", or "cisco"
107
 
108
+ Analyze the user's request and determine which MCP server would be most appropriate.
 
 
 
109
 
110
  Respond in JSON format with:
111
+ {
112
  "recommended_server": "terraform|linux|cisco",
113
  "reasoning": "explanation of why this server was chosen",
114
+ "suggested_action": "what action to take"
115
+ }
 
116
  """
117
 
118
  try:
119
+ response = self.openai_client.chat.completions.create(
120
+ model="gpt-4",
121
+ messages=[
122
+ {"role": "system", "content": system_prompt},
123
+ {"role": "user", "content": user_input}
124
+ ],
125
+ temperature=0.3
126
+ )
127
+
128
+ content = response.choices[0].message.content.strip()
129
+
130
+ # Try to parse JSON, handle cases where GPT returns non-JSON
131
  try:
132
+ return json.loads(content)
133
+ except json.JSONDecodeError:
134
+ # If not valid JSON, extract server recommendation manually
135
+ content_lower = content.lower()
136
+ if "cisco" in content_lower:
137
+ recommended_server = "cisco"
138
+ elif "linux" in content_lower:
139
+ recommended_server = "linux"
140
+ elif "terraform" in content_lower:
141
+ recommended_server = "terraform"
142
+ else:
143
+ recommended_server = "cisco" # Default for network questions
144
 
145
+ return {
146
+ "recommended_server": recommended_server,
147
+ "reasoning": "Based on content analysis",
148
+ "suggested_action": content
149
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
  except Exception as e:
152
  return {
153
  "error": f"Failed to analyze request: {str(e)}",
154
+ "recommended_server": "cisco"
155
  }
156
 
157
  def generate_response(self, user_input: str, mcp_results: Dict[str, Any] = None) -> str:
 
317
  history.append([message, error_msg])
318
  return history, ""
319
 
320
+ def get_server_status():
321
  status_info = "## MCP Servers Status\n\n"
322
  for key, config in (mcp_client.mcp_servers.items() if mcp_client else []):
323
  try:
 
466
  server_port=7860,
467
  share=True,
468
  debug=True
469
+ )