Feat: Change embedding formats from float to base64 for efficiency
- Add base64 support for Jina embeddings - Add base64 support for OpenAI embeddings - Update env.example with new embedding options
This commit is contained in:
parent
4d492abf41
commit
c5babf61d7
3 changed files with 28 additions and 8 deletions
17
env.example
17
env.example
|
|
@ -121,6 +121,16 @@ LLM_MODEL=gpt-4o
|
||||||
LLM_BINDING_HOST=https://api.openai.com/v1
|
LLM_BINDING_HOST=https://api.openai.com/v1
|
||||||
LLM_BINDING_API_KEY=your_api_key
|
LLM_BINDING_API_KEY=your_api_key
|
||||||
|
|
||||||
|
### Optional for Azure
|
||||||
|
# AZURE_OPENAI_API_VERSION=2024-08-01-preview
|
||||||
|
# AZURE_OPENAI_DEPLOYMENT=gpt-4o
|
||||||
|
|
||||||
|
### Openrouter example
|
||||||
|
# LLM_MODEL=google/gemini-2.5-flash
|
||||||
|
# LLM_BINDING_HOST=https://openrouter.ai/api/v1
|
||||||
|
# LLM_BINDING_API_KEY=your_api_key
|
||||||
|
# LLM_BINDING=openai
|
||||||
|
|
||||||
### Most Commont Parameters for Ollama Server
|
### Most Commont Parameters for Ollama Server
|
||||||
### Time out in seconds, None for infinite timeout
|
### Time out in seconds, None for infinite timeout
|
||||||
TIMEOUT=240
|
TIMEOUT=240
|
||||||
|
|
@ -132,14 +142,11 @@ OLLAMA_LLM_NUM_CTX=32768
|
||||||
# OLLAMA_LLM_TEMPERATURE=0.85
|
# OLLAMA_LLM_TEMPERATURE=0.85
|
||||||
### see also env.ollama-binding-options.example for fine tuning ollama
|
### see also env.ollama-binding-options.example for fine tuning ollama
|
||||||
|
|
||||||
### Optional for Azure
|
|
||||||
# AZURE_OPENAI_API_VERSION=2024-08-01-preview
|
|
||||||
# AZURE_OPENAI_DEPLOYMENT=gpt-4o
|
|
||||||
|
|
||||||
####################################################################################
|
####################################################################################
|
||||||
### Embedding Configuration (Should not be changed after the first file processed)
|
### Embedding Configuration (Should not be changed after the first file processed)
|
||||||
####################################################################################
|
####################################################################################
|
||||||
### Embedding Binding type: openai, ollama, lollms, azure_openai, jina
|
### Embedding Binding type: ollama, openai, azure_openai, jina, lollms
|
||||||
|
|
||||||
### see also env.ollama-binding-options.example for fine tuning ollama
|
### see also env.ollama-binding-options.example for fine tuning ollama
|
||||||
EMBEDDING_BINDING=ollama
|
EMBEDDING_BINDING=ollama
|
||||||
|
|
@ -149,7 +156,7 @@ EMBEDDING_BINDING_API_KEY=your_api_key
|
||||||
# If the embedding service is deployed within the same Docker stack, use host.docker.internal instead of localhost
|
# If the embedding service is deployed within the same Docker stack, use host.docker.internal instead of localhost
|
||||||
EMBEDDING_BINDING_HOST=http://localhost:11434
|
EMBEDDING_BINDING_HOST=http://localhost:11434
|
||||||
|
|
||||||
### OpenAI compatible
|
### OpenAI compatible (VoyageAI embedding openai compatible)
|
||||||
# EMBEDDING_BINDING=openai
|
# EMBEDDING_BINDING=openai
|
||||||
# EMBEDDING_MODEL=text-embedding-3-large
|
# EMBEDDING_MODEL=text-embedding-3-large
|
||||||
# EMBEDDING_DIM=3072
|
# EMBEDDING_DIM=3072
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ if not pm.is_installed("tenacity"):
|
||||||
pm.install("tenacity")
|
pm.install("tenacity")
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import base64
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from tenacity import (
|
from tenacity import (
|
||||||
retry,
|
retry,
|
||||||
|
|
@ -82,6 +83,7 @@ async def jina_embed(
|
||||||
"model": "jina-embeddings-v4",
|
"model": "jina-embeddings-v4",
|
||||||
"task": "text-matching",
|
"task": "text-matching",
|
||||||
"dimensions": dimensions,
|
"dimensions": dimensions,
|
||||||
|
"embedding_type": "base64",
|
||||||
"input": texts,
|
"input": texts,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,7 +110,12 @@ async def jina_embed(
|
||||||
f"Jina API returned {len(data_list)} embeddings for {len(texts)} texts"
|
f"Jina API returned {len(data_list)} embeddings for {len(texts)} texts"
|
||||||
)
|
)
|
||||||
|
|
||||||
embeddings = np.array([dp["embedding"] for dp in data_list])
|
embeddings = np.array(
|
||||||
|
[
|
||||||
|
np.frombuffer(base64.b64decode(dp["embedding"]), dtype=np.float32)
|
||||||
|
for dp in data_list
|
||||||
|
]
|
||||||
|
)
|
||||||
logger.debug(f"Jina embeddings generated: shape {embeddings.shape}")
|
logger.debug(f"Jina embeddings generated: shape {embeddings.shape}")
|
||||||
|
|
||||||
return embeddings
|
return embeddings
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ from lightrag.types import GPTKeywordExtractionFormat
|
||||||
from lightrag.api import __api_version__
|
from lightrag.api import __api_version__
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import base64
|
||||||
from typing import Any, Union
|
from typing import Any, Union
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
@ -472,6 +473,11 @@ async def openai_embed(
|
||||||
|
|
||||||
async with openai_async_client:
|
async with openai_async_client:
|
||||||
response = await openai_async_client.embeddings.create(
|
response = await openai_async_client.embeddings.create(
|
||||||
model=model, input=texts, encoding_format="float"
|
model=model, input=texts, encoding_format="base64"
|
||||||
|
)
|
||||||
|
return np.array(
|
||||||
|
[
|
||||||
|
np.frombuffer(base64.b64decode(dp.embedding), dtype=np.float32)
|
||||||
|
for dp in response.data
|
||||||
|
]
|
||||||
)
|
)
|
||||||
return np.array([dp.embedding for dp in response.data])
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue