File size: 7,916 Bytes
03cd67b
 
ca811b8
c984898
03cd67b
 
 
 
 
0d6f67d
03cd67b
e3e0d7c
03cd67b
 
8715e5d
 
559e643
dd9278b
 
03cd67b
 
 
 
 
 
 
0d6f67d
03cd67b
 
 
 
 
 
 
 
 
 
 
523374b
03cd67b
 
 
ca811b8
 
 
 
 
 
8715e5d
 
 
ca811b8
 
 
 
 
 
 
 
 
6d176e2
559e643
 
 
ca811b8
 
 
03cd67b
 
 
46b6a52
 
 
 
03cd67b
523374b
03cd67b
 
 
 
 
 
 
523374b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03cd67b
 
 
 
 
 
 
 
 
 
 
 
21595fa
523374b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03cd67b
0e3f38f
 
 
 
03cd67b
 
 
 
820aa0b
04e04c8
03cd67b
b0ded06
03cd67b
 
c984898
03cd67b
 
 
 
 
 
 
 
 
 
ca811b8
 
 
 
 
 
523374b
 
ca811b8
523374b
03cd67b
0e3f38f
3c965e9
8abd124
ca811b8
8abd124
ca811b8
 
 
 
 
 
 
890b4e9
03cd67b
 
890b4e9
ca811b8
 
2e3cb7c
03467ba
03cd67b
890b4e9
8abd124
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import os
from typing import TypedDict, List, Dict, Any, Optional
from langchain.agents import create_tool_calling_agent, AgentExecutor, initialize_agent
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.tools import tool
from langchain_core.messages import HumanMessage
from langchain_core.prompts import ChatPromptTemplate

# 1. Web Browsing
from langchain_community.tools import DuckDuckGoSearchResults
from langchain_community.document_loaders import ImageCaptionLoader
import requests, time
import pandas as pd
from pypdf import PdfReader
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_community.document_loaders import YoutubeLoader
from langchain_community.document_loaders import UnstructuredExcelLoader
from langchain_community.document_loaders import AssemblyAIAudioTranscriptLoader

@tool
def web_search(query: str) -> str:
    """Allows search through DuckDuckGo.
    Args:
        query: what you want to search
    """
    search = DuckDuckGoSearchResults()
    results = search.invoke(query)
    return "\n".join(results)

@tool
def visit_webpage(url: str) -> str:
    """Fetches raw HTML content of a web page.
    Args:
        url: the webpage url
    """
    try:
        response = requests.get(url, timeout=5)
        return response.text[:5000]
    except Exception as e:
        return f"[ERROR fetching {url}]: {str(e)}"

@tool
def wiki_search(query: str) -> str:
    """Wiki search tools.
    Args:
        query: what you want to wiki
    """
    api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=100)
    wikipediatool = WikipediaQueryRun(api_wrapper=api_wrapper)
    return wikipediatool.run({"query": query})


@tool
def youtube_transcript(video_url: str) -> str:
    """Fetched youtube transcript
    Args:
        video_url: YouTube video url
    """
    try:
        loader = YoutubeLoader.from_youtube_url(video_url)
        # video_id = video_url.split("v=")[-1].split("&")[0]
        # transcript = YouTubeTranscriptApi.get_transcript(video_id)
        return loader.load()
    except Exception as e:
        return f"Error fetching transcript: {str(e)}"

# 4. File Reading
@tool
def read_file(dir: str) -> str:
    """Read the content of the file
    Args:
        dir: the filepath
    """
    extension = dir.split['.'][-1]
    if extension == 'pdf':
        reader = PdfReader(dir)
        contents = [p.extract_text() for p in reader.pages]
        return "\n".join(contents)
    else:
        with open(dir) as f:
            return f.read()

@tool
def excel_read(dir: str) -> str:
    """Read the content of the excel file
    Args:
        dir: the filepath
    """
    loader = UnstructuredExcelLoader(dir, mode="elements")
    docs = loader.load()
    contents = [doc.page_content for doc in docs]
    return "\n".join(contents)

@tool
def mp3_listen(dir: str) -> str:
    """Listen to the provided mp3 file
    Args:
        dir: the filepath
    """
    loader = AssemblyAIAudioTranscriptLoader(file_path=dir)
    docs = loader.load()
    contents = [doc.page_content for doc in docs]
    return "\n".join(contents)
    
# 5. Image Open
@tool
def image_caption(dir: str) -> str:
    """Understand the content of the provided image
    Args:
        dir: the image url link
    """
    loader = ImageCaptionLoader(images=[dir])
    metadata = loader.load()
    return metadata[0].page_content

# 2. Coding
from langchain_experimental.tools import PythonREPLTool
python_tool = PythonREPLTool()

@tool
def multiply(a: float, b: float) -> float:
    """Multiply two numbers.
    Args:
        a: first float
        b: second float
    """
    return a * b

@tool
def add(a: float, b: float) -> float:
    """Add two numbers.
    Args:
        a: first float
        b: second float
    """
    return a + b

@tool
def subtract(a: float, b: float) -> float:
    """Subtract two numbers.
    Args:
        a: first float
        b: second float
    """
    return a - b

@tool
def divide(a: float, b: float) -> float:
    """Divide two numbers.
    Args:
        a: first float
        b: second float
    """
    if b == 0:
        raise ValueError("Cannot divide by zero.")
    return a / b

# 3. Multi-Modality
# - multiply: multiply two numbers, A and B
# - add: add two numbers, A and B
# - subtract: Subtract A by B with passing A as the first argument
# - divide: Divide A by B with passing A as the first argument

# ("human", f"Question: {question}\nReport to validate: {final_answer}")
class BasicAgent:
    def __init__(self):
        self.model = ChatGoogleGenerativeAI(
            model="gemini-2.0-flash",
            temperature=0,
            max_tokens=128,
            timeout=None,
            max_retries=2,
            google_api_key="AIzaSyAxVUPaGJIgdxB46ZR0RWPKSjB9a63Z80o",
            # other params...
        )
        # System Prompt for few shot prompting
        self.sys_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 separared 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 (eg. 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 put in the list is a number or a string.

                You have access to the following tools:
                - web_search: web search the content of the query by passing the query as input
                - visit_webpage: visit the given webpage url by passing the url as input
                - wiki_search: wiki search the content of the query by passing the query as input if the question asks for wiki search it
                - youtube_transcript: fetch the transcript of the Youtube video by passing the video url as input if the question asks for watching a Youtube video
                - read_file: read the content of the attached file by passing the file directory as input
                - excel_read: read the content of the attached excel file by passing the file directory as input
                - mp3_listen: listen to the content of the attached mp3 file by passing the file directory as input
                - image_caption: understand the visual content of the attached image by passing the image directory as input
                - python_tool: run the python code 
        """
        self.tools = [web_search, visit_webpage, wiki_search, youtube_transcript, read_file, excel_read, mp3_listen, image_caption, python_tool]
        self.prompt = ChatPromptTemplate.from_messages([
            ("system", self.sys_prompt),
            ("human", "{input}")
        ])
        self.agent = initialize_agent(
            tools=self.tools,
            llm=self.model,
            agent="zero-shot-react-description",  # ReAct agent type
            verbose=True,
            system_prompt=self.prompt
        )
        print("BasicAgent initialized.")
    
    def __call__(self, question: str) -> str:
        print(f"Agent received question (first 50 chars): {question[:50]}...")
        # response = self.agent_exe.invoke({"input": f"Question: {question}"})
        # fixed_answer = response['message'][-1].content
        time.sleep(15)
        fixed_answer = self.agent.run(question)
        # fixed_answer = "This is a default answer."
        print(f"Agent returning fixed answer: {fixed_answer}")
        return fixed_answer