Skip to content

Commit f8f3621

Browse files
[WIP] Add support for OPEA LLMs in Llama-Index (#16666)
1 parent 3f7e66e commit f8f3621

19 files changed

Lines changed: 662 additions & 0 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
llama_index/_static
2+
.DS_Store
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
bin/
14+
build/
15+
develop-eggs/
16+
dist/
17+
downloads/
18+
eggs/
19+
.eggs/
20+
etc/
21+
include/
22+
lib/
23+
lib64/
24+
parts/
25+
sdist/
26+
share/
27+
var/
28+
wheels/
29+
pip-wheel-metadata/
30+
share/python-wheels/
31+
*.egg-info/
32+
.installed.cfg
33+
*.egg
34+
MANIFEST
35+
36+
# PyInstaller
37+
# Usually these files are written by a python script from a template
38+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
39+
*.manifest
40+
*.spec
41+
42+
# Installer logs
43+
pip-log.txt
44+
pip-delete-this-directory.txt
45+
46+
# Unit test / coverage reports
47+
htmlcov/
48+
.tox/
49+
.nox/
50+
.coverage
51+
.coverage.*
52+
.cache
53+
nosetests.xml
54+
coverage.xml
55+
*.cover
56+
*.py,cover
57+
.hypothesis/
58+
.pytest_cache/
59+
.ruff_cache
60+
61+
# Translations
62+
*.mo
63+
*.pot
64+
65+
# Django stuff:
66+
*.log
67+
local_settings.py
68+
db.sqlite3
69+
db.sqlite3-journal
70+
71+
# Flask stuff:
72+
instance/
73+
.webassets-cache
74+
75+
# Scrapy stuff:
76+
.scrapy
77+
78+
# Sphinx documentation
79+
docs/_build/
80+
81+
# PyBuilder
82+
target/
83+
84+
# Jupyter Notebook
85+
.ipynb_checkpoints
86+
notebooks/
87+
88+
# IPython
89+
profile_default/
90+
ipython_config.py
91+
92+
# pyenv
93+
.python-version
94+
95+
# pipenv
96+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
97+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
98+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
99+
# install all needed dependencies.
100+
#Pipfile.lock
101+
102+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
103+
__pypackages__/
104+
105+
# Celery stuff
106+
celerybeat-schedule
107+
celerybeat.pid
108+
109+
# SageMath parsed files
110+
*.sage.py
111+
112+
# Environments
113+
.env
114+
.venv
115+
env/
116+
venv/
117+
ENV/
118+
env.bak/
119+
venv.bak/
120+
pyvenv.cfg
121+
122+
# Spyder project settings
123+
.spyderproject
124+
.spyproject
125+
126+
# Rope project settings
127+
.ropeproject
128+
129+
# mkdocs documentation
130+
/site
131+
132+
# mypy
133+
.mypy_cache/
134+
.dmypy.json
135+
dmypy.json
136+
137+
# Pyre type checker
138+
.pyre/
139+
140+
# Jetbrains
141+
.idea
142+
modules/
143+
*.swp
144+
145+
# VsCode
146+
.vscode
147+
148+
# pipenv
149+
Pipfile
150+
Pipfile.lock
151+
152+
# pyright
153+
pyrightconfig.json
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
poetry_requirements(
2+
name="poetry",
3+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
GIT_ROOT ?= $(shell git rev-parse --show-toplevel)
2+
3+
help: ## Show all Makefile targets.
4+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-30s\033[0m %s\n", $$1, $$2}'
5+
6+
format: ## Run code autoformatters (black).
7+
git ls-files | black
8+
9+
lint: ## Run linters: pre-commit (black, ruff, codespell) and mypy
10+
pre-commit install && git ls-files | xargs pre-commit run --show-diff-on-failure --files
11+
12+
test: ## Run tests via pytest.
13+
pytest tests
14+
15+
watch-docs: ## Build and watch documentation.
16+
sphinx-autobuild docs/ docs/_build/html --open-browser --watch $(GIT_ROOT)/llama_index/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# LlamaIndex Embeddings Integration: OPEA Embeddings
2+
3+
OPEA (Open Platform for Enterprise AI) is a platform for building, deploying, and scaling AI applications. As part of this platform, many core gen-ai components are available for deployment as microservices, including LLMs.
4+
5+
Visit [https://opea.dev](https://opea.dev) for more information, and their [GitHub](https://github.com/opea-project/GenAIComps) for the source code of the OPEA components.
6+
7+
## Installation
8+
9+
1. Install the required Python packages:
10+
11+
```bash
12+
%pip install llama-index-embeddings-opea
13+
```
14+
15+
## Usage
16+
17+
```python
18+
from llama_index.embeddings.opea import OPEAEmbedding
19+
20+
embed_model = OPEAEmbedding(
21+
model="<model_name>",
22+
api_base="http://localhost:8080/v1",
23+
embed_batch_size=10,
24+
)
25+
26+
embeddings = embed_model.get_text_embedding("text")
27+
28+
embeddings = embed_model.get_text_embedding_batch(["text1", "text2"])
29+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python_sources()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from llama_index.embeddings.opea.base import OPEAEmbedding
2+
3+
__all__ = ["OPEAEmbedding"]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import httpx
2+
from typing import Any, Dict, Optional
3+
4+
from llama_index.core.base.embeddings.base import (
5+
DEFAULT_EMBED_BATCH_SIZE,
6+
)
7+
from llama_index.core.callbacks import CallbackManager
8+
from llama_index.embeddings.openai import OpenAIEmbedding
9+
10+
11+
class OPEAEmbedding(OpenAIEmbedding):
12+
"""
13+
OPEA class for embeddings.
14+
15+
Args:
16+
model (str): Model for embedding.
17+
api_base (str): The base URL for OPEA Embeddings microservice.
18+
additional_kwargs (Dict[str, Any]): Additional kwargs for the OpenAI API.
19+
20+
Examples:
21+
`pip install llama-index-embeddings-opea`
22+
23+
```python
24+
from llama_index.embeddings.opea import OPEAEmbedding
25+
26+
embed_model = OPEAEmbedding(
27+
model_name="...",
28+
api_base="http://localhost:8080",
29+
)
30+
```
31+
"""
32+
33+
def __init__(
34+
self,
35+
model_name: str,
36+
api_base: str,
37+
dimensions: Optional[int] = None,
38+
embed_batch_size: int = DEFAULT_EMBED_BATCH_SIZE,
39+
additional_kwargs: Optional[Dict[str, Any]] = None,
40+
max_retries: int = 10,
41+
timeout: float = 60.0,
42+
reuse_client: bool = True,
43+
callback_manager: Optional[CallbackManager] = None,
44+
default_headers: Optional[Dict[str, str]] = None,
45+
http_client: Optional[httpx.Client] = None,
46+
api_key: Optional[str] = "fake",
47+
**kwargs: Any,
48+
) -> None:
49+
super().__init__(
50+
model_name=model_name,
51+
dimensions=dimensions,
52+
embed_batch_size=embed_batch_size,
53+
additional_kwargs=additional_kwargs,
54+
api_key=api_key,
55+
api_base=api_base,
56+
max_retries=max_retries,
57+
timeout=timeout,
58+
reuse_client=reuse_client,
59+
callback_manager=callback_manager,
60+
default_headers=default_headers,
61+
http_client=http_client,
62+
**kwargs,
63+
)
64+
65+
@classmethod
66+
def class_name(cls) -> str:
67+
return "OPEAEmbedding"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[build-system]
2+
build-backend = "poetry.core.masonry.api"
3+
requires = ["poetry-core"]
4+
5+
[tool.codespell]
6+
check-filenames = true
7+
check-hidden = true
8+
skip = "*.csv,*.html,*.json,*.jsonl,*.pdf,*.txt,*.ipynb"
9+
10+
[tool.llamahub]
11+
contains_example = false
12+
import_path = "llama_index.embeddings.opea"
13+
14+
[tool.llamahub.class_authors]
15+
OPEAEmbedding = "llama-index"
16+
17+
[tool.mypy]
18+
disallow_untyped_defs = true
19+
exclude = ["_static", "build", "examples", "notebooks", "venv"]
20+
ignore_missing_imports = true
21+
python_version = "3.8"
22+
23+
[tool.poetry]
24+
authors = ["logan-markewich"]
25+
description = "llama-index embeddings opea integration"
26+
exclude = ["**/BUILD"]
27+
license = "MIT"
28+
name = "llama-index-embeddings-opea"
29+
readme = "README.md"
30+
version = "0.1.0"
31+
32+
[tool.poetry.dependencies]
33+
python = ">=3.8.1,<4.0"
34+
llama-index-llms-openai = "^0.2.0"
35+
llama-index-core = "^0.11.0"
36+
37+
[tool.poetry.group.dev.dependencies]
38+
ipython = "8.10.0"
39+
jupyter = "^1.0.0"
40+
mypy = "0.991"
41+
pre-commit = "3.2.0"
42+
pylint = "2.15.10"
43+
pytest = "7.2.1"
44+
pytest-mock = "3.11.1"
45+
ruff = "0.0.292"
46+
tree-sitter-languages = "^1.8.0"
47+
types-Deprecated = ">=0.1.0"
48+
types-PyYAML = "^6.0.12.12"
49+
types-protobuf = "^4.24.0.4"
50+
types-redis = "4.5.5.0"
51+
types-requests = "2.28.11.8"
52+
types-setuptools = "67.1.0.0"
53+
54+
[tool.poetry.group.dev.dependencies.black]
55+
extras = ["jupyter"]
56+
version = "<=23.9.1,>=23.7.0"
57+
58+
[tool.poetry.group.dev.dependencies.codespell]
59+
extras = ["toml"]
60+
version = ">=v2.2.6"
61+
62+
[[tool.poetry.packages]]
63+
include = "llama_index/"

0 commit comments

Comments
 (0)