Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,53 +1,39 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
class BasicAgent:
|
| 6 |
def __init__(self):
|
| 7 |
-
|
| 8 |
-
print(f"Loading model: {model_id}")
|
| 9 |
-
self.llm = pipeline(
|
| 10 |
-
"text-generation",
|
| 11 |
-
model=model_id,
|
| 12 |
-
token=os.getenv("HF_TOKEN"),
|
| 13 |
-
model_kwargs={"temperature": 0.2, "max_new_tokens": 200}
|
| 14 |
-
)
|
| 15 |
|
| 16 |
-
def
|
|
|
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
results = list(ddgs.text(query, max_results=1))
|
| 20 |
-
if results:
|
| 21 |
-
return results[0]["body"]
|
| 22 |
except Exception as e:
|
| 23 |
-
|
| 24 |
-
return ""
|
| 25 |
-
|
| 26 |
-
def __call__(self, question: str) -> str:
|
| 27 |
-
context = self.search(question)
|
| 28 |
-
system_prompt = (
|
| 29 |
-
"You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: "
|
| 30 |
-
"FINAL ANSWER: [YOUR FINAL ANSWER]. "
|
| 31 |
-
"YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. "
|
| 32 |
-
"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. "
|
| 33 |
-
"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. "
|
| 34 |
-
"If you are asked for a comma separated list, apply the above rules depending on whether the element to be put in the list is a number or a string."
|
| 35 |
-
)
|
| 36 |
-
prompt = f"""{system_prompt}
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
Answer:"""
|
| 43 |
-
|
| 44 |
-
try:
|
| 45 |
-
result = self.llm(prompt)[0]["generated_text"]
|
| 46 |
-
if "FINAL ANSWER:" in result:
|
| 47 |
-
answer = result.split("FINAL ANSWER:")[-1].strip().split("\n")[0]
|
| 48 |
-
return answer.lower().strip(" .")
|
| 49 |
-
else:
|
| 50 |
-
return "unknown"
|
| 51 |
-
except Exception as e:
|
| 52 |
-
print(f"LLM error: {e}")
|
| 53 |
-
return "unknown"
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 2 |
+
|
| 3 |
+
# Load FLAN-T5 base model
|
| 4 |
+
model_name = "google/flan-t5-base"
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 7 |
+
generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 8 |
+
|
| 9 |
+
# GAIA system prompt (applied manually)
|
| 10 |
+
system_prompt = (
|
| 11 |
+
"You are a general AI assistant. I will ask you a question. Report your thoughts, "
|
| 12 |
+
"and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. "
|
| 13 |
+
"YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of "
|
| 14 |
+
"numbers and/or strings. If you are asked for a number, don't use comma to write your number "
|
| 15 |
+
"neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, "
|
| 16 |
+
"don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. "
|
| 17 |
+
"If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list "
|
| 18 |
+
"is a number or a string.\n"
|
| 19 |
+
)
|
| 20 |
|
| 21 |
class BasicAgent:
|
| 22 |
def __init__(self):
|
| 23 |
+
print("Flan-T5 GAIA agent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
def __call__(self, question: str) -> str:
|
| 26 |
+
prompt = system_prompt + "\nQuestion: " + question
|
| 27 |
try:
|
| 28 |
+
result = generator(prompt, max_length=256, do_sample=False)[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
+
return f"ERROR: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Extract FINAL ANSWER
|
| 33 |
+
final_answer = "None"
|
| 34 |
+
if "FINAL ANSWER:" in result:
|
| 35 |
+
final_answer = result.split("FINAL ANSWER:")[-1].strip()
|
| 36 |
+
else:
|
| 37 |
+
final_answer = result.strip()
|
| 38 |
|
| 39 |
+
return final_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|