shubham7080 commited on
Commit
408cf88
·
verified ·
1 Parent(s): fee8740

Create agent.py

Browse files
Files changed (1) hide show
  1. agent.py +81 -0
agent.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, List, Optional
2
+
3
+ from smolagents import CodeAgent
4
+
5
+ from utils.logger import get_logger
6
+
7
+ logger = get_logger(__name__)
8
+
9
+
10
+ class Agent:
11
+ """
12
+ Agent class that wraps a CodeAgent and provides a callable interface for answering questions.
13
+ Args:
14
+ model (Any): The language model to use.
15
+ tools (Optional[List[Any]]): List of tools to provide to the agent.
16
+ prompt (Optional[str]): Custom prompt template for the agent.
17
+ """
18
+
19
+ def __init__(
20
+ self,
21
+ model: Any,
22
+ tools: Optional[List[Any]] = None,
23
+ prompt: Optional[str] = None,
24
+ ):
25
+ logger.info("Initializing Agent")
26
+ self.model = model
27
+ self.tools = tools
28
+ self.imports = [
29
+ "pandas",
30
+ "numpy",
31
+ "os",
32
+ "requests",
33
+ "tempfile",
34
+ "datetime",
35
+ "json",
36
+ "time",
37
+ "re",
38
+ "openpyxl",
39
+ "pathlib",
40
+ "sys",
41
+ ]
42
+ self.agent = CodeAgent(
43
+ model=self.model,
44
+ tools=self.tools,
45
+ add_base_tools=True,
46
+ additional_authorized_imports=self.imports,
47
+ )
48
+ self.prompt = prompt or (
49
+ """
50
+ You are an advanced AI assistant specialized in solving complex, real-world tasks that require multi-step reasoning, factual accuracy, and use of external tools.
51
+
52
+ Follow these principles:
53
+ - Be precise and concise. The final answer must strictly match the required format with no extra commentary.
54
+ - Use tools intelligently. If a question involves external information, structured data, images, or audio, call the appropriate tool to retrieve or process it.
55
+ - Reason step-by-step. Think through the solution logically and plan your actions carefully before answering.
56
+ - Validate information. Always verify facts when possible instead of guessing.
57
+ - Use code if needed. For calculations, parsing, or transformations, generate Python code and execute it. But be careful, some questions contains time-consuming tasks, so you should be careful with the code you run. Better analyze the question and think about the best way to solve it.
58
+ - Don't forget to use `final_answer` to give the final answer.
59
+ - Use name of file ONLY FROM "FILE:" section. THIS IF ALWAYS A FILE.
60
+ IMPORTANT: When giving the final answer, output only the direct required result without any extra text like "Final Answer:" or explanations. YOU MUST RESPOND IN THE EXACT FORMAT AS THE QUESTION.
61
+ QUESTION: {question}
62
+ FILE: {context}
63
+ ANSWER:
64
+ """
65
+ )
66
+ logger.info("Agent initialized")
67
+
68
+ def __call__(self, question: str, file_path: Optional[str] = None) -> str:
69
+ """
70
+ Run the agent to answer a question, optionally using a file as context.
71
+ Args:
72
+ question (str): The question to answer.
73
+ file_path (Optional[str]): Path to a file to use as context (if any).
74
+ Returns:
75
+ str: The agent's answer as a string.
76
+ """
77
+ answer = self.agent.run(
78
+ self.prompt.format(question=question, context=file_path)
79
+ )
80
+ answer = str(answer).strip("'").strip('"').strip()
81
+ return answer