1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Deno: ChatGPT API の使い方

Last updated at Posted at 2023-04-11

こちらのプログラムを Deno を使って書きました。
Curl: ChatGPT API の使い方

プログラム

deno_fuji.sh
export OPENAI_API_KEY="sk-****************'
deno run --allow-all deno_fuji.ts
deno_fuji.ts
// ---------------------------------------------------------------
//	deno_fuji.ts
//
//					Apr/11/2023
//
// ---------------------------------------------------------------
import process from "node:process"

console.error ("*** 開始 ***")

const url = "https://api.openai.com/v1/chat/completions"

const response = await fetch(url, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": `Bearer ${process.env.OPENAI_API_KEY}`,
  },
  body: JSON.stringify({
    "model": "gpt-3.5-turbo",
    "messages": [{ "role": "user", "content": "富士山の高さは" }]
  }),
})

const data = await response.json()
// console.log(data)
console.log(data.choices[0].message)

console.error ("*** 終了 ***")
// ---------------------------------------------------------------

実行結果

$ ./deno_fuji.sh 
*** 開始 ***
{ role: "assistant", content: "富士山の高さは、3776メートルです。" }
*** 終了 ***

確認したバージョン

$ deno --version
deno 1.32.3 (release, x86_64-unknown-linux-gnu)
v8 11.2.214.9
typescript 5.0.3
1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?