Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -137,57 +137,55 @@ def should_continue(state: AgentState) -> str:
|
|
137 |
return "continue"
|
138 |
|
139 |
|
140 |
-
|
141 |
def reasoning_node(state: AgentState) -> AgentState:
|
142 |
import os
|
|
|
143 |
from langchain.schema import HumanMessage, AIMessage
|
144 |
-
from langchain.prompts import
|
145 |
-
from langchain_community.llms import HuggingFaceHub
|
146 |
-
from langchain.chains import LLMChain
|
147 |
|
148 |
-
#
|
149 |
-
|
150 |
-
if not
|
151 |
-
raise ValueError("
|
152 |
|
153 |
-
# Defensive: Ensure valid history
|
154 |
if not state["history"] or not isinstance(state["history"][-1], HumanMessage):
|
155 |
state["history"].append(HumanMessage(content="Continue."))
|
156 |
|
157 |
-
#
|
158 |
-
llm =
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
)
|
163 |
-
|
164 |
-
# Flattened prompt (not chat-based)
|
165 |
-
flat_prompt = PromptTemplate.from_template(
|
166 |
-
"You're an expert problem solver. Analyze the question, select the best tool, "
|
167 |
-
"and provide reasoning.\n\n"
|
168 |
-
"Context:\n{context}\n\n"
|
169 |
-
"Reasoning Steps:\n{reasoning}\n\n"
|
170 |
-
"Question:\n{question}\n\n"
|
171 |
-
"Response Format:\nReasoning: [Your analysis]\nAction: [Tool name OR 'Final Answer']\n"
|
172 |
-
"Action Input: [Input for tool OR final response]"
|
173 |
)
|
174 |
|
175 |
-
# Build
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
response = chain.invoke({
|
180 |
"context": state["context"],
|
181 |
"reasoning": state["reasoning"],
|
182 |
"question": state["question"]
|
183 |
})
|
184 |
|
185 |
-
content = response
|
186 |
-
|
187 |
-
# Parse response
|
188 |
reasoning, action, action_input = parse_agent_response(content)
|
189 |
|
190 |
-
# Update state
|
191 |
state['history'].append(AIMessage(content=content))
|
192 |
state['reasoning'] += f"\nStep {state['iterations']+1}: {reasoning}"
|
193 |
state['iterations'] += 1
|
@@ -202,7 +200,6 @@ def reasoning_node(state: AgentState) -> AgentState:
|
|
202 |
|
203 |
return state
|
204 |
|
205 |
-
|
206 |
|
207 |
|
208 |
def tool_node(state: AgentState) -> AgentState:
|
|
|
137 |
return "continue"
|
138 |
|
139 |
|
|
|
140 |
def reasoning_node(state: AgentState) -> AgentState:
|
141 |
import os
|
142 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
143 |
from langchain.schema import HumanMessage, AIMessage
|
144 |
+
from langchain.prompts import ChatPromptTemplate
|
|
|
|
|
145 |
|
146 |
+
# Explicitly load the Google API key
|
147 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
148 |
+
if not GOOGLE_API_KEY:
|
149 |
+
raise ValueError("GOOGLE_API_KEY not set in environment variables.")
|
150 |
|
151 |
+
# Defensive: Ensure valid message history
|
152 |
if not state["history"] or not isinstance(state["history"][-1], HumanMessage):
|
153 |
state["history"].append(HumanMessage(content="Continue."))
|
154 |
|
155 |
+
# Initialize Gemini model
|
156 |
+
llm = ChatGoogleGenerativeAI(
|
157 |
+
model="gemini-1.5-flash",
|
158 |
+
temperature=0.1,
|
159 |
+
google_api_key=GOOGLE_API_KEY
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
)
|
161 |
|
162 |
+
# Build prompt
|
163 |
+
prompt = ChatPromptTemplate.from_messages([
|
164 |
+
("system", (
|
165 |
+
"You're an expert problem solver. Analyze the question, select the best tool, "
|
166 |
+
"and provide reasoning. Available tools: duckduckgo_search, wikipedia_search, "
|
167 |
+
"arxiv_search, document_qa, python_execution.\n\n"
|
168 |
+
"Current Context:\n{context}\n\n"
|
169 |
+
"Reasoning Steps:\n{reasoning}\n\n"
|
170 |
+
"Response Format:\nReasoning: [Your analysis]\nAction: [Tool name OR 'Final Answer']\n"
|
171 |
+
"Action Input: [Input for tool OR final response]"
|
172 |
+
)),
|
173 |
+
*state['history']
|
174 |
+
])
|
175 |
+
|
176 |
+
chain = prompt | llm
|
177 |
+
|
178 |
+
# Invoke chain with inputs
|
179 |
response = chain.invoke({
|
180 |
"context": state["context"],
|
181 |
"reasoning": state["reasoning"],
|
182 |
"question": state["question"]
|
183 |
})
|
184 |
|
185 |
+
content = response.content
|
|
|
|
|
186 |
reasoning, action, action_input = parse_agent_response(content)
|
187 |
|
188 |
+
# Update agent state
|
189 |
state['history'].append(AIMessage(content=content))
|
190 |
state['reasoning'] += f"\nStep {state['iterations']+1}: {reasoning}"
|
191 |
state['iterations'] += 1
|
|
|
200 |
|
201 |
return state
|
202 |
|
|
|
203 |
|
204 |
|
205 |
def tool_node(state: AgentState) -> AgentState:
|