AGI-rag学习:ChromaDB使用2,json类型文档,问答类内容,【20251016课复习】

说明

记录学习llm过程中的学习代码

下面的代码是学习如何使用chromaDB的例子,文档是json类型,问题和答案组成,向量化问题,答案不需要向量化

py代码

python 复制代码
# chromaDB使用.py
import chromadb
from chromadb.config import Settings
import json
from models import *


# 打开train.json文件,并读取数据
with open('../Data/train.json', 'r', encoding='utf-8') as f:
    data = [json.loads(line)  for line in f.readlines()]

# 将读取的数据按字段拆出放入instructions和outputs中
# 在这个业务中,instruction因为需要被查询,需要向量化,output是和instruction对应的查询结果
print(len(data))
data = data[:10]
instructions = [d['instruction'] for d in data]
outputs = [d['output'] for d in data]
print("instructions:", instructions)
print("outputs:", outputs)
print('-' * 100)


# 负责和向量数据库打交道,接收文档转为向量,并保存到向量数据库中,然后根据需要从向量库中检索出最相似的记录
class MyVectorDBConnector:
    # 初始化,传入集合名称,和向量化函数名
    def __init__(self, collection_name):
        # 当前配置中,数据保存在内存中,如果需要持久化到磁盘,需使用 PersistentClient创建客户端
        chroma_client = chromadb.Client(Settings(allow_reset=True))

        # 持久化到磁盘
        chroma_client = chromadb.PersistentClient(path="./chroma_data")
        # 为了演示,实际不需要每次 reset()
        # chroma_client.reset()

        # 创建一个 collection
        self.collection = chroma_client.get_or_create_collection(name=collection_name)

        # 连接大模型的客户端
        self.client = get_normal_client()

    # 向量化
    def get_embeddings(self, texts, model=ALI_TONGYI_EMBEDDING_V4):
        '''封装 OpenAI 的 Embedding 模型接口'''
        data = self.client.embeddings.create(input=texts, model=model).data
        return [x.embedding for x in data]

    # 添加文档与向量
    def add_documents(self, instructions, outputs):
        '''向 collection 中添加文档与向量'''
        embeddings = self.get_embeddings(instructions)

        self.collection.add(
            embeddings=embeddings,  # 每个文档的向量
            documents=outputs,  # 文档的原文
            ids=[f"id{i+100}" for i in range(len(outputs))]  # 每个文档的 id
        )
        print("self.collection.count():", self.collection.count())

    # 检索向量数据库
    def search(self, query, n_results):
        ''' 检索向量数据库
           query是用户的查询,
           n_results:查出n个相似最高的记录
        '''
        results = self.collection.query(
            query_embeddings=self.get_embeddings([query]),
            n_results=n_results
        )
        return results


# 创建一个向量数据库对象
vector_db = MyVectorDBConnector("demo_wenda")

# 向 向量数据库 中添加文档
vector_db.add_documents(instructions, outputs)

user_query = "得了白癜风怎么办?"
results = vector_db.search(user_query, 2)
print(results)
print('-' * 100)

for para in results['documents'][0]:
    print(para + "\n----\n")
相关推荐
listhi52019 小时前
压缩感知信号重构的块稀疏贝叶斯学习(BSBL)算法:原理、实现与应用
学习·算法·重构
漏刻有时19 小时前
微信小程序学习实录15:微信小程序基于百度云人脸识别的刷脸打卡开发方案
学习·微信小程序·百度云
盐焗西兰花19 小时前
鸿蒙学习实战之路-PDF转换指定页面或指定区域为图片
学习·pdf·harmonyos
鄭郑19 小时前
【Playwright学习笔记 06】用户视觉定位的方法
笔记·学习
楼田莉子19 小时前
Linux系统小项目——“主从设计模式”进程池
linux·服务器·开发语言·c++·vscode·学习
云边散步19 小时前
godot2D游戏教程系列一(9)-终结
学习·游戏·游戏开发
jrlong19 小时前
DataWhale大模型基础与量化微调task4学习笔记(第 1章:参数高效微调_LoRA 方法详解)
笔记·学习
鄭郑20 小时前
【Playwright学习笔记 07】其它用户视觉定位的方法
笔记·学习
LYS_061820 小时前
寒假学习(5)(C语言5+模数电5)
c语言·学习·模数电
火云洞红孩儿20 小时前
2026年,用PyMe可视化编程重塑Python学习
开发语言·python·学习