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

网站页脚怎么做今天上海最新事件

网站页脚怎么做,今天上海最新事件,怎么做购物网站系统文本,网站建设哪家公司好招聘目录 一、集合(Collection)增删改查操作 1.1 客户端类型简单对比 1.2 Chroma 本地客户端(local client)集合操作代码示例 二、Chroma Client-Server Mode ✅ 2.1、启动 Chroma Server 端 1. 安装(确保你安装了 chr…

目录

 

一、集合(Collection)增删改查操作

1.1 客户端类型简单对比

1.2 Chroma 本地客户端(local client)集合操作代码示例

 二、Chroma Client-Server Mode

✅ 2.1、启动 Chroma Server 端

1. 安装(确保你安装了 chromadb 0.4+)

2. 运行 Server 服务端

✅ 2.2、客户端连接并使用

1. 连接服务端

2. 创建集合(和嵌入函数不能直接绑定)

 3. 添加数据(你需要自己生成 embedding 向量)

 4. 查询

 总结区别:Client-Server 模式 vs 本地模式

✅ 推荐实践

 三、Chroma Client-Server 模式使用 Demo流程

✅3.1 Server 端启动服务(只需要运行一次)

✅ 3.2 Client 端 Python 脚本

✅ 3.3 执行流程总结

✅ 可选:扩展用法


 

一、集合(Collection)增删改查操作

▲集合是 Chroma 中管理数据的基本单元,类似关系数据库的表;

▲client = chromadb.PersistentClient(path="D:/chroma_db_data") 这种方式属于 本地客户端(local client) 的使用方式。

1.1 客户端类型简单对比

客户端类型是否本地说明
chromadb.Client()否(默认使用内存)临时内存中的数据,不持久化,适合测试
chromadb.PersistentClient(path=...)✅ 本地持久化本地存储数据,重启后数据依然保留
远程服务连接(如 HTTP API)❌ 远程服务连接部署在服务器或云端的 Chroma 实例

1.2 Chroma 本地客户端(local client)集合操作代码示例

解释:

  • chromadb.PersistentClient 是 Chroma 提供的一个持久化客户端类。

  • 参数 path="D:/chroma_db_data" 表示 Chroma 会将数据保存在本地的这个路径下。

  • 所有的数据索引、向量存储、元数据等都会持久化在你指定的目录中,而不是运行在一个远程的服务或云端中。

import chromadb
from chromadb.utils import embedding_functions
import numpy as np# 创建客户端(本地持久化存储)
client = chromadb.PersistentClient(path="D:/chroma_db_data")# 嵌入函数
embedding_function = embedding_functions.DefaultEmbeddingFunction()# 创建集合,异常处理避免重复创建
try:collection = client.create_collection(name="my_collection",configuration={"embedding_function": embedding_function,"hnsw": {"space": "cosine","ef_search": 100,"ef_construction": 200,"max_neighbors": 16,"num_threads": 4}})
except Exception as e:print("集合已存在,获取已存在集合")collection = client.get_collection(name="my_collection")#------------------------------增加数据1----------------------------------
# 数据
texts = ["今天天气很好","明天可能下雨","股票行情波动较大"
]
ids = ["1", "2", "3"]# 生成向量,转成纯列表
vectors = [embedding_function(text)[0].tolist() for text in texts]# 添加数据
collection.add(documents=texts,ids=ids,embeddings=vectors
)#------------------------------增加数据2----------------------------------
# 方式1:自动生成向量(使用集合指定的嵌入模型)
collection.add(documents = ["RAG是一种检索增强生成技术", "向量数据库存储文档的嵌入表示", "在机器学习领域,智能体(Agent)通常指能够感知环境、做出决策并采取行动以实现特定目标的实体"],metadatas = [{"source": "RAG"}, {"source": "向量数据库"}, {"source": "Agent"}],ids = ["id1", "id2", "id3"]
)#------------------------------增加数据3----------------------------------
# 方式2:手动传入预计算向量
# collection.add(
#     embeddings = [[0.1, 0.2, ...], [0.3, 0.4, ...]],
#     documents = ["文本1", "文本2"],
#     ids = ["id3", "id4"]
# )print("数据插入完成")#------------------------------查询数据1----------------------------------
# 查询示例
query_text = "天气预报"
query_vector = embedding_function(query_text)[0].tolist()results = collection.query(query_embeddings=[query_vector],n_results=3
)#------------------------------查询数据2----------------------------------
#文本查询(自动向量化):
results2 = collection.query(query_texts = ["RAG是什么?"],n_results = 3,# where = {"source": "RAG"}, # 按元数据过滤# where_document = {"$contains": "检索增强生成"} # 按文档内容过滤
)#------------------------------更新数据1----------------------------------
#更新集合中的数据:
collection.update(ids=["id1"], documents=["RAG是一种检索增强生成技术,在智能客服系统中大量使用"])results3 = collection.query(query_texts = ["RAG是什么?"],n_results = 3,
)#------------------------------删除数据----------------------------------
#删除集合中的数据:
collection.delete(ids=["id3"])
results4 = collection.query(query_texts = ["机器学习领域Agent能干嘛?"],n_results = 3,
)#------------------------------查询集合----------------------------------
collection = client.get_collection(name="my_collection")
print("✅查询集合:\n",collection,'\n')
print("✅查看前10条集合数据:\n",collection.peek(),'\n')#查看集合中的前几条数据(默认最多 10 条)。
print("✅查看所有集合数据:\n",collection.get(),'\n')#查看所有数据(超过 10 条)
#limit:最多返回多少条记录(上限);默认值是 10,最大值取决于系统资源
#offset:从第几条数据开始获取(偏移量);0 表示从头开始,10 表示跳过前 10 条
print("✅查看带分页控制集合数据:\n",collection.get(limit=11, offset=4),'\n')
print("✅集合中总共有多少条数据:\n",collection.count(),'\n')print("✅查询结果:\n", results,'\n')
print("✅查询结果2:\n",results2,'\n')
print("✅查询结果3:\n",results3,'\n')
print("✅查询结果4:\n",results4,'\n')

 二、Chroma Client-Server Mode

Chroma 有 Client-Server 模式(分布式/远程访问),分为 Server 端运行服务Client 端连接服务 两部分。下面是完整的使用方法(✅适用于你本地启动服务并连接):

✅ 2.1、启动 Chroma Server 端

1. 安装(确保你安装了 chromadb 0.4+)

pip install chromadb

2. 运行 Server 服务端

使用 CLI 启动服务端,并指定数据库存储路径:

chroma run --path "/your/path/to/chroma_data"

 例如,上面代码存储位置是D:/chroma_db_data:

 chroma run --path "D:/chroma_db_data"

默认会启动在 localhost:8000

成功启动后会显示日志:

[INFO] Starting Chroma server on http://localhost:8000


✅ 2.2、客户端连接并使用

你可以在另一个脚本或另一个终端里,作为“客户端”来访问这个 Server:

1. 连接服务端
import chromadb# 用 HttpClient 连接远程(或本地)服务端
client = chromadb.HttpClient(host='localhost', port=8000)

2. 创建集合(和嵌入函数不能直接绑定)

注意:在 Server 模式中,不能直接传嵌入函数!嵌入必须你自己提前生成好再传进去。

# 创建或获取集合
try:
    collection = client.create_collection(name="my_collection")
except:
    collection = client.get_collection(name="my_collection")
 

 3. 添加数据(你需要自己生成 embedding 向量)
# 示例嵌入(假设你用 OpenAI 或 SentenceTransformer 生成)
import numpy as nptexts = ["今天天气很好", "明天可能下雨", "股票行情波动较大"]
ids = ["1", "2", "3"]# 假设你已经有嵌入函数了
from chromadb.utils import embedding_functions
embedding_func = embedding_functions.DefaultEmbeddingFunction()
embeddings = [embedding_func(text).tolist() for text in texts]# 添加数据
collection.add(documents=texts, ids=ids, embeddings=embeddings)

 4. 查询
query_text = "天气预报"
query_embedding = embedding_func(query_text).tolist()results = collection.query(query_embeddings=[query_embedding], n_results=3)
print(results)

 总结区别:Client-Server 模式 vs 本地模式
模式特点
chromadb.PersistentClient(path=...)本地嵌入式数据库,运行在 Python 内部进程,单机使用
chromadb.HttpClient(host=..., port=...)客户端访问外部 Server,适合分布式、多用户环境

✅ 推荐实践

  1. 启动 Server(长期后台服务)

  2. Client 脚本中只需连接,不要传嵌入函数

  3. 所有 embedding 向量由你自己负责生成并传入


 三、Chroma Client-Server 模式使用 Demo流程

✅3.1 Server 端启动服务(只需要运行一次)

你可以在终端或命令行中运行以下命令:

chroma run --path "D:/chroma_db_data"

  • 会启动在 http://localhost:8000

  • 数据将保存在 D:/chroma_db_data

  • 不用写 Python 脚本,直接启动服务即可


✅ 3.2 Client 端 Python 脚本

这是你平时使用的代码逻辑:连接服务、生成向量、插入数据、查询。

import chromadb
from chromadb.utils import embedding_functions# 连接远程 Chroma Server
client = chromadb.HttpClient(host='localhost', port=8000)# 嵌入函数(在 Client 端自己生成向量)
embedding_func = embedding_functions.DefaultEmbeddingFunction()# 获取或创建集合(不能传 embedding_function)
try:collection = client.create_collection(name="my_collection")
except:collection = client.get_collection(name="my_collection")# 示例数据
texts = ["今天天气很好", "明天可能下雨", "股票行情波动较大"]
ids = ["1", "2", "3"]# 生成向量(注意 tolist())
embeddings = [embedding_func(text).tolist() for text in texts]# 插入数据
collection.add(documents=texts, ids=ids, embeddings=embeddings)print("✅ 插入完成")# 查询
query = "天气预报"
query_embedding = embedding_func(query).tolist()results = collection.query(query_embeddings=[query_embedding], n_results=3)print("🔍 查询结果:")
for doc, score in zip(results['documents'][0], results['distances'][0]):print(f"文档: {doc}, 相似度: {1 - score:.4f}")

✅ 3.3 执行流程总结

步骤操作
1. 启动服务端chroma run --path "D:/chroma_db_data"
2. 运行客户端脚本用上面 Python 脚本连接 localhost:8000 插入和查询数据


✅ 可选:扩展用法

你还可以用:

  • OpenAI Embedding:替换 embedding_func,使用更强大的模型

  • 多用户共享数据库:部署服务在云服务器或局域网

  • 持久化嵌入搜索:本地文件夹中保存 .parquet 等数据结构


文章转载自:

http://J68NvnZe.Lywpd.cn
http://A39CDmYl.Lywpd.cn
http://edwAMX7e.Lywpd.cn
http://tYAi2IAS.Lywpd.cn
http://ip2T8pQ2.Lywpd.cn
http://gGBxMVzi.Lywpd.cn
http://LUnmsH5J.Lywpd.cn
http://ka8kGkXg.Lywpd.cn
http://nbi3LxN8.Lywpd.cn
http://7alyOC4m.Lywpd.cn
http://5ckH47OI.Lywpd.cn
http://RXtwfbKJ.Lywpd.cn
http://QSHQ2URA.Lywpd.cn
http://GOCzGvVd.Lywpd.cn
http://qaGVD7z6.Lywpd.cn
http://V5RUh9Ne.Lywpd.cn
http://3TC0annI.Lywpd.cn
http://jIOZjPaq.Lywpd.cn
http://DQJ0ZAxs.Lywpd.cn
http://TI1zcQvo.Lywpd.cn
http://HTT4Jdaj.Lywpd.cn
http://SrG0hI7x.Lywpd.cn
http://95U7gZg0.Lywpd.cn
http://6PgiGTd8.Lywpd.cn
http://DBbYoc02.Lywpd.cn
http://KBGHc49q.Lywpd.cn
http://GH2I26Qe.Lywpd.cn
http://2OdePE4J.Lywpd.cn
http://5vIgJfSO.Lywpd.cn
http://BPIwZzGC.Lywpd.cn
http://www.dtcms.com/wzjs/763032.html

相关文章:

  • 做俄罗斯外贸的网站设计建网站商城
  • 毕业设计网站开发流程南京江北新区规划
  • 建站公司怎么接单网站的建设与维护工资
  • 贵阳做网站 优帮云网站平台开发
  • 农产品网站建设策划书范文wordpress template_redirect
  • 哪个网站有做兼职的石家庄免费建站
  • 微信公众号手机网站公司网站需要多少钱
  • 网站成功案例设计小程序开发公司介绍
  • 做网站找模板杭州网站 建设
  • 学习做网站的网站公司网站百度地图
  • 为什么建设厅的网站不好打开商务 服务类网站模板
  • 内蒙古网站建设熊掌号网站运营专员具体每天怎么做
  • 用英文介绍购物网站济南高端网站
  • visio做网站效果html5网页设计作业代码
  • 自适应型网站建设价格网站编辑适不适合男生做
  • 免费模板网站都有什么长沙的网站建设
  • 网站建设方案编写人织梦做的网站打不开网页
  • 如何形容网站有没有做网站的软件
  • 沐雪专业网站建设wordpress ssl插件
  • 免费制作公司网站wordpress 产品属性
  • 网站维护公司哈尔滨联通公司做网站吗
  • 品牌网站建设权威查关键词排名软件
  • 苏州网站建设企业网站制作沧州高端网站制作
  • 企业网站建设市场报价网页文件的扩展名
  • 网站首页做301聊城手机网站服务
  • 电商网站开发建设用搬瓦工搭建wordpress
  • wordpress 淘客网站app外包公司有哪些
  • 哪些网站做代理网络规划设计师备考
  • 网站建设多少钱比较合适福州网站建设营销方案
  • 心悦会员免做卡网站杭州西湖区网站建设