Bagisto怎么配置阿里千问,自动生成描述文案

Bagisto 通义千问模型配置与错误解决方案

一、申请通义千问API密钥

准备工作

  • 阿里云账号(需完成实名认证)

  • 浏览器访问阿里云控制台

申请步骤

  1. 登录阿里云账号
  • 访问 阿里云官网

  • 登录您的账号

  1. 开通通义千问服务
  • 进入 DashScope模型服务灵积控制台

  • 点击开通服务(部分服务可能需要完成实名认证)

  1. 创建API密钥
  • 进入API-KEY管理页面

  • 点击「创建API密钥」按钮

  • 保存生成的API密钥(请妥善保管,密钥仅显示一次)

  1. 了解免费额度
  • 通义千问服务提供约100万tokens的免费额度

  • 超出免费额度后将按实际使用量计费

二、Bagisto后台配置

1. 访问Magic AI设置

  • 登录Bagisto管理后台

  • 导航至「系统设置」→「配置」→「Magic AI」

2. 基础配置

3. 模型选择

  • 在相关功能配置中选择Qwen系列模型

  • 推荐选项:qwen2.5-14b(对应qwen-max)

4. 启用功能模块

  • 根据需要启用内容生成、评论翻译等功能

  • 配置相应的提示词模板

三、解决常见错误

错误1: 401 Unauthorized (No API-key provided) 错误原因

API请求中缺少认证信息,通义千问API要求在请求头中提供API密钥。

解决方案

修改 `Ollama.php` 文件,添加API密钥认证:

复制代码
// 获取API密钥
$apiKey = core()->getConfigData('general.magic_ai.settings.api_key');

// 在请求头中添加Authorization
'headers' => [
    'Accept' => 'application/json',
    'Authorization' => 'Bearer ' . $apiKey,
]

错误2: 400 Bad Request (Model not exist) 错误原因

模型名称与通义千问API要求不匹配,请求格式不符合API规范。

解决方案

修改 `Ollama.php` 文件,添加通义千问模型特定处理:

复制代码
// 检查是否为通义千问模型
$isQwenModel = strpos(strtolower($this->model), 'qwen') !== false;

// 通义千问模型的特殊处理
if ($isQwenModel && strpos($endpoint, 'dashscope.aliyuncs.com') !== false) {
    // 映射正确的千问模型名称
    $modelMap = [
        'qwen2.5-0.5b' => 'qwen-turbo',
        'qwen2.5-1.5b' => 'qwen-turbo',
        'qwen2.5-3b' => 'qwen-plus',
        'qwen2.5-7b' => 'qwen-plus',
        'qwen2.5-14b' => 'qwen-max',
        'qwen-turbo' => 'qwen-turbo',
        'qwen-plus' => 'qwen-plus',
        'qwen-max' => 'qwen-max',
    ];
    
    $qwenModel = $modelMap[strtolower($this->model)] ?? 'qwen-turbo';
    
    // 通义千问API的请求格式
    $result = $httpClient->request('POST', $endpoint, [
        'headers' => [
            'Accept' => 'application/json',
            'Authorization' => 'Bearer ' . $apiKey,
            'Content-Type' => 'application/json',
        ],
        'json' => [
            'model' => $qwenModel,
            'input' => [
                'messages' => [
                    [
                        'role' => 'user',
                        'content' => $this->prompt
                    ]
                ]
            ],
            'parameters' => [
                'temperature' => $this->temperature,
                'top_p' => 0.95,
            ]
        ],
    ]);
    
    $resultData = json_decode($result->getBody()->getContents(), true);
    return $resultData['output']['text'] ?? $resultData['output']['choices'][0]['message']['content'] ?? 'No response';
}

四、完整代码修改

修改文件

  • `Ollama.php`

完整修改后代码

复制代码
<?php

namespace Webkul\MagicAI\Services;

use GuzzleHttp\Client;

class Ollama
{
    /**
     * New service instance.
     */
    public function __construct(
        protected string $model,
        protected string $prompt,
        protected float $temperature,
        protected bool $stream,
        protected bool $raw,
    ) {}

    /**
     * Set LLM prompt text.
     */
    public function ask(): string
    {
        $httpClient = new Client;
        $endpoint = core()->getConfigData('general.magic_ai.settings.api_domain');
        $apiKey = core()->getConfigData('general.magic_ai.settings.api_key');
        
        // 检查是否为通义千问模型
        $isQwenModel = strpos(strtolower($this->model), 'qwen') !== false;
        
        // 通义千问模型的特殊处理
        if ($isQwenModel && strpos($endpoint, 'dashscope.aliyuncs.com') !== false) {
            // 映射正确的千问模型名称
            $modelMap = [
                'qwen2.5-0.5b' => 'qwen-turbo',
                'qwen2.5-1.5b' => 'qwen-turbo',
                'qwen2.5-3b' => 'qwen-plus',
                'qwen2.5-7b' => 'qwen-plus',
                'qwen2.5-14b' => 'qwen-max',
                'qwen-turbo' => 'qwen-turbo',
                'qwen-plus' => 'qwen-plus',
                'qwen-max' => 'qwen-max',
            ];
            
            $qwenModel = $modelMap[strtolower($this->model)] ?? 'qwen-turbo';
            
            // 通义千问API的请求格式
            $result = $httpClient->request('POST', $endpoint, [
                'headers' => [
                    'Accept' => 'application/json',
                    'Authorization' => 'Bearer ' . $apiKey,
                    'Content-Type' => 'application/json',
                ],
                'json' => [
                    'model' => $qwenModel,
                    'input' => [
                        'messages' => [
                            [
                                'role' => 'user',
                                'content' => $this->prompt
                            ]
                        ]
                    ],
                    'parameters' => [
                        'temperature' => $this->temperature,
                        'top_p' => 0.95,
                    ]
                ],
            ]);
            
            $resultData = json_decode($result->getBody()->getContents(), true);
            return $resultData['output']['text'] ?? $resultData['output']['choices'][0]['message']['content'] ?? 'No response';
        }
        
        // 原始Ollama处理逻辑
        $result = $httpClient->request('POST', $endpoint, [
            'headers' => [
                'Accept' => 'application/json',
                'Authorization' => 'Bearer ' . $apiKey,
            ],
            'json' => [
                'model' => $this->model,
                'prompt' => $this->prompt,
                'raw' => $this->raw,
                'stream' => $this->stream,
            ],
        ]);

        $result = json_decode($result->getBody()->getContents(), true);
        return $result['response'] ?? 'No response';
    }
}

五、使用说明

  1. 配置验证
  • 完成上述配置后,可通过生成产品描述等功能测试API连接

  • 检查是否成功获取AI生成的内容

  1. 注意事项
  • 确保API密钥安全,不要在前端代码中暴露

  • 合理设置API请求频率,避免触发限流

  • 监控API使用量,及时了解计费情况

  1. 故障排除
  • 如果遇到其他错误,建议检查网络连接和API密钥有效性

  • 查看阿里云控制台的API调用日志获取详细错误信息

通过以上配置和修改,Bagisto系统将能够成功集成通义千问模型,实现AI内容生成、评论翻译等功能。

相关推荐
BingoGo2 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack2 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo3 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack3 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack4 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo4 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
JaguarJack5 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理6 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
QQ5110082856 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php
WeiXin_DZbishe6 天前
基于django在线音乐数据采集的设计与实现-计算机毕设 附源码 22647
javascript·spring boot·mysql·django·node.js·php·html5