当前位置: 欣欣网 > 码农

Openai 异步客户端接入国产大模型 Kimi

2024-03-16码农

Moonshot

介绍

Kimi Chat 是由月之暗面科技有限公司(Moonshot AI)开发的一款人工智能助手,支持长达20万字的上下文处理能力,并且能够记住之前的对话内容,提供更加准确和有条理的回答。Kimi Chat 的特点在于其强大的长文本处理能力,这使得它能够在多轮对话中保持对上下文的理解和记忆,减少了信息丢失和误解,提高了对话的连贯性和准确性。

Moonshot AI 是一家专注于通用人工智能领域的公司,其愿景是寻求将能源转化为智能的最优解,通过产品与用户共创智能,实现普惠AI。Moonshot AI 的核心团队曾参与开发Transformer XL、RoPE等关键算法,并且在大模型领域有着深厚的技术积累。公司的名字来源于英国摇滚乐队Pink Floyd的专辑【Dark Side of the Moon】,象征着对未知的探索和创新。

kimi 是他们家智能助手的名字,真正的大模型是叫 Moonshot 。这一点我们从 API 的 model 参数中也能发现。因此后面谈到大模型时,就统一称呼为 Moonshot

Moonshot 这个名字可能取自 「登月计划」,可见他们的雄心壮志。公司取名 「月之暗面」,更是增添了几分神秘色彩。

官方地址:https://www.moonshot.cn

API 设计

Moonshot 还有一点让魔法哥很有好感——它的 API 语法完全兼容 OpenAI。这意味着海量基于 GPT 的开源项目和商业项目都有极大可能在 Moonshot 上跑起来。

对于开发者来说,这也是一项巨大的优势。现有基于 GPT 的老项目都可以无缝接入 Moonshot,基于 Moonshot 的新项目也可以随时更换引擎。不管是迁移来还是迁移走都毫无压力!

开放平台

开放地址:https://platform.moonshot.cn/docs/docs

平台还为每个新用户赠送了 15.00 元 元的 API 调用额度。对于开发测试来说,足够用一阵子了。

创建 API Key

创建的Key记得保存,后面要用!!!

webman/openai

简介

传统php-fpm架构调用openai等大模型接口时只能做到阻塞调用,由于大模型接口返回速度很慢,一个php-fpm进程一分钟只能调用几次,几个人一刷系统就会明显的卡顿甚至不可用状态,所以php-fpm不适合做大模型调用,而webman这类的常驻内存类型的框架非常适合大模型应用的开发。

webman/openai 是一个异步非阻塞的openai客户端,配合webman可以做到同一时刻支持上万并发调用,使用简单,返回如丝般的顺滑,无卡顿。

开源地址:https://github.com/webman-php/openai

安装

compsoerrequirewebman/openai

安装该插件之前记得先安装webman框架。

使用

Chat流式返回

新建一个控制器 ChatController

<?php
namespaceapp\controller;
usesupport\Request;
usesupport\Response;
useWebman\Openai\Chat;
useWorkerman\Protocols\Http\Chunk;
classChatController
{
/**
@desc completions
@param Request $request
@return Response
@author Tinywan(ShaoBo Wan)
*/

publicfunctioncompletions(Request $request)Response
{
$connection = $request->connection;
$chat = new Chat(['apikey' => 'sk-xxxxxxxxxxxxxxxxxxxxxxx''api' => 'https://api.moonshot.cn']);
$chat->completions(
[
'model' => 'moonshot-v1-8k',
'stream' => true,
'messages' => [['role' => 'user''content' => 'Tinywan 你好!']],
], [
'stream' => function($data)use($connection){
// 当openai接口返回数据时转发给浏览器
$connection->send(new Chunk(json_encode($data, JSON_UNESCAPED_UNICODE) . "\n"));
},
'complete' => function($result, $response)use($connection){
// 响应结束时检查是否有错误
if (isset($result['error'])) {
$connection->send(new Chunk(json_encode($result, JSON_UNESCAPED_UNICODE) . "\n"));
}
// 返回空的chunk代表响应结束
$connection->send(new Chunk(''));
},
]);
// 先返回一个http头,后面数据异步返回
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}
}

请求参数

  • apikey : Moonshot开放平台申请到的 Key

  • api : Moonshot公开的服务地址 https://api.moonshot.cn

  • model : 模型填写 moonshot-v1-8k

  • 以上确认没问题,启动webman

    phpstart.phpstart

    浏览器访问 http://127.0.0.1:8117/chat/completions

    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"你好"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"很高兴"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"见到"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"你"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"。"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"有什么"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"我"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"可以帮助"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"你的"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"吗"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"content":"?"},"finish_reason":null}]} 
    {"id":"chatcmpl-cdd0f9f085d14ab79e43264bb3d056bb","object":"chat.completion.chunk","created":5219720,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":[],"finish_reason":"stop","usage":{"prompt_tokens":8,"completion_tokens":12,"total_tokens":20}}]}










    截图

    Chat非流式返回

    <?php
    declare(strict_types=1);
    namespaceapp\controller;
    usesupport\Request;
    usesupport\Response;
    useWebman\Openai\Chat;
    useWorkerman\Protocols\Http\Chunk;
    classChatController
    {
    /**
    @desc completions
    @param Request $request
    @return Response
    @author Tinywan(ShaoBo Wan)
    */

    publicfunctioncompletions(Request $request)Response
    {
    $connection = $request->connection;
    $chat = new Chat(['apikey' => 'sk-xxxxxxxxxxxxxxxxxxxxxxx''api' => 'https://api.moonshot.cn']);
    $chat->completions(
    [
    'model' => 'moonshot-v1-8k',
    'messages' => [['role' => 'user''content' => '你好呀!']],
    ], [
    'complete' => function($result, $response)use($connection){
    $connection->send(new Chunk(json_encode($result, JSON_UNESCAPED_UNICODE) . "\n"));
    $connection->send(new Chunk(''));
    },
    ]);
    return response()->withHeaders([
    "Transfer-Encoding" => "chunked",
    ]);
    }
    }

    浏览器访问 http://127.0.0.1:8117/chat/completions

    {
    "id""chatcmpl-778d76fcd9264e73b6ced8f7ffd75f3a",
    "object""chat.completion",
    "created": 2078947,
    "model""moonshot-v1-8k",
    "choices": [
    {
    "index": 0,
    "message": {
    "role""assistant",
    "content""你好!很高兴和你交流。有什么问题我可以帮你解答吗?"
    },
    "finish_reason""stop"
    }
    ],
    "usage": {
    "prompt_tokens": 6,
    "completion_tokens": 14,
    "total_tokens": 20
    }
    }

    截图