Commit
·
4ad672b
1
Parent(s):
b9a6c6b
Refine LangGraphAgent message handling and adjust webpage extraction tool depth to basic
Browse files
agents.py
CHANGED
|
@@ -37,7 +37,7 @@ rate_limiter = InMemoryRateLimiter(
|
|
| 37 |
|
| 38 |
class LangGraphAgent:
|
| 39 |
def __init__(self, model_name: str = "gpt-4o",):
|
| 40 |
-
self.llm = ChatOpenAI(model=model_name, max_tokens=
|
| 41 |
self.llm_with_tools = self.llm.bind_tools(tools)
|
| 42 |
self.tool_node = ToolNode(tools)
|
| 43 |
self.graph = self.create_graph().compile()
|
|
@@ -63,12 +63,17 @@ class LangGraphAgent:
|
|
| 63 |
if len(messages) == 1:
|
| 64 |
system_prompt = ("You are a helpful assistant that can answer questions using various tools. "
|
| 65 |
"You must answer the given question using as few words as possible, or the given format, if any."
|
| 66 |
-
"
|
| 67 |
-
"
|
| 68 |
-
"If you need to search the web for information, always use a search tool before using a wepbage extraction tool so you always have a legit website."
|
| 69 |
"If given a Python file, execute it with the code interpreter tool (riza_exec_python)")
|
| 70 |
messages = [SystemMessage(system_prompt)] + messages
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
while True:
|
| 73 |
response = self.llm_with_tools.invoke(messages)
|
| 74 |
messages.append(response)
|
|
|
|
| 37 |
|
| 38 |
class LangGraphAgent:
|
| 39 |
def __init__(self, model_name: str = "gpt-4o",):
|
| 40 |
+
self.llm = ChatOpenAI(model=model_name, max_tokens=2000, temperature=0, rate_limiter=rate_limiter)
|
| 41 |
self.llm_with_tools = self.llm.bind_tools(tools)
|
| 42 |
self.tool_node = ToolNode(tools)
|
| 43 |
self.graph = self.create_graph().compile()
|
|
|
|
| 63 |
if len(messages) == 1:
|
| 64 |
system_prompt = ("You are a helpful assistant that can answer questions using various tools. "
|
| 65 |
"You must answer the given question using as few words as possible, or the given format, if any."
|
| 66 |
+
"If the answer is a number, you must return the number only, do not include symbols or commas."
|
| 67 |
+
"If you need to search the web for information and aren't given a URL, always use a search tool before using a wepbage extraction tool so you always have a legit website."
|
|
|
|
| 68 |
"If given a Python file, execute it with the code interpreter tool (riza_exec_python)")
|
| 69 |
messages = [SystemMessage(system_prompt)] + messages
|
| 70 |
|
| 71 |
+
MAX_HISTORY = 3 # tune as needed
|
| 72 |
+
|
| 73 |
+
# Keep system + last N messages
|
| 74 |
+
if len(messages) > MAX_HISTORY:
|
| 75 |
+
messages = [m for m in messages if isinstance(m, SystemMessage)] + messages[-MAX_HISTORY:]
|
| 76 |
+
|
| 77 |
while True:
|
| 78 |
response = self.llm_with_tools.invoke(messages)
|
| 79 |
messages.append(response)
|
tools.py
CHANGED
|
@@ -11,21 +11,6 @@ import requests
|
|
| 11 |
import base64
|
| 12 |
import pandas as pd
|
| 13 |
|
| 14 |
-
# def search_tool(query: str) -> str:
|
| 15 |
-
# """
|
| 16 |
-
# Search the web for information using Tavily API (or similar service).
|
| 17 |
-
# """
|
| 18 |
-
# api_key = os.environ.get("TAVILY_API_KEY")
|
| 19 |
-
# if not api_key:
|
| 20 |
-
# return "TAVILY_API_KEY not set."
|
| 21 |
-
# url = f"https://api.tavily.com/search?q={query}&key={api_key}"
|
| 22 |
-
# try:
|
| 23 |
-
# response = requests.get(url)
|
| 24 |
-
# response.raise_for_status()
|
| 25 |
-
# return response.text
|
| 26 |
-
# except Exception as e:
|
| 27 |
-
# return f"Error searching web: {e}"
|
| 28 |
-
|
| 29 |
@tool
|
| 30 |
def describe_image_tool(image_path: str, prompt: str = "Describe the following image:") -> str:
|
| 31 |
"""
|
|
@@ -69,58 +54,6 @@ def parse_excel_tool(file_path: str) -> str:
|
|
| 69 |
except Exception as e:
|
| 70 |
return f"Error parsing Excel file: {e}"
|
| 71 |
|
| 72 |
-
@tool
|
| 73 |
-
def access_webpage_tool(url: str) -> str:
|
| 74 |
-
"""
|
| 75 |
-
Access a webpage and return its content. This should not be used for YouTube
|
| 76 |
-
Args:
|
| 77 |
-
url (str): URL of the webpage to access.
|
| 78 |
-
Returns:
|
| 79 |
-
str: Content of the webpage or an error message.
|
| 80 |
-
"""
|
| 81 |
-
print(f"Accessing webpage: {url}")
|
| 82 |
-
try:
|
| 83 |
-
response = requests.get(url)
|
| 84 |
-
response.raise_for_status()
|
| 85 |
-
return response.text
|
| 86 |
-
except Exception as e:
|
| 87 |
-
return f"Error accessing {url}: {e}"
|
| 88 |
-
# @tool
|
| 89 |
-
# def string_functions_tool(input_string: str, operation: str) -> str:
|
| 90 |
-
# """
|
| 91 |
-
# Perform various string operations based on the specified operation.
|
| 92 |
-
# Args:
|
| 93 |
-
# input_string (str): The string to operate on.
|
| 94 |
-
# operation (str): The operation to perform. Supported operations:
|
| 95 |
-
# 'uppercase', 'lowercase', 'reverse', 'length',
|
| 96 |
-
# 'count_vowels', 'count_consonants', 'count_words',
|
| 97 |
-
# 'count_sentences'.
|
| 98 |
-
# Returns:
|
| 99 |
-
# str: Result of the operation or an error message if the operation is invalid.
|
| 100 |
-
# """
|
| 101 |
-
# if operation == "uppercase":
|
| 102 |
-
# return input_string.upper()
|
| 103 |
-
# elif operation == "lowercase":
|
| 104 |
-
# return input_string.lower()
|
| 105 |
-
# elif operation == "reverse":
|
| 106 |
-
# return input_string[::-1]
|
| 107 |
-
# elif operation == "length":
|
| 108 |
-
# return str(len(input_string))
|
| 109 |
-
# elif operation == "count_vowels":
|
| 110 |
-
# vowels = "aeiouAEIOU"
|
| 111 |
-
# return str(sum(1 for char in input_string if char in vowels))
|
| 112 |
-
# elif operation == "count_consonants":
|
| 113 |
-
# vowels = "aeiouAEIOU"
|
| 114 |
-
# return str(sum(1 for char in input_string if char.isalpha() and char not in vowels))
|
| 115 |
-
# elif operation == "count_words":
|
| 116 |
-
# return str(len(input_string.split()))
|
| 117 |
-
# elif operation == "count_sentences":
|
| 118 |
-
# import re
|
| 119 |
-
# sentences = re.split(r'[.!?]+', input_string)
|
| 120 |
-
# return str(len([s for s in sentences if s.strip()]))
|
| 121 |
-
# else:
|
| 122 |
-
# return "Invalid operation. Supported operations: uppercase, lowercase, reverse."
|
| 123 |
-
|
| 124 |
@tool
|
| 125 |
def audio_file_transcriber(file_path: str) -> str :
|
| 126 |
"""
|
|
@@ -135,7 +68,7 @@ def audio_file_transcriber(file_path: str) -> str :
|
|
| 135 |
return docs[0].page_content
|
| 136 |
|
| 137 |
|
| 138 |
-
brave_web_search = BraveSearch.from_search_kwargs({"max_results":
|
| 139 |
|
| 140 |
|
| 141 |
# search_tool = TavilySearch(
|
|
@@ -147,7 +80,7 @@ brave_web_search = BraveSearch.from_search_kwargs({"max_results": 5})
|
|
| 147 |
# )
|
| 148 |
|
| 149 |
webpage_extraction_tool = TavilyExtract(
|
| 150 |
-
extract_depth="
|
| 151 |
include_images=True
|
| 152 |
)
|
| 153 |
|
|
|
|
| 11 |
import base64
|
| 12 |
import pandas as pd
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@tool
|
| 15 |
def describe_image_tool(image_path: str, prompt: str = "Describe the following image:") -> str:
|
| 16 |
"""
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
return f"Error parsing Excel file: {e}"
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
@tool
|
| 58 |
def audio_file_transcriber(file_path: str) -> str :
|
| 59 |
"""
|
|
|
|
| 68 |
return docs[0].page_content
|
| 69 |
|
| 70 |
|
| 71 |
+
brave_web_search = BraveSearch.from_search_kwargs({"max_results": 4})
|
| 72 |
|
| 73 |
|
| 74 |
# search_tool = TavilySearch(
|
|
|
|
| 80 |
# )
|
| 81 |
|
| 82 |
webpage_extraction_tool = TavilyExtract(
|
| 83 |
+
extract_depth="basic",
|
| 84 |
include_images=True
|
| 85 |
)
|
| 86 |
|