Skip to main content

基于OPENAI兼容API接口的embeddings服务封装,以解决langchain_community.vectorstores在使用bge-m3/bge-reranker-v2-m3等模型提供的OPENAI兼容API接口服务时遇到的兼容性问题。

Project description

openai-simple-embeddings

基于OPENAI兼容API接口的embeddings服务封装,以解决langchain_community.vectorstores在使用bge-m3/bge-reranker-v2-m3等模型提供的OPENAI兼容API接口服务时遇到的兼容性问题。

安装

pip install openai-simple-embeddings

使用

配置变量设置

# OPENAI兼容API服务,可以xinference提供
export OPENAI_BASE_URL="http://localhost/v1"
# OPENAI兼容API服务密钥,一般以sk-开头,共16位长
export OPENAI_API_KEY=""
# 默认的文本向量化模型
export OPENAI_EMBEDDINGS_MODEL="bge-m3"
# 向量数据库(以redis-stack为例)
export REDIS_STACK_URL="redis://localhost:6379/0"
# 字符串长度控制
export OPENAI_EMBEDDINGS_MAX_SIZE=1024

获取文本向量

代码

from openai_simple_embeddings.base import get_text_embeddings

r1 = get_text_embeddings("hello")
print(r1)

输出

[-0.032024841755628586, 0.023251207545399666, ..., -0.037223849445581436, 0.05963246524333954]

集成到向量数据库客户端

代码

from openai_simple_embeddings.langchain_embeddings import OpenAISimpleEmbeddings
from langchain_community.vectorstores.redis import Redis as LangchainRedisVectorStore
import python_environment_settings

REDIS_STACK_URL = python_environment_settings.get("REDIS_STACK_URL")
index_name = "kb:test"
embeddings = OpenAISimpleEmbeddings()
lrvs = LangchainRedisVectorStore(
    redis_url=REDIS_STACK_URL,
    index_name=index_name,
    key_prefix=index_name,
    embedding=embeddings,
)
uids = lrvs.add_texts(["hello"])
print(uids)

输出

['kb:test:984af7f2ffea4d49952af82dd992c8f8']

关于字符串长度控制

  • 模型本身一般没有字符串长度控制。
  • 但过长的字符串会导入模型占用内存的增长。
  • 所以默认将字符串长度控制在:1024字。
  • 通过OPENAI_EMBEDDINGS_MAX_SIZE设置默认最大字符串长度。
  • 也可以函数调用中指定最大字符串长度。
  • 注意:所有超过最大长度的字符串将被截断。

版本记录

v0.1.0

  • 版本首发。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openai-simple-embeddings-0.1.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openai_simple_embeddings-0.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file openai-simple-embeddings-0.1.0.tar.gz.

File metadata

File hashes

Hashes for openai-simple-embeddings-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6505d4d8580398d01f9d0e5810e6fb7fceb5aba4a3028f4beef104c2262b7aa7
MD5 04de05d5ac2841197f9c0208938b427a
BLAKE2b-256 fd71fb0cd3455000dcdf758f2ed79ccbb9d2e1ba5260579abe4599d617a52b0f

See more details on using hashes here.

File details

Details for the file openai_simple_embeddings-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_simple_embeddings-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c7a8bc0e1958dd578f635d8382f8a8e1b86449aa9ee0438843d7222d510478f
MD5 26c086178dda2bfc03bc0132090d2845
BLAKE2b-256 dfe0cee14187ac987679d07512e5a65529ce1deacc0c524cccd1301d710a21dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page