惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
aimingoo的专栏
aimingoo的专栏
I
InfoQ
C
Check Point Blog
IT之家
IT之家
MyScale Blog
MyScale Blog
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
Last Week in AI
Last Week in AI
GbyAI
GbyAI
P
Proofpoint News Feed
量子位
Stack Overflow Blog
Stack Overflow Blog
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
B
Blog
T
The Blog of Author Tim Ferriss
H
Help Net Security
云风的 BLOG
云风的 BLOG

博客园 - 温柔的风

宝塔面板定时任务目录清理日志-定时检测阈值清理 Mysql8 允许远程登录 【uni-app】申请高德地图key,封装map.js,实现H5、iOS、Android通过getlocation获取地图定位信息(摘) centos安装atop工具,检测服务器情况 mysql 8.0查看正在执行的事务锁 webman 安装gateway实现socket https站点websocket连接失败问题 layui table 表格过滤tableFilter 谷歌浏览器无法播放音频mp3怎么办? uniapp下实现心跳检测服务端并且结合生命周期自动再次连接绑定客户端 git push Git远端意外挂断 Layer.js最大化最小化监听 Linux磁盘阈值及内存阈值检测脚本 fastadmin控制列显示与隐藏 基于uniapp的全局监听websocket连接及接收服务端消息 fastadmin自定义主题并随切换变更iframe颜色 fastadmin-PHP-导出少量数据PhpOffice以及百万级别数据csv压缩 Macbook M1下安装Kibana Macbook M1下安装elasticsearch Fastadmin表格的列头对齐以及内容对齐设定方式
php对接阿里通义AI模型,简单实现浏览器端以sse方式输出
温柔的风 · 2024-12-29 · via 博客园 - 温柔的风

阿里云百炼地址:https://bailian.console.aliyun.com/#/home

 在百炼模型服务控制台的右上角鼠标悬浮在人物图标上,选择API-KEY,然后创建API Key,用于通过API调用大模型。


php 控制器端
//ajax发起请求 public function ajaxSendAskQuestion(){ if (!$this->request->isPost()) { $this->error('非法操作'); } $content = $this->request->post('content','','trim'); if ($content == '') { $this->error('请输入您要提问的问题'); } return json([ 'code' => 1, 'message' => 'SSE stream initialized', 'sse_url' => $this->domain_uri . '/TongyiAi/sseRequestQuestion?ask_question='. urlencode($content) ]); } //该方法可以get访问,浏览器端会sse方式持续输出内容 public function sseRequestQuestion($ask_question = ''){ set_time_limit(0); if ($ask_question == '') { http_response_code(400); echo json_encode(['error' => 'No question provided']); exit; } $question = $_GET['ask_question']; TongyiAiService::service()->requestQuestion($question); ob_flush(); flush(); }
php service

<?php

namespace app\common\service;


use GatewayWorker\Lib\Gateway;
use think\Env;
use think\Log;

/**
 * socket service
 */
class TongyiAiService extends BaseService
{

    public static function service($className = __CLASS__)
    {
        return parent::service($className);
    }

    const API_URL   = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';

    const API_KEY   = 'sk-xxxxxx';


    //发起提问
    public function requestQuestion($ask_question = ''){


        $app_key        = self::API_KEY;
        $app_url        = self::API_URL;
        $model          = 'qwen-plus';

        $model          = 'qwen-max';
        // 设置请求体
        // 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
        //文档地址:https://help.aliyun.com/zh/model-studio/developer-reference/error-code
        //剩余次数 计费详情
        //https://bailian.console.aliyun.com/?spm=a2c4g.11186623.0.0.136955efmZn66H#/model-market/detail/qwen-plus

        header('Content-Type: text/event-stream');
        header('Cache-Control: no-cache');
        header('X-Accel-Buffering: no');
        header('Connection: keep-alive');
        header('Access-Control-Allow-Origin: *');



        set_time_limit(0);
        ob_end_clean();
        ob_implicit_flush(1);



        $headers = [
            'Authorization: Bearer ' . $app_key,
            'Content-Type: application/json',
            'X-DashScope-SSE: enable'
        ];
        $params =  [
            'model' => $model,
            'input' => ['messages' => []],
            'parameters' => [
                'result_format'=>'message',
                'incremental_output'=>true
            ],
        ];
        $params['input']['messages'][] = [
            'role' => 'user',
            'content' => $ask_question
        ];


        $ch = curl_init($app_url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//有时候希望返回的内容作为变量储存,而不是直接输出。这个时候就必需设置curl的CURLOPT_RETURNTRANSFER选项为1或true。
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//设置这个选项为一个非零值(象 “Location: “)的头,服务器会把它当做HTTP头的一部分发送(注意这是递归的,PHP将发送形如 “Location: “的头)。
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//curl_exec()获取的信息以文件流的形式返回,而不是直接输出
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) {
            $res = substr(explode("\n",$data)[3],5);
            $res = json_decode($res,true);
            if($res['output']){
                echo "data: " . $res['output']['choices'][0]['message']['content']. "\n\n";
                flush(); // 立即将输出发送到客户端
            }else{
                echo $res['message']. "\n\n";
                flush(); // 立即将输出发送到客户端
            }
            return strlen($data);
        });
        curl_exec($ch);
        curl_close($ch);
    }
}
js 端


$(document).on('click','#send',function(){
                var content = $('#ask_question_content').val();
                if (content.trim() === '' || content == undefined || content == '') {
                    window.top.notify.error('请输入您要提问的问题');
                    return false;
                }
                Fast.api.ajax({
                    url: 'TongyiAi/ajaxSendAskQuestion',
                    data: {content: content}
                }, function (data,ret) {
                    if (ret.code === 1) {
                        $('#answer_div').addClass('hide');
                        $('#answer_content').html('');
                        startSseStream(ret.sse_url);
                    } else {
                        window.top.notify.error('操作失败,请稍后再试');
                    }
                }, function (data, ret) {
                    window.top.notify.error('操作失败');
                    return false;
                });
            })

            function startSseStream(sse_url) {
                if (typeof(EventSource) !== "undefined") {
                    var source = new EventSource(sse_url);
                    source.onmessage = function(event) {
                        // 每次接收到消息时更新页面
                        $('#answer_div').removeClass('hide');
                        $('#answer_content').append(event.data);
                    };

                    source.onerror = function(event) {
                        console.error("EventSource failed:", event);
                        source.close(); // 关闭连接以防无限重试;
                    }
                } else {
                    $('#answer_content').text("抱歉,您的浏览器不支持该功能,请使用谷歌浏览器");
                }

            }

浏览器访问效果,会持续输出

ajax实现效果