-
Notifications
You must be signed in to change notification settings - Fork 34
feat: add reranker support to Python SDK #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| MOSS_PROJECT_ID=your_project_id | ||
| MOSS_PROJECT_KEY=your_project_key | ||
| COHERE_API_KEY=your_cohere_api_key |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| moss>=1.0.0 | ||
| -e sdks/python/sdk | ||
| cohere>=5.0.0 | ||
| python-dotenv | ||
| openai | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import asyncio | ||
| import json | ||
| import os | ||
|
|
||
| from dotenv import load_dotenv | ||
| from moss import MossClient, DocumentInfo, QueryOptions, RerankOptions | ||
|
|
||
| load_dotenv() | ||
|
|
||
| INDEX_NAME = "rerank-demo-full" | ||
|
|
||
|
|
||
| async def setup_index(client): | ||
| """Delete old index, create fresh one with all FAQ data.""" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the delete old index step missing ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. darn, I think I removed it for a test and then forgot to add, let me do it real quick |
||
| faqs_path = os.path.join(os.path.dirname(__file__), "faqs.json") | ||
| with open(faqs_path, "r") as f: | ||
| faqs = json.load(f) | ||
|
|
||
| docs = [ | ||
| DocumentInfo( | ||
| id=faq["id"], | ||
| text=faq["text"], | ||
| metadata={k: str(v) for k, v in faq.get("metadata", {}).items()}, | ||
| ) | ||
| for faq in faqs | ||
| ] | ||
|
|
||
| print(f"Setting up index '{INDEX_NAME}'") | ||
| try: | ||
| await client.delete_index(INDEX_NAME) | ||
| print("Deleted old index.") | ||
| except Exception: | ||
| pass | ||
|
|
||
| await client.create_index(INDEX_NAME, docs) | ||
| print("Index created.") | ||
|
|
||
| await client.load_index(INDEX_NAME) | ||
| print("Index loaded.\n") | ||
|
|
||
|
|
||
| async def main(): | ||
| client = MossClient(os.getenv("MOSS_PROJECT_ID"), os.getenv("MOSS_PROJECT_KEY")) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you please create .env.example
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| await setup_index(client) | ||
|
|
||
| print("Without Reranking") | ||
| results = await client.query( | ||
| INDEX_NAME, | ||
| "How to get discount?", | ||
| QueryOptions(top_k=5, alpha=0.8), | ||
| ) | ||
| for i, doc in enumerate(results.docs): | ||
| print(f" {i + 1}. [{doc.score:.3f}] {doc.text[:100]}...") | ||
|
|
||
| print("\nWith Cohere Reranking") | ||
| results = await client.query( | ||
| INDEX_NAME, | ||
| "How to get discount?", | ||
| QueryOptions( | ||
| top_k=10, | ||
| alpha=0.8, | ||
| rerank=RerankOptions( | ||
| provider="cohere", | ||
| api_key=os.getenv("COHERE_API_KEY"), | ||
| top_n=5, | ||
| ), | ||
| ), | ||
| ) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would reranking definition return type searchresult ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes it would be of this type |
||
| for i, doc in enumerate(results.docs): | ||
| print(f" {i + 1}. [{doc.score:.3f}] {doc.text[:100]}...") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,62 +1,64 @@ | ||
| """ | ||
| Moss Semantic Search SDK | ||
|
|
||
| Powerful Python SDK for semantic search using state-of-the-art embedding models. | ||
|
|
||
| Example: | ||
| ```python | ||
| from moss import MossClient, DocumentInfo | ||
|
|
||
| client = MossClient('your-project-id', 'your-project-key') | ||
|
|
||
| docs = [DocumentInfo(id="1", text="Example document")] | ||
|
|
||
| result = await client.create_index('my-index', docs, 'moss-minilm') | ||
|
|
||
| await client.load_index('my-index') | ||
| results = await client.query('my-index', 'search query') | ||
| ``` | ||
| """ | ||
|
|
||
| from moss_core import ( | ||
| DocumentInfo, | ||
| GetDocumentsOptions, | ||
| IndexInfo, | ||
| IndexStatus, | ||
| IndexStatusValues, | ||
| ModelRef, | ||
| MutationOptions, | ||
| MutationResult, | ||
| JobStatus, | ||
| JobPhase, | ||
| JobProgress, | ||
| JobStatusResponse, | ||
| QueryOptions, | ||
| QueryResultDocumentInfo, | ||
| SearchResult, | ||
| ) | ||
|
|
||
| from .client.moss_client import MossClient | ||
|
|
||
| __version__ = "1.0.0b19" | ||
|
|
||
| __all__ = [ | ||
| "MossClient", | ||
| # Core data types | ||
| "DocumentInfo", | ||
| "GetDocumentsOptions", | ||
| "IndexInfo", | ||
| "SearchResult", | ||
| "QueryResultDocumentInfo", | ||
| "ModelRef", | ||
| "IndexStatus", | ||
| "IndexStatusValues", | ||
| "QueryOptions", | ||
| # Mutation types | ||
| "MutationResult", | ||
| "MutationOptions", | ||
| "JobStatus", | ||
| "JobPhase", | ||
| "JobProgress", | ||
| "JobStatusResponse", | ||
| ] | ||
| """ | ||
| Moss Semantic Search SDK | ||
|
|
||
| Powerful Python SDK for semantic search using state-of-the-art embedding models. | ||
|
|
||
| Example: | ||
| ```python | ||
| from moss import MossClient, DocumentInfo | ||
|
|
||
| client = MossClient('your-project-id', 'your-project-key') | ||
|
|
||
| docs = [DocumentInfo(id="1", text="Example document")] | ||
|
|
||
| result = await client.create_index('my-index', docs, 'moss-minilm') | ||
|
|
||
| await client.load_index('my-index') | ||
| results = await client.query('my-index', 'search query') | ||
| ``` | ||
| """ | ||
|
|
||
| from moss_core import ( | ||
| DocumentInfo, | ||
| GetDocumentsOptions, | ||
| IndexInfo, | ||
| IndexStatus, | ||
| IndexStatusValues, | ||
| ModelRef, | ||
| MutationOptions, | ||
| MutationResult, | ||
| JobStatus, | ||
| JobPhase, | ||
| JobProgress, | ||
| JobStatusResponse, | ||
| QueryResultDocumentInfo, | ||
| SearchResult, | ||
| ) | ||
|
|
||
| from .client.models import QueryOptions, RerankOptions | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: QueryOptions replaced from Rust class to Python dataclass — backward compatibility The PR replaces Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| from .client.moss_client import MossClient | ||
|
|
||
| __version__ = "1.0.0b19" | ||
|
|
||
| __all__ = [ | ||
| "MossClient", | ||
| # Core data types | ||
| "DocumentInfo", | ||
| "GetDocumentsOptions", | ||
| "IndexInfo", | ||
| "SearchResult", | ||
| "QueryResultDocumentInfo", | ||
| "ModelRef", | ||
| "IndexStatus", | ||
| "IndexStatusValues", | ||
| "QueryOptions", | ||
| # Mutation types | ||
| "MutationResult", | ||
| "MutationOptions", | ||
| "JobStatus", | ||
| "JobPhase", | ||
| "JobProgress", | ||
| "JobStatusResponse", | ||
| # Reranking | ||
| "RerankOptions", | ||
| ] | ||
Uh oh!
There was an error while loading. Please reload this page.