mirror of
https://github.com/MODSetter/SurfSense.git
synced 2025-09-09 13:54:40 +00:00
feat: Added LinkUP Search Engine Connector
This commit is contained in:
parent
273c16a611
commit
3675505eb1
18 changed files with 492 additions and 27 deletions
|
@ -5,6 +5,7 @@ from sqlalchemy.future import select
|
|||
from app.retriver.chunks_hybrid_search import ChucksHybridSearchRetriever
|
||||
from app.db import SearchSourceConnector, SearchSourceConnectorType
|
||||
from tavily import TavilyClient
|
||||
from linkup import LinkupClient
|
||||
|
||||
|
||||
class ConnectorService:
|
||||
|
@ -643,3 +644,97 @@ class ConnectorService:
|
|||
}
|
||||
|
||||
return result_object, linear_chunks
|
||||
|
||||
async def search_linkup(self, user_query: str, user_id: str, mode: str = "standard") -> tuple:
|
||||
"""
|
||||
Search using Linkup API and return both the source information and documents
|
||||
|
||||
Args:
|
||||
user_query: The user's query
|
||||
user_id: The user's ID
|
||||
mode: Search depth mode, can be "standard" or "deep"
|
||||
|
||||
Returns:
|
||||
tuple: (sources_info, documents)
|
||||
"""
|
||||
# Get Linkup connector configuration
|
||||
linkup_connector = await self.get_connector_by_type(user_id, SearchSourceConnectorType.LINKUP_API)
|
||||
|
||||
if not linkup_connector:
|
||||
# Return empty results if no Linkup connector is configured
|
||||
return {
|
||||
"id": 10,
|
||||
"name": "Linkup Search",
|
||||
"type": "LINKUP_API",
|
||||
"sources": [],
|
||||
}, []
|
||||
|
||||
# Initialize Linkup client with API key from connector config
|
||||
linkup_api_key = linkup_connector.config.get("LINKUP_API_KEY")
|
||||
linkup_client = LinkupClient(api_key=linkup_api_key)
|
||||
|
||||
# Perform search with Linkup
|
||||
try:
|
||||
response = linkup_client.search(
|
||||
query=user_query,
|
||||
depth=mode, # Use the provided mode ("standard" or "deep")
|
||||
output_type="searchResults", # Default to search results
|
||||
)
|
||||
|
||||
# Extract results from Linkup response - access as attribute instead of using .get()
|
||||
linkup_results = response.results if hasattr(response, 'results') else []
|
||||
|
||||
# Process each result and create sources directly without deduplication
|
||||
sources_list = []
|
||||
documents = []
|
||||
|
||||
for i, result in enumerate(linkup_results):
|
||||
# Fix for UI
|
||||
linkup_results[i]['document']['id'] = self.source_id_counter
|
||||
# Create a source entry
|
||||
source = {
|
||||
"id": self.source_id_counter,
|
||||
"title": result.name if hasattr(result, 'name') else "Linkup Result",
|
||||
"description": result.content[:100] if hasattr(result, 'content') else "",
|
||||
"url": result.url if hasattr(result, 'url') else ""
|
||||
}
|
||||
sources_list.append(source)
|
||||
|
||||
# Create a document entry
|
||||
document = {
|
||||
"chunk_id": f"linkup_chunk_{i}",
|
||||
"content": result.content if hasattr(result, 'content') else "",
|
||||
"score": 1.0, # Default score since not provided by Linkup
|
||||
"document": {
|
||||
"id": self.source_id_counter,
|
||||
"title": result.name if hasattr(result, 'name') else "Linkup Result",
|
||||
"document_type": "LINKUP_API",
|
||||
"metadata": {
|
||||
"url": result.url if hasattr(result, 'url') else "",
|
||||
"type": result.type if hasattr(result, 'type') else "",
|
||||
"source": "LINKUP_API"
|
||||
}
|
||||
}
|
||||
}
|
||||
documents.append(document)
|
||||
self.source_id_counter += 1
|
||||
|
||||
# Create result object
|
||||
result_object = {
|
||||
"id": 10,
|
||||
"name": "Linkup Search",
|
||||
"type": "LINKUP_API",
|
||||
"sources": sources_list,
|
||||
}
|
||||
|
||||
return result_object, documents
|
||||
|
||||
except Exception as e:
|
||||
# Log the error and return empty results
|
||||
print(f"Error searching with Linkup: {str(e)}")
|
||||
return {
|
||||
"id": 10,
|
||||
"name": "Linkup Search",
|
||||
"type": "LINKUP_API",
|
||||
"sources": [],
|
||||
}, []
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue