Skip to main content

Here is an enhanced version of the LocalEmbeddings class that provides similar functionality to the OpenAI embedding model and includes more features and detailed comments in English and Chinese

Project description

Utils

Mimic langchain to customize some tools for working with local models

Utils-RAG

EnhancedLocalEmbeddings.py

Here is an enhanced version of the LocalEmbeddings class that provides similar functionality to the OpenAI embedding model and includes more features and detailed comments in English and Chinese: | 以下是增强版的 LocalEmbeddings 类,它提供了与 OpenAI 嵌入模型类似的功能,并且包括更多特性和详细的中英文注释:

  • Initializing the Local Model | 初始化本地嵌入模型
# 1. 初始化本地嵌入模型 | Initialize local embedding model
# 使用 SentenceTransformer 模型
# Use a SentenceTransformer model
local_embeddings = EnhancedLocalEmbeddings(model_path="sentence-transformers/all-MiniLM-L6-v2")

# 使用 Hugging Face 模型(需要指定分词器路径)
# Use a Hugging Face model (requires tokenizer path)
local_embeddings_hf = EnhancedLocalEmbeddings(
    model_path="bert-base-uncased",
    tokenizer_path="bert-base-uncased"
)
  • Embedding Single Text | 嵌入单个文本
embedding = local_embeddings.embed_text("The quick brown fox jumps over the lazy dog.")
print("单文本嵌入结果 | Single text embedding:", embedding[:10])  # 打印前 10 个维度
  • Embedding Multiple Texts | 嵌入多个文本
texts = ["Text 1", "Text 2"]
embeddings = local_embeddings.embed_documents(texts)
print("多文本嵌入结果 | Multiple text embeddings:", [embedding[:10] for embedding in embeddings])
  • Embedding Single Texts Tsynchronously | 异步嵌入单个文本
import asyncio
async def async_example():
    embedding = await local_embeddings.aembed_text("Async embedding example.")
    print("异步单文本嵌入结果 | Async single text embedding:", embedding[:10])

asyncio.run(async_example())
  • Embedding Multipes Texts Tsynchronously | 异步嵌入单个文本
import asyncio

# 异步嵌入多个文本
async def async_example():
    texts = ["Text 1 for async.", "Text 2 for async."]
    embeddings = await local_embeddings.aembed_documents(texts)
    print("异步多文本嵌入结果 | Async multiple text embeddings:", [embedding[:10] for embedding in embeddings])

asyncio.run(async_example())
  • Embedded Query Text | 嵌入查询文本
query = "What is the meaning of life?"
query_embedding = local_embeddings.embed_query(query)
print("查询嵌入结果 | Query embedding:", query_embedding[:10])
  • Batch Embedding of Multiple Text | 批量嵌入多个文本
large_texts = ["Sample text " + str(i) for i in range(100)]
batch_embeddings = local_embeddings.embed_batch(large_texts, batch_size=16)
print("批量嵌入结果 | Batch embeddings:", [embedding[:10] for embedding in batch_embeddings[:3]])
  • Direct Call Example | 直接调用实例
texts = ["Direct call example 1.", "Direct call example 2."]
embeddings = local_embeddings(texts)
print("直接调用嵌入结果 | Direct call embeddings:", [embedding[:10] for embedding in embeddings])

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

utils_rag-1.0.6.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

utils_rag-1.0.6-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file utils_rag-1.0.6.tar.gz.

File metadata

  • Download URL: utils_rag-1.0.6.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for utils_rag-1.0.6.tar.gz
Algorithm Hash digest
SHA256 59d4e4c2e8b0bef060117cd6a03ddba4f539c3b82ca39f4b5973c149ae9a65d9
MD5 1059cd7a654ad6d5728c42fbf6d7a71b
BLAKE2b-256 420b6cd234d4411c8159a8515a42f5c3ee02b480f21ee408fee48d89ee414ada

See more details on using hashes here.

Provenance

The following attestation bundles were made for utils_rag-1.0.6.tar.gz:

Publisher: python-publish.yml on Fangzhou-Code/Utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file utils_rag-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: utils_rag-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for utils_rag-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 59b9c00e87c5ed0aea75adc84b4a9c4d05142b508e88de5deeb38aec3ddba6a6
MD5 ea04d2e3be45801c9fa0af756fcbe3ca
BLAKE2b-256 0317b107835d81042988c58189df7ed728569d5cd4451dcbebf319b037189b2b

See more details on using hashes here.

Provenance

The following attestation bundles were made for utils_rag-1.0.6-py3-none-any.whl:

Publisher: python-publish.yml on Fangzhou-Code/Utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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