Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,6 +1,30 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def run(self, question: str) -> str:
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
|
4 |
+
class MistralAgent:
|
5 |
+
def __init__(self, hf_token: str, model_id: str = "mistralai/Mistral-7B-Instruct-v0.2"):
|
6 |
+
self.api_url = f"https://api-inference.huggingface.co/models/{model_id}"
|
7 |
+
self.headers = {
|
8 |
+
"Authorization": f"Bearer {hf_token}",
|
9 |
+
"Content-Type": "application/json"
|
10 |
+
}
|
11 |
|
12 |
def run(self, question: str) -> str:
|
13 |
+
payload = {
|
14 |
+
"inputs": f"[INST] {question} [/INST]",
|
15 |
+
"parameters": {
|
16 |
+
"temperature": 0.2,
|
17 |
+
"max_new_tokens": 512,
|
18 |
+
"return_full_text": False
|
19 |
+
}
|
20 |
+
}
|
21 |
+
response = requests.post(self.api_url, headers=self.headers, json=payload)
|
22 |
+
if response.status_code != 200:
|
23 |
+
return f"Error: {response.text}"
|
24 |
+
|
25 |
+
output = response.json()
|
26 |
+
if isinstance(output, list) and "generated_text" in output[0]:
|
27 |
+
return output[0]["generated_text"].strip()
|
28 |
+
else:
|
29 |
+
return "Error: Unexpected API output."
|
30 |
+
|