{"id":1661,"date":"2023-03-03T13:05:00","date_gmt":"2023-03-03T04:05:00","guid":{"rendered":"https:\/\/tech.at-iroha.jp\/?p=1661"},"modified":"2026-04-06T13:52:19","modified_gmt":"2026-04-06T04:52:19","slug":"chatgpt-api-%e3%81%ae%e5%88%a9%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/tech.at-iroha.jp\/?p=1661","title":{"rendered":"ChatGPT API (GPT-3.5) \u306e\u5229\u7528\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u6628\u65e5\u3001OpenAI \u304b\u3089\u516c\u958b\u3055\u308c\u305f\u3070\u304b\u308a\u306e ChatGPT \u306eAPI\u3092\u5229\u7528\u3057\u3066\u3001\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u516c\u5f0f\u306e ChatGPT \u3088\u308a\u3082\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u683c\u6bb5\u306b\u901f\u3044\u3067\u3059\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/tech.at-iroha.jp\/?p=1649\">\u524d\u56de\u306e\u5b9f\u88c5<\/a>\u3067\u306f\u5358\u7d14\u306b\u30d5\u30a9\u30fc\u30e0\u304b\u3089 POST \u3055\u308c\u305f question \u306e\u30c6\u30ad\u30b9\u30c8\u306e\u4e2d\u8eab\u3092 API \u306b\u9001\u4fe1\u3057\u3066\u304a\u308a\u307e\u3057\u305f\u304c\u3001ChatGPT \u3067\u6587\u8108\u3092\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u904e\u53bb\u306e\u5bfe\u8a71\u306e\u5c65\u6b74\u3092\u5206\u5272\u3057\u3066\u914d\u5217\uff08messages\uff09\u3068\u3057\u3066\u9001\u4fe1\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u9001\u4fe1\u5074\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u914d\u5217\u3092JSON\u5f62\u5f0f\u306b\u5909\u63db\u3057\u3001PHP\u5074\u3067JSON\u3092\u30c7\u30b3\u30fc\u30c9\u3057\u3001API \u306b\u9001\u4fe1\u3057\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ messages \u306e\u4e2d\u8eab\n&#91;\r\n    {\r\n        \"role\": \"user\",\r\n        \"content\": \"\u904e\u53bb\u306e\u30d7\u30ed\u30f3\u30d7\u30c81\"\r\n    },\r\n    {\r\n        \"role\": \"assistant\",\r\n        \"content\": \"\u904e\u53bb\u306e\u56de\u7b541\"\r\n    },\r\n    {\r\n        \"role\": \"user\",\r\n        \"content\": \"\u904e\u53bb\u306e\u30d7\u30ed\u30f3\u30d7\u30c82\"\r\n    },\r\n    {\r\n        \"role\": \"user\",\r\n        \"assistant\": \"\u904e\u53bb\u306e\u56de\u7b542\"\r\n    },\r\n    {\r\n        \"role\": \"user\",\r\n        \"content\": \"\u65b0\u3057\u3044\u30d7\u30ed\u30f3\u30d7\u30c8\"\r\n    }\r\n]\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/\n\/\/ OpenAI \u306e\u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u4f5c\u6210 ( https:\/\/beta.openai.com\/signup )\n\/\/ API Key \u3092\u53d6\u5f97 ( https:\/\/beta.openai.com\/account\/api-keys )\n\/\/\n$API_KEY = 'API Key';\n\n$header = &#91;\n\t'Authorization: Bearer '.$API_KEY,\n\t'Content-type: application\/json',\n];\n\n$messages = json_decode($_POST&#91;'messages'], true);\n\n$params = json_encode(&#91;\n\t'model'\t\t\t=&gt; 'gpt-3.5-turbo',\n\t'messages'\t\t=&gt; $messages,\n\t'temperature'\t=&gt; floatval($_POST&#91;'temperature']),\n\t'max_tokens'\t=&gt; floatval($_POST&#91;'max_tokens']),\n\t'top_p'\t\t\t=&gt; floatval($_POST&#91;'top_p']),\n\t'frequency_penalty'\t=&gt; floatval($_POST&#91;'frequency_penalty']),\n\t'presence_penalty'\t=&gt; floatval($_POST&#91;'presence_penalty'])\n]);\n\n$curl = curl_init('https:\/\/api.openai.com\/v1\/chat\/completions');\n\n$options = &#91;\n\tCURLOPT_POST =&gt; true,\n\tCURLOPT_HTTPHEADER =&gt;$header,\n\tCURLOPT_POSTFIELDS =&gt; $params,\n\tCURLOPT_RETURNTRANSFER =&gt; true,\n];\n\ncurl_setopt_array($curl, $options);\n$response = curl_exec($curl);\n$httpcode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);\n\nif(200 == $httpcode)\n{\n\t$json_array = json_decode($response, true);\n\t$choices = $json_array&#91;'choices'];\n\t\n\tforeach($choices as $c)\n\t{\n\t\techo $c&#91;'message']&#91;'content'].'&lt;br&gt;';\n\t}\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>API \u306b\u9001\u4fe1\u3059\u308b\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u5185\u5bb9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>model<\/td><td>\u4f7f\u7528\u3059\u308b\u8a00\u8a9e\u30e2\u30c7\u30eb GPT-3.5 \u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u3001gpt-3.5-turbo \u3092\u6307\u5b9a\u3002<\/td><\/tr><tr><td>messages<\/td><td>\u5bfe\u8a71\u306e\u5c65\u6b74\u304a\u3088\u3073\u30ed\u30fc\u30eb\u3092\u6307\u5b9a\u3002<br>{ &#8220;role&#8221;: &#8220;assistant&#8221;,<br>&#8220;content&#8221;: &#8220;\\n\\nHello there, how may I assist you today?&#8221;,}<\/td><\/tr><tr><td>temperature<\/td><td>\u751f\u6210\u3059\u308b\u30c8\u30fc\u30af\u30f3\u3092\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u3059\u308b\u78ba\u7387\u3001\u30e9\u30f3\u30c0\u30e0\u6027\u3002<br>\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\uff1a1.0<\/td><\/tr><tr><td>max_tokens<\/td><td>\u30c8\u30fc\u30af\u30f3\u6570\u306e\u4e0a\u9650\u3002<br>1024<\/td><\/tr><tr><td>top_p<\/td><td>Top-p \u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u3001\u591a\u69d8\u6027\u3002<br>\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\uff1a1.0<\/td><\/tr><tr><td>frequency_penalty<\/td><td>\u983b\u51fa\u30da\u30ca\u30eb\u30c6\u30a3\u3001\u9ad8\u3044\u307b\u3069\u540c\u3058\u8a71\u984c\u3092\u7e70\u308a\u8fd4\u3055\u306a\u304f\u306a\u308b\u3002<br>\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\uff1a0<\/td><\/tr><tr><td>presence_penalty<\/td><td>\u30d7\u30ec\u30bc\u30f3\u30b9\u30da\u30ca\u30eb\u30c6\u30a3\u3001\u9ad8\u3044\u307b\u3069\u65b0\u3057\u3044\u30c8\u30d4\u30c3\u30af\u304c\u51fa\u73fe\u3057\u3084\u3059\u304f\u306a\u308b\u3002<br>\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\uff1a0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8a73\u7d30\u306b\u306f\u4ee5\u4e0b\u306eOpenAI\u793e\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<p>Open AI API reference &#8211; Chat<br><a href=\"https:\/\/platform.openai.com\/docs\/api-reference\/chat\">https:\/\/platform.openai.com\/docs\/api-reference\/chat<\/a><\/p>\n\n\n\n<p>\u5b9f\u969b\u306b\u30c7\u30e2\u30da\u30fc\u30b8\u3092\u4f5c\u6210\u3044\u305f\u3057\u307e\u3057\u305f\u3002\u4e88\u7b97\u306e\u4e0a\u9650\u306b\u9054\u3057\u305f\u3089\u505c\u6b62\u3057\u307e\u3059\u3002\u5c65\u6b74\u7b49\u306f\u53d6\u3063\u3066\u3044\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<p>ChatGPT(GPT3.5)\u3068\u306e\u5bfe\u8a71<br><a href=\"https:\/\/labs.irohasoft.com\/chatgpt\/\">https:\/\/labs.irohasoft.com\/chatgpt\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6628\u65e5\u3001OpenAI \u304b\u3089\u516c\u958b\u3055\u308c\u305f\u3070\u304b\u308a\u306e ChatGPT \u306eAPI\u3092\u5229\u7528\u3057\u3066\u3001\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u516c\u5f0f\u306e ChatGPT \u3088\u308a\u3082\u30ec\u30b9\u30dd\u30f3\u30b9\u304c\u683c\u6bb5\u306b\u901f\u3044\u3067\u3059\u3002 \u524d\u56de\u306e\u5b9f\u88c5\u3067\u306f\u5358\u7d14\u306b\u30d5\u30a9\u30fc\u30e0\u304b\u3089 POST \u3055 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1653,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-1661","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nlp"],"_links":{"self":[{"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/posts\/1661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1661"}],"version-history":[{"count":15,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/posts\/1661\/revisions"}],"predecessor-version":[{"id":1733,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/posts\/1661\/revisions\/1733"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=\/wp\/v2\/media\/1653"}],"wp:attachment":[{"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.at-iroha.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}