AI魔法学院客服
Openai 异步客户端接入国产大模型 Kimi
Moonshot AI开发的人工智能助手Kimi Chat具备强大的长文本处理能力,支持长达20万字的上下文处理,提升对话连贯性和准确性。其API设计兼容OpenAI,便于开发者迁移和接入。Moonshot开放平台为新用户赠送API调用额度。文章还介绍了适合大模型调用的webman/openai异步非阻塞客户端,通过流式和非流式返回实现高效的大模型对话功能。
 2024-10-08
收藏 复制地址分享海报

Moonshot

介绍

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

Moonshot AI 是一家专注于通用人工智能领域的公司,其愿景是寻求将能源转化为智能的最优解,通过产品与用户共创智能,实现普惠AIMoonshot AI 的核心团队曾参与开发Transformer XLRoPE等关键算法,并且在大模型领域有着深厚的技术积累。公司的名字来源于英国摇滚乐队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记得保存,后面要用!!!

IMG_258

webman/openai

简介

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

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

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

安装

composerrequirewebman/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}}]}

截图

IMG_260

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

截图

出自:https://mp.weixin.qq.com/s/y20FvvTKSI7slJrdnk8O1g

本文档由网友提供,仅限参考学习,如有不妥或产生版权问题,请联系我们及时删除。 客服请加微信:skillupvip
评论
1 评论
我十七2024/10/8 10:26:52
"哇塞!Kimi Chat这是要上天啊,20万字上下文处理,对话流畅度MAX!兼容OpenAI API,开发者福音,迁移无缝对接爽歪歪。Moonshot这波操作,新人福利也是杠杠的,API额度免费送,良心!再加上webman/openai的异步非阻塞黑科技,大模型对话效率飞起,流畅对话不再是梦,简直是AI界的‘月球漫步’!"
20秒读懂全文
伴读
### 1. 一句话总结文章摘要
文章介绍了由Moonshot AI公司开发的Kimi Chat人工智能助手及其API设计,以及如何通过webman/openai框架高效实现大模型接口调用的方法。

### 2. 生成关键词和可能相关的关键词
- 关键词:Moonshot AI, Kimi Chat, 人工智能助手, 长文本处理, API设计, OpenAI兼容, we
One More Thing
One More Thing again ...

找组织,加入AI魔法学院群