File size: 7,303 Bytes
e57c8b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# AnssiO 17/08/2025

from langgraph.graph import StateGraph, START, END
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain_experimental.tools.python.tool import PythonREPLTool
from youtube_transcript_api import YouTubeTranscriptApi
from urllib.parse import urlparse, parse_qs
import os
from langchain_core.messages import SystemMessage, HumanMessage, ToolMessage
from langgraph.graph import MessagesState
from langchain_tavily import TavilySearch
from huggingface_hub import InferenceClient
import time
import requests
from io import BytesIO
from pypdf import PdfReader
from bs4 import BeautifulSoup
from markdownify import markdownify as md

openai_key = os.getenv("OPENAI_API_KEY")
os.environ["OPENAI_API_KEY"] = openai_key
tavily_key = os.getenv("TAVILY_API_KEY")
os.environ["TAVILY_API_KEY"] = tavily_key


@tool
def youtube_transcript(url: str) -> str:
    """Get the transcript of a YouTube video from the full URL."""
    def extract_video_id(url):
        parsed = urlparse(url)
        if parsed.hostname == "youtu.be":
            return parsed.path[1:]
        elif "youtube.com" in parsed.hostname:
            return parse_qs(parsed.query).get("v", [None])[0]
        return None

    video_id = extract_video_id(url)
    if not video_id:
        return "Invalid YouTube URL."
    transcript = YouTubeTranscriptApi.get_transcript(video_id)
    return "\n".join([t["text"] for t in transcript])

@tool
def describe_image_url(image_url: str) -> str:
    """Describe an image from a public URL using GPT-4o mini."""
    client = ChatOpenAI(model="gpt-4o-mini", temperature=0, max_tokens=10_000)
    response = client.invoke([
        {"role": "user", "content": [
            {"type": "text", "text": "Describe this image."},
            {"type": "image_url", "image_url": {"url": image_url}}
        ]}
    ])
    return response.content

@tool
def calculator(expression: str) -> str:
    """Evaluate a basic math expression."""
    try:
        return str(eval(expression))
    except Exception as e:
        return f"Error: {e}"
    
@tool
def get_webpage(page_url: str) -> str:
    """Load a web page and return it to markdown if possible"""
    try:
        r = requests.get(page_url)
        r.raise_for_status()
        text = ""
        # special case if page is a PDF file
        if r.headers.get('Content-Type', '') == 'application/pdf':
            pdf_file = BytesIO(r.content)
            reader = PdfReader(pdf_file)
            for page in reader.pages:
                text += page.extract_text()
        else:
            soup = BeautifulSoup((r.text), 'html.parser')
            if soup.body:
                # convert to markdown
                text = md(str(soup.body))
            else:
                # return the raw content
                text = r.text
        return text
    except Exception as e:
        return f"get_webpage_content failed: {e}"

search_tool = TavilySearch(api_key=tavily_key)

python_tool = PythonREPLTool()


tools = [
    calculator,
    search_tool,
    python_tool,
    get_webpage,
    youtube_transcript,
    describe_image_url,
]


llm = ChatOpenAI(model="gpt-4o-mini", temperature=0, max_tokens=16384)

tools_by_name = {tool.name: tool for tool in tools}
llm_with_tools = llm.bind_tools(tools)


system_prompt = """\
You are a general AI assistant with tools.
I will ask you a question. Use your tools, and 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 number, just give your FINAL ANSWER as that number.
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.
If you are asked to give the answer without abbreviations, please use the full spelling instead of abbreviations, e.g., transform Mr. to Mister, Dr. to Doctor, or St. to Saint.
If you use the python_repl tool (code interpreter), always end your code with `print(...)` to see the output.
"""


def tool_node(state: dict):
    
    result = []
    for tool_call in state["messages"][-1].tool_calls:
        tool = tools_by_name[tool_call["name"]]
        observation = tool.invoke(tool_call["args"])
        result.append(ToolMessage(content=observation, tool_call_id=tool_call["id"]))
    return {"messages": result}    


def llm_decision_node(state: MessagesState):   
    messages = state["messages"]
    response = [llm_with_tools.invoke([SystemMessage(system_prompt)]+messages)]
    return {"messages": response + messages}


def condition_router(state: MessagesState) -> str:
    last_msg = state["messages"][-1]
    if last_msg.tool_calls:
        return "continue"
    return END


builder = StateGraph(MessagesState)

# Nodes
builder.add_node("tool_node", tool_node)
builder.add_node("llm_decision", llm_decision_node)

# # Entry
builder.add_edge(START, "llm_decision")

# # Conditional loop back or exit
builder.add_conditional_edges("llm_decision", condition_router, {
     END: END,
     "continue": "tool_node"
})

builder.add_edge("tool_node", "llm_decision")

agent = builder.compile()


class BasicAgent:
    def __init__(self):
        print("BasicAgent initialized.")
    def __call__(self, question: str, file_name_text="") -> str:
        print(f"Agent received question (first 50 chars): {question[:50]}...")
        # create the input
        if file_name_text:
            file_name, suffix = file_name_text.split(".")
            if suffix == "mp3":
                client = InferenceClient(provider="fal-ai")
                file_url = "https://agents-course-unit4-scoring.hf.space/files/" + file_name
                try:
                    audio_text = client.automatic_speech_recognition(file_url, model="openai/whisper-large-v3")
                    question = question + " The attached audio has been translated to text. Here is the text: " + audio_text
                except:
                    question = question + " File URL:" + " 'https://agents-course-unit4-scoring.hf.space/files/" + file_name + "' (." + suffix + " file)"
            else:
                question = question + " File URL:" + " 'https://agents-course-unit4-scoring.hf.space/files/" + file_name + "' (." + suffix + " file)"
        messages = [HumanMessage(content=question)]
        
        # call the agent
        messages = agent.invoke(
            {"messages": messages},
            {"recursion_limit": 30}
        ) # maximum number of steps before hitting a stop condition
           
        # post-process the response (keep only what's after "FINAL ANSWER:" for the exact match)
        answer = str(messages["messages"][-1].content)
        try:
            answer = answer.split("FINAL ANSWER:")[-1].strip()
        except:
            print('Error in splitting final answer')

        print(f"Agent returning the answer: {answer}")
        return answer