Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,68 +21,62 @@ import uuid
|
|
| 21 |
# (Keep Constants as is)
|
| 22 |
# --- Constants ---
|
| 23 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
-
|
| 27 |
-
wikipedia = WikipediaAPIWrapper()
|
| 28 |
-
def wikipedia_search(query: str) -> str:
|
| 29 |
-
return wikipedia.run(query)
|
| 30 |
-
|
| 31 |
# --- Basic Agent Definition ---
|
| 32 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
| 33 |
-
|
| 34 |
-
"""Search the web for information."""
|
| 35 |
-
return DuckDuckGoSearchRun().run(query)
|
| 36 |
|
| 37 |
-
def wikipedia_search(query: str):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
"Report your thoughts, and finish your answer with the following template: "
|
| 49 |
-
"
|
| 50 |
"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. "
|
| 51 |
"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. "
|
| 52 |
-
"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."
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
chat_with_tools = chat.bind_tools(tools)
|
| 61 |
-
|
| 62 |
-
class AgentState(TypedDict):
|
| 63 |
-
messages: Annotated[list[AnyMessage], add_messages]
|
| 64 |
-
|
| 65 |
-
def assistant(state: AgentState):
|
| 66 |
-
return {
|
| 67 |
-
"messages": [chat_with_tools.invoke(state["messages"])],
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
builder = StateGraph(AgentState)
|
| 71 |
-
builder.add_node("assistant", assistant)
|
| 72 |
-
builder.add_node("tools", ToolNode(tools))
|
| 73 |
-
builder.add_edge(START, "assistant")
|
| 74 |
-
builder.add_conditional_edges("assistant", tools_condition)
|
| 75 |
-
builder.add_edge("tools", "assistant")
|
| 76 |
-
self.alfred = builder.compile()
|
| 77 |
|
| 78 |
def __call__(self, question: str) -> str:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
HumanMessage(content=question)
|
| 82 |
-
]
|
| 83 |
-
response = self.alfred.invoke({"messages": messages})
|
| 84 |
-
model_answer = response["messages"][-1].content
|
| 85 |
-
return model_answer
|
| 86 |
|
| 87 |
|
| 88 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 21 |
# (Keep Constants as is)
|
| 22 |
# --- Constants ---
|
| 23 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 24 |
+
SERPER_API_KEY=os.environ.get("SERPER_API_KEY")
|
| 25 |
+
OPENAI_KEY=os.environ.get("OPENAI_KEY")
|
| 26 |
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# --- Basic Agent Definition ---
|
| 29 |
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
| 30 |
+
serper_tool = GoogleSerperAPIWrapper()
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
def wikipedia_search(query: str) -> str:
|
| 33 |
+
return WikipediaAPIWrapper().run(query)
|
| 34 |
+
|
| 35 |
+
serper_tool = GoogleSerperAPIWrapper()
|
| 36 |
+
|
| 37 |
+
tools = [
|
| 38 |
+
Tool(
|
| 39 |
+
name = 'wikipedia_search',
|
| 40 |
+
func = wikipedia_search,
|
| 41 |
+
description = 'Search Wikipedia for factual information.'
|
| 42 |
+
),
|
| 43 |
+
Tool(
|
| 44 |
+
name = 'google_search',
|
| 45 |
+
func = serper_tool.run,
|
| 46 |
+
description = 'Search the web using Google Serper.'
|
| 47 |
+
)
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
llm = ChatOpenAI(
|
| 51 |
+
model = 'gpt-4o-mini',
|
| 52 |
+
temperature = 0,
|
| 53 |
+
openai_api_key = OPENAI_KEY,
|
| 54 |
+
max_tokens = 4096
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
agent = initialize_agent(
|
| 58 |
+
tools=tools,
|
| 59 |
+
llm=llm,
|
| 60 |
+
agent=AgentType.OPENAI_FUNCTIONS,
|
| 61 |
+
verbose=True,
|
| 62 |
+
agent_kwargs={
|
| 63 |
+
"system_message": SystemMessage(content="You are a general AI assistant. I will ask you a question. "
|
| 64 |
"Report your thoughts, and finish your answer with the following template: "
|
| 65 |
+
"[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. "
|
| 66 |
"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. "
|
| 67 |
"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. "
|
| 68 |
+
"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.")
|
| 69 |
+
}
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class BasicAgent:
|
| 74 |
+
def __init__(self):
|
| 75 |
+
self.agent = agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
def __call__(self, question: str) -> str:
|
| 78 |
+
print(f"\nQuestion: {question}")
|
| 79 |
+
return self.agent.run(question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|