当前位置: 首页 > news >正文

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

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

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

1、安装huggingface_hub:

pip install huggingface_hub

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

set HF_ENDPOINT=https://hf-mirror.com

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

hf env

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

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模型)示例:

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中使用本地词嵌入向量模型示例:

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 youn_results=2 # how many results to return
)print(results)

http://www.xdnf.cn/news/1351441.html

相关文章:

  • Node.js特训专栏-实战进阶:23. CI/CD流程搭建
  • 通过官方文档详解Ultralytics YOLO 开源工程-熟练使用 YOLO11实现分割、分类、旋转框检测和姿势估计(附测试代码)
  • 优先使用 `delete` 关键字删除函数,而不是将函数声明为 `private` 但不实现 (Effective Modern C++ 条款11)
  • 2025年Java在中国开发语言排名分析报告
  • 深度学习之PyTorch框架(安装,手写数字识别)
  • Redis 从入门到实践:Python操作指南与核心概念解析
  • Redis全面详解:从配置入门到实战应用
  • 联邦学习之----联邦批量归一化(FedBN)
  • 非线性规划学习笔记
  • 【KO】前端面试题一
  • 浮点数比较的致命陷阱与正确解法(精度问题)
  • 【Linux】深度学习Linux下的包管理器yum/apt
  • 自动化知识工作AI代理的工程与产品实现
  • 文献阅读笔记【物理信息神经网络】:Physics-informed neural networks: A deep learning framework...
  • 深入理解 Linux 系统文件 I/O:从 open 到重定向的底层逻辑》
  • CA6150主轴箱系统设计cad+设计说明书
  • Spring:IOC(控制反转 )、DI(依赖注入 )、AOP(通知类型、事务、拦截器)
  • 博士招生 | 美国圣地亚哥州立大学 Yifan Zhang 课题组博士招生,AI 安全领域顶尖平台等你加入!
  • ​崩坏世界观中的安全漏洞与哲学映射:从渗透测试视角解构虚拟秩序的脆弱性​
  • lanczso算法中的额外正交化代码解释
  • Linux问答题:分析和存储日志
  • Leetcode—931. 下降路径最小和【中等】
  • 告别静态网页:我用Firefly AI + Spline,构建次世代交互式Web体验
  • 同类软件对比(一):Visual Studio(IDE) VS Visual Studio Code
  • 支持电脑课程、游戏、会议、网课、直播录屏 多场景全能录屏工具
  • LeetCode 448.找到所有数组中消失的数字
  • Ubuntu通过 systemd 管理 gpt4free,需为其创建 g4f.service 文件,定义服务的启动、停止等操作(未实践)
  • 97. 小明逛公园,Floyd 算法,127. 骑士的攻击,A * 算法
  • SQL注入1----(sql注入原理)
  • csrf漏洞学习笔记