{"id":2337,"date":"2023-05-20T09:49:28","date_gmt":"2023-05-20T01:49:28","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2337"},"modified":"2023-05-20T09:49:28","modified_gmt":"2023-05-20T01:49:28","slug":"2337","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2337.html","title":{"rendered":"Chatgpt\uff0cThinkPHP6\u4f7f\u7528openai api\u793a\u4f8b"},"content":{"rendered":"

TP6\u81ea\u884c\u5b89\u88c5\uff0c\u672c\u6587\u53ea\u4ecb\u7ecdopenai api\u63a5\u53e3<\/span><\/p>\n

1.composer\u5b89\u88c5open-ai<\/span><\/span><\/p>\n

composer require orhanerday\/open-ai<\/pre>\n

2.\u547d\u540d\u7a7a\u95f4\u5f15\u5165<\/span><\/p>\n

use Orhanerday\\OpenAi\\OpenAi;<\/pre>\n

3.\u8bf7\u6c42\u63a5\u53e3<\/span><\/span><\/p>\n

OPENAI_API_KEY\u7533\u8bf7\u5730\u5740<\/a><\/span><\/p>\n

$question = $this->request->param('question','');\r\n$open_ai_key = "xxx";\/\/\u4f60\u7684key\r\n$open_ai = new OpenAi($open_ai_key);\r\n$open_ai->setProxy("http:\/\/127.0.0.1:2023");\/\/\u672c\u5730\u8c03\u8bd5\u4ee3\u7406\r\n\/\/ \u8fd4\u56de\u6587\u672c\r\n$complete = $open_ai->chat([\r\n    'model' => 'gpt-3.5-turbo',\r\n    'messages' => [\r\n        [\r\n            "role" => "user",\r\n            "content" => $question\r\n        ],\r\n    ],\r\n    'temperature' => 1.0,\r\n    'max_tokens' => 4000,\r\n    'frequency_penalty' => 0,\r\n    'presence_penalty' => 0,\r\n]);\r\n \r\n$returnData = json_decode($complete,true);\r\n$this->success('\u8fd4\u56de\u6210\u529f', $returnData);<\/pre>\n

\"image.png\"\/<\/span><\/p>\n

4.\u5176\u5b83\u7c7b\u578b\u63a5\u53e3\u4f7f\u7528<\/span> GitHub\u6e90\u7801<\/a><\/p>\n

\/\/ \u8fd4\u56de\u56fe\u7247\r\n$complete = $open_ai->image([\r\n    "prompt" => $question,\r\n    "n" => 1,\r\n    "size" => "256x256",\r\n    "response_format" => "url",\r\n]);\r\n \r\n\/\/ \u5c06\u97f3\u9891\u8f6c\u5f55\u6210\u8f93\u5165\u8bed\u8a00\r\n$c_file = curl_file_create('.\/assets\/img\/CKwRIRwE9j-9ABqphgDaXVV7.m4a');\r\n$result = $open_ai->transcribe([\r\n    "model" => "whisper-1",\r\n    "file" => $c_file,\r\n]);\r\n \r\n\/\/ \u4e0a\u4f20\u6587\u4ef6\r\n$c_file = curl_file_create('.\/assets\/img\/openai.docx');\r\n$result = $open_ai->uploadFile([\r\n        "purpose" => "answers",\r\n        "file" => $c_file,\r\n]);<\/pre>\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

TP6\u81ea\u884c\u5b89\u88c5\uff0c\u672c\u6587\u53ea\u4ecb\u7ecdopenai api\u63a5\u53e3 1.composer\u5b89\u88c5open-ai composer& […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[146],"tags":[],"class_list":["post-2337","post","type-post","status-publish","format-standard","hentry","category-chat-gpt-open-ai"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2337"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2337"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2337\/revisions"}],"predecessor-version":[{"id":2338,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2337\/revisions\/2338"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}