Update app.py
Browse files
app.py
CHANGED
|
@@ -6,23 +6,82 @@ import pandas as pd
|
|
| 6 |
#from tavily import TavilyClient
|
| 7 |
from langchain_tavily import TavilySearch
|
| 8 |
from langgraph.prebuilt import create_react_agent
|
| 9 |
-
from
|
| 10 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
|
|
|
|
| 16 |
def _set_if_undefined(var: str):
|
| 17 |
if not os.environ.get(var):
|
| 18 |
os.environ[var] = userdata.get(var)
|
| 19 |
|
| 20 |
_set_if_undefined("TAVILY_API_KEY")
|
| 21 |
-
_set_if_undefined("
|
|
|
|
| 22 |
|
| 23 |
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# --- Basic Agent Definition ---
|
| 27 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 28 |
class BasicAgent:
|
|
@@ -37,23 +96,6 @@ class BasicAgent:
|
|
| 37 |
# chat_with_tools = chat.bind_tools(tools)
|
| 38 |
|
| 39 |
def __call__(self, question: str) -> str:
|
| 40 |
-
# Generate the chat interface, including the tools
|
| 41 |
-
print("Generate the chat interface, including the tools")
|
| 42 |
-
llm = HuggingFaceEndpoint(
|
| 43 |
-
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 44 |
-
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
print("ChatHuggingFace")
|
| 48 |
-
chat = ChatHuggingFace(llm=llm, verbose=True)
|
| 49 |
-
|
| 50 |
-
# Initialize Tavily Search Tool
|
| 51 |
-
print("Initialize Tavily Search Tool")
|
| 52 |
-
tavily_search_tool = TavilySearch(
|
| 53 |
-
max_results=5,
|
| 54 |
-
topic="general",
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 58 |
|
| 59 |
messages = [
|
|
|
|
| 6 |
#from tavily import TavilyClient
|
| 7 |
from langchain_tavily import TavilySearch
|
| 8 |
from langgraph.prebuilt import create_react_agent
|
| 9 |
+
from langgraph.graph.message import add_messages
|
| 10 |
+
from langgraph_supervisor import create_supervisor
|
| 11 |
+
#from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 12 |
+
from langchain_core.tools import tool
|
| 13 |
+
from langchain_core.messages import HumanMessage, SystemMessage, BaseMessage
|
| 14 |
+
from langchain.chat_models import init_chat_model
|
| 15 |
+
from typing import Annotated,Sequence, TypedDict
|
| 16 |
|
| 17 |
# (Keep Constants as is)
|
| 18 |
# --- Constants ---
|
| 19 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 20 |
|
| 21 |
+
#ENV
|
| 22 |
def _set_if_undefined(var: str):
|
| 23 |
if not os.environ.get(var):
|
| 24 |
os.environ[var] = userdata.get(var)
|
| 25 |
|
| 26 |
_set_if_undefined("TAVILY_API_KEY")
|
| 27 |
+
_set_if_undefined("GEMINI_API_KEY")
|
| 28 |
+
##_set_if_undefined("HUGGINGFACEHUB_API_TOKEN")
|
| 29 |
|
| 30 |
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
| 31 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 32 |
+
##HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 33 |
+
|
| 34 |
+
#PROMPTS
|
| 35 |
+
prompt_recomendado = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 36 |
+
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.
|
| 37 |
+
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.
|
| 38 |
+
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.
|
| 39 |
+
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."""
|
| 40 |
+
|
| 41 |
+
prompt_search = """You are a web research agent.
|
| 42 |
+
INSTRUCTIONS:
|
| 43 |
+
Assist ONLY with research-related tasks, DO NOT do any math
|
| 44 |
+
After you're done with your tasks, respond to the supervisor directly
|
| 45 |
+
Respond ONLY with the results of your work, do NOT include ANY other text."""
|
| 46 |
+
|
| 47 |
+
#TOOLS
|
| 48 |
+
web_search = TavilySearch(
|
| 49 |
+
max_results=5,
|
| 50 |
+
topic="general",
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
tools = [web_search]
|
| 54 |
+
|
| 55 |
+
#LLMS
|
| 56 |
+
# Create LLM class
|
| 57 |
+
#gemini_llm = ChatGoogleGenerativeAI(
|
| 58 |
+
# model= "gemini-2.0-flash", # replace with "gemini-2.0-flash"
|
| 59 |
+
# temperature=1.0,
|
| 60 |
+
# max_tokens=None,
|
| 61 |
+
# timeout=None,
|
| 62 |
+
# max_retries=2,
|
| 63 |
+
# google_api_key=GEMINI_API_KEY,
|
| 64 |
+
#)
|
| 65 |
+
# Bind tools to the model
|
| 66 |
+
#general_model = gemini_llm.bind_tools([get_weather_forecast])
|
| 67 |
+
|
| 68 |
+
#AGENTS
|
| 69 |
+
web_research_agent = create_react_agent(
|
| 70 |
+
model="gemini-2.0-flash", # gpt-4o-mini-2024-07-18
|
| 71 |
+
tools=[web_search],
|
| 72 |
+
prompt=prompt_search,
|
| 73 |
+
name="web_research_agent",
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
supervisor = create_supervisor(
|
| 77 |
+
model=init_chat_model("gemini-2.0-flash"),
|
| 78 |
+
agents=[web_research_agent],
|
| 79 |
+
prompt=prompt_recomendado,
|
| 80 |
+
add_handoff_back_messages=True,
|
| 81 |
+
output_mode="full_history",
|
| 82 |
+
).compile()
|
| 83 |
+
|
| 84 |
+
|
| 85 |
# --- Basic Agent Definition ---
|
| 86 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 87 |
class BasicAgent:
|
|
|
|
| 96 |
# chat_with_tools = chat.bind_tools(tools)
|
| 97 |
|
| 98 |
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 100 |
|
| 101 |
messages = [
|