thomassinjo commited on
Commit
8609b68
·
1 Parent(s): 81917a3

First iteration of question answering agent

Browse files
Files changed (4) hide show
  1. .gitignore +3 -0
  2. agent.py +69 -0
  3. app.py +1 -1
  4. requirements.txt +0 -0
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # Environment
2
+ .env
3
+ venv/
agent.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, TypedDict, Annotated
2
+ from langchain_openai import ChatOpenAI
3
+ from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage
4
+ from langgraph.graph.message import add_messages
5
+ from langgraph.graph import START, StateGraph
6
+ from langgraph.prebuilt import ToolNode, tools_condition
7
+ from langchain_community.tools import DuckDuckGoSearchRun
8
+ from langchain.tools import Calculator
9
+ from dotenv import load_dotenv
10
+
11
+
12
+ class AgentState(TypedDict):
13
+ messages: Annotated[list[AnyMessage], add_messages]
14
+
15
+
16
+ search_tool = DuckDuckGoSearchRun()
17
+ calculator = Calculator()
18
+ tools = [search_tool, calculator]
19
+
20
+ load_dotenv()
21
+ llm = ChatOpenAI("gpt-4o")
22
+ llm_with_tools = llm.bind_tools(tools)
23
+
24
+
25
+ def assistant(state: AgentState):
26
+ system_prompt = """
27
+ You are a well-educated research assistant with access to the web and a calculator.
28
+ Please answer the questions by outputting only the answer and nothing else.
29
+ """
30
+
31
+ system_message = SystemMessage(content=system_prompt)
32
+
33
+ return {
34
+ "messages": [llm_with_tools.invoke([system_message] + state["messages"])],
35
+ }
36
+
37
+
38
+
39
+ class Agent:
40
+ """
41
+ A research assistant capable of searching the web and basic arithmetics.
42
+ """
43
+
44
+ def __init__(self):
45
+ """
46
+ Initializes the agent.
47
+ """
48
+ builder = StateGraph(AgentState)
49
+ builder.add_node("assistant", assistant)
50
+ builder.add_node("tools", ToolNode(tools))
51
+
52
+ builder.add_edge(START, "assistant")
53
+ builder.add_conditional_edges("assistant", tools_condition)
54
+
55
+ builder.add_edge("tools", "assistant")
56
+ self.agent = builder.compile()
57
+
58
+ def __call__(self, question: str) -> str:
59
+ """
60
+ Answers a given question.
61
+
62
+ Args:
63
+ question (str): Question to be answered.
64
+
65
+ Returns:
66
+ str: The answer to the question.
67
+ """
68
+ response = self.agent.invoke({"messages": [HumanMessage(content=f"Question:\n {question}")]})
69
+ return response['messages'][-1].content
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
6
 
7
  # (Keep Constants as is)
@@ -10,6 +9,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
13
  class BasicAgent:
14
  def __init__(self):
15
  print("BasicAgent initialized.")
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
 
6
  # (Keep Constants as is)
 
9
 
10
  # --- Basic Agent Definition ---
11
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
12
+ from agent import *
13
  class BasicAgent:
14
  def __init__(self):
15
  print("BasicAgent initialized.")
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ