Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,23 +40,47 @@ def wikipedia_search(query: str):
|
|
| 40 |
class BasicAgent:
|
| 41 |
def __init__(self, model_id="ollama/qwen2:7b"):
|
| 42 |
print('BasicAgent initialized.')
|
| 43 |
-
self.
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
),
|
| 49 |
-
|
| 50 |
-
verbosity_level=2,
|
| 51 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def __call__(self, question: str) -> str:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 62 |
"""
|
|
|
|
| 40 |
class BasicAgent:
|
| 41 |
def __init__(self, model_id="ollama/qwen2:7b"):
|
| 42 |
print('BasicAgent initialized.')
|
| 43 |
+
self.system_prompt = (
|
| 44 |
+
"You are a general AI assistant. I will ask you a question. "
|
| 45 |
+
"Report your thoughts, and finish your answer with the following template: "
|
| 46 |
+
"FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. "
|
| 47 |
+
"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. "
|
| 48 |
+
"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. "
|
| 49 |
+
"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."
|
|
|
|
| 50 |
)
|
| 51 |
+
web_search_tool = DuckDuckGoSearchRun()
|
| 52 |
+
#wikipedia_search_tool = wikipedia_search()
|
| 53 |
+
tools = [web_search_tool]
|
| 54 |
+
llm = HuggingFaceEndpoint(repo_id="ollama/qwen2:7b", huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN"))
|
| 55 |
+
|
| 56 |
+
chat = ChatHuggingFace(llm=llm, verbose=True)
|
| 57 |
+
chat_with_tools = chat.bind_tools(tools)
|
| 58 |
+
|
| 59 |
+
class AgentState(TypedDict):
|
| 60 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 61 |
+
|
| 62 |
+
def assistant(state: AgentState):
|
| 63 |
+
return {
|
| 64 |
+
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
builder = StateGraph(AgentState)
|
| 68 |
+
builder.add_node("assistant", assistant)
|
| 69 |
+
builder.add_node("tools", ToolNode(tools))
|
| 70 |
+
builder.add_edge(START, "assistant")
|
| 71 |
+
builder.add_conditional_edges("assistant", tools_condition)
|
| 72 |
+
builder.add_edge("tools", "assistant")
|
| 73 |
+
self.alfred = builder.compile()
|
| 74 |
|
| 75 |
def __call__(self, question: str) -> str:
|
| 76 |
+
messages = [
|
| 77 |
+
SystemMessage(content=self.system_prompt),
|
| 78 |
+
HumanMessage(content=question)
|
| 79 |
+
]
|
| 80 |
+
response = self.alfred.invoke({"messages": messages})
|
| 81 |
+
model_answer = response["messages"][-1].content
|
| 82 |
+
return model_answer
|
| 83 |
+
|
| 84 |
|
| 85 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 86 |
"""
|