php结合通义千问和qdrant实现RAG向量检索增强知识库问答示例代码
代码语言:php
所属分类:其他
代码描述:php结合通义千问和qdrant实现RAG向量检索增强知识库问答示例代码,通过通义千问将知识库文本向量化插入qdrant中,然后在进行近似查询,最后交给ai总结输出。
代码标签: php 结合 通义千问 qdrant RAG 向量 检索 增强 知识库 问答 示例 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<?php
//key申请地址:https://dashscope.console.aliyun.com/apiKey
function aichat($query) {
$apikey = "自己申请key";
if ($query == "") {
return "";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = [
'model' => 'qwen-turbo',
// 'model' => 'qwen-plus-v1',
'messages' => [['role' => "system",
"content" => "你是一个ai助手"],
['role' => "user",
"content" => $query],
],
];
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body, JSON_UNESCAPED_UNICODE));
curl_setopt($ch, CURLOPT_POST, 1);
// Set the API key as an HTTP header
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer ".$apikey;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Send the request and parse the response
$response = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
return "";
} else {
curl_close($ch);
$response_data = json_decode($response, true);
if (isset($response_data['choices'][0]['message']['content'])) {
return $response_data['choices'][0]['message']['content'];
} else {
return "";
}
}
}
function getembeding($text) {
if ($text == "") {
return "";
}
//key申请地址:https://dashscope.console.aliyun.com/apiKey
$apikey = "自己申请key0";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = [
'model' => 'text-embedding-v2',
'input' => ['texts' => [$text]],
"parameters" => [
"text_type" => "query"
]
];
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body, JSON_UNESCAPED_UNICODE));
curl_setopt($ch, CURLOPT_POST, 1);
// Set the API key as an HTTP header
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer ".$apikey;
curl_setop.........完整代码请登录后点击上方下载按钮下载查看
网友评论0