Alexandre Gazola commited on
Commit
cf26711
·
1 Parent(s): f844bc4

refactoring

Browse files
Files changed (2) hide show
  1. app.py +1 -59
  2. langchain_agent.py +55 -0
app.py CHANGED
@@ -5,72 +5,14 @@ import requests
5
  import inspect
6
  import pandas as pd
7
  import time
 
8
 
9
- # --- LangChain Imports
10
- import constants
11
- from langchain_google_genai import ChatGoogleGenerativeAI
12
- from langchain.agents import AgentExecutor, create_tool_calling_agent
13
- from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
14
- from langchain_core.messages import SystemMessage
15
-
16
- # --- Custom Tools ---
17
- from wikipedia_tool import wikipedia_revision_by_year_keyword
18
- from count_max_bird_species_tool import count_max_bird_species_in_video
19
- from image_to_text_tool import image_to_text
20
- from internet_search_tool import internet_search
21
- from botanical_classification_tool import get_botanical_classification
22
- from excel_parser_tool import parse_excel
23
- from download_task_file import download_file_as_base64
24
  from utils import get_bytes, get_text_file_contents, get_base64
25
 
26
  # (Keep Constants as is) ok!
27
  # --- Constants ---
28
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
29
 
30
- class LangChainAgent:
31
- def __init__(self):
32
- llm = ChatGoogleGenerativeAI(
33
- model=constants.MODEL,
34
- api_key=constants.API_KEY,
35
- temperature=0.7)
36
-
37
- tools = [
38
- wikipedia_revision_by_year_keyword,
39
- count_max_bird_species_in_video,
40
- image_to_text,
41
- internet_search,
42
- get_botanical_classification,
43
- parse_excel
44
- ]
45
-
46
- prompt = ChatPromptTemplate.from_messages([
47
- SystemMessage(content=(constants.PROMPT_LIMITADOR_LLM)),
48
- MessagesPlaceholder(variable_name="chat_history"),
49
- ("human", "{input}"),
50
- MessagesPlaceholder(variable_name="agent_scratchpad"),
51
- ])
52
-
53
- agent = create_tool_calling_agent(llm, tools, prompt=prompt)
54
- self.executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
55
-
56
- def __call__(self, question: str) -> str:
57
- print(f"LangChain agent received: {question[:50]}...")
58
-
59
- #print("Waiting 60s before answering")
60
- #time.sleep(10) # Delay for 60 seconds
61
-
62
- result = self.executor.invoke({
63
- "input": question,
64
- "chat_history": []
65
- })
66
- output = result.get("output", "No answer returned.")
67
- print(f"Agent response: {output}")
68
- match = re.search(r"FINAL ANSWER:\s*(.*)", output)
69
- if match:
70
- return match.group(1).strip()
71
- else:
72
- return output
73
-
74
  def run_and_submit_all( profile: gr.OAuthProfile | None):
75
  """
76
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
5
  import inspect
6
  import pandas as pd
7
  import time
8
+ from langchain_agent import LangChainAgent
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  from utils import get_bytes, get_text_file_contents, get_base64
11
 
12
  # (Keep Constants as is) ok!
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def run_and_submit_all( profile: gr.OAuthProfile | None):
17
  """
18
  Fetches all questions, runs the BasicAgent on them, submits all answers,
langchain_agent.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import constants
3
+ import time
4
+ from langchain_google_genai import ChatGoogleGenerativeAI
5
+ from langchain.agents import AgentExecutor, create_tool_calling_agent
6
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
7
+ from langchain_core.messages import SystemMessage
8
+
9
+ # --- Custom Tools ---
10
+ from wikipedia_tool import wikipedia_revision_by_year_keyword
11
+ from count_max_bird_species_tool import count_max_bird_species_in_video
12
+ from image_to_text_tool import image_to_text
13
+ from internet_search_tool import internet_search
14
+ from botanical_classification_tool import get_botanical_classification
15
+ from excel_parser_tool import parse_excel
16
+
17
+ class LangChainAgent:
18
+ def __init__(self):
19
+ llm = ChatGoogleGenerativeAI(
20
+ model=constants.MODEL,
21
+ api_key=constants.API_KEY,
22
+ temperature=0.7)
23
+
24
+ tools = [
25
+ wikipedia_revision_by_year_keyword,
26
+ count_max_bird_species_in_video,
27
+ image_to_text,
28
+ internet_search,
29
+ get_botanical_classification,
30
+ parse_excel
31
+ ]
32
+
33
+ prompt = ChatPromptTemplate.from_messages([
34
+ SystemMessage(content=constants.PROMPT_LIMITADOR_LLM),
35
+ MessagesPlaceholder(variable_name="chat_history"),
36
+ ("human", "{input}"),
37
+ MessagesPlaceholder(variable_name="agent_scratchpad"),
38
+ ])
39
+
40
+ agent = create_tool_calling_agent(llm, tools, prompt=prompt)
41
+ self.executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
42
+
43
+ def __call__(self, question: str) -> str:
44
+ print(f"LangChain agent received: {question[:50]}...")
45
+ result = self.executor.invoke({
46
+ "input": question,
47
+ "chat_history": []
48
+ })
49
+ output = result.get("output", "No answer returned.")
50
+ print(f"Agent response: {output}")
51
+ match = re.search(r"FINAL ANSWER:\s*(.*)", output)
52
+ if match:
53
+ return match.group(1).strip()
54
+ else:
55
+ return output