Skip to content
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

feat: support self-hosted embedding service via BentoML #324

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

parano
Copy link

@parano parano commented Aug 10, 2023

This PR adds an option for RealChar to use a self-hosted embedding service powered by SentenceBert and BentoML.

By default, this integration uses the docker image published here

The default model that comes with the docker image is all-MiniLM-L6-v2. RealChar users may customize it to use a different text embedding model based on their needs. Check out the source code for the embedding service here: https://github.com/bentoml/sentence-embedding-bento,

TODO:

  • Test the integration end-to-end

@Shaunwei Shaunwei self-requested a review August 10, 2023 18:25
.env.example Outdated Show resolved Hide resolved
.env.example Outdated Show resolved Hide resolved
cli.py Outdated Show resolved Hide resolved
@Shaunwei
Copy link
Owner

Shaunwei commented Aug 11, 2023

BentoML does not support loading pydantic models from URLs; output will be a normal dictionary.

Traceback (most recent call last):
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/bin/uvicorn", line 8, in <module>
    sys.exit(main())
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/main.py", line 410, in main
    run(
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/main.py", line 578, in run
    server.run()
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/server.py", line 61, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/server.py", line 68, in serve
    config.load()
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/config.py", line 473, in load
    self.loaded_app = import_from_string(self.app)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/main.py", line 37, in <module>
    CatalogManager.initialize(overwrite=overwrite_chroma)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/utils.py", line 65, in initialize
    cls._instances[cls] = cls(*args, **kwargs)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/character_catalog/catalog_manager.py", line 26, in __init__
    self.db = get_chroma()
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/database/chroma.py", line 31, in get_chroma
    chroma = Chroma(
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/langchain/vectorstores/chroma.py", line 128, in __init__
    embedding_function=self._embedding_function.embed_documents
AttributeError: 'function' object has no attribute 'embed_documents'

Looks like the embed function is not compatible

@aarnphm
Copy link

aarnphm commented Aug 11, 2023

BentoML does not support loading pydantic models from URLs; output will be a normal dictionary.

Traceback (most recent call last):
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/bin/uvicorn", line 8, in <module>
    sys.exit(main())
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/main.py", line 410, in main
    run(
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/main.py", line 578, in run
    server.run()
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/server.py", line 61, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/server.py", line 68, in serve
    config.load()
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/config.py", line 473, in load
    self.loaded_app = import_from_string(self.app)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/main.py", line 37, in <module>
    CatalogManager.initialize(overwrite=overwrite_chroma)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/utils.py", line 65, in initialize
    cls._instances[cls] = cls(*args, **kwargs)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/character_catalog/catalog_manager.py", line 26, in __init__
    self.db = get_chroma()
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/database/chroma.py", line 31, in get_chroma
    chroma = Chroma(
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/langchain/vectorstores/chroma.py", line 128, in __init__
    embedding_function=self._embedding_function.embed_documents
AttributeError: 'function' object has no attribute 'embed_documents'

Looks like the embed function is not compatible

The client return back a dictionary, but we can load that dict into a pydantic model if needed. It seems like this _embedding_function here is a callable instead of pydantic model.

@parano

Copy link

@aarnphm aarnphm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Shaunwei
Copy link
Owner

    CatalogManager.initialize(overwrite=overwrite_chroma)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/utils.py", line 65, in initialize
    cls._instances[cls] = cls(*args, **kwargs)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/character_catalog/catalog_manager.py", line 37, in __init__
    self.load_characters_from_community(overwrite)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/character_catalog/catalog_manager.py", line 134, in load_characters_from_community
    self.load_data(character_name, directory / 'data')
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/character_catalog/catalog_manager.py", line 150, in load_data
    self.db.add_documents(docs)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/langchain/vectorstores/base.py", line 104, in add_documents
    return self.add_texts(texts, metadatas, **kwargs)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/langchain/vectorstores/chroma.py", line 187, in add_texts
    embeddings = self._embedding_function.embed_documents(texts)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/database/chroma.py", line 18, in embed_documents
    return self.client.encode(texts).tolist()
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/bentoml/_internal/client/__init__.py", line 133, in _sync_call
    return asyncio.run(self._call(inp, _bentoml_api=_bentoml_api, **kwargs))
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/asyncio/runners.py", line 33, in run
    raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop
sys:1: RuntimeWarning: coroutine 'HTTPClient._call' was never awaited

I still run into errors

README.md Show resolved Hide resolved
cli.py Show resolved Hide resolved
Copy link

@aarnphm aarnphm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @parano. The port inside container has to be 3000

@parano
Copy link
Author

parano commented Aug 13, 2023

    CatalogManager.initialize(overwrite=overwrite_chroma)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/utils.py", line 65, in initialize
    cls._instances[cls] = cls(*args, **kwargs)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/character_catalog/catalog_manager.py", line 37, in __init__
    self.load_characters_from_community(overwrite)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/character_catalog/catalog_manager.py", line 134, in load_characters_from_community
    self.load_data(character_name, directory / 'data')
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/character_catalog/catalog_manager.py", line 150, in load_data
    self.db.add_documents(docs)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/langchain/vectorstores/base.py", line 104, in add_documents
    return self.add_texts(texts, metadatas, **kwargs)
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/langchain/vectorstores/chroma.py", line 187, in add_texts
    embeddings = self._embedding_function.embed_documents(texts)
  File "/Users/shaun/Documents/GitHub/Realtime-AI-Character/realtime_ai_character/database/chroma.py", line 18, in embed_documents
    return self.client.encode(texts).tolist()
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/site-packages/bentoml/_internal/client/__init__.py", line 133, in _sync_call
    return asyncio.run(self._call(inp, _bentoml_api=_bentoml_api, **kwargs))
  File "/opt/homebrew/anaconda3/envs/realtime-ai-companion/lib/python3.10/asyncio/runners.py", line 33, in run
    raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop
sys:1: RuntimeWarning: coroutine 'HTTPClient._call' was never awaited

I still run into errors

Thanks @Shaunwei, will look into this

@pycui
Copy link
Collaborator

pycui commented Aug 16, 2023

Hi - any updates to this PR?

@aarnphm
Copy link

aarnphm commented Aug 17, 2023

Hi - any updates to this PR?

Hey you can try out this patch

openllm.patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants