fyerfyer commited on
Commit
bd7109b
·
1 Parent(s): 81917a3

init basic tools

Browse files
Files changed (2) hide show
  1. app.py +68 -8
  2. requirements.txt +10 -1
app.py CHANGED
@@ -1,23 +1,83 @@
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)
8
  # --- Constants ---
9
  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.")
16
- def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
@@ -80,7 +140,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
80
  print(f"Skipping item with missing task_id or question: {item}")
81
  continue
82
  try:
83
- submitted_answer = agent(question_text)
84
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
85
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
86
  except Exception as e:
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
 
6
+ from langchain_openai import ChatOpenAI
7
+ from langchain.agents import create_agent
8
+ from langchain_community.tools import DuckDuckGoSearchRun
9
+ from langchain_experimental.utilities import PythonREPL
10
+ from langchain_core.tools import tool
11
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
12
+
13
  # (Keep Constants as is)
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
 
17
+ search_tool = DuckDuckGoSearchRun()
18
+ python_repl = PythonREPL()
19
+
20
+ @tool
21
+ def python_calculator(code: str):
22
+ """
23
+ Useful for solving math problems, analyzing data, or reading files.
24
+ Input should be valid python code.
25
+ If you need to read a file, assume it is in the current directory.
26
+ ALWAYS print the final result using `print(...)`.
27
+ """
28
+ return python_repl.run(code)
29
+
30
+ REACT_PROMPT = """
31
+ You are a helpful and powerful assistant. Your goal is to answer the user's question accurately.
32
+
33
+ You have access to the following tools:
34
+ {tools}
35
+
36
+ To answer the question, you MUST follow this strict procedure:
37
+ 1. **Thought**: First, think about the user's question and decide if you need a tool.
38
+ 2. **Action**: If you need to use a tool, you MUST use the following JSON format:
39
+ {{
40
+ "thought": "Your reasoning for using this tool and what you plan to do with the result.",
41
+ "tool": "tool_name",
42
+ "tool_input": "the input for the tool"
43
+ }}
44
+
45
+ The `tool_name` must be one of the following: {tool_names}.
46
+
47
+ 3. After getting the result from the tool (the "Observation"), think again. If you have the final answer, proceed to the next step. If not, go back to step 1 and decide if you need to use another tool.
48
+
49
+ 4. **Final Answer**: When you have the final answer, you MUST use the following JSON format. Do not add any other text outside this JSON block:
50
+ {{
51
+ "thought": "I have now found the final answer to the user's question.",
52
+ "final_answer": "The final answer is..."
53
+ }}
54
+ """
55
+
56
  # --- Basic Agent Definition ---
57
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
58
  class BasicAgent:
59
  def __init__(self):
60
+ self.llm = ChatOpenAI(
61
+ model="deepseek-chat",
62
+ temperature=0,
63
+ openai_api_key=os.getenv("OPENAI_API_KEY")
64
+ )
65
+
66
+ self.tools = [search_tool, python_calculator]
67
+
68
+ # Initiate ReAct agent
69
+ self.agent_executor = create_agent(
70
+ model=self.llm,
71
+ tools=self.tools,
72
+ system_prompt=REACT_PROMPT
73
+ )
74
+
75
+ def run(self, question: str) -> str:
76
+ response = self.agent_executor.invoke({
77
+ "input": question
78
+ })
79
+
80
+ return response['output']
81
 
82
  def run_and_submit_all( profile: gr.OAuthProfile | None):
83
  """
 
140
  print(f"Skipping item with missing task_id or question: {item}")
141
  continue
142
  try:
143
+ submitted_answer = agent.run(question_text)
144
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
145
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
146
  except Exception as e:
requirements.txt CHANGED
@@ -1,2 +1,11 @@
1
  gradio
2
- requests
 
 
 
 
 
 
 
 
 
 
1
  gradio
2
+ requests
3
+ langchain
4
+ langchain-community
5
+ langchain-experimental
6
+ langchain-openai
7
+ ddgs
8
+ duckduckgo-search
9
+ pandas
10
+ openpyxl
11
+ tabulate