How to change gpt-3.5-turbo api to gpt-4

The api of gpt-3.5-turbo has been added to my system, and now I want to upgrade it to gpt-4. I have tried changing model=gpt-3.5-turbo to model=gpt-4 but this does not work.
Contains two files, model.php and chatgpt.php. I hope you can help me. There is also a config→chatgpt.php query that includes the api key(i think it don’t need change.).
Below is their code.

  1. Model.php:
<?php namespace chatgpt; /** * Class Model * @package chatgpt */ class Model { /** * 文字提示和对话 */ const GPT_35_TURBO = 'gpt-3.5-turbo'; const TEXT_DAVINCI_003 = 'text-davinci-003'; const TEXT_DAVINCI_002 = 'text-davinci-002'; /** * 代码生成 */ const CODEX_DAVINCI_002 = 'code-davinci-002'; const CODEX_CUSHMAN_001 = 'code-cushman-001'; /** * 嵌入(衡量文本相关性) */ const TEXT_EMBEDDING_ADA_002 = 'text-embedding-ada-002'; /** * 图像创建 */ // const DALL_E = ''; /** * 语音 */ const WHISPER_1 = 'whisper-1'; } 2. Chatgpt.php: <?php namespace chatgpt; use app\common\model\Debug; use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Handler\CurlHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\CachingStream; use GuzzleHttp\Psr7\Stream; use GuzzleHttp\RequestOptions; use Mytools\Util\ConvertUtil; use Mytools\Util\StringUtil; use think\facade\Config; /** * Class ChatGPT4 * @package chatgpt */ class ChatGPT { //配置参数 private $config = [ 'app_id' => '', 'app_secret' => '', 'http' => [ // 'base_uri' => '', 'timeout' => 20, ], ]; public function __construct(array $config = []) { $conf = Config::pull('chatgpt'); $this->config = array_merge($this->config, $conf, $config); } /** * 列出模型 * @return array|null */ public function models(): ?array { $uri = 'https://api.openai.com/v1/models'; return $this->get($uri, []); } /** * 聊天接口,可以传入上下文 * @param array $messages [{"role": "system", "content": "Hello!"},{"role": "user", "content": "Hello!"}] * @param int $max_messages * @param string $model * @return array|null */ public function chatCompletions(array $messages, int $max_messages = 3, string $model = Model::GPT_35_TURBO): ?array { // 控制最大上下文消息数量 if (count($messages) > $max_messages) { $messages = array_slice($messages, $max_messages * -1); } $post = [ 'model' => $model, 'messages' => $messages, ]; $uri = 'https://api.openai.com/v1/chat/completions'; return $this->postJson($uri, [], $post); } /** * 聊天接口,可以传入上下文 * @param callable $callable 处理流中的每行消息 * @param array $messages [{"role": "system", "content": "Hello!"},{"role": "user", "content": "Hello!"}] * @param int $max_messages * @param string $model * @return array|null */ public function chatCompletionsStream(callable $callable, array $messages, int $max_messages = 3, string $model = Model::GPT_35_TURBO): ?array { // 控制最大上下文消息数量 if (count($messages) > $max_messages) { $messages = array_slice($messages, $max_messages * -1); } $post = [ 'model' => $model, 'messages' => $messages, 'stream' => true, ]; $uri = 'https://api.openai.com/v1/chat/completions'; return $this->postJsonStream($uri, [], $post, $callable); } /** * 完成 * @param string $prompt * @param string $model * @return array|null */ public function completions(string $prompt, string $model = Model::TEXT_DAVINCI_003): ?array { $post = [ 'model' => $model, 'prompt' => $prompt, 'max_tokens' => 1000, ]; $uri = 'https://api.openai.com/v1/completions'; return $this->postJson($uri, [], $post); } /** * 编辑 * @param string $input * @param string $instruction * @param string $model * @return array|null */ public function edits(string $input, string $instruction, string $model = Model::TEXT_DAVINCI_003): ?array { $post = [ 'input' => $input, 'instruction' => $instruction, 'model' => $model, ]; $uri = 'https://api.openai.com/v1/edits'; return $this->postJson($uri, [], $post); } /** * 图像生成 * @param string $prompt * @param int $w * @param int|null $h * @return void */ public function imagesGenerations(string $prompt, int $w = 1024, ?int $h = null): ?array { if (is_null($h)) { $h = $w; } $size = $w . 'x' . $h; $post = [ 'prompt' => $prompt, 'size' => $size, ]; $uri = 'https://api.openai.com/v1/images/generations'; return $this->postJson($uri, [], $post); } /** * 图像编辑 * @param string $prompt * @param string $image 图片地址 * @param string $mask * @param int $w * @param int|null $h * @return array|null */ public function imagesEdits(string $prompt, string $image, string $mask, int $w = 1024, ?int $h = null): ?array { if (is_null($h)) { $h = $w; } $size = $w . 'x' . $h; $post = [ 'prompt' => $prompt, 'size' => $size, ]; $files = [ 'image' => $image, 'mask' => $mask, ]; $uri = 'https://api.openai.com/v1/images/edits'; return $this->postMultipart($uri, [], $post, $files); } /** * 图像变化 * @param string $image 图像地址 * @param int $w * @param int|null $h * @return array|null */ public function imagesVariations(string $image, int $w = 1024, ?int $h = null): ?array { if (is_null($h)) { $h = $w; } $size = $w . 'x' . $h; $post = [ 'size' => $size, ]; $files = [ 'image' => $image, ]; $uri = 'https://api.openai.com/v1/images/variations'; return $this->postMultipart($uri, [], $post, $files); } /** * 语音转文字 * @param string $file 语音文件地址 * @param string $prompt * @param string $model * @return array|null */ public function audioTranscriptions(string $file, string $prompt, string $model = Model::WHISPER_1): ?array { $post = [ 'prompt' => $prompt, 'model' => $model, ]; $files = [ 'file' => $file, ]; $uri = 'https://api.openai.com/v1/audio/transcriptions'; return $this->postMultipart($uri, [], $post, $files); } /** * 语音转文字翻译(翻译到英语) * @param string $file 语音文件地址 * @param string $prompt * @param string $model * @return array|null */ public function audioTranslations(string $file, string $prompt, string $model = Model::WHISPER_1): ?array { $post = [ 'prompt' => $prompt, 'model' => $model, ]; $files = [ 'file' => $file, ]; $uri = 'https://api.openai.com/v1/audio/translations'; return $this->postMultipart($uri, [], $post, $files); } /** * 发起请求 * @param string $uri * @param array $get * @return array|null */ private function get(string $uri, array $get): ?array { try { $key = $this->config['key']; $organization = $this->config['organization']; $headers = [ 'Authorization' => 'Bearer ' . $key, 'OpenAI-Organization' => $organization, 'Content-Type' => 'application/json', ]; $options = [ RequestOptions::HEADERS => $headers, RequestOptions::QUERY => $get, RequestOptions::PROXY => [ 'http' => 'socks5h://104.243.29.54:10000/', 'https' => 'socks5h://104.243.29.54:10000/', // 'no' => ['.mit.edu', 'foo.com'] // Don't use a proxy with these ], RequestOptions::VERIFY => false, ]; $response = $this->getHttp()->get($uri, $options); $body = $response->getBody(); $content = $body->getContents(); if (is_string($content)) { $content = ConvertUtil::jsonDecode($content); } Debug::store('chatGPT', $content); return $content; } catch (\Exception $exception) { Debug::store('chatGPT Exception', $exception->getMessage()); if ($exception instanceof RequestException) { } return null; } } /** * 发起请求 * @param string $uri * @param array $get * @param array $post * @return array|null */ private function postJson(string $uri, array $get, array $post): ?array { try { $key = $this->config['key']; $organization = $this->config['organization']; $headers = [ 'Authorization' => 'Bearer ' . $key, 'OpenAI-Organization' => $organization, 'Content-Type' => 'application/json', ]; $options = [ RequestOptions::HEADERS => $headers, RequestOptions::QUERY => $get, RequestOptions::JSON => $post, RequestOptions::PROXY => [ 'http' => 'socks5h://104.243.29.54:10000/', 'https' => 'socks5h://104.243.29.54:10000/', // 'no' => ['.mit.edu', 'foo.com'] // Don't use a proxy with these ], RequestOptions::VERIFY => false, ]; $response = $this->getHttp()->post($uri, $options); $body = $response->getBody(); if ($body->isReadable()) { } $content = $body->getContents(); if (is_string($content)) { $content = ConvertUtil::jsonDecode($content); } Debug::store('chatGPT', ['post' => $post, 'content' => $content]); return $content; } catch (\Exception $exception) { Debug::store('chatGPT Exception', $exception->getMessage()); return null; } } private function postJsonStream(string $uri, array $get, array $post, callable $callable) { try { $key = $this->config['key']; $organization = $this->config['organization']; $headers = [ 'Authorization' => 'Bearer ' . $key, 'OpenAI-Organization' => $organization, 'Content-Type' => 'application/json', ]; $options = [ RequestOptions::STREAM => true, RequestOptions::HEADERS => $headers, RequestOptions::QUERY => $get, RequestOptions::JSON => $post, RequestOptions::PROXY => [ 'http' => 'socks5h://104.243.29.54:10000/', 'https' => 'socks5h://104.243.29.54:10000/', // 'no' => ['.mit.edu', 'foo.com'] // Don't use a proxy with these ], RequestOptions::VERIFY => false, ]; $response = $this->getHttp(true)->post($uri, $options); $body = $response->getBody(); $result = []; while (!$body->eof()) { $line = $body->read(100000); $callable($line); $result[] = $line; } return $result; } catch (\Exception $exception) { Debug::store('chatGPT Exception', $exception->getMessage()); return null; } } /** * 发起请求 * @param string $uri * @param array $get * @param array $post * @param array $files * @return array|null */ private function postMultipart(string $uri, array $get, array $post, array $files): ?array { try { $key = $this->config['key']; $organization = $this->config['organization']; $headers = [ 'Authorization' => 'Bearer ' . $key, 'OpenAI-Organization' => $organization, 'Content-Type' => 'multipart/form-data', ]; $resources = []; $multipart = []; foreach ($post as $name => $contents) { $multipart[] = [ 'name' => $name, 'contents' => $contents, ]; } foreach ($files as $name => $file) { $resource = fopen($file, 'r'); $multipart[] = [ 'name' => $name, 'contents' => new CachingStream(new Stream($resource)), 'filename' => basename($file), ]; $resources[] = $resource; } $options = [ RequestOptions::HEADERS => $headers, RequestOptions::QUERY => $get, RequestOptions::MULTIPART => $multipart, RequestOptions::PROXY => [ 'http' => 'socks5h://104.243.29.54:10000/', 'https' => 'socks5h://104.243.29.54:10000/', // 'no' => ['.mit.edu', 'foo.com'] // Don't use a proxy with these ], RequestOptions::VERIFY => false, ]; $response = $this->getHttp()->post($uri, $options); // 关闭资源 foreach ($resources as $resource) { fclose($resource); } $body = $response->getBody(); $content = $body->getContents(); if (is_string($content)) { $content = ConvertUtil::jsonDecode($content); } Debug::store('chatGPT', ['post' => $post, 'content' => $content]); return $content; } catch (\Exception $exception) { Debug::store('chatGPT Exception', $exception->getMessage()); return null; } } /** * @param bool $stream * @param string|null $base_uri * @return Client */ private function getHttp(bool $stream = false, ?string $base_uri = null): Client { $http_config = $this->config['http']; if (StringUtil::notBlank($base_uri)) { $http_config['base_uri'] = $base_uri; } // 使用 cURL 处理程序,这样才能使用流式传输 if ($stream) { $http_config['handler'] = HandlerStack::create(new CurlHandler()); } return new Client($http_config); } } [model.php](https://drive.google.com/file/d/1Z-0_enfmIkKBKw_ksAWhwYhj8th-ufBC/view?usp=drive_link)[chatgpt.php](https://drive.google.com/file/d/1s9kGextaCCT8fk0jzXu9l2bbjc37ochP/view?usp=drive_link)