chromadb使用hugging face模型时利用镜像网站下载注意事项

chromadb默认使用sentence-transformers/all-MiniLM-L6-v2的词嵌入(词向量)模型,如果在程序首次运行时,collection的add或query操作时如果没有指定embeddings或query_embeddings,程序会自动下载相关嵌入向量模型,但是由于默认hugging face后端网络下载速度常常非常慢,所以需要指定镜像网站以加快模型下载速度。

windows系统下具体操作步骤如下:

1、安装huggingface_hub:

bash 复制代码
pip install huggingface_hub

2、设置huggingface后端镜像网址系统变量:

bash 复制代码
set HF_ENDPOINT=https://hf-mirror.com

3、检查系统变量是否设置成功:

bash 复制代码
hf env

4、x下载指定模型(如all-MiniLM-L6-v2模型)到本地指定文件夹中:

bash 复制代码
huggingface-cli download sentence-transformers/all-MiniLM-L6-v2 --local-dir ./models/all-MiniLM-L6-v2 --resume-download --local-dir-use-symlinks False

5、在程序中使用本地模型(如all-MiniLM-L6-v2模型)示例:

python 复制代码
from sentence_transformers import SentenceTransformer

# 指定本地模型路径(注意替换为实际路径)
model_path = r".\models\all-MiniLM-L6-v2"  # Windows路径建议用r""避免转义问题
model = SentenceTransformer(model_path)  # 从本地加载模型

# 输入句子列表
sentences = ["This is an example sentence.", "Each sentence is converted."]
embeddings = model.encode(sentences)  # 生成384维向量

# 打印结果(示例)
print("向量维度:", embeddings.shape)
for i, emb in enumerate(embeddings):
    print(f"句子 '{sentences[i]}' 的前5维向量: {emb[:5]}")

6、在chromadb中使用本地词嵌入向量模型示例:

python 复制代码
import chromadb
from sentence_transformers import SentenceTransformer

# 指定本地模型路径(注意替换为实际路径)
model_path = r".\models\all-MiniLM-L6-v2"  # Windows路径建议用r""避免转义问题
model = SentenceTransformer(model_path)  # 从本地加载模型

chroma_client = chromadb.Client()

collection = chroma_client.create_collection(
    name="my_collection"
)

#文本
documents=[
    "This is a document about pineapple",
    "This is an island of the USA",
    "This is a location where there are many tourists",
    "This is a document about oranges"
    
]

#文本通过模型转换为向量
embeddings = model.encode(documents) 

#像集合中添加记录
collection.add(
    embeddings=embeddings,
    ids=["id1", "id2","id3","id4"],
    documents=documents
)

#查询语句
query_texts=["This is a query document about hawaii"]
#查询语句通过模型转换为向量
query_embeddings = model.encode(query_texts)

#查询数据
results = collection.query(
    query_embeddings=query_embeddings,
    query_texts=query_texts, # Chroma will embed this for you
    n_results=2 # how many results to return
)

print(results)
相关推荐
开心-开心急了3 分钟前
Flask入门教程——李辉 第5章: 数据库 关键知识梳理
笔记·后端·python·flask·1024程序员节
CodeCraft Studio1 小时前
国产化Excel开发组件Spire.XLS教程:在Python中将Pandas DataFrame导出到Excel的详细教程
python·excel·pandas
B站_计算机毕业设计之家2 小时前
python舆情分析可视化系统 情感分析 微博 爬虫 scrapy爬虫技术 朴素贝叶斯分类算法大数据 计算机✅
大数据·爬虫·python·scrapy·数据分析·1024程序员节·舆情分析
B站_计算机毕业设计之家2 小时前
基于python人脸识别系统 人脸检测 实时检测 深度学习 Dlib库 ResNet深度卷积神经网络 pyqt设计 大数据(源码)✅
python·深度学习·目标检测·计算机视觉·信息可视化·人脸识别·1024程序员节
汤姆yu2 小时前
2026版基于python大数据的电影分析可视化系统
大数据·python·1024程序员节·电影分析可视化
Pa2sw0rd丶2 小时前
Python 循环导入详解:为什么会导致生产环境崩溃及企业级解决方案
后端·python
梨轻巧2 小时前
Maya Python入门:创建locator、getAttr() 获取属性、setAttr() 设置属性、定位xform()
python·maya
Xxxx. .Xxxx3 小时前
Classwork 5 - Check the HTML Tags
python
程序员大雄学编程3 小时前
「用Python来学微积分」11. 夹逼定理与单调有界收敛定理
python·数学·微积分·1024程序员节
deephub3 小时前
vLLM 性能优化实战:批处理、量化与缓存配置方案
人工智能·python·大语言模型·1024程序员节·vllm