File size: 3,140 Bytes
9c49c2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
from typing import Annotated, Optional, TypedDict

from dotenv import find_dotenv, load_dotenv
from langchain.chat_models import init_chat_model
from langchain_core.messages import AnyMessage, HumanMessage
from langgraph.graph.message import add_messages
from langgraph.prebuilt import create_react_agent

from tools import (add, ask_about_image, divide, get_current_time_and_date,
                   get_sum, get_weather_info, get_youtube_transcript,
                   get_youtube_video_info, inspect_file_as_text, multiply,
                   reverse_text, subtract, visit_website, web_search,
                   wiki_search)


class AgentState(TypedDict):
    input_file: Optional[str]  # Contains file path
    messages: Annotated[list[AnyMessage], add_messages]


class BasicAgent:
    def __init__(self):
        load_dotenv(find_dotenv())
        model = init_chat_model("groq:meta-llama/llama-4-scout-17b-16e-instruct")
        system_prompt = (
            "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."
            "Give it all you can: I know for a fact that you have access to all the relevant tools to solve it and find "
            "the correct answer (the answer does exist). Failure or 'I cannot answer' or 'None found' will not be "
            "tolerated, success will be rewarded. Run verification steps if that's needed, you must make sure you find "
            "the correct answer! "
        )
        tools = [
            get_weather_info,
            add,
            get_sum,
            subtract,
            multiply,
            divide,
            get_current_time_and_date,
            wiki_search,
            web_search,
            visit_website,
            inspect_file_as_text,
            ask_about_image,
            reverse_text,
            get_youtube_video_info,
            get_youtube_transcript,
        ]

        self.agent = create_react_agent(model=model, tools=tools, prompt=system_prompt)
        print("BasicAgent initialized.")

    def __call__(self, question: str) -> str:
        print(f"Agent received question (first 50 chars): {question[:50]}...")
        messages = [HumanMessage(content=question)]
        response = self.agent.invoke({"messages": messages})
        response_string = response["messages"][-1].content
        print(f"Agent's response: {response_string}")
        return response_string