Update agent.py
Browse files
agent.py
CHANGED
|
@@ -21,7 +21,6 @@ load_dotenv()
|
|
| 21 |
@tool
|
| 22 |
def multiply(a: int, b: int) -> int:
|
| 23 |
"""Multiply two numbers.
|
| 24 |
-
|
| 25 |
Args:
|
| 26 |
a: first int
|
| 27 |
b: second int
|
|
@@ -152,7 +151,7 @@ tools = [
|
|
| 152 |
]
|
| 153 |
|
| 154 |
# Build graph function
|
| 155 |
-
def build_graph(provider: str = "
|
| 156 |
"""Build the graph"""
|
| 157 |
# Load environment variables from .env file
|
| 158 |
if provider == "google":
|
|
@@ -180,36 +179,12 @@ def build_graph(provider: str = "google"):
|
|
| 180 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 181 |
|
| 182 |
def retriever(state: MessagesState):
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
similar_question = similar_doc.metadata["Question"]
|
| 186 |
-
similar_answer = similar_doc.page_content
|
| 187 |
-
|
| 188 |
-
# Wenn gleich: direkt beantworten
|
| 189 |
-
if current_question.strip() == similar_question.strip():
|
| 190 |
-
return {
|
| 191 |
-
"messages": state["messages"] + [
|
| 192 |
-
sys_msg,
|
| 193 |
-
HumanMessage(content=f"FINAL ANSWER: {similar_answer}")
|
| 194 |
-
],
|
| 195 |
-
"should_end": True # überspringt Tool-Nutzung etc.
|
| 196 |
-
}
|
| 197 |
-
|
| 198 |
-
# Andernfalls Beispiel einbauen
|
| 199 |
example_msg = HumanMessage(
|
| 200 |
-
content=f""
|
| 201 |
-
Here I provide a similar question and answer for reference:
|
| 202 |
-
|
| 203 |
-
Question: {similar_question}
|
| 204 |
-
Final answer: {similar_answer}
|
| 205 |
-
|
| 206 |
-
If the current question is the same, return the same final answer.
|
| 207 |
-
"""
|
| 208 |
)
|
| 209 |
-
return {
|
| 210 |
-
"messages": [sys_msg] + state["messages"] + [example_msg],
|
| 211 |
-
"should_end": False # weitere Bearbeitung notwendig
|
| 212 |
-
}
|
| 213 |
|
| 214 |
builder = StateGraph(MessagesState)
|
| 215 |
builder.add_node("retriever", retriever)
|
|
@@ -225,14 +200,3 @@ def build_graph(provider: str = "google"):
|
|
| 225 |
|
| 226 |
# Compile graph
|
| 227 |
return builder.compile()
|
| 228 |
-
|
| 229 |
-
# test
|
| 230 |
-
if __name__ == "__main__":
|
| 231 |
-
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 232 |
-
# Build the graph
|
| 233 |
-
graph = build_graph(provider="groq")
|
| 234 |
-
# Run the graph
|
| 235 |
-
messages = [HumanMessage(content=question)]
|
| 236 |
-
messages = graph.invoke({"messages": messages})
|
| 237 |
-
for m in messages["messages"]:
|
| 238 |
-
m.pretty_print()
|
|
|
|
| 21 |
@tool
|
| 22 |
def multiply(a: int, b: int) -> int:
|
| 23 |
"""Multiply two numbers.
|
|
|
|
| 24 |
Args:
|
| 25 |
a: first int
|
| 26 |
b: second int
|
|
|
|
| 151 |
]
|
| 152 |
|
| 153 |
# Build graph function
|
| 154 |
+
def build_graph(provider: str = "groq"):
|
| 155 |
"""Build the graph"""
|
| 156 |
# Load environment variables from .env file
|
| 157 |
if provider == "google":
|
|
|
|
| 179 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 180 |
|
| 181 |
def retriever(state: MessagesState):
|
| 182 |
+
"""Retriever node"""
|
| 183 |
+
similar_question = vector_store.similarity_search(state["messages"][0].content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
example_msg = HumanMessage(
|
| 185 |
+
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
)
|
| 187 |
+
return {"messages": [sys_msg] + state["messages"] + [example_msg]}
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
builder = StateGraph(MessagesState)
|
| 190 |
builder.add_node("retriever", retriever)
|
|
|
|
| 200 |
|
| 201 |
# Compile graph
|
| 202 |
return builder.compile()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|