-
Notifications
You must be signed in to change notification settings - Fork 780
feat: support self-hosted embedding service via BentoML #324
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
Open
parano
wants to merge
9
commits into
Shaunwei:main
Choose a base branch
from
parano:bentoml-text-embedding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
406ae6a
feat: support self-hosted embedding service via BentoML
parano 0bbca1c
Merge branch 'main' into bentoml-text-embedding
parano 4ca94cf
fix: chroma embedding func expects list[float]
parano cb637d4
chore: avoid using 3000 port
parano 23fcb35
chore: fix ruff warning
parano af9b556
Update embedding service port in CLI
parano e164bc2
Update embed_func protocol to work with Chroma interface
parano 20285e7
Fix embeddings protocol
parano 0aa6caf
Merge branch 'main' into bentoml-text-embedding
parano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,47 @@ | ||
| import os | ||
| from dotenv import load_dotenv | ||
| from bentoml.client import Client | ||
| from langchain.vectorstores import Chroma | ||
| from langchain.embeddings import OpenAIEmbeddings | ||
| from langchain.embeddings.base import Embeddings | ||
| from realtime_ai_character.logger import get_logger | ||
|
|
||
| load_dotenv() | ||
| logger = get_logger(__name__) | ||
|
|
||
| embedding = OpenAIEmbeddings(openai_api_key=os.getenv("OPENAI_API_KEY")) | ||
| if os.getenv('OPENAI_API_TYPE') == 'azure': | ||
| embedding = OpenAIEmbeddings(openai_api_key=os.getenv("OPENAI_API_KEY"), deployment=os.getenv( | ||
| "OPENAI_API_EMBEDDING_DEPLOYMENT_NAME", "text-embedding-ada-002"), chunk_size=1) | ||
|
|
||
| class BentoEmbeddings(Embeddings): | ||
| def __init__(self, embedding_svc_client: Client): | ||
| self.client = embedding_svc_client | ||
|
|
||
| def embed_documents(self, texts: list[str]) -> list[list[float]]: | ||
| return self.client.encode(texts).tolist() | ||
|
|
||
| def embed_query(self, text: str) -> list[float]: | ||
| return self.client.encode([text]).tolist()[0] | ||
|
|
||
|
|
||
| embedding_endpoint = os.getenv("BENTOML_EMBEDDING_ENDPOINT") | ||
|
|
||
| if embedding_endpoint: | ||
| # Use self-hosted embedding model via BentoML API endpoint | ||
| client = Client.from_url(embedding_endpoint) | ||
| embedding_func = BentoEmbeddings(client) | ||
| else: | ||
| embedding_func = OpenAIEmbeddings(openai_api_key=os.getenv("OPENAI_API_KEY")) | ||
| if os.getenv('OPENAI_API_TYPE') == 'azure': | ||
| embedding_func = OpenAIEmbeddings( | ||
| openai_api_key=os.getenv("OPENAI_API_KEY"), | ||
| deployment=os.getenv( | ||
| "OPENAI_API_EMBEDDING_DEPLOYMENT_NAME", "text-embedding-ada-002" | ||
| ), | ||
| chunk_size=1) | ||
|
|
||
|
|
||
| def get_chroma(): | ||
| chroma = Chroma( | ||
| collection_name='llm', | ||
| embedding_function=embedding, | ||
| embedding_function=embedding_func, | ||
| persist_directory='./chroma.db' | ||
| ) | ||
| return chroma |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ aioconsole | |
| aiofiles | ||
| alembic | ||
| anthropic | ||
| bentoml>=1.1 | ||
| chromadb>=0.4.2 | ||
| click | ||
| EbookLib | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # For advanced BentoML deployment on kubernetes, see: | ||
| # https://www.kubeflow.org/docs/external-add-ons/serving/bentoml/ | ||
| # https://github.com/bentoml/yatai | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: bentoml-embedding-deployment | ||
| labels: | ||
| app: bentoml-text-embedding | ||
| spec: | ||
| replicas: 1 | ||
| selector: | ||
| matchLabels: | ||
| app: bentoml-text-embedding | ||
| template: | ||
| metadata: | ||
| labels: | ||
| app: bentoml-text-embedding | ||
| spec: | ||
| containers: | ||
| - name: bentoml-text-embedding | ||
| image: ghcr.io/bentoml/sentence-embedding-bento:0.1.0 | ||
| ports: | ||
| - containerPort: 3000 | ||
| env: | ||
| - name: BENTOML_CONFIG_OPTIONS | ||
| value: "api_server.metrics.namespace=realchar,api_server.traffic.timeout=10" | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: bentoml-embedding-service | ||
| spec: | ||
| type: ClusterIP | ||
| selector: | ||
| app: bentoml-text-embedding | ||
| ports: | ||
| - protocol: TCP | ||
| port: 80 | ||
| targetPort: 3000 | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.