Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,42 +1,36 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
from
|
4 |
-
from
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
""
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
llm = pipeline("text-generation", model="tiiuae/falcon-7b-instruct", max_new_tokens=100)
|
38 |
-
result = llm(question)[0]['generated_text']
|
39 |
-
|
40 |
-
# Return a trimmed response (just the answer, no explanation, no prefix)
|
41 |
-
return result.strip()
|
42 |
-
|
|
|
1 |
+
from tools import get_tools
|
2 |
+
from retriever import retrieve_context
|
3 |
+
from config import LLM_MODEL
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
5 |
+
|
6 |
+
class Agent:
|
7 |
+
def __init__(self):
|
8 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
9 |
+
LLM_MODEL,
|
10 |
+
device_map="auto",
|
11 |
+
trust_remote_code=True
|
12 |
+
)
|
13 |
+
self.tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL)
|
14 |
+
self.generator = pipeline("text-generation", model=self.model, tokenizer=self.tokenizer)
|
15 |
+
self.tools = get_tools()
|
16 |
+
|
17 |
+
def generate_answer(self, question: str, context: str = "") -> str:
|
18 |
+
prompt = f"""
|
19 |
+
You are an expert AI agent answering academic and logical questions concisely.
|
20 |
+
Use the context below to help answer the user's question.
|
21 |
+
|
22 |
+
Context:
|
23 |
+
{context}
|
24 |
+
|
25 |
+
Question:
|
26 |
+
{question}
|
27 |
+
|
28 |
+
Answer:
|
29 |
+
"""
|
30 |
+
outputs = self.generator(prompt, max_new_tokens=100, do_sample=False)
|
31 |
+
return outputs[0]['generated_text'].split("Answer:")[-1].strip()
|
32 |
+
|
33 |
+
def run(self, task: dict) -> str:
|
34 |
+
question = task.get("question", "")
|
35 |
+
context = retrieve_context(task)
|
36 |
+
return self.generate_answer(question, context)
|
|
|
|
|
|
|
|
|
|
|
|