Update app.py
Browse files
app.py
CHANGED
|
@@ -3,28 +3,34 @@ import requests
|
|
| 3 |
|
| 4 |
MCP_URL = "https://free-wombats-make.loca.lt"
|
| 5 |
|
| 6 |
-
API_URL = "https://api-inference.huggingface.co/models/
|
| 7 |
|
| 8 |
def query_llm(prompt):
|
| 9 |
response = requests.post(API_URL, json={"inputs": prompt})
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def extract_query_params(query):
|
| 15 |
prompt = f"""
|
| 16 |
-
Extract date and quantity from
|
| 17 |
-
|
| 18 |
-
{{"date_from":"YYYY-MM-DD","min_quantity":number}}
|
| 19 |
"""
|
|
|
|
| 20 |
llm_response = query_llm(prompt)
|
| 21 |
|
| 22 |
-
# Clean up and safely parse response
|
| 23 |
import re, json
|
| 24 |
try:
|
| 25 |
json_text = re.search(r"\{.*\}", llm_response, re.DOTALL).group()
|
| 26 |
return json.loads(json_text)
|
| 27 |
except:
|
|
|
|
| 28 |
return {"date_from":"2023-01-01","min_quantity":0}
|
| 29 |
|
| 30 |
def agent_query(user_query):
|
|
|
|
| 3 |
|
| 4 |
MCP_URL = "https://free-wombats-make.loca.lt"
|
| 5 |
|
| 6 |
+
API_URL = "https://api-inference.huggingface.co/models/google/flan-t5-base"
|
| 7 |
|
| 8 |
def query_llm(prompt):
|
| 9 |
response = requests.post(API_URL, json={"inputs": prompt})
|
| 10 |
+
if response.status_code == 200:
|
| 11 |
+
result = response.json()
|
| 12 |
+
if isinstance(result, list):
|
| 13 |
+
return result[0].get('generated_text', '')
|
| 14 |
+
else:
|
| 15 |
+
raise ValueError("Unexpected response structure:", result)
|
| 16 |
+
else:
|
| 17 |
+
raise ValueError(f"API Error: {response.status_code}")
|
| 18 |
|
| 19 |
def extract_query_params(query):
|
| 20 |
prompt = f"""
|
| 21 |
+
Extract date and quantity from the following sentence '{query}'.
|
| 22 |
+
Return the response exactly in JSON format like:
|
| 23 |
+
{{"date_from": "YYYY-MM-DD", "min_quantity": number}}
|
| 24 |
"""
|
| 25 |
+
|
| 26 |
llm_response = query_llm(prompt)
|
| 27 |
|
|
|
|
| 28 |
import re, json
|
| 29 |
try:
|
| 30 |
json_text = re.search(r"\{.*\}", llm_response, re.DOTALL).group()
|
| 31 |
return json.loads(json_text)
|
| 32 |
except:
|
| 33 |
+
# Default fallback if parsing fails
|
| 34 |
return {"date_from":"2023-01-01","min_quantity":0}
|
| 35 |
|
| 36 |
def agent_query(user_query):
|