Spaces:
Running
Running
Didier Guillevic
commited on
Commit
·
ede8a06
1
Parent(s):
8507fc0
Minor fixes.
Browse files
app.py
CHANGED
@@ -98,7 +98,10 @@ default_model = smolagents.HfApiModel()
|
|
98 |
mistral_api_key = os.environ["MISTRAL_API_KEY"]
|
99 |
mistral_model_id = "mistral/codestral-latest"
|
100 |
mistral_model = smolagents.LiteLLMModel(
|
101 |
-
model_id=mistral_model_id,
|
|
|
|
|
|
|
102 |
|
103 |
#
|
104 |
# Define the agent
|
@@ -108,17 +111,73 @@ agent = smolagents.CodeAgent(
|
|
108 |
model=mistral_model
|
109 |
)
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
def generate_response(query: str) -> str:
|
112 |
"""Generate a response given query.
|
113 |
|
114 |
Args:
|
|
|
115 |
|
116 |
Returns:
|
117 |
- the response from the agent having access to a database over the ICIJ
|
118 |
data and a large language model.
|
119 |
"""
|
120 |
agent_output = agent.run(query)
|
121 |
-
|
|
|
|
|
|
|
|
|
122 |
|
123 |
|
124 |
#
|
@@ -127,7 +186,7 @@ def generate_response(query: str) -> str:
|
|
127 |
with gr.Blocks() as demo:
|
128 |
gr.Markdown("""
|
129 |
# SQL agent
|
130 |
-
Database: ICIJ data on offshore financial data leaks.
|
131 |
""")
|
132 |
|
133 |
# Inputs: question
|
@@ -157,6 +216,9 @@ with gr.Blocks() as demo:
|
|
157 |
"Please give the name of the entity an its address."
|
158 |
),
|
159 |
],
|
|
|
|
|
|
|
160 |
],
|
161 |
inputs=[question,],
|
162 |
outputs=[response,],
|
|
|
98 |
mistral_api_key = os.environ["MISTRAL_API_KEY"]
|
99 |
mistral_model_id = "mistral/codestral-latest"
|
100 |
mistral_model = smolagents.LiteLLMModel(
|
101 |
+
model_id=mistral_model_id,
|
102 |
+
api_key=mistral_api_key,
|
103 |
+
temperature=0.0
|
104 |
+
)
|
105 |
|
106 |
#
|
107 |
# Define the agent
|
|
|
111 |
model=mistral_model
|
112 |
)
|
113 |
|
114 |
+
#
|
115 |
+
# Handler to extract the response's content
|
116 |
+
#
|
117 |
+
from typing import Union, Any
|
118 |
+
from dataclasses import is_dataclass
|
119 |
+
import json
|
120 |
+
|
121 |
+
class ResponseHandler:
|
122 |
+
@staticmethod
|
123 |
+
def extract_content(response: Any) -> str:
|
124 |
+
"""
|
125 |
+
Extract content from various types of agent responses.
|
126 |
+
|
127 |
+
Args:
|
128 |
+
response: The response from the agent, could be string, Message object, or dict
|
129 |
+
|
130 |
+
Returns:
|
131 |
+
str: The extracted content
|
132 |
+
"""
|
133 |
+
# If it's already a string, return it
|
134 |
+
if isinstance(response, str):
|
135 |
+
return response
|
136 |
+
|
137 |
+
# If it's a Message object
|
138 |
+
if hasattr(response, 'content') and isinstance(response.content, str):
|
139 |
+
return response.content
|
140 |
+
|
141 |
+
# If it's a dictionary (e.g., from json.loads())
|
142 |
+
if isinstance(response, dict) and 'content' in response:
|
143 |
+
return response['content']
|
144 |
+
|
145 |
+
# If it's a dataclass
|
146 |
+
if is_dataclass(response):
|
147 |
+
if hasattr(response, 'content'):
|
148 |
+
return response.content
|
149 |
+
|
150 |
+
# If it's JSON string
|
151 |
+
if isinstance(response, str):
|
152 |
+
try:
|
153 |
+
parsed = json.loads(response)
|
154 |
+
if isinstance(parsed, dict) and 'content' in parsed:
|
155 |
+
return parsed['content']
|
156 |
+
except json.JSONDecodeError:
|
157 |
+
pass
|
158 |
+
|
159 |
+
# If we can't determine the type, return the string representation
|
160 |
+
return str(response)
|
161 |
+
|
162 |
+
handler = ResponseHandler()
|
163 |
+
|
164 |
+
|
165 |
def generate_response(query: str) -> str:
|
166 |
"""Generate a response given query.
|
167 |
|
168 |
Args:
|
169 |
+
- query: the question from the user
|
170 |
|
171 |
Returns:
|
172 |
- the response from the agent having access to a database over the ICIJ
|
173 |
data and a large language model.
|
174 |
"""
|
175 |
agent_output = agent.run(query)
|
176 |
+
|
177 |
+
# At times, the response appears to be a class instance with a 'content'
|
178 |
+
# part. Hence, we will pass the agent's response to some handler that will
|
179 |
+
# extract the response's content.
|
180 |
+
return handler.extract_content(agent_output)
|
181 |
|
182 |
|
183 |
#
|
|
|
186 |
with gr.Blocks() as demo:
|
187 |
gr.Markdown("""
|
188 |
# SQL agent
|
189 |
+
Database: ICIJ data on offshore financial data leaks. Very early "fast" prorotyping.
|
190 |
""")
|
191 |
|
192 |
# Inputs: question
|
|
|
216 |
"Please give the name of the entity an its address."
|
217 |
),
|
218 |
],
|
219 |
+
[
|
220 |
+
"Are there any entities located on Montreal, Canada?",
|
221 |
+
]
|
222 |
],
|
223 |
inputs=[question,],
|
224 |
outputs=[response,],
|