File size: 10,857 Bytes
6accb61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Database retrieval tools for GAIA question similarity search.
Connects to Supabase database to find similar questions and answers.
Combines efficiency of LangChain SupabaseVectorStore with custom logic.
"""

import os
import json
from typing import List, Dict, Optional, Tuple
from supabase import create_client, Client
from langchain_openai import OpenAIEmbeddings
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import SupabaseVectorStore
from langchain_core.tools import tool

class GAIADatabaseRetriever:
    """Handles similarity search against the GAIA Q&A database with dual embedding support."""
    
    def __init__(self, use_huggingface: bool = True):
        # Initialize Supabase client
        self.supabase_url = os.getenv("SUPABASE_URL")
        self.supabase_key = os.getenv("SUPABASE_SERVICE_KEY") or os.getenv("SUPABASE_KEY")
        
        if not self.supabase_url or not self.supabase_key:
            raise ValueError("SUPABASE_URL and SUPABASE_SERVICE_KEY (or SUPABASE_KEY) must be set in environment variables")
        
        self.supabase: Client = create_client(self.supabase_url, self.supabase_key)
        
        # Choose embedding model
        if use_huggingface:
            try:
                # Use HuggingFace embeddings (free and often better for similarity)
                self.embeddings = HuggingFaceEmbeddings(
                    model_name="sentence-transformers/all-mpnet-base-v2"
                )
                self.embedding_model = "huggingface"
            except ImportError:
                print("⚠️  HuggingFace embeddings not available, falling back to OpenAI")
                self.embeddings = OpenAIEmbeddings(
                    model="text-embedding-3-small",
                    openai_api_key=os.getenv("OPENAI_API_KEY")
                )
                self.embedding_model = "openai"
        else:
            # Use OpenAI embeddings
            self.embeddings = OpenAIEmbeddings(
                model="text-embedding-3-small",
                openai_api_key=os.getenv("OPENAI_API_KEY")
            )
            self.embedding_model = "openai"
        
        # Initialize vector store
        try:
            self.vector_store = SupabaseVectorStore(
                client=self.supabase,
                embedding=self.embeddings,
                table_name="documents",
                query_name="match_documents_langchain",  # Assumes you have this function
            )
            self.use_vector_store = True
        except Exception as e:
            print(f"⚠️  Vector store not available: {e}")
            print("Falling back to manual similarity search")
            self.use_vector_store = False
    
    def search_similar_questions_efficient(self, question: str, top_k: int = 3) -> List[Dict]:
        """
        Efficient search using LangChain SupabaseVectorStore.
        """
        try:
            if not self.use_vector_store:
                return self.search_similar_questions_manual(question, top_k)
            
            # Use LangChain's efficient vector search
            docs = self.vector_store.similarity_search(question, k=top_k)
            
            similar_docs = []
            for doc in docs:
                page_content = doc.page_content
                
                # Extract question and answer from page_content
                if 'Q:' in page_content and 'A:' in page_content:
                    parts = page_content.split('A:')
                    if len(parts) >= 2:
                        question_part = parts[0].replace('Q:', '').strip()
                        answer_part = parts[1].strip()
                        
                        similar_docs.append({
                            'id': doc.metadata.get('id', 'unknown'),
                            'question': question_part,
                            'answer': answer_part,
                            'similarity': doc.metadata.get('similarity', 0.8),  # Estimated
                            'page_content': page_content
                        })
            
            return similar_docs
            
        except Exception as e:
            print(f"Error in efficient search: {e}")
            return self.search_similar_questions_manual(question, top_k)
    
    def search_similar_questions_manual(self, question: str, top_k: int = 3, similarity_threshold: float = 0.75) -> List[Dict]:
        """
        Fallback manual search with precise similarity scoring.
        """
        try:
            # Get embedding for the input question
            query_embedding = self.embeddings.embed_query(question)
            
            # Fetch all documents from Supabase
            response = self.supabase.table("documents").select("*").execute()
            
            if not response.data:
                return []
            
            # Calculate similarities manually
            similar_docs = []
            
            for doc in response.data:
                # Parse the stored embedding
                try:
                    stored_embedding = json.loads(doc['embedding'])
                except:
                    continue
                
                # Calculate cosine similarity (manual implementation)
                dot_product = sum(a * b for a, b in zip(query_embedding, stored_embedding))
                norm_a = sum(a * a for a in query_embedding) ** 0.5
                norm_b = sum(b * b for b in stored_embedding) ** 0.5
                
                if norm_a == 0 or norm_b == 0:
                    continue
                    
                similarity = dot_product / (norm_a * norm_b)
                
                # Extract question and answer from page_content
                page_content = doc['page_content']
                if 'Q:' in page_content and 'A:' in page_content:
                    parts = page_content.split('A:')
                    if len(parts) >= 2:
                        question_part = parts[0].replace('Q:', '').strip()
                        answer_part = parts[1].strip()
                        
                        if similarity >= similarity_threshold:
                            similar_docs.append({
                                'id': doc['id'],
                                'question': question_part,
                                'answer': answer_part,
                                'similarity': float(similarity),
                                'page_content': page_content
                            })
            
            # Sort by similarity
            similar_docs.sort(key=lambda x: x['similarity'], reverse=True)
            return similar_docs[:top_k]
            
        except Exception as e:
            print(f"Error in manual search: {e}")
            return []

# Initialize the retriever lazily to avoid import errors when env vars are missing
retriever = None

def get_retriever():
    """Get the database retriever, initializing it if needed."""
    global retriever
    if retriever is None:
        retriever = GAIADatabaseRetriever(use_huggingface=True)
    return retriever

@tool
def create_retriever_from_supabase(query: str) -> str:
    """
    Search for similar documents in the Supabase vector store using efficient LangChain integration.
    This tool uses semantic search to find documents that are semantically similar to the provided query.
    
    Args:
        query (str): The search query to find similar documents.
    
    Returns:
        str: A formatted list of documents that are semantically similar to the query.
    """
    try:
        retriever = get_retriever()
        similar_questions = retriever.search_similar_questions_efficient(query, top_k=3)
        
        if not similar_questions:
            return "No similar questions found in the database."
        
        result = f"Found {len(similar_questions)} similar questions:\n\n"
        
        for i, doc in enumerate(similar_questions, 1):
            result += f"Similar Question {i}:\n"
            result += f"Q: {doc['question']}\n"
            result += f"A: {doc['answer']}\n"
            result += "-" * 50 + "\n"
        
        return result
        
    except Exception as e:
        return f"Error searching database: {str(e)}"

@tool
def search_similar_gaia_questions(question: str, max_results: int = 3) -> str:
    """
    Search for similar GAIA questions in the database with precise similarity scoring.
    
    Args:
        question: The question to search for
        max_results: Maximum number of similar questions to return (default: 3)
    
    Returns:
        Formatted string with similar questions and their answers
    """
    try:
        retriever = get_retriever()
        similar_questions = retriever.search_similar_questions_manual(
            question, 
            top_k=max_results, 
            similarity_threshold=0.75
        )
        
        if not similar_questions:
            return "No similar questions found in the database."
        
        result = f"Found {len(similar_questions)} similar questions:\n\n"
        
        for i, doc in enumerate(similar_questions, 1):
            result += f"Similar Question {i} (Similarity: {doc['similarity']:.3f}):\n"
            result += f"Q: {doc['question']}\n"
            result += f"A: {doc['answer']}\n"
            result += "-" * 50 + "\n"
        
        return result
        
    except Exception as e:
        return f"Error searching database: {str(e)}"

@tool
def get_exact_answer_if_highly_similar(question: str, similarity_threshold: float = 0.95) -> str:
    """
    Get the exact answer if a highly similar question exists in the database.
    
    Args:
        question: The question to search for
        similarity_threshold: High threshold for considering an exact match (default: 0.95)
    
    Returns:
        The answer if found, or indication that no exact match exists
    """
    try:
        retriever = get_retriever()
        similar_questions = retriever.search_similar_questions_manual(
            question, 
            top_k=1, 
            similarity_threshold=similarity_threshold
        )
        
        if similar_questions:
            best_match = similar_questions[0]
            return f"EXACT_MATCH_FOUND: {best_match['answer']}"
        else:
            return "NO_EXACT_MATCH: Proceed with normal agent processing"
            
    except Exception as e:
        return f"Error checking for exact match: {str(e)}"

# Export tools for use in agents - include both approaches
DATABASE_TOOLS = [
    create_retriever_from_supabase,  # Efficient LangChain approach
    search_similar_gaia_questions,   # Precise similarity scoring
    get_exact_answer_if_highly_similar  # Exact match detection
]