Update agent.py
Browse files
agent.py
CHANGED
|
@@ -181,43 +181,44 @@ tools = [
|
|
| 181 |
arvix_search,
|
| 182 |
create_retriever_tool
|
| 183 |
]
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
builder
|
| 207 |
-
builder.add_node("
|
| 208 |
-
builder.add_node("
|
| 209 |
-
builder.
|
| 210 |
-
builder.add_edge(
|
| 211 |
-
builder.
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
| 221 |
|
| 222 |
if __name__ == "__main__":
|
| 223 |
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
|
|
|
| 181 |
arvix_search,
|
| 182 |
create_retriever_tool
|
| 183 |
]
|
| 184 |
+
def build_graph()
|
| 185 |
+
"""Build the graph"""
|
| 186 |
+
llm = init_chat_model("google_genai:gemini-2.0-flash",google_api_key=os.environ["GOOGLE_API_KEY"])
|
| 187 |
+
llm_with_tools = llm.bind_tools(tools)
|
| 188 |
+
|
| 189 |
+
sys_msg = SystemMessage(content=system_prompt)
|
| 190 |
+
|
| 191 |
+
class MessagesState(TypedDict):
|
| 192 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 193 |
+
# Node
|
| 194 |
+
def assistant(state: MessagesState):
|
| 195 |
+
"""Assistant node"""
|
| 196 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 197 |
+
def retriever(state: MessagesState):
|
| 198 |
+
"""Retriever node"""
|
| 199 |
+
similar_question = vector_store.similarity_search(state["messages"][0].content)
|
| 200 |
+
example_msg = HumanMessage(
|
| 201 |
+
content=f"Here I provide a question and answer using query for reference if it is similar to question below: \n\n{similar_question[0].page_content}",
|
| 202 |
+
)
|
| 203 |
+
return {"messages": [sys_msg] + state["messages"] + [example_msg]}
|
| 204 |
+
|
| 205 |
+
# Build graph
|
| 206 |
+
builder = StateGraph(MessagesState)
|
| 207 |
+
builder.add_node("retriever", retriever)
|
| 208 |
+
builder.add_node("assistant", assistant)
|
| 209 |
+
builder.add_node("tools", ToolNode(tools))
|
| 210 |
+
builder.add_edge(START, "retriever")
|
| 211 |
+
builder.add_edge("retriever", "assistant")
|
| 212 |
+
builder.add_conditional_edges(
|
| 213 |
+
"assistant",
|
| 214 |
+
# If the latest message (result) from assistant is a tool call -> tools_condition routes to tools
|
| 215 |
+
# If the latest message (result) from assistant is a not a tool call -> tools_condition routes to END
|
| 216 |
+
tools_condition,
|
| 217 |
+
)
|
| 218 |
+
builder.add_edge("tools", "assistant")
|
| 219 |
+
|
| 220 |
+
# Compile graph
|
| 221 |
+
return builder.compile()
|
| 222 |
|
| 223 |
if __name__ == "__main__":
|
| 224 |
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|