Spaces:
Sleeping
Sleeping
Nguyen Nhu Trung
commited on
Commit
·
528d7ce
1
Parent(s):
e6f45d4
fix system prompt
Browse files
agent.py
CHANGED
|
@@ -100,12 +100,17 @@ def web_search(query: str) -> str:
|
|
| 100 |
])
|
| 101 |
return {"web_results": formatted_search_docs}
|
| 102 |
|
| 103 |
-
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
sys_msg = SystemMessage(content=system_prompt)
|
| 108 |
|
|
|
|
| 109 |
tools = [
|
| 110 |
wiki_search,
|
| 111 |
web_search,
|
|
@@ -114,6 +119,7 @@ tools = [
|
|
| 114 |
transcribe_audio
|
| 115 |
]
|
| 116 |
|
|
|
|
| 117 |
llm = ChatGroq(model="qwen-qwq-32b", temperature=0.7)
|
| 118 |
llm_with_tools = llm.bind_tools(tools)
|
| 119 |
|
|
@@ -121,24 +127,9 @@ def assistant(state: MessagesState):
|
|
| 121 |
"""Assistant node"""
|
| 122 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 123 |
|
| 124 |
-
def final_answer(
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
of a question.
|
| 128 |
-
|
| 129 |
-
Example:
|
| 130 |
-
**QUESTION:** what is 1+1?
|
| 131 |
-
**ANSWER:** the result is 2
|
| 132 |
-
**OUTPUT:** 2
|
| 133 |
-
"""
|
| 134 |
-
conversation = f"Extract final answer from\QUESTION:{question}\ANSWER:{answer}\OUTPUT:"
|
| 135 |
-
print(f"conversation: {conversation}")
|
| 136 |
-
messages = [
|
| 137 |
-
{"role": "system", "content": sys_prompt},
|
| 138 |
-
{"role": "user", "content": conversation},
|
| 139 |
-
]
|
| 140 |
-
response = llm.invoke(messages)
|
| 141 |
-
return response.content.replace("**OUTPUT:**","").replace("**ANSWER:**","")
|
| 142 |
|
| 143 |
builder = StateGraph(MessagesState)
|
| 144 |
builder.add_node("assistant", assistant)
|
|
@@ -153,14 +144,15 @@ graph = builder.compile()
|
|
| 153 |
|
| 154 |
|
| 155 |
def get_answer(query):
|
| 156 |
-
messages = [HumanMessage(content=query)]
|
| 157 |
results = graph.invoke({"messages": messages})
|
| 158 |
-
return final_answer(
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
question = "In the video https://www.youtube.com/watch?v=L1vXCYZAYYM, what is the highest number of bird species to be on camera simultaneously?"
|
| 162 |
question = "Hi, I was out sick from my classes on Friday, so I'm trying to figure out what I need to study for my Calculus mid-term next week. My friend from class sent me an audio recording of Professor Willowbrook giving out the recommended reading for the test, but my headphones are broken :(\n\nCould you please listen to the recording for me and tell me the page numbers I'm supposed to go over? I've attached a file called Homework.mp3 that has the recording. Please provide just the page numbers as a comma-delimited list. And please provide the list in ascending order."
|
| 163 |
question = "What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?"
|
|
|
|
| 164 |
# getmessages = [HumanMessage(content=question)]
|
| 165 |
# messages = graph.invoke({"messages": messages})
|
| 166 |
# for m in messages["messages"]:
|
|
|
|
| 100 |
])
|
| 101 |
return {"web_results": formatted_search_docs}
|
| 102 |
|
|
|
|
| 103 |
|
| 104 |
+
system_prompt = """
|
| 105 |
+
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 106 |
+
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 107 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 108 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 109 |
+
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
| 110 |
+
"""
|
| 111 |
sys_msg = SystemMessage(content=system_prompt)
|
| 112 |
|
| 113 |
+
|
| 114 |
tools = [
|
| 115 |
wiki_search,
|
| 116 |
web_search,
|
|
|
|
| 119 |
transcribe_audio
|
| 120 |
]
|
| 121 |
|
| 122 |
+
|
| 123 |
llm = ChatGroq(model="qwen-qwq-32b", temperature=0.7)
|
| 124 |
llm_with_tools = llm.bind_tools(tools)
|
| 125 |
|
|
|
|
| 127 |
"""Assistant node"""
|
| 128 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 129 |
|
| 130 |
+
def final_answer(answer):
|
| 131 |
+
print(f"ANSWER: {answer}")
|
| 132 |
+
return answer.replace("FINAL ANSWER:","")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
builder = StateGraph(MessagesState)
|
| 135 |
builder.add_node("assistant", assistant)
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
def get_answer(query):
|
| 147 |
+
messages = [sys_msg, HumanMessage(content=query)]
|
| 148 |
results = graph.invoke({"messages": messages})
|
| 149 |
+
return final_answer(results["messages"][-1].content)
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
question = "In the video https://www.youtube.com/watch?v=L1vXCYZAYYM, what is the highest number of bird species to be on camera simultaneously?"
|
| 153 |
question = "Hi, I was out sick from my classes on Friday, so I'm trying to figure out what I need to study for my Calculus mid-term next week. My friend from class sent me an audio recording of Professor Willowbrook giving out the recommended reading for the test, but my headphones are broken :(\n\nCould you please listen to the recording for me and tell me the page numbers I'm supposed to go over? I've attached a file called Homework.mp3 that has the recording. Please provide just the page numbers as a comma-delimited list. And please provide the list in ascending order."
|
| 154 |
question = "What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?"
|
| 155 |
+
question = "Where were the Vietnamese specimens described by Kuznetzov in Nedoshivina's 2010 paper eventually deposited? Just give me the city name without abbreviations."
|
| 156 |
# getmessages = [HumanMessage(content=question)]
|
| 157 |
# messages = graph.invoke({"messages": messages})
|
| 158 |
# for m in messages["messages"]:
|