BoddyGus commited on
Commit
98d66c5
·
verified ·
1 Parent(s): 1695ec5

Updating app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -32
app.py CHANGED
@@ -31,22 +31,12 @@ SERPER_API_KEY = os.environ.get("SERPER_API_KEY")
31
  HUGGINGFACEHUB_API_TOKEN=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
32
  class BasicAgent:
33
  def __init__(self):
34
- print("BasicAgent initialized.")
35
- self.system_prompt = (
36
- "You are a general AI assistant. I will ask you a question. "
37
- "Report your thoughts, and finish your answer with the following template: "
38
- "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. "
39
- "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. "
40
- "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. "
41
- "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."
42
- )
43
  llm = ChatOpenAI(
44
  model = 'gpt-4o-mini',
45
  temperature = 0,
46
  openai_api_key = OPENAI_KEY,
47
  max_tokens = 4096
48
  )
49
- chat = ChatHuggingFace(llm=llm, verbose=True)
50
  search = GoogleSerperAPIWrapper()
51
  wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
52
  tools = [Tool(
@@ -59,30 +49,22 @@ class BasicAgent:
59
  func=wikipedia.run,
60
  description="useful for when you need to ask with Wikipedia",
61
  )]
62
- chat_with_tools = chat.bind_tools(tools)
63
-
64
- class AgentState(TypedDict):
65
- messages: Annotated[list[AnyMessage], add_messages]
66
-
67
- def assistant(state: AgentState):
68
- return {
69
- "messages": [chat_with_tools.invoke(state["messages"])],
70
- }
71
-
72
- builder = StateGraph(AgentState)
73
- builder.add_node("assistant", assistant)
74
- builder.add_node("tools", ToolNode(tools))
75
- builder.add_edge(START, "assistant")
76
- builder.add_conditional_edges("assistant", tools_condition)
77
- builder.add_edge("tools", "assistant")
78
- self.alfred = builder.compile()
79
-
80
 
81
  def __call__(self, question: str) -> str:
82
- messages = [
83
- SystemMessage(content=self.system_prompt),
84
- HumanMessage(content=question)
85
- ]
86
  def extract_model_answer(full_response: str) -> str:
87
  marker = "FINAL ANSWER:"
88
  idx = full_response.find(marker)
 
31
  HUGGINGFACEHUB_API_TOKEN=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
32
  class BasicAgent:
33
  def __init__(self):
 
 
 
 
 
 
 
 
 
34
  llm = ChatOpenAI(
35
  model = 'gpt-4o-mini',
36
  temperature = 0,
37
  openai_api_key = OPENAI_KEY,
38
  max_tokens = 4096
39
  )
 
40
  search = GoogleSerperAPIWrapper()
41
  wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
42
  tools = [Tool(
 
49
  func=wikipedia.run,
50
  description="useful for when you need to ask with Wikipedia",
51
  )]
52
+
53
+ self.agent = initialize_agent(
54
+ tools=tools,
55
+ llm=llm,
56
+ agent=AgentType.OPENAI_FUNCTIONS,
57
+ verbose=True,
58
+ agent_kwargs={
59
+ "system_message": SystemMessage(content="You are a general AI assistant. I will ask you a question. "
60
+ "Report your thoughts, and finish your answer with the following template: "
61
+ "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. "
62
+ "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. "
63
+ "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. "
64
+ "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." }
65
+ )
 
 
 
 
66
 
67
  def __call__(self, question: str) -> str:
 
 
 
 
68
  def extract_model_answer(full_response: str) -> str:
69
  marker = "FINAL ANSWER:"
70
  idx = full_response.find(marker)