chatgpt 对接 上下文

chatgpt 对接

chatgpt 对接
相关代码

<?php
$message = $this->request->post('newMessage');
$selType = $this->request->post('selType');
try {
    switch ($selType) {
        case 1: // 问答模式
            $response = static::$openaiClient->completions()
                ->create(
                    ['model'       => 'text-davinci-003',
                     'prompt'      => $message,
                     'temperature' => 1,
                     'max_tokens'  => 2048,
                     'stop'        => '\n'
                    ]
                )->toModel();
            if ($response->choices) {
                $user = $this->auth->getUser();
                $data = [
                    'user_id'  => $user->id,
                    'from_msg' => $message,
                    'to_msg'   => $response->choices[0]->text,
                ];
                Message::create($data);
                $this->success('', $response->choices[0]->text);
            }
            break;
        case 2: // 上下文模式
            /** @var CreateResponse $response */
            $gpt      = cookie('chat-gpt');
            $user     = $this->auth->getUser();
            $messageM = $message;
            if ($gpt) {
                $msg  = '';
                $time = $gpt + 60 * 9;
                Message::where(['user_id'    => $user->id,
                                'type'       => 2,
                                'createtime' => ['between', "{$gpt},{$time}"]
                ])->order('createtime', 'asc')
                    ->select()
                    ->each(function ($item) use (&$msg) {
                        $msg .= "(You:{$item->from_msg}\n){$item->to_msg} ";
                    });
                $message = $msg . "(You:{$message})";
            }
            $response = static::$openaiClient->completions()
                ->create(
                    ['model'      => 'text-davinci-003',
                     'prompt'     => $message,
                     'max_tokens' => 2048,
                     'stop'       => '\n\n'
                    ]
                )->toModel();
            if ($response->choices) {
                $data = [
                    'user_id'  => $user->id,
                    'from_msg' => $messageM,
                    'type'     => $selType,
                    'to_msg'   => $response->choices[0]->text,
                ];
                Message::create($data);
                if (!$gpt) {
                    cookie('chat-gpt', time(), ['expire' => 10 * 60]);
                }
                $this->success('', $response->choices[0]->text);
            }
            break;
        case 3: // 代码模式
            $response = static::$openaiClient->completions()
                ->create(
                    ['model'       => 'text-davinci-003',
                     'prompt'      => $message,
                     'temperature' => 0,
                     'max_tokens'  => 2048,
                    ]
                )->toModel();
            if ($response->choices) {
                $user = $this->auth->getUser();
                $data = [
                    'user_id'  => $user->id,
                    'from_msg' => $message,
                    'type'     => $selType,
                    'to_msg'   => $response->choices[0]->text,
                ];
                Message::create($data);
                $this->success('', $response->choices[0]->text);
            }
            break;
        case 4: // 图片模式
            $response = static::$openaiClient->imagesGenerations()
                ->create(
                    ['prompt'          => $message,
                     'size'            => '512x512',
                     'n'               => 2,
                     "response_format" => "url",
                    ]
                )->toArray();
            if ($response['data']) {
                $user = $this->auth->getUser();
                $data = [
                    'user_id'  => $user->id,
                    'from_msg' => $message,
                    'type'     => $selType,
                    'to_msg'   => json_encode($response['data'], JSON_UNESCAPED_UNICODE),
                ];
                Message::create($data);
                $this->success('', $response['data']);
            }
            break;
    }
} catch (\Tectalic\OpenAi\ClientException|\think\exception\ErrorException $exception) {
    $this->error($exception->getMessage());
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 31
Mutoulee

自己开发接入的吗? 响应速度如何? 我接入后平均2秒左右才返回内容。

2年前 评论
把代码写成诗 (楼主) 2年前

费用怎么算的?

2年前 评论
把代码写成诗 (楼主) 2年前
把代码写成诗 (楼主) 2年前

openAI 中国手机支持注册吗

2年前 评论
把代码写成诗 (楼主) 2年前
道法自然 2年前
黑将军

这个是PC端吗

2年前 评论
把代码写成诗 (楼主) 2年前

代码没注释 可以说一下原理吗

2年前 评论
把代码写成诗 (楼主) 2年前
aba66 (作者) 2年前
把代码写成诗 (楼主) 2年前
air93610 2年前
风中絮 2年前
air93610 2年前
风中絮 2年前
air93610 2年前

这用的是哪个composer包啊

2年前 评论
把代码写成诗 (楼主) 2年前
猪猪

兄弟你的手机号问题怎么解决的

2年前 评论
把代码写成诗 (楼主) 2年前
猪猪 (作者) 2年前
NickDraw 2年前

真受不了上面排版

<?php
$message = $this->request->post('newMessage');
$selType = $this->request->post('selType');
try {
    switch ($selType) {
        case 1: // 问答模式
            $response = static::$openaiClient->completions()
                ->create(
                    ['model'       => 'text-davinci-003',
                     'prompt'      => $message,
                     'temperature' => 1,
                     'max_tokens'  => 2048,
                     'stop'        => '\n'
                    ]
                )->toModel();
            if ($response->choices) {
                $user = $this->auth->getUser();
                $data = [
                    'user_id'  => $user->id,
                    'from_msg' => $message,
                    'to_msg'   => $response->choices[0]->text,
                ];
                Message::create($data);
                $this->success('', $response->choices[0]->text);
            }
            break;
        case 2: // 上下文模式
            /** @var CreateResponse $response */
            $gpt      = cookie('chat-gpt');
            $user     = $this->auth->getUser();
            $messageM = $message;
            if ($gpt) {
                $msg  = '';
                $time = $gpt + 60 * 9;
                Message::where(['user_id'    => $user->id,
                                'type'       => 2,
                                'createtime' => ['between', "{$gpt},{$time}"]
                ])->order('createtime', 'asc')
                    ->select()
                    ->each(function ($item) use (&$msg) {
                        $msg .= "(You:{$item->from_msg}\n){$item->to_msg} ";
                    });
                $message = $msg . "(You:{$message})";
            }
            $response = static::$openaiClient->completions()
                ->create(
                    ['model'      => 'text-davinci-003',
                     'prompt'     => $message,
                     'max_tokens' => 2048,
                     'stop'       => '\n\n'
                    ]
                )->toModel();
            if ($response->choices) {
                $data = [
                    'user_id'  => $user->id,
                    'from_msg' => $messageM,
                    'type'     => $selType,
                    'to_msg'   => $response->choices[0]->text,
                ];
                Message::create($data);
                if (!$gpt) {
                    cookie('chat-gpt', time(), ['expire' => 10 * 60]);
                }
                $this->success('', $response->choices[0]->text);
            }
            break;
        case 3: // 代码模式
            $response = static::$openaiClient->completions()
                ->create(
                    ['model'       => 'text-davinci-003',
                     'prompt'      => $message,
                     'temperature' => 0,
                     'max_tokens'  => 2048,
                    ]
                )->toModel();
            if ($response->choices) {
                $user = $this->auth->getUser();
                $data = [
                    'user_id'  => $user->id,
                    'from_msg' => $message,
                    'type'     => $selType,
                    'to_msg'   => $response->choices[0]->text,
                ];
                Message::create($data);
                $this->success('', $response->choices[0]->text);
            }
            break;
        case 4: // 图片模式
            $response = static::$openaiClient->imagesGenerations()
                ->create(
                    ['prompt'          => $message,
                     'size'            => '512x512',
                     'n'               => 2,
                     "response_format" => "url",
                    ]
                )->toArray();
            if ($response['data']) {
                $user = $this->auth->getUser();
                $data = [
                    'user_id'  => $user->id,
                    'from_msg' => $message,
                    'type'     => $selType,
                    'to_msg'   => json_encode($response['data'], JSON_UNESCAPED_UNICODE),
                ];
                Message::create($data);
                $this->success('', $response['data']);
            }
            break;
    }
} catch (\Tectalic\OpenAi\ClientException|\think\exception\ErrorException $exception) {
    $this->error($exception->getMessage());
}
2年前 评论
把代码写成诗 (楼主) 2年前

GPT3.5一出来,api.openai.com 就被墙了,本地也开发不了了

2年前 评论
2年前 评论
把代码写成诗 (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!