Ubik80 commited on
Commit
e0f838b
·
verified ·
1 Parent(s): 76f29b6

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +52 -12
tools.py CHANGED
@@ -1,16 +1,56 @@
1
-
2
- from typing import Any
 
3
  from smolagents.tools import Tool
4
 
5
- class FinalAnswerTool(Tool):
6
- name = "final_answer"
7
- description = "Returns the final answer generated by the LLM."
8
- inputs = {"answer": {"type": "any", "description": "The final answer to output"}}
9
- output_type = "any"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- def forward(self, answer: Any) -> Any:
12
- # Just pass through whatever the LLM returns.
13
- return answer
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- def __init__(self, *args, **kwargs):
16
- self.is_initialized = False
 
1
+ # tools.py
2
+ import os
3
+ import openai
4
  from smolagents.tools import Tool
5
 
6
+ # Carichiamo la tua chiave
7
+ openai.api_key = os.getenv("OPENAI_API_KEY")
8
+
9
+ SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
10
+ FINAL ANSWER: [YOUR FINAL ANSWER].
11
+
12
+ YOUR FINAL ANSWER should be:
13
+ - a number OR as few words as possible OR a comma separated list of numbers and/or strings.
14
+ - If you are asked for a number, don't use commas, symbols or units (e.g. %, $, km) unless explicitly asked.
15
+ - If you are asked for a string, don't use articles ("a", "the"), abbreviations (e.g. "NYC"), or extra words; write digits in plain text unless specified otherwise.
16
+ - If you are asked for a comma separated list, apply the above rules to each element.
17
+
18
+ Examples:
19
+ Q: How many legs does a spider have?
20
+ Thought: It's basic biology.
21
+ FINAL ANSWER: 8
22
+
23
+ Q: What are the primary colors in light?
24
+ Thought: Physics concept.
25
+ FINAL ANSWER: red, green, blue
26
+
27
+ Q: Who won the Best Picture Oscar in 1994?
28
+ Thought: Film history.
29
+ FINAL ANSWER: Forrest Gump
30
+
31
+ Now it's your turn.
32
+ """
33
+
34
+ class AnswerTool(Tool):
35
+ name = "answer_tool"
36
+ description = "Answer GAIA Level 1 questions in exact-match format."
37
+ inputs = {"question": {"type": "string", "description": "The GAIA question text."}}
38
+ output_type = "string"
39
 
40
+ def forward(self, question: str) -> str:
41
+ # Build chat messages
42
+ messages = [
43
+ {"role": "system", "content": SYSTEM_PROMPT},
44
+ {"role": "user", "content": question},
45
+ ]
46
+ resp = openai.ChatCompletion.create(
47
+ model="gpt-3.5-turbo", # o "gpt-4" se hai ancora quota
48
+ messages=messages,
49
+ temperature=0.0,
50
+ max_tokens=64,
51
+ )
52
+ text = resp.choices[0].message.content.strip()
53
+ if "FINAL ANSWER:" in text:
54
+ return text.split("FINAL ANSWER:")[-1].strip()
55
+ return text
56