Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGener
|
|
4 |
from langchain_community.agent_toolkits.load_tools import load_tools
|
5 |
from langchain.agents import create_react_agent
|
6 |
from langchain.prompts import PromptTemplate
|
7 |
-
from langchain_core.output_parsers.base import BaseOutputParser
|
8 |
|
9 |
# Set up Google API keys from environment variables
|
10 |
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
@@ -43,18 +42,14 @@ prompt = PromptTemplate(
|
|
43 |
# Initialize the agent with the prompt
|
44 |
agent = create_react_agent(tools=tools, llm=llm, prompt=prompt)
|
45 |
|
46 |
-
# Custom output
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
return {"text": text}
|
55 |
-
|
56 |
-
# Assign the custom output parser to the agent
|
57 |
-
agent.output_parser = CustomOutputParser()
|
58 |
|
59 |
# Function to run the agent
|
60 |
def search(query):
|
@@ -67,13 +62,15 @@ def search(query):
|
|
67 |
}
|
68 |
try:
|
69 |
output = agent.invoke(inputs)
|
70 |
-
# Process
|
71 |
-
|
72 |
-
|
|
|
|
|
73 |
exec_globals = {"search": tools[0].func} # Assuming 'search' is the first tool
|
74 |
exec(tool_code, exec_globals)
|
75 |
return exec_globals.get("result", "Executed tool code.")
|
76 |
-
return
|
77 |
except Exception as e:
|
78 |
# Print the exception and the inputs for debugging
|
79 |
print(f"Error: {e}")
|
|
|
4 |
from langchain_community.agent_toolkits.load_tools import load_tools
|
5 |
from langchain.agents import create_react_agent
|
6 |
from langchain.prompts import PromptTemplate
|
|
|
7 |
|
8 |
# Set up Google API keys from environment variables
|
9 |
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
|
|
42 |
# Initialize the agent with the prompt
|
43 |
agent = create_react_agent(tools=tools, llm=llm, prompt=prompt)
|
44 |
|
45 |
+
# Custom output parsing function
|
46 |
+
def custom_output_parser(text):
|
47 |
+
if "tool_code" in text:
|
48 |
+
tool_code_start = text.find("```tool_code") + len("```tool_code")
|
49 |
+
tool_code_end = text.find("```", tool_code_start)
|
50 |
+
tool_code = text[tool_code_start:tool_code_end].strip()
|
51 |
+
return {"tool_code": tool_code}
|
52 |
+
return {"text": text}
|
|
|
|
|
|
|
|
|
53 |
|
54 |
# Function to run the agent
|
55 |
def search(query):
|
|
|
62 |
}
|
63 |
try:
|
64 |
output = agent.invoke(inputs)
|
65 |
+
# Process the output with the custom output parser
|
66 |
+
parsed_output = custom_output_parser(output)
|
67 |
+
# Execute tool code if exists
|
68 |
+
if "tool_code" in parsed_output:
|
69 |
+
tool_code = parsed_output["tool_code"]
|
70 |
exec_globals = {"search": tools[0].func} # Assuming 'search' is the first tool
|
71 |
exec(tool_code, exec_globals)
|
72 |
return exec_globals.get("result", "Executed tool code.")
|
73 |
+
return parsed_output["text"]
|
74 |
except Exception as e:
|
75 |
# Print the exception and the inputs for debugging
|
76 |
print(f"Error: {e}")
|