LoginSignup
4

More than 1 year has passed since last update.

続:ChatGPTのAPI調査

Last updated at Posted at 2022-12-02

ChatGPTのAPI調査の続きです。

ソースコード確認

openai-quickstart-node/pages/apiの
generate.js
を確認します。

  1 import { Configuration, OpenAIApi } from "openai";
  2 
  3 const configuration = new Configuration({
  4   apiKey: process.env.OPENAI_API_KEY,
  5 });
  6 const openai = new OpenAIApi(configuration);
  7 
  8 export default async function (req, res) {
  9   const completion = await openai.createCompletion({
 10     model: "text-davinci-002",
 11     prompt: generatePrompt(req.body.animal),
 12     temperature: 0.6,
 13   });
 14   res.status(200).json({ result: completion.data.choices[0].text });
 15 }
 16 
 17 function generatePrompt(animal) {
 18   const capitalizedAnimal =
 19     animal[0].toUpperCase() + animal.slice(1).toLowerCase();
 20   return `Suggest three names for an animal that is a superhero.
 21 
 22 Animal: Cat
 23 Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
 24 Animal: Dog
 25 Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
 26 Animal: ${capitalizedAnimal}
 27 Names:`;
 28 }

9行目のawait openai.createCompletionでapiを呼んでいる。
Lionと入力すると

King Courageous, The Majestic Defender, Lord Roar-a-Lot

これがLionっぽいのかは不明ですが。。。
Penguinを入力すると

Captain Beak, Wingman, SuperWaddle

となった。
12行目のtemperature: 0.6  の値を変えてチューニングするみたいです。

Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer.

0.9だとクリエイティブで、0だと明確な答えらしい。

続いて、ちょっとソースコード解析からずれて、チュートリアルがそうなってます。

チャットと会話する

では、色々と会話が出来ました。

Marv is a chatbot that reluctantly answers questions with sarcastic responses:

You: How many pounds are in a kilogram?
Marv: This again? There are 2.2 pounds in a kilogram. Please make a note of this.
You: What does HTML stand for?
Marv: Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future.
You: When did the first airplane fly?
Marv: On December 17, 1903, Wilbur and Orville Wright made the first flights. I wish they’d come and take me away.
You: What is the meaning of life?
Marv: I’m not sure. I’ll ask my friend Google.
You: What time is it?
Marv: It's always time to ask better questions.

ここまでが起動します。先ずはhelloと打ってみました。

hello
Marv: Hi there! How can I help?

コーヒでもお願いしてみます。

I'd like to drink some coffee
Marv: Sure, I can help you find a coffee shop nearby. What city are you in?

東京

I live in tokyo
Marv: Great! There are plenty of coffee shops in Tokyo. Let me see what I can find for you.
yes please
Marv: Alright, here are some of the top-rated coffee shops in Tokyo: 
1. Coffee Kizoku
2. The Roastery by Nozy Coffee
3. % Arabica
4. Onibus Coffee
5. Streamer Coffee Company
6. Bear

コーヒショップを提案してくれましたが、本当にあるの?w

thanks! I'll take coffee from Coffe Kizoku
Marv: Sounds good! Enjoy your coffee from Coffee Kizoku.
Can you pick some coffee up for me from Coffee Kizoku
Marv: I'm sorry, I'm not able to do that. But I can help you find the address and phone number of Coffee Kizoku so you can order your coffee.
OK! good bye
Marv: Bye! Have a great day.

流石に買いには行ってくれませんでした。
普通に会話出来ますね。

reactのソースコードを生成

こちらは、reactのソースコードを補完生成してくれます

import React from 'react';
const HeaderComponent = () => (
    <h1>Welcome to my React App</h1>
);

export default HeaderComponent;

ソースコード生成は、Codexがコード生成をやってくれるみたいで、下記に参考動画があります。
https://www.youtube.com/watch?v=cLQc-5BJz4g&list=PLOXw6I10VTv_FhQbbvYh1FvbiaPf43Ve2

こちらは、

query = "select * from users";

を入力すると

+$result = $db->query($query);
+
+$users = [];
+while($row = $result->fetch_assoc()){
+    $users[] = $row;
+}
+
+echo json_encode($users);
+
+?>

を返しくれました。

なんか凄すぎますね。
この後、apiリファレンス読み進めます。ちょっと疲れたので後日。

続:ChatGPTのAPI調査・その2

ChatGPTの中身についてはこちらにまとめました。
必見:ChatGPTのテクノロジーの凄さについて

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4