日期:2025/03/30 00:11来源:未知 人气:58
接入流式对话
、上下文对话
、图片对话
、文件对话
上篇文章有很多小伙伴再问:开启了流式(SSE)
如何以API
的形式返回数据呢?
这篇文章就来给大家解惑。
如何生成key和模型类型请翻找之前的文章,这里就不一一讲述了,直接上代码
流式问答 上下文问答 千问long文件问答
php 代码封装
/**
@return void */ publicfunctiondashChat($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($messageListas$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(); returnstrlen($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(); } }
流式信息返回
publicfunctiongainFlow(Request$request) { $response=newStreamedResponse(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同名
AI早报|阿里通义千问站上全球开源模型榜首;DeepSeek新专利公布
「产业互联网周报」阿里通义千问与DeepSeek开源两款新模型;谷歌发布旗舰推理模型,单次可处理百万token;OpenAI推出GPT-4o图像生成功能;美国科技企业高管和外国领导人据悉敦促特朗普重新考虑AI芯片限制