Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,11 @@ from langchain_core.tools import tool
|
|
| 13 |
from langchain_core.messages import AIMessage, ToolMessage, HumanMessage, BaseMessage, SystemMessage
|
| 14 |
from random import randint
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
import wikipedia
|
|
|
|
| 17 |
|
| 18 |
import gradio as gr
|
| 19 |
import logging
|
|
@@ -24,27 +28,58 @@ class OrderState(TypedDict):
|
|
| 24 |
order: list[str]
|
| 25 |
finished: bool
|
| 26 |
|
| 27 |
-
# System instruction for the
|
| 28 |
SYSINT = (
|
| 29 |
"system",
|
| 30 |
-
"You are a general AI assistant. I will ask you a question.
|
|
|
|
|
|
|
| 31 |
"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."
|
| 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 of whether the element to be put in the list is a number or a string."
|
| 35 |
-
"If a tool required for task completion is
|
| 36 |
)
|
| 37 |
|
| 38 |
-
WELCOME_MSG = "Welcome to
|
| 39 |
|
| 40 |
# Initialize the Google Gemini LLM
|
| 41 |
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest")
|
| 42 |
|
| 43 |
@tool
|
| 44 |
-
def
|
| 45 |
-
"""Provides
|
| 46 |
page = wikipedia.page(title, auto_suggest=False)
|
| 47 |
-
return page.content[:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
def agent_node(state: OrderState) -> OrderState:
|
| 50 |
"""agent with tool handling."""
|
|
@@ -80,10 +115,27 @@ def interactive_tools_node(state: OrderState) -> OrderState:
|
|
| 80 |
tool_name = tool_call["name"]
|
| 81 |
tool_args = tool_call["args"]
|
| 82 |
|
| 83 |
-
if tool_name == "
|
| 84 |
print(str(tool_args))
|
| 85 |
page = wikipedia.page(tool_args.get("title"), auto_suggest=False)
|
| 86 |
-
response = page.content[:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
else:
|
| 89 |
raise NotImplementedError(f'Unknown tool call: {tool_name}')
|
|
@@ -239,12 +291,12 @@ def gradio_chat(message: str, history: list) -> str:
|
|
| 239 |
return f"An error occurred: {str(e)}"
|
| 240 |
|
| 241 |
# Gradio interface
|
| 242 |
-
def
|
| 243 |
gr.ChatInterface(
|
| 244 |
gradio_chat,
|
| 245 |
type="messages",
|
| 246 |
-
title="
|
| 247 |
-
description="
|
| 248 |
theme="ocean"
|
| 249 |
).launch()
|
| 250 |
|
|
@@ -254,4 +306,4 @@ if __name__ == "__main__":
|
|
| 254 |
stream=sys.stdout,
|
| 255 |
level=logging.INFO,
|
| 256 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
| 257 |
-
|
|
|
|
| 13 |
from langchain_core.messages import AIMessage, ToolMessage, HumanMessage, BaseMessage, SystemMessage
|
| 14 |
from random import randint
|
| 15 |
|
| 16 |
+
import requests
|
| 17 |
+
from bs4 import BeautifulSoup
|
| 18 |
+
import openpyxl
|
| 19 |
import wikipedia
|
| 20 |
+
import pandas as pd
|
| 21 |
|
| 22 |
import gradio as gr
|
| 23 |
import logging
|
|
|
|
| 28 |
order: list[str]
|
| 29 |
finished: bool
|
| 30 |
|
| 31 |
+
# System instruction for the Agent
|
| 32 |
SYSINT = (
|
| 33 |
"system",
|
| 34 |
+
"You are a general AI assistant. I will ask you a question."
|
| 35 |
+
"The question requires a tool to solve. You must attempt to use at least one of the available tools before returning an answer."
|
| 36 |
+
"Report your thoughts, and finish your answer with the following template: "
|
| 37 |
"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."
|
| 38 |
"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."
|
| 39 |
"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."
|
| 40 |
"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 |
+
"If a tool required for task completion is not functioning, return 0."
|
| 42 |
)
|
| 43 |
|
| 44 |
+
WELCOME_MSG = "Welcome to my general-purpose AI agent. Type `q` to quit. How shall I fail to serve you today?"
|
| 45 |
|
| 46 |
# Initialize the Google Gemini LLM
|
| 47 |
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest")
|
| 48 |
|
| 49 |
@tool
|
| 50 |
+
def wikipedia_search_tool(title: str) -> str:
|
| 51 |
+
"""Provides an excerpt from a Wikipedia article with the given title."""
|
| 52 |
page = wikipedia.page(title, auto_suggest=False)
|
| 53 |
+
return page.content[:3000]
|
| 54 |
+
|
| 55 |
+
@tool
|
| 56 |
+
def media_tool(file_path: str) -> str:
|
| 57 |
+
"""Used for deciphering video and audio files."""
|
| 58 |
+
return "This tool hasn't been implemented yet. Please return 0 if the task cannot be solved without knowing the contents of this file."
|
| 59 |
+
|
| 60 |
+
@tool
|
| 61 |
+
def internet_search_tool(search_query: str) -> str:
|
| 62 |
+
"""Does a google search with using the input as the search query. Returns a long batch of textual information related to the query."""
|
| 63 |
+
search_tool = DuckDuckGoSearchTool()
|
| 64 |
+
result = search_tool(question)
|
| 65 |
+
return result
|
| 66 |
+
|
| 67 |
+
@tool
|
| 68 |
+
def webscraper_tool(url: str) -> str:
|
| 69 |
+
"""Returns the page's html content from the input url."""
|
| 70 |
+
response = requests.get(url, stream=True)
|
| 71 |
+
if response.status_code == 200:
|
| 72 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 73 |
+
html_text = soup.get_text()
|
| 74 |
+
return html_text
|
| 75 |
+
else:
|
| 76 |
+
raise Exception(f"Failed to retrieve the webpage. Status code: {response.status_code}")
|
| 77 |
+
|
| 78 |
+
@tool
|
| 79 |
+
def read_excel_tool(file_path: str) -> str:
|
| 80 |
+
"""Returns the contents of an Excel file as a Pandas dataframe."""
|
| 81 |
+
df = pd.read_excel(path, engine = "openpyxl")
|
| 82 |
+
return df
|
| 83 |
|
| 84 |
def agent_node(state: OrderState) -> OrderState:
|
| 85 |
"""agent with tool handling."""
|
|
|
|
| 115 |
tool_name = tool_call["name"]
|
| 116 |
tool_args = tool_call["args"]
|
| 117 |
|
| 118 |
+
if tool_name == "wikipedia_search_tool":
|
| 119 |
print(str(tool_args))
|
| 120 |
page = wikipedia.page(tool_args.get("title"), auto_suggest=False)
|
| 121 |
+
response = page.content[:3000]
|
| 122 |
+
elif tool_name == "media_tool":
|
| 123 |
+
print(str(tool_args))
|
| 124 |
+
response = "This tool hasn't been implemented yet. Please return 0 if the task cannot be solved without knowing the contents of this file."
|
| 125 |
+
elif tool_name == "internet_search_tool":
|
| 126 |
+
search_tool = DuckDuckGoSearchTool()
|
| 127 |
+
response = search_tool(question)[:3000]
|
| 128 |
+
elif tool_name == "webscraper_tool":
|
| 129 |
+
response = requests.get(url, stream=True)
|
| 130 |
+
if response.status_code == 200:
|
| 131 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 132 |
+
html_text = soup.get_text()
|
| 133 |
+
response = html_text
|
| 134 |
+
else:
|
| 135 |
+
response = Exception(f"Failed to retrieve the webpage. Status code: {response.status_code}")
|
| 136 |
+
elif tool_name == "read_excel_tool":
|
| 137 |
+
df = pd.read_excel(path, engine = "openpyxl")
|
| 138 |
+
response = df
|
| 139 |
|
| 140 |
else:
|
| 141 |
raise NotImplementedError(f'Unknown tool call: {tool_name}')
|
|
|
|
| 291 |
return f"An error occurred: {str(e)}"
|
| 292 |
|
| 293 |
# Gradio interface
|
| 294 |
+
def launch_agent():
|
| 295 |
gr.ChatInterface(
|
| 296 |
gradio_chat,
|
| 297 |
type="messages",
|
| 298 |
+
title="Agent",
|
| 299 |
+
description="An AI agent (work in progress)",
|
| 300 |
theme="ocean"
|
| 301 |
).launch()
|
| 302 |
|
|
|
|
| 306 |
stream=sys.stdout,
|
| 307 |
level=logging.INFO,
|
| 308 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
| 309 |
+
launch_agent()
|