當前位置: 妍妍網 > 碼農

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
    }
    }

    截圖