repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import os |
| #2 | from typing import List, Dict, Any |
| #3 | |
| #4 | from mem0.reranker.base import BaseReranker |
| #5 | |
| #6 | try: |
| #7 | from zeroentropy import ZeroEntropy |
| #8 | ZERO_ENTROPY_AVAILABLE = True |
| #9 | except ImportError: |
| #10 | ZERO_ENTROPY_AVAILABLE = False |
| #11 | |
| #12 | |
| #13 | class ZeroEntropyReranker(BaseReranker): |
| #14 | """Zero Entropy-based reranker implementation.""" |
| #15 | |
| #16 | def __init__(self, config): |
| #17 | """ |
| #18 | Initialize Zero Entropy reranker. |
| #19 | |
| #20 | Args: |
| #21 | config: ZeroEntropyRerankerConfig object with configuration parameters |
| #22 | """ |
| #23 | if not ZERO_ENTROPY_AVAILABLE: |
| #24 | raise ImportError("zeroentropy package is required for ZeroEntropyReranker. Install with: pip install zeroentropy") |
| #25 | |
| #26 | self.config = config |
| #27 | self.api_key = config.api_key or os.getenv("ZERO_ENTROPY_API_KEY") |
| #28 | if not self.api_key: |
| #29 | raise ValueError("Zero Entropy API key is required. Set ZERO_ENTROPY_API_KEY environment variable or pass api_key in config.") |
| #30 | |
| #31 | self.model = config.model or "zerank-1" |
| #32 | |
| #33 | # Initialize Zero Entropy client |
| #34 | if self.api_key: |
| #35 | self.client = ZeroEntropy(api_key=self.api_key) |
| #36 | else: |
| #37 | self.client = ZeroEntropy() # Will use ZERO_ENTROPY_API_KEY from environment |
| #38 | |
| #39 | def rerank(self, query: str, documents: List[Dict[str, Any]], top_k: int = None) -> List[Dict[str, Any]]: |
| #40 | """ |
| #41 | Rerank documents using Zero Entropy's rerank API. |
| #42 | |
| #43 | Args: |
| #44 | query: The search query |
| #45 | documents: List of documents to rerank |
| #46 | top_k: Number of top documents to return |
| #47 | |
| #48 | Returns: |
| #49 | List of reranked documents with rerank_score |
| #50 | """ |
| #51 | if not documents: |
| #52 | return documents |
| #53 | |
| #54 | # Extract text content for reranking |
| #55 | doc_texts = [] |
| #56 | for doc in documents: |
| #57 | if 'memory' in doc: |
| #58 | doc_texts.append(doc['memory']) |
| #59 | elif 'text' in doc: |
| #60 | doc_texts.append(doc['text']) |
| #61 | elif 'content' in doc: |
| #62 | doc_texts.append(doc['content']) |
| #63 | else: |
| #64 | doc_texts.append(str(doc)) |
| #65 | |
| #66 | try: |
| #67 | # Call Zero Entropy rerank API |
| #68 | response = self.client.models.rerank( |
| #69 | model=self.model, |
| #70 | query=query, |
| #71 | documents=doc_texts, |
| #72 | ) |
| #73 | |
| #74 | # Create reranked results |
| #75 | reranked_docs = [] |
| #76 | for result in response.results: |
| #77 | original_doc = documents[result.index].copy() |
| #78 | original_doc['rerank_score'] = result.relevance_score |
| #79 | reranked_docs.append(original_doc) |
| #80 | |
| #81 | # Sort by relevance score in descending order |
| #82 | reranked_docs.sort(key=lambda x: x['rerank_score'], reverse=True) |
| #83 | |
| #84 | # Apply top_k limit |
| #85 | if top_k: |
| #86 | reranked_docs = reranked_docs[:top_k] |
| #87 | elif self.config.top_k: |
| #88 | reranked_docs = reranked_docs[:self.config.top_k] |
| #89 | |
| #90 | return reranked_docs |
| #91 | |
| #92 | except Exception: |
| #93 | # Fallback to original order if reranking fails |
| #94 | for doc in documents: |
| #95 | doc['rerank_score'] = 0.0 |
| #96 | return documents[:top_k] if top_k else documents |