Spaces:
Running
Running
RobertoBarrosoLuque
commited on
Commit
·
1b4db50
1
Parent(s):
22e33ed
Fix tool use frontend implementation
Browse files- src/app.py +2 -18
- src/modules/llm_completions.py +18 -49
src/app.py
CHANGED
|
@@ -47,7 +47,7 @@ def respond_for_chat_interface(message: str, history: list, api_key_input: str =
|
|
| 47 |
"""Enhanced response function for gr.ChatInterface with Fed AI Savant capabilities"""
|
| 48 |
|
| 49 |
api_key = api_key_input.strip() if api_key_input else os.getenv("FIREWORKS_API_KEY", "")
|
| 50 |
-
|
| 51 |
if not api_key:
|
| 52 |
yield "❌ Please enter your Fireworks AI API key in the configuration section above."
|
| 53 |
return
|
|
@@ -63,30 +63,14 @@ def respond_for_chat_interface(message: str, history: list, api_key_input: str =
|
|
| 63 |
history=message_with_history
|
| 64 |
):
|
| 65 |
if isinstance(messages, list) and len(messages) > 0:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
for i, msg in enumerate(messages):
|
| 69 |
-
if hasattr(msg, 'content'):
|
| 70 |
-
content = msg.content
|
| 71 |
-
|
| 72 |
-
if i > 0:
|
| 73 |
-
formatted_content += "\n\n"
|
| 74 |
-
|
| 75 |
-
formatted_content += content
|
| 76 |
-
else:
|
| 77 |
-
formatted_content += str(msg)
|
| 78 |
-
|
| 79 |
-
final_response = formatted_content
|
| 80 |
-
yield formatted_content
|
| 81 |
else:
|
| 82 |
-
final_response = str(messages)
|
| 83 |
yield str(messages)
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
error_msg = f"❌ Error: {str(e)}"
|
| 87 |
yield error_msg
|
| 88 |
|
| 89 |
-
|
| 90 |
# Function to create searchable FOMC meetings accordion
|
| 91 |
def create_fomc_meetings_accordion():
|
| 92 |
"""Create searchable accordion for FOMC meetings"""
|
|
|
|
| 47 |
"""Enhanced response function for gr.ChatInterface with Fed AI Savant capabilities"""
|
| 48 |
|
| 49 |
api_key = api_key_input.strip() if api_key_input else os.getenv("FIREWORKS_API_KEY", "")
|
| 50 |
+
|
| 51 |
if not api_key:
|
| 52 |
yield "❌ Please enter your Fireworks AI API key in the configuration section above."
|
| 53 |
return
|
|
|
|
| 63 |
history=message_with_history
|
| 64 |
):
|
| 65 |
if isinstance(messages, list) and len(messages) > 0:
|
| 66 |
+
yield messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
else:
|
|
|
|
| 68 |
yield str(messages)
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
error_msg = f"❌ Error: {str(e)}"
|
| 72 |
yield error_msg
|
| 73 |
|
|
|
|
| 74 |
# Function to create searchable FOMC meetings accordion
|
| 75 |
def create_fomc_meetings_accordion():
|
| 76 |
"""Create searchable accordion for FOMC meetings"""
|
src/modules/llm_completions.py
CHANGED
|
@@ -151,68 +151,37 @@ def execute_tool_calls(tool_calls: List[Any], fed_tools: Dict[str, callable]) ->
|
|
| 151 |
|
| 152 |
return results
|
| 153 |
|
| 154 |
-
def create_planning_message_from_response(orchestrator_message: Any) -> ChatMessage:
|
| 155 |
-
"""Create the planning/analysis message from orchestrator response"""
|
| 156 |
-
content = orchestrator_message.content or "Analyzing Fed data requirements..."
|
| 157 |
-
|
| 158 |
-
if orchestrator_message.tool_calls:
|
| 159 |
-
tool_summary = f"\n\nTools to execute: {len(orchestrator_message.tool_calls)}"
|
| 160 |
-
for i, tool_call in enumerate(orchestrator_message.tool_calls, 1):
|
| 161 |
-
tool_summary += f"\n{i}. {tool_call.function.name}"
|
| 162 |
-
content += tool_summary
|
| 163 |
-
|
| 164 |
-
return ChatMessage(
|
| 165 |
-
role="assistant",
|
| 166 |
-
content=content,
|
| 167 |
-
metadata={"title": "🧠 Planning", "status": "done"}
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
def create_tool_messages_from_calls(tool_calls: List[Any]) -> List[ChatMessage]:
|
| 172 |
-
"""Create initial tool execution messages from tool calls"""
|
| 173 |
-
tool_messages = []
|
| 174 |
-
|
| 175 |
-
for i, tool_call in enumerate(tool_calls):
|
| 176 |
-
try:
|
| 177 |
-
parameters = json.loads(tool_call.function.arguments)
|
| 178 |
-
params_str = ', '.join([f'{k}={v}' for k, v in parameters.items()])
|
| 179 |
-
except json.JSONDecodeError:
|
| 180 |
-
params_str = tool_call.function.arguments
|
| 181 |
-
|
| 182 |
-
tool_msg = ChatMessage(
|
| 183 |
-
role="assistant",
|
| 184 |
-
content=f"Executing: {tool_call.function.name}({params_str})\n\nTool Call ID: {tool_call.id}",
|
| 185 |
-
metadata={"title": f"🔧 Tool {i + 1}: {tool_call.function.name}", "status": "pending"}
|
| 186 |
-
)
|
| 187 |
-
tool_messages.append(tool_msg)
|
| 188 |
-
|
| 189 |
-
return tool_messages
|
| 190 |
|
| 191 |
def extract_citations_from_tool_results(tool_results: List[Dict[str, Any]]) -> List[Dict[str, str]]:
|
| 192 |
"""Extract unique citations from tool results"""
|
| 193 |
citations = []
|
| 194 |
-
|
| 195 |
for result in tool_results:
|
| 196 |
if result["success"] and result["result"].get("success"):
|
| 197 |
# Check if result has meeting data with URLs
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
elif "results" in result["result"]:
|
| 207 |
-
# Handle search results
|
| 208 |
for meeting in result["result"]["results"]:
|
| 209 |
-
if meeting.get("url"):
|
| 210 |
citations.append({
|
| 211 |
"date": meeting.get("date", "Unknown date"),
|
| 212 |
"url": meeting["url"],
|
| 213 |
"title": meeting.get("title", f"FOMC Meeting {meeting.get('date', '')}")
|
| 214 |
})
|
| 215 |
-
|
| 216 |
# Remove duplicate citations
|
| 217 |
unique_citations = []
|
| 218 |
seen_urls = set()
|
|
@@ -220,7 +189,7 @@ def extract_citations_from_tool_results(tool_results: List[Dict[str, Any]]) -> L
|
|
| 220 |
if citation["url"] not in seen_urls:
|
| 221 |
unique_citations.append(citation)
|
| 222 |
seen_urls.add(citation["url"])
|
| 223 |
-
|
| 224 |
return unique_citations
|
| 225 |
|
| 226 |
def format_response_with_citations(response: str, citations: List[Dict[str, str]]) -> str:
|
|
|
|
| 151 |
|
| 152 |
return results
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
def extract_citations_from_tool_results(tool_results: List[Dict[str, Any]]) -> List[Dict[str, str]]:
|
| 156 |
"""Extract unique citations from tool results"""
|
| 157 |
citations = []
|
| 158 |
+
|
| 159 |
for result in tool_results:
|
| 160 |
if result["success"] and result["result"].get("success"):
|
| 161 |
# Check if result has meeting data with URLs
|
| 162 |
+
meeting_data = result["result"].get("meeting")
|
| 163 |
+
if meeting_data:
|
| 164 |
+
# Handle both single meeting object and list of meetings
|
| 165 |
+
meetings_to_process = meeting_data if isinstance(meeting_data, list) else [meeting_data]
|
| 166 |
+
|
| 167 |
+
for meeting in meetings_to_process:
|
| 168 |
+
if isinstance(meeting, dict) and meeting.get("url"):
|
| 169 |
+
citations.append({
|
| 170 |
+
"date": meeting.get("date", "Unknown date"),
|
| 171 |
+
"url": meeting["url"],
|
| 172 |
+
"title": meeting.get("title", f"FOMC Meeting {meeting.get('date', '')}")
|
| 173 |
+
})
|
| 174 |
+
|
| 175 |
+
# Handle search results
|
| 176 |
elif "results" in result["result"]:
|
|
|
|
| 177 |
for meeting in result["result"]["results"]:
|
| 178 |
+
if isinstance(meeting, dict) and meeting.get("url"):
|
| 179 |
citations.append({
|
| 180 |
"date": meeting.get("date", "Unknown date"),
|
| 181 |
"url": meeting["url"],
|
| 182 |
"title": meeting.get("title", f"FOMC Meeting {meeting.get('date', '')}")
|
| 183 |
})
|
| 184 |
+
|
| 185 |
# Remove duplicate citations
|
| 186 |
unique_citations = []
|
| 187 |
seen_urls = set()
|
|
|
|
| 189 |
if citation["url"] not in seen_urls:
|
| 190 |
unique_citations.append(citation)
|
| 191 |
seen_urls.add(citation["url"])
|
| 192 |
+
|
| 193 |
return unique_citations
|
| 194 |
|
| 195 |
def format_response_with_citations(response: str, citations: List[Dict[str, str]]) -> str:
|