通义千问接入SSE
接入流式对话
、上下文对话
、图片对话
、文件对话
上篇文章有很多小伙伴再问:开启了流式(SSE)
如何以API
的形式返回数据呢?
这篇文章就来给大家解惑。
实现过程
如何生成key和模型类型请翻找之前的文章,这里就不一一讲述了,直接上代码
流式问答 上下文问答 千问long文件问答
php 代码封装
/*** qwlong流式输出* @param $message* @param $fileId* @return void*/public function dashChat($message, $fileId=null, $messageList=null){try {$api_key = env("QwLongKey","输入自己的key");$url = env("QwLongUrl") ?? 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions';$data = ['model' => 'qwen-long','messages' => [['role' => 'system','content' => 'You are a helpful assistant.'],],'stream' => true];//可以根据逻辑传入上下文问答if ($messageList) {foreach ($messageList as $val){if ($val->type == 2){$data['messages'][] = ['role' => 'system','content' => 'fileid://' . $val->file_id];} else {$data['messages'][] =['role' => 'user','content' => $val->question];$data['messages'][] =['role' => 'assistant','content' => $val->answer];}}}//文件回答 需要先把文件传到阿里云接口 将返回的文件id传入即可if ($fileId){$data['messages'][] = ['role' => 'system','content' => 'fileid://' . $fileId];}$data['messages'][] = ['role' => 'user','content' => $message];// 创建一个变量来存储流式传输的数据$response = '';$ch = curl_init($url);curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $api_key,'Content-Type: application/json']);curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($curl, $data) use (&$response) {//$resData = json_decode($data);$result = processStreamedData($data);$response .= $result;//流式信息打印echo $result;ob_flush();flush();return strlen($data);});curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 执行请求$success = curl_exec($ch);// 检查是否有错误发生if (!$success) {echo "data: {\"error\": \"" . curl_error($ch) . "\"}\n\n";ob_flush();flush();}curl_close($ch);return $response;} catch (\Exception $exception) {echo "data: {\"error\": \"服务器错误: " . $exception->getMessage() . "\"}\n\n";ob_flush();flush();}}
流式信息返回
public function gainFlow(Request $request){$response = new StreamedResponse(function (){//发起会话$result = $this->dashChat('输入需要提问的问题');});//设置流式返回的header头$response->headers->set('Content-Type', 'text/event-stream');$response->headers->set('Cache-Control', 'no-cache');$response->headers->set('Connection', 'keep-alive');$response->headers->set('X-Accel-Buffering', 'no');return $response;}
这样我们就实现流式回答
返回结果截图
结论
根据上文可以实现类似于 chatgpt
和通义千问一样的流式对话
。
并且支持上下文问答
,文件问答
,图片问答
等操作
– 欢迎点赞、关注、转发、收藏【我码玄黄】,gonghao同名