File size: 2,051 Bytes
b6fae09
 
 
 
 
 
 
6bc07c7
b6fae09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb9324b
b6fae09
 
 
6bc07c7
b6fae09
 
 
6bc07c7
b6fae09
 
 
 
 
 
6bc07c7
32ed98c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""LangGraph Agent"""
import os
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_groq import ChatGroq
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
from langchain_core.messages import SystemMessage
from langgraph.prebuilt import create_react_agent  # Importação corrigida para o LangGraph
from tools import multiply, wiki_search, web_search, arvix_search, execute_python_code, YouTubeVideoAnalysisTool, read_excel_format, transcribe_mp3

load_dotenv()

_AGENT_DIR = os.path.dirname(os.path.abspath(__file__))

# load the system prompt from the file
with open(os.path.join(_AGENT_DIR, "system_prompt.txt"), "r", encoding="utf-8") as f:
    system_prompt = f.read()

# System message
sys_msg = SystemMessage(content=system_prompt)

tools = [
    multiply,
    wiki_search,
    web_search,
    arvix_search,
    execute_python_code,
    YouTubeVideoAnalysisTool,
    read_excel_format,
    transcribe_mp3,
]

# Build graph function
def build_graph(provider: str | None = None):
    if provider is None:
        provider = os.getenv("LLM_PROVIDER", "groq").strip().lower()

    if provider == "google":
        llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0)
    elif provider == "groq":
        model = os.getenv("GROQ_MODEL")
        seed = int(os.getenv("GROQ_SEED", "42"))
        llm = ChatGroq(model=model, temperature=0, model_kwargs={"seed": seed})            
    elif provider == "huggingface":
        # TODO: Add huggingface endpoint. crédits tres limités...
        llm = ChatHuggingFace(
            llm=HuggingFaceEndpoint(                
                url="https://api-inference.huggingface.co/models/Meta-DeepLearning/llama-2-7b-chat-hf",
                temperature=0,
            ),
        )
    else:
        raise ValueError("Invalid provider. Choose 'google', 'groq' or 'huggingface'.")

    # Formato correto e atualizado para as novas versões do LangGraph:
    return create_react_agent(llm, tools, prompt=system_prompt)