Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import requests
|
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
7 |
-
from tools import InternetSearchTool, WebscraperTool, ExcelTool, WikipediaTool
|
8 |
from smolagents import CodeAgent, InferenceClientModel
|
9 |
|
10 |
# (Keep Constants as is)
|
@@ -25,23 +25,39 @@ class BasicAgent:
|
|
25 |
excel_tool = ExcelTool()
|
26 |
python_tool = PythonTool()
|
27 |
wikipedia_tool = WikipediaTool()
|
28 |
-
summarize_tool = SummarizeTool()
|
29 |
|
30 |
# Create agent
|
31 |
self.agent= CodeAgent(
|
32 |
-
tools=[search_tool, webscraper_tool, excel_tool, python_tool, wikipedia_tool
|
33 |
model=InferenceClientModel(token=os.getenv("HF_TOKEN"), provider="together"),
|
34 |
add_base_tools=True, # Add any additional base tools
|
35 |
planning_interval=3, # Enable planning every 3 steps
|
36 |
max_steps=10
|
37 |
)
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def __call__(self, question: str) -> str:
|
40 |
prompt = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. 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. 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. 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.
|
41 |
|
42 |
QUESTION:
|
43 |
"""
|
44 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
|
|
|
|
45 |
answer = self.agent.run(prompt + question)
|
46 |
print(type(answer))
|
47 |
print(f"Agent returning answer: {str(answer)}")
|
|
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
7 |
+
from tools import InternetSearchTool, WebscraperTool, ExcelTool, WikipediaTool
|
8 |
from smolagents import CodeAgent, InferenceClientModel
|
9 |
|
10 |
# (Keep Constants as is)
|
|
|
25 |
excel_tool = ExcelTool()
|
26 |
python_tool = PythonTool()
|
27 |
wikipedia_tool = WikipediaTool()
|
|
|
28 |
|
29 |
# Create agent
|
30 |
self.agent= CodeAgent(
|
31 |
+
tools=[search_tool, webscraper_tool, excel_tool, python_tool, wikipedia_tool],
|
32 |
model=InferenceClientModel(token=os.getenv("HF_TOKEN"), provider="together"),
|
33 |
add_base_tools=True, # Add any additional base tools
|
34 |
planning_interval=3, # Enable planning every 3 steps
|
35 |
max_steps=10
|
36 |
)
|
37 |
|
38 |
+
def summarize(self, question: str) -> str:
|
39 |
+
prompt = """You are an AI assistant for summarizing tasks. You will receive a long request message, your task is to make it shorter by removing irrelevant details. Do no attempt to find an answer to the request or any part of the request.
|
40 |
+
|
41 |
+
EXAMPLE:
|
42 |
+
'Hi, we've been learning about reptiles in biology and I'd like to know more about crocodiles. Can you tell me how many legs an average crocodile has? Please answer only with a number! Thanks in advance!'
|
43 |
+
'How many legs does an average crocodile have? Answer with only a number.'
|
44 |
+
|
45 |
+
REQUEST:
|
46 |
+
"""
|
47 |
+
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest")
|
48 |
+
summary = llm.invoke(prompt+question)
|
49 |
+
return summary
|
50 |
+
|
51 |
def __call__(self, question: str) -> str:
|
52 |
prompt = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. 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. 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. 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.
|
53 |
|
54 |
QUESTION:
|
55 |
"""
|
56 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
57 |
+
|
58 |
+
if len(question) > 400:
|
59 |
+
question = self.summary(question)
|
60 |
+
|
61 |
answer = self.agent.run(prompt + question)
|
62 |
print(type(answer))
|
63 |
print(f"Agent returning answer: {str(answer)}")
|