Spaces:
Sleeping
Sleeping
Create agent.py
Browse files
agent.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
|
3 |
+
SYSTEM_PROMPT = """
|
4 |
+
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
5 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
6 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
7 |
+
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
|
8 |
+
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
9 |
+
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
10 |
+
"""
|
11 |
+
|
12 |
+
class GaiaAgent:
|
13 |
+
def __init__(self, model_id="mistralai/Mistral-7B-Instruct-v0.1"):
|
14 |
+
self.generator = pipeline("text-generation", model=model_id, tokenizer=model_id, max_new_tokens=512)
|
15 |
+
|
16 |
+
def __call__(self, question: str) -> tuple[str, str]:
|
17 |
+
try:
|
18 |
+
prompt = f"<s>[INST] {SYSTEM_PROMPT}\n\n{question} [/INST]"
|
19 |
+
output = self.generator(prompt)[0]["generated_text"]
|
20 |
+
|
21 |
+
# Extrahera bara svaret efter FINAL ANSWER:
|
22 |
+
if "FINAL ANSWER:" in output:
|
23 |
+
final = output.split("FINAL ANSWER:")[-1].strip().split("\n")[0].strip()
|
24 |
+
else:
|
25 |
+
final = output.strip()
|
26 |
+
return final, output
|
27 |
+
except Exception as e:
|
28 |
+
return "ERROR", f"Agent failed: {e}"
|