php使用PostgreSQL和openai api向量插入查询进行文档知识库问答示例代码

代码语言:php

所属分类:其他

代码描述:php使用PostgreSQL和openai api向量插入查询进行文档知识库问答示例代码,将大文档拆分成小块,利用openai的embedding获取每个小块的embedding插入PostgreSQL数据库(扩展pgvector),每个问题进行向量相似查询,最终交给chatgpt进行总结归纳返回即可。

代码标签: php PostgreSQL openai api 向量 插入 查询 进行 文档 知识库 问答

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php
//composer require pgvector/pgvector

//注意需要php7.4以上运行
require_once __DIR__ . '/../vendor/autoload.php';

use Pgvector\Vector;

$db = pg_connect('postgres://localhost/pgvector_example');

pg_query($db, 'CREATE EXTENSION IF NOT EXISTS vector');
pg_query($db, 'DROP TABLE IF EXISTS documents');
pg_query($db, 'CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(1536))');

function fetchEmbeddings($input) {
    $apiKey = getenv('OPENAI_API_KEY');
    $url = 'https://api.openai.com/v1/embeddings';
    $data = [
        'input' => $input,
        'model' => 'text-embedding-ada-002'
    ];
    $opts = [
        'http' => [
            'method' => 'POST',
            'header' => "Authorization: Bearer $apiKey\r\nContent-Type: application/json\r\n",
            'content' => json_encode($data)
        ]
    ];
    $context = stream_context_create($opts);
    $response = file_get_contents($url, false, $context);
    return array_map(fn ($v) => $v['embedding'], json_decode($response, true)['data']);
}

function askgpt($input) {
    $ch = curl_init();
    $apiKey = getenv('OPENAI_API_KEY');
    curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/completions");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, '{
 "model": "gpt-3.5-turbo",
  "prompt": "'.$input.'",
  "max_tokens": 1120
}');


    curl_setopt($ch, CURLOPT_POST, 1);

    // Set the API key as an.........完整代码请登录后点击上方下载按钮下载查看

网友评论0