Spaces:
Sleeping
Sleeping
Alexandre Gazola
commited on
Commit
·
857c202
1
Parent(s):
f66d8b7
codigo inicial agente com langchain
Browse files- app.py +53 -14
- constants.py +3 -3
- requirements.txt +16 -1
app.py
CHANGED
@@ -4,22 +4,60 @@ 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 |
-
|
12 |
-
|
13 |
-
# --- Basic Agent Definition ---
|
14 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
15 |
-
class BasicAgent:
|
16 |
def __init__(self):
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def __call__(self, question: str) -> str:
|
19 |
-
print(f"
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
25 |
"""
|
@@ -42,10 +80,11 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
42 |
|
43 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
44 |
try:
|
45 |
-
agent =
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
49 |
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
50 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
51 |
print(agent_code)
|
|
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
|
7 |
+
# --- LangChain Imports ---
|
8 |
+
import constants
|
9 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
10 |
+
from langchain.agents import AgentExecutor, create_tool_calling_agent
|
11 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
12 |
+
from langchain_core.messages import SystemMessage
|
13 |
+
|
14 |
+
# --- Custom Tools ---
|
15 |
+
from wikipedia_tool import wikipedia_revision_by_year_keyword
|
16 |
+
from count_max_distinct_bird_species_tool import count_max_distinct_bird_species_in_video
|
17 |
+
from image_to_text_tool import image_to_text
|
18 |
+
from internet_search_tool import internet_search
|
19 |
+
from botanical_classification_tool import get_botanical_classification
|
20 |
+
from excel_parser_tool import parse_excel
|
21 |
+
|
22 |
# (Keep Constants as is)
|
23 |
# --- Constants ---
|
24 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
25 |
|
26 |
+
class LangChainAgent:
|
|
|
|
|
|
|
|
|
27 |
def __init__(self):
|
28 |
+
llm = ChatGoogleGenerativeAI(
|
29 |
+
model=constants.MODEL,
|
30 |
+
api_key=constants.API_KEY,
|
31 |
+
temperature=0.7)
|
32 |
+
|
33 |
+
tools = [
|
34 |
+
wikipedia_revision_by_year_keyword,
|
35 |
+
count_max_distinct_bird_species_in_video,
|
36 |
+
image_to_text,
|
37 |
+
internet_search,
|
38 |
+
get_botanical_classification,
|
39 |
+
parse_excel
|
40 |
+
]
|
41 |
+
|
42 |
+
prompt = ChatPromptTemplate.from_messages([
|
43 |
+
SystemMessage(content=(constants.PROMPT_LIMITADOR_LLM)),
|
44 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
45 |
+
("human", "{input}"),
|
46 |
+
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
47 |
+
])
|
48 |
+
|
49 |
+
agent = create_tool_calling_agent(llm, tools, prompt=prompt)
|
50 |
+
self.executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
51 |
+
|
52 |
def __call__(self, question: str) -> str:
|
53 |
+
print(f"LangChain agent received: {question[:50]}...")
|
54 |
+
result = self.executor.invoke({
|
55 |
+
"input": question,
|
56 |
+
"chat_history": []
|
57 |
+
})
|
58 |
+
answer = result.get("output", "No answer returned.")
|
59 |
+
print(f"Agent response: {answer}")
|
60 |
+
return answer
|
61 |
|
62 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
63 |
"""
|
|
|
80 |
|
81 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
82 |
try:
|
83 |
+
agent = LangChainAgent()
|
84 |
+
except Exception as e:
|
85 |
+
print(f"Error instantiating agent: {e}")
|
86 |
+
return f"Error initializing agent: {e}", None
|
87 |
+
|
88 |
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
89 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
90 |
print(agent_code)
|
constants.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
# MODEL = 'gemini-2.0-flash'
|
2 |
MODEL = 'gemini-2.0-flash-exp'
|
3 |
# MODEL = 'gemini-2.5-pro-exp-03-25'
|
@@ -5,9 +7,7 @@ MODEL = 'gemini-2.0-flash-exp'
|
|
5 |
# gemini-2.0-flash
|
6 |
# gemini-2.5-pro-exp-03-25
|
7 |
|
8 |
-
API_KEY =
|
9 |
-
|
10 |
-
OPENAI_KEY = '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'
|
11 |
|
12 |
PROMPT_LIMITADOR_LLM = """
|
13 |
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: 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. 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. 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. 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.
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
# MODEL = 'gemini-2.0-flash'
|
4 |
MODEL = 'gemini-2.0-flash-exp'
|
5 |
# MODEL = 'gemini-2.5-pro-exp-03-25'
|
|
|
7 |
# gemini-2.0-flash
|
8 |
# gemini-2.5-pro-exp-03-25
|
9 |
|
10 |
+
API_KEY = os.getenv("GEMINI_API_KEY")
|
|
|
|
|
11 |
|
12 |
PROMPT_LIMITADOR_LLM = """
|
13 |
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: 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. 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. 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. 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.
|
requirements.txt
CHANGED
@@ -1,2 +1,17 @@
|
|
1 |
gradio
|
2 |
-
requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
gradio
|
2 |
+
requests
|
3 |
+
certifi
|
4 |
+
duckduckgo_search
|
5 |
+
httpx
|
6 |
+
langchain
|
7 |
+
langchain-core
|
8 |
+
langchain-google-genai
|
9 |
+
langchain-openai
|
10 |
+
langgraph
|
11 |
+
numpy
|
12 |
+
openai
|
13 |
+
pandas
|
14 |
+
pytube
|
15 |
+
ultralytics
|
16 |
+
whisper
|
17 |
+
yt-dlp
|