Commit ·
4ad672b
1
Parent(s): b9a6c6b
Refine LangGraphAgent message handling and adjust webpage extraction tool depth to basic
Browse files36 unmodified lines3738394041424319 unmodified lines63646566676869707172737436 unmodified linesclass LangGraphAgent:def __init__(self, model_name: str = "gpt-4o",):self.llm = ChatOpenAI(model=model_name, max_tokens=3000, temperature=0, streaming=True, rate_limiter=rate_limiter)self.llm_with_tools = self.llm.bind_tools(tools)self.tool_node = ToolNode(tools)self.graph = self.create_graph().compile()19 unmodified linesif len(messages) == 1:system_prompt = ("You are a helpful assistant that can answer questions using various tools. ""You must answer the given question using as few words as possible, or the given format, if any."" If the answer is a number, you must return the number only, do not include symbols or commas. If you don't know the answer, say 'I don't know'. ""These questions are nontrivial and may require advanced critical thinking and multiple tool calls.""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.""If given a Python file, execute it with the code interpreter tool (riza_exec_python)")messages = [SystemMessage(system_prompt)] + messageswhile True:response = self.llm_with_tools.invoke(messages)messages.append(response)36 unmodified lines3738394041424319 unmodified lines636465666768697071727374757677787936 unmodified linesclass LangGraphAgent:def __init__(self, model_name: str = "gpt-4o",):self.llm = ChatOpenAI(model=model_name, max_tokens=2000, temperature=0, rate_limiter=rate_limiter)self.llm_with_tools = self.llm.bind_tools(tools)self.tool_node = ToolNode(tools)self.graph = self.create_graph().compile()19 unmodified linesif len(messages) == 1:system_prompt = ("You are a helpful assistant that can answer questions using various tools. ""You must answer the given question using as few words as possible, or the given format, if any.""If the answer is a number, you must return the number only, do not include symbols or commas.""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.""If given a Python file, execute it with the code interpreter tool (riza_exec_python)")messages = [SystemMessage(system_prompt)] + messagesMAX_HISTORY = 3 # tune as needed# Keep system + last N messagesif len(messages) > MAX_HISTORY:messages = [m for m in messages if isinstance(m, SystemMessage)] + messages[-MAX_HISTORY:]while True:response = self.llm_with_tools.invoke(messages)messages.append(response)
10 unmodified lines11121314151617181920212223242526272829303137 unmodified lines697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251268 unmodified lines1351361371381391401415 unmodified lines14714814915015115215310 unmodified linesimport base64import pandas as pd# def search_tool(query: str) -> str:# """# Search the web for information using Tavily API (or similar service).# """# api_key = os.environ.get("TAVILY_API_KEY")# if not api_key:# return "TAVILY_API_KEY not set."# url = f"https://api.tavily.com/search?q={query}&key={api_key}"# try:# response = requests.get(url)# response.raise_for_status()# return response.text# except Exception as e:# return f"Error searching web: {e}"@tooldef describe_image_tool(image_path: str, prompt: str = "Describe the following image:") -> str:"""37 unmodified linesexcept Exception as e:return f"Error parsing Excel file: {e}"@tooldef access_webpage_tool(url: str) -> str:"""Access a webpage and return its content. This should not be used for YouTubeArgs:url (str): URL of the webpage to access.Returns:str: Content of the webpage or an error message."""print(f"Accessing webpage: {url}")try:response = requests.get(url)response.raise_for_status()return response.textexcept Exception as e:return f"Error accessing {url}: {e}"# @tool# def string_functions_tool(input_string: str, operation: str) -> str:# """# Perform various string operations based on the specified operation.# Args:# input_string (str): The string to operate on.# operation (str): The operation to perform. Supported operations:# 'uppercase', 'lowercase', 'reverse', 'length',# 'count_vowels', 'count_consonants', 'count_words',# 'count_sentences'.# Returns:# str: Result of the operation or an error message if the operation is invalid.# """# if operation == "uppercase":# return input_string.upper()# elif operation == "lowercase":# return input_string.lower()# elif operation == "reverse":# return input_string[::-1]# elif operation == "length":# return str(len(input_string))# elif operation == "count_vowels":# vowels = "aeiouAEIOU"# return str(sum(1 for char in input_string if char in vowels))# elif operation == "count_consonants":# vowels = "aeiouAEIOU"# return str(sum(1 for char in input_string if char.isalpha() and char not in vowels))# elif operation == "count_words":# return str(len(input_string.split()))# elif operation == "count_sentences":# import re# sentences = re.split(r'[.!?]+', input_string)# return str(len([s for s in sentences if s.strip()]))# else:# return "Invalid operation. Supported operations: uppercase, lowercase, reverse."@tooldef audio_file_transcriber(file_path: str) -> str :"""8 unmodified linesreturn docs[0].page_contentbrave_web_search = BraveSearch.from_search_kwargs({"max_results": 5})# search_tool = TavilySearch(5 unmodified lines# )webpage_extraction_tool = TavilyExtract(extract_depth="advanced",include_images=True)10 unmodified lines11121314151637 unmodified lines5455565758598 unmodified lines686970717273745 unmodified lines8081828384858610 unmodified linesimport base64import pandas as pd@tooldef describe_image_tool(image_path: str, prompt: str = "Describe the following image:") -> str:"""37 unmodified linesexcept Exception as e:return f"Error parsing Excel file: {e}"@tooldef audio_file_transcriber(file_path: str) -> str :"""8 unmodified linesreturn docs[0].page_contentbrave_web_search = BraveSearch.from_search_kwargs({"max_results": 4})# search_tool = TavilySearch(5 unmodified lines# )webpage_extraction_tool = TavilyExtract(extract_depth="basic",include_images=True)
agents.py+9-4
tools.py+2-69