Spaces:
Sleeping
Sleeping
modify prompt
Browse files- Alfred_Agent.py +29 -15
Alfred_Agent.py
CHANGED
|
@@ -62,26 +62,37 @@ tools = [
|
|
| 62 |
chat_with_tools = llm.bind_tools(tools)
|
| 63 |
|
| 64 |
#setting up prompt
|
| 65 |
-
ai_message = SystemMessage(content="""You are a helpful assistant tasked with answering questions using a set of tools.
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
# Generate the AgentState and Agent graph
|
| 75 |
from langgraph.graph import MessagesState #the same as AgentState
|
| 76 |
-
class AgentState(TypedDict):
|
| 77 |
-
|
| 78 |
|
| 79 |
-
def assistant(state:
|
| 80 |
return {
|
| 81 |
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 82 |
}
|
| 83 |
|
| 84 |
-
def retriever(state:
|
| 85 |
"""Retriever node"""
|
| 86 |
similar_question = vector_search(state["messages"][0].content)
|
| 87 |
|
|
@@ -89,13 +100,15 @@ def retriever(state: AgentState):
|
|
| 89 |
example_msg = HumanMessage(
|
| 90 |
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question}",
|
| 91 |
)
|
|
|
|
| 92 |
return {"messages": [ai_message] + state["messages"] + [example_msg]}
|
| 93 |
else:
|
| 94 |
# Handle the case when no similar questions are found
|
|
|
|
| 95 |
return {"messages": [ai_message] + state["messages"]}
|
| 96 |
|
| 97 |
## The graph
|
| 98 |
-
builder = StateGraph(
|
| 99 |
|
| 100 |
|
| 101 |
# Define nodes: these do the work
|
|
@@ -115,8 +128,9 @@ builder.add_conditional_edges(
|
|
| 115 |
builder.add_edge("tools", "assistant")
|
| 116 |
alfred = builder.compile()
|
| 117 |
|
| 118 |
-
|
| 119 |
-
#
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
# print(response['messages'][-1].content)
|
|
|
|
| 62 |
chat_with_tools = llm.bind_tools(tools)
|
| 63 |
|
| 64 |
#setting up prompt
|
| 65 |
+
ai_message = SystemMessage(content="""You are a helpful assistant tasked with answering questions using a set of tools and reference materials.
|
| 66 |
+
You may be provided with a reference set of questions and their corresponding answers from a retriever.
|
| 67 |
+
If the current question is the same as or semantically equivalent to a question in the reference set, or if the reference answer clearly applies to the current question, use that answer as your final answer.
|
| 68 |
+
Otherwise, reason through the question as needed and report your thoughts before providing the final answer.
|
| 69 |
+
|
| 70 |
+
Finish your answer with the following template:
|
| 71 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 72 |
+
|
| 73 |
+
YOUR FINAL ANSWER should be:
|
| 74 |
+
- A number without commas or units (unless explicitly requested),
|
| 75 |
+
- Or a string without articles or abbreviations, with digits written in plain text unless specified otherwise,
|
| 76 |
+
- Or a comma separated list, applying the above rules to each item and ensuring exactly one space after each comma.
|
| 77 |
+
|
| 78 |
+
If the question is identical or functionally equivalent to a reference question, respond with:
|
| 79 |
+
FINAL ANSWER: [the answer to the reference question].
|
| 80 |
+
""")
|
| 81 |
+
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
# Generate the AgentState and Agent graph
|
| 86 |
from langgraph.graph import MessagesState #the same as AgentState
|
| 87 |
+
# class AgentState(TypedDict):
|
| 88 |
+
# messages: Annotated[list[AnyMessage], add_messages]
|
| 89 |
|
| 90 |
+
def assistant(state: MessagesState):
|
| 91 |
return {
|
| 92 |
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 93 |
}
|
| 94 |
|
| 95 |
+
def retriever(state: MessagesState):
|
| 96 |
"""Retriever node"""
|
| 97 |
similar_question = vector_search(state["messages"][0].content)
|
| 98 |
|
|
|
|
| 100 |
example_msg = HumanMessage(
|
| 101 |
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question}",
|
| 102 |
)
|
| 103 |
+
print(f"Similar question found: {similar_question}")
|
| 104 |
return {"messages": [ai_message] + state["messages"] + [example_msg]}
|
| 105 |
else:
|
| 106 |
# Handle the case when no similar questions are found
|
| 107 |
+
print( "No similar question found.")
|
| 108 |
return {"messages": [ai_message] + state["messages"]}
|
| 109 |
|
| 110 |
## The graph
|
| 111 |
+
builder = StateGraph(MessagesState)
|
| 112 |
|
| 113 |
|
| 114 |
# Define nodes: these do the work
|
|
|
|
| 128 |
builder.add_edge("tools", "assistant")
|
| 129 |
alfred = builder.compile()
|
| 130 |
|
| 131 |
+
messages = [HumanMessage(content="When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?")]
|
| 132 |
+
#messages = [HumanMessage(content="What the remainder of 30 divided by 7?")]
|
| 133 |
+
response = alfred.invoke({"messages": messages})
|
| 134 |
+
|
| 135 |
|
| 136 |
+
print(response['messages'][-1].content)
|
|
|