File size: 9,788 Bytes
1f7e799
 
 
06b7378
f6f5921
1f7e799
4deab6e
3ebf566
 
d8e428c
2eec41f
1f7e799
cdc2562
 
 
 
 
1f7e799
 
a802863
1f7e799
 
 
 
 
 
06b7378
9d189f2
 
2eec41f
9d189f2
 
 
 
1f7e799
3ebf566
 
2eec41f
 
3ebf566
 
2eec41f
 
 
 
 
 
 
1f7e799
2eec41f
1f7e799
9251adf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06b7378
a5a5843
3ebf566
a6f4ba5
 
3ebf566
a5a5843
3ebf566
4deab6e
d8e428c
 
a6f4ba5
 
 
3ebf566
9251adf
a6f4ba5
d8e428c
 
 
 
 
 
 
 
4deab6e
a6f4ba5
9251adf
a5a5843
88ce819
34b8e74
f6f5921
34b8e74
88ce819
 
 
34b8e74
 
 
88ce819
 
34b8e74
 
 
 
 
 
 
88ce819
34b8e74
 
88ce819
 
34b8e74
88ce819
04dbfeb
88ce819
 
34b8e74
f6f5921
 
87e0ee4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a802863
cdc2562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c96f80
 
 
 
bf21663
 
8c96f80
 
 
 
 
 
 
cdc2562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a802863
9d189f2
f6f5921
 
 
5a8170d
a5a5843
8c96f80
ece7512
 
9d189f2
1f7e799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0610dbf
 
 
 
9251adf
1f7e799
 
 
 
 
 
a5a5843
0610dbf
9251adf
 
1f7e799
 
 
 
 
 
 
 
 
 
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from langgraph.graph import StateGraph
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.runnables import RunnableLambda
from langchain_core.tools import tool
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
from langchain_openai import ChatOpenAI
from openai import OpenAI # audio

import os
import requests
import subprocess

from typing import TypedDict, Annotated, Optional, List, Dict, Any
import tempfile
from urllib.parse import urlparse
import uuid

from langgraph.graph.message import AnyMessage, add_messages

from langchain_tavily import TavilySearch
from langgraph.prebuilt import tools_condition
from langgraph.prebuilt import ToolNode
from langgraph.graph import START


# Tools
@tool
def execute_python_code(code_path: str) -> str:
    """
    Execute a Python script and return the final output or error.

    Args:
        code_path (str): the path to the Python file to be executed
    """
    try:
        if not os.path.exists(code_path):
            return f"Error: file not found at {code_path}"
        # Execute the Python file and capture output
        result = subprocess.run(
            ['python', code_path],
            capture_output=True,
            text=True,
            check=True
        )
        return result.stdout
    except subprocess.CalledProcessError as e:
        # Capture any error that occurs during execution
        return f"Execution error: {e.stderr}"
    except Exception as e:
        return f"Unexpected error: {str(e)}"

#@tool
#def speech_to_text(file_path: str) -> str:
#    """
#    Transcribe an audio file from a local path to text.

#    Args:
#        file_path (str): Local path of the audio file to be transcribed.
#    """
#    client = OpenAI()

#    try:
        # Check if the file exists
#        if not os.path.exists(file_path):
#            return f"Error: file not found at {file_path}"

        # Step 2: Transcribe the audio
#        with open(file_path, "rb") as file:
#            transcription = client.audio.transcriptions.create(
#                model="gpt-4o-mini-transcribe",
#                file=file
#            )
#        print(f"Transcription result: {transcription['text']}")
#        return transcription["text"]
#    except Exception as e:
#        return f"Error during transcription: {str(e)}"

@tool
def speech_to_text(file_path: str) -> str:
    """
    Transcribe an audio file from a local path to text.

    Args:
        file_path (str): Local path of the audio file to be transcribed.
    """
    client = OpenAI()

    try:
        # Check if the file exists
        if not os.path.exists(file_path):
            return f"Error: file not found at {file_path}"

        # Transcribe the audio
        with open(file_path, "rb") as file:
            transcription = client.audio.transcriptions.create(
                model="gpt-4o-mini-transcribe",
                file=file
            )
        print(f"Transcription result: {transcription['text']}")
        return transcription["text"]
    except Exception as e:
        return f"Error during transcription: {str(e)}"




@tool
def web_search(query: str) -> str:
    """
    Search Tavily for a query and return formatted results.

    Args:
        query (str): The search query.

    Returns:
        str: A formatted string with the search results.
    """
    try:
        search_tool = TavilySearch(max_results=3, topic="general")
        search_response = search_tool.invoke(input=query)

        # Check if the response contains results
        if search_response and "results" in search_response:
            results = search_response["results"]
            formatted_results = "\n\n---\n\n".join(
                [
                    f"Title: {result['title']}\nURL: {result['url']}\nContent: {result['content']}"
                    for result in results
                ]
            )
            return formatted_results
        else:
            return "No results found."
    except Exception as e:
        print(f"Error during web search: {str(e)}")
        return f"Error during web search: {str(e)}"


@tool
def arvix_search(query: str) -> str:
    """
    Search Arxiv for a query and return maximum 3 results.
    Args:
        query: The search query.
    """
    try:
        search_docs = ArxivLoader(query=query, load_max_docs=3).load()
        return "\n\n---\n\n".join(
            [
                f'<Document source="{doc.metadata.get("source", "Unknown")}" page="{doc.metadata.get("page", "N/A")}"/>\n{doc.page_content[:1000]}\n</Document>'
                for doc in search_docs
            ]
        )
    except Exception as e:
        return f"Error during Arxiv search: {str(e)}"

@tool
def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
    """
    Save content to a file and return the path.
    Args:
        content (str): the content to save to the file
        filename (str, optional): the name of the file. If not provided, a random name file will be created.
    """
    temp_dir = tempfile.gettempdir()
    if filename is None:
        temp_file = tempfile.NamedTemporaryFile(delete=False, dir=temp_dir)
        filepath = temp_file.name
    else:
        filepath = os.path.join(temp_dir, filename)

    with open(filepath, "w") as f:
        f.write(content)

    return f"File saved to {filepath}. You can read this file to process its contents."

@tool
def read_file(file_path: str) -> str:
    """
    Return the raw text of a local file.
    Args:
        file_path (str): Local path of the file to be read.
    """
    try:
        with open(file_path, "r", encoding="utf‑8", errors="ignore") as f:
            return f.read()
    except Exception as e:
        return f"Error reading {file_path}: {e}"



@tool
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
    """
    Download a file from a URL and save it to a temporary location.
    Args:
        url (str): the URL of the file to download.
        filename (str, optional): the name of the file. If not provided, a random name file will be created.
    """
    try:
        # Parse URL to get filename if not provided
        if not filename:
            path = urlparse(url).path
            filename = os.path.basename(path)
        if not filename:
            filename = f"downloaded_{uuid.uuid4().hex[:8]}"

        # Create temporary file
        temp_dir = tempfile.gettempdir()
        filepath = os.path.join(temp_dir, filename)

        # Download the file
        response = requests.get(url, stream=True)
        response.raise_for_status()

        # Save the file
        with open(filepath, "wb") as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)

        return f"File downloaded to {filepath}. You can read this file to process its contents."
    except Exception as e:
        return f"Error downloading file: {str(e)}"


tools = [
    execute_python_code,
    speech_to_text,
    web_search,
    #arvix_search,
    #wiki_search
    read_file,
    save_and_read_file,
    download_file_from_url
]


# State
class DummyState(TypedDict):
    messages: Annotated[list[AnyMessage], add_messages]


# Agent
class ReActAgent:
    def __init__(
        self,
        system_prompt_path: str = "prompts/system_prompt.txt",
        model: str = "gpt-4o"
        ) -> None:

        # Prompt
        self.system_prompt = self.read_system_prompt(system_prompt_path)
        # Initialize the LLM with tools
        self.llm = ChatOpenAI(
            model=model,
            temperature=0
        ).bind_tools(tools)

        # Create a state graph
        self.compiled_graph = (
            StateGraph(DummyState)
            .add_node("llm", RunnableLambda(self.llm_response))
            .add_node("tools", ToolNode(tools))
            .add_edge(START, "llm")
            .add_conditional_edges(
                "llm",
                # If the latest message (result) from assistant is a tool call -> tools_condition routes to tools
                # If the latest message (result) from assistant is a not a tool call -> tools_condition routes to END
                tools_condition,
            )
            .add_edge("tools", "llm")
            .compile()
        )

    def read_system_prompt(self, path:str) -> str:
        with open(path, "r") as file:
            return file.read()

    def llm_response(self, state: DummyState) -> dict:
        print("LLM node called with state:", state)
        response = self.llm.invoke(state["messages"])
        return {"messages": [response]}

    def extract_final_answer(self, response: str) -> str:
        if "FINAL ANSWER:" in response:
            answer = response.split("FINAL ANSWER:")[1].strip().replace(".", "")
        else:
            # fallback if model did not follow instruction perfectly
            answer = response.strip()
        # Remove trailing period, but only at the end
        if answer.endswith("."):
            answer = answer[:-1].strip()
        return answer

    def __call__(
        self, question: str,
        #file_path: str=None
        ) -> str:

        inputs = {
            "messages": [
                SystemMessage(content=self.system_prompt),
                HumanMessage(content=question)
            ]
        }
         # Add file path if available
        #inputs["file_path"] = file_path or None # type: ignore

        # Run the graph with the inputs
        result = self.compiled_graph.invoke(
            inputs,
            config={
                "configurable": {"thread_id": "benchmark-test"}
            }
        )
        final_msg = result["messages"][-1].content

        return self.extract_final_answer(final_msg)