forked from YassKhazzan/openperplex_backend_os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jina_rerank.py
51 lines (42 loc) · 1.46 KB
/
jina_rerank.py
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
import os
import requests
from typing import List
import logging
from requests.exceptions import RequestException
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Constants
API_URL = "https://api.jina.ai/v1/rerank"
API_KEY = os.getenv("JINA_API_KEY")
MODEL = "jina-reranker-v2-base-multilingual"
HEADERS = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
def get_reranking_jina(docs: List[str], query: str, top_res: int) -> List[str]:
"""
Get reranked documents using Jina AI API.
:param docs: List of documents to rerank
:param query: Query string
:param top_res: Number of top results to return
:return: List of reranked documents
"""
try:
data = {
"model": MODEL,
"query": query,
"documents": docs,
"top_n": top_res
}
response = requests.post(API_URL, headers=HEADERS, json=data, timeout=10)
response.raise_for_status()
response_data = response.json()
return [item['document']['text'] for item in response_data.get('results', [])]
except RequestException as e:
logger.error(f"HTTP error occurred while reranking: {e}")
except KeyError as e:
logger.error(f"Unexpected response format: {e}")
except Exception as e:
logger.exception(f"An unexpected error occurred: {e}")
return []