File size: 8,726 Bytes
3be3277
 
139805e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d9e0a4
47e2a37
4d9e0a4
47e2a37
6440130
139805e
 
442e89c
139805e
 
3be3277
139805e
3be3277
 
139805e
 
eb0b446
139805e
47e2a37
ba7d685
 
9feda56
 
 
47e2a37
 
 
 
 
ba7d685
 
 
9feda56
 
 
47e2a37
 
 
 
 
ba7d685
 
47e2a37
9feda56
 
 
47e2a37
 
 
 
 
 
139805e
 
9feda56
 
 
139805e
 
 
 
 
9feda56
 
 
139805e
9feda56
139805e
 
9feda56
 
 
139805e
9feda56
139805e
 
9feda56
 
 
139805e
9feda56
139805e
 
9feda56
 
 
139805e
 
 
9feda56
139805e
 
9feda56
 
 
139805e
9feda56
139805e
 
9feda56
 
 
139805e
9feda56
139805e
 
9feda56
 
 
139805e
 
 
 
 
 
 
9feda56
 
 
139805e
 
 
 
 
 
 
 
9feda56
 
 
139805e
 
 
 
 
 
9feda56
 
139805e
 
 
 
 
 
9feda56
 
 
139805e
 
 
 
 
 
 
 
9feda56
 
 
139805e
 
 
 
 
9feda56
 
 
139805e
 
 
 
 
 
9feda56
 
 
139805e
9feda56
 
139805e
 
 
9feda56
 
 
139805e
9feda56
139805e
 
 
442e89c
47e2a37
139805e
 
 
 
 
442e89c
4d9e0a4
139805e
3be3277
139805e
 
9feda56
3269d51
 
 
 
 
 
 
 
6440130
3269d51
 
 
94a8b36
3269d51
41cd066
 
9feda56
 
 
41cd066
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9feda56
 
 
 
 
 
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
import os
from dotenv import load_dotenv
from typing import List, Dict, Any, Optional
import tempfile
import re
import json
import requests
from urllib.parse import urlparse
import pytesseract
from PIL import Image, ImageDraw, ImageFont, ImageEnhance, ImageFilter
import cmath
import pandas as pd
import uuid
import numpy as np
from code_interpreter import CodeInterpreter

# bring in your image processing helpers
from image_processing import encode_image, decode_image, save_image

# LangGraph and tooling imports
from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import ToolNode, tools_condition
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
from langchain_community.vectorstores import SupabaseVectorStore
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from langchain_core.tools import tool
from supabase.client import create_client
from langchain_openai import ChatOpenAI
from langchain_huggingface import HuggingFaceEmbeddings

# Initialize environment
load_dotenv()

# Initialize code interpreter for execute_code_multilang tool
interpreter_instance = CodeInterpreter()

# === TOOL DEFINITIONS ===

@tool
def wiki_search(query: str) -> str:
    """
    Search Wikipedia for a query and return up to 2 formatted results.
    """
    docs = WikipediaLoader(query=query, load_max_docs=2).load()
    return {"wiki_results": "\n\n---\n\n".join(
        f'<Document source="{d.metadata["source"]}" page="{d.metadata.get("page","")}"/>\n{d.page_content}\n</Document>'
        for d in docs
    )}

@tool
def web_search(query: str) -> str:
    """
    Search the web via Tavily for a query and return up to 3 formatted results.
    """
    docs = TavilySearchResults(max_results=3).invoke(query=query)
    return {"web_results": "\n\n---\n\n".join(
        f'<Document source="{d.metadata["source"]}" page="{d.metadata.get("page","")}"/>\n{d.page_content}\n</Document>'
        for d in docs
    )}

@tool
def arxiv_search(query: str) -> str:
    """
    Search arXiv for a query and return up to 3 formatted results.
    """
    docs = ArxivLoader(query=query, load_max_docs=3).load()
    return {"arxiv_results": "\n\n---\n\n".join(
        f'<Document source="{d.metadata["source"]}" page="{d.metadata.get("page","")}"/>\n{d.page_content[:1000]}\n</Document>'
        for d in docs
    )}

@tool
def execute_code_multilang(code: str, language: str = "python") -> str:
    """
    Execute code in multiple languages (Python, Bash, SQL, C, Java) and return execution output.
    """
    return interpreter_instance.execute_code(code, language=language)

# example numeric tools
@tool
def multiply(a: float, b: float) -> float:
    """
    Multiply two numbers and return the product.
    """
    return a * b

@tool
def add(a: float, b: float) -> float:
    """
    Add two numbers and return the sum.
    """
    return a + b

@tool
def subtract(a: float, b: float) -> float:
    """
    Subtract the second number from the first and return the result.
    """
    return a - b

@tool
def divide(a: float, b: float) -> float:
    """
    Divide the first number by the second; raises error if division by zero.
    """
    if b == 0:
        raise ValueError("Cannot divide by zero.")
    return a / b

@tool
def modulus(a: int, b: int) -> int:
    """
    Return the remainder of a divided by b.
    """
    return a % b

@tool
def power(a: float, b: float) -> float:
    """
    Raise a to the power of b and return the result.
    """
    return a ** b

@tool
def square_root(a: float) -> float | complex:
    """
    Return the square root of a number; returns complex for negative inputs.
    """
    if a >= 0:
        return a ** 0.5
    return cmath.sqrt(a)

# file and document tools (save/read, download, OCR, CSV/Excel)
@tool
def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
    """
    Save content to a temporary file and return the file path.
    """
    temp_dir = tempfile.gettempdir()
    filepath = os.path.join(temp_dir, filename or f"file_{uuid.uuid4().hex[:8]}.txt")
    with open(filepath, "w") as f:
        f.write(content)
    return f"Saved to {filepath}"

@tool
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
    """
    Download a file from a URL, save locally, and return the file path or error string.
    """
    try:
        fname = filename or os.path.basename(urlparse(url).path) or f"file_{uuid.uuid4().hex[:8]}"
        path = os.path.join(tempfile.gettempdir(), fname)
        resp = requests.get(url, stream=True)
        resp.raise_for_status()
        with open(path, "wb") as f:
            for chunk in resp.iter_content(8192):
                f.write(chunk)
        return f"Downloaded to {path}"
    except Exception as e:
        return str(e)

@tool
def extract_text_from_image(image_path: str) -> str:
    """
    Extract and return text from an image file using OCR.
    """
    try:
        img = Image.open(image_path)
        return pytesseract.image_to_string(img)
    except Exception as e:
        return str(e)

@tool
def analyze_csv_file(file_path: str, query: str) -> str:
    """
    Analyze a CSV file: return row/column counts and summary statistics.
    """
    df = pd.read_csv(file_path)
    return f"Rows: {len(df)}, Columns: {list(df.columns)}\n{df.describe()}"

@tool
def analyze_excel_file(file_path: str, query: str) -> str:
    """
    Analyze an Excel file: return row/column counts and summary statistics.
    """
    df = pd.read_excel(file_path)
    return f"Rows: {len(df)}, Columns: {list(df.columns)}\n{df.describe()}"

# image analysis/transforms
@tool
def analyze_image(image_base64: str) -> Dict[str, Any]:
    """
    Analyze a base64-encoded image: return dimensions and mode.
    """
    img = decode_image(image_base64)
    w, h = img.size
    return {"dimensions": (w, h), "mode": img.mode}

@tool
def transform_image(image_base64: str, operation: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
    """
    Apply a transformation to a base64-encoded image; placeholder implementation.
    """
    img = decode_image(image_base64)
    # operations logic here
    return {"error": "placeholder"}

# combine all tools into list
tools = [
    wiki_search, web_search, arxiv_search,
    execute_code_multilang,
    multiply, add, subtract, divide, modulus, power, square_root,
    save_and_read_file, download_file_from_url, extract_text_from_image,
    analyze_csv_file, analyze_excel_file,
    analyze_image, transform_image
]

# system prompt loader
with open("system_prompt.txt", "r", encoding="utf-8") as f:
    sys_msg = SystemMessage(content=f.read())

# vectorstore setup (Supabase)
emb = HuggingFaceEmbeddings(
    model_name="sentence-transformers/all-mpnet-base-v2"
)

sup = create_client(
    os.getenv("SUPABASE_URL"),
    os.getenv("SUPABASE_SERVICE_ROLE_KEY")
)
vector_store = SupabaseVectorStore(
    client=sup,
    embedding=emb,
    table_name=os.getenv("VECTORTABLE_NAME","documents2"),
    query_name=os.getenv("VECTOR_QUERY_NAME","match_documents_langchain")
)

def build_graph():
    """
    Build the LangGraph agent using OpenAI ChatGPT only.
    """
    # Initialize the OpenAI LLM
    llm = ChatOpenAI(
        model="gpt-3.5-turbo",
        temperature=0,
        openai_api_key=os.getenv("OPENAI_API_KEY")
    )
    llm_with_tools = llm.bind_tools(tools)

    # Retriever: try vector lookup first, else prompt LLM
    def retriever(state: MessagesState):
        query = state["messages"][0].content
        hits = vector_store.similarity_search(query, k=1)
        if hits:
            return {"messages": [sys_msg, HumanMessage(content=hits[0].page_content)]}
        resp = llm_with_tools.invoke([sys_msg] + state["messages"])
        return {"messages": [resp]}

    # Assistant: always call LLM-with-tools
    def assistant(state: MessagesState):
        resp = llm_with_tools.invoke(state["messages"])
        return {"messages": [resp]}

    # Wire up the graph
    builder = StateGraph(MessagesState)
    builder.add_node("retriever", retriever)
    builder.add_node("assistant", assistant)
    builder.add_node("tools", ToolNode(tools))
    builder.add_edge(START, "retriever")
    builder.add_edge("retriever", "assistant")
    builder.add_conditional_edges("assistant", tools_condition)
    builder.add_edge("tools", "assistant")

    return builder.compile()

# Optional test
if __name__ == "__main__":
    graph = build_graph()
    msgs = graph.invoke({"messages": [HumanMessage(content="Hello world")]})
    for m in msgs["messages"]:
        print(m.content)