OpenAI与向量数据库应用

写在前面

本篇文章写的是如何用GPT与向量数据库链接。为什么要用向量数据库。原因很简单,GPT再回答问题的时候,有时候会一本正经的胡说八道,有人称之为幻觉。所以这时我们可以用向量数据库,来存储我们想要的答案。下面就写下我是如何使用的,当然向量数据库也有别的用法,欢迎评论讨论。

这里我使用的Milvus,当然你也可以用别的向量数据库。如何本地搭建向量数据库之前已经写过了,这里我们主要看看如何使用。

这里我使用NodeJS的方式,链接向量数据库。

Milvus应用示例

安装Milvus的node依赖

你可以使用npm (Node包管理器)或Yarn在你的项目中安装@zilliz/milvus2-sdk-node依赖:

npm install @zilliz/milvus2-sdk-node
# or ...
yarn add @zilliz/milvus2-sdk-node

编写程序

创建一个js文件,并添加以下代码,

import { MilvusClient, DataType } from '@zilliz/milvus2-sdk-node';

const address = 'your-milvus-ip-with-port';
const username = 'your-milvus-username'; // optional username
const password = 'your-milvus-password'; // optional password

// connect to milvus
const client = new MilvusClient({ address, username, password });

如果你是按照我的方法安装的本地数据库,那么不需要username, password,只需要address即可

创建集合

// define schema
const collection_name = `hello_milvus`;
const dim = 128;
const schema = [
  {
    name: 'age',
    description: 'ID field',
    data_type: DataType.Int64,
    is_primary_key: true,
    autoID: true,
  },
  {
    name: 'vector',
    description: 'Vector field',
    data_type: DataType.FloatVector,
    dim: 8,
  },
  { name: 'height', description: 'int64 field', data_type: DataType.Int64 },
  {
    name: 'name',
    description: 'VarChar field',
    data_type: DataType.VarChar,
    max_length: 128,
  },
],
await client.createCollection({
  collection_name,
  fields: schema,
});

准备数据

const fields_data = [
  {
    vector: [
      0.11878310581111173, 0.9694947902934701, 0.16443679307243175,
      0.5484226189097237, 0.9839246709011924, 0.5178387104937776,
      0.8716926129208069, 0.5616972243831446,
    ],
    height: 20405,
    name: 'zlnmh',
  },
  {
    vector: [
      0.9992090731236536, 0.8248790611809487, 0.8660083940881405,
      0.09946359318481224, 0.6790698063908669, 0.5013786801063624,
      0.795311915725105, 0.9183033261617566,
    ],
    height: 93773,
    name: '5lr9y',
  },
  {
    vector: [
      0.8761291569818763, 0.07127366044153227, 0.775648976160332,
      0.5619757601304878, 0.6076543120476996, 0.8373907516027586,
      0.8556140171597648, 0.4043893119391049,
    ],
    height: 85122,
    name: 'nes0j',
  },
];

插入数据

await client.insert({
  collection_name,
  fields_data,
});

创建索引,这里我尝试跳过创建索引,但是如果不创建的化,似乎数据库查询会失败,你可以自己尝试下,所以我不得不给数据库添加了索引。

await client.createIndex({
  // required
  collection_name,
  field_name: 'vector', // optional if you are using milvus v2.2.9+
  index_name: 'myindex', // optional
  index_type: 'HNSW', // optional if you are using milvus v2.2.9+
  params: { efConstruction: 10, M: 4 }, // optional if you are using milvus v2.2.9+
  metric_type: 'L2', // optional if you are using milvus v2.2.9+
});

加载集合,这一步是必须的,不然数据库查询也会报错

await client.loadCollectionSync({
  collection_name,
});

查询数据

// get the search vector
const searchVector = fields_data[0].vector;

// Perform a vector search on the collection
const res = await client.search({
  // required
  collection_name, // required, the collection name
  vector: searchVector, // required, vector used to compare other vectors in milvus
  // optionals
  filter: 'height > 0', // optional, filter
  params: { nprobe: 64 }, // optional, specify the search parameters
  limit: 10, // optional, specify the number of nearest neighbors to return
  metric_type: 'L2', // optional, metric to calculate similarity of two vectors
  output_fields: ['height', 'name'], // optional, specify the fields to return in the search results
});

上面简单介绍了Milvus的使用,下面来看看如何结合GPT使用它 。

GPT与向量数据库结合使用,以及源码:公众号回复“向量”获取。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小~小

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值