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

TypeScriptで自作ミニCLIアプリケーションを活用してClaude / OpenAI APIを叩く

0
Posted at

はじめに

TypeScriptとOpenAI APIを組み合わせて、自作のミニCLIアプリケーションを作成する方法について説明します。この記事では、TypeScriptの基礎知識とOpenAI APIの利用方法を理解していることを前提とします。

OpenAI APIの概要

OpenAI APIは、自然言語処理タスクを実行するために使用されるRESTful APIです。このAPIを使用して、テキスト生成、翻訳、質問回答などのタスクを実行できます。

Claude / OpenAI APIを叩くミニCLIの作成

ここでは、TypeScriptで自作のミニCLIアプリケーションを作成して、Claude / OpenAI APIを叩く方法について説明します。

プロジェクトの作成

まず、TypeScriptのプロジェクトを作成します。

npm init -y
tsc --init

次に、必要なパッケージをインストールします。

npm install axios

ミニCLIアプリケーションの作成

次に、ミニCLIアプリケーションを作成します。

// cli.ts
import axios from 'axios';

interface Args {
  prompt: string;
}

const args: Args = {
  prompt: process.argv[2],
};

const apiEndpoint = 'https://api.openai.com/v1/completions';
const apiKey = 'YOUR_API_KEY';

const params = {
  model: 'text-davinci-003',
  prompt: args.prompt,
  max_tokens: 2048,
};

axios
  .post(apiEndpoint, params, {
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${apiKey}`,
    },
  })
  .then((response) => {
    console.log(response.data.choices[0].text);
  })
  .catch((error) => {
    console.error(error);
  });

ミニCLIアプリケーションの実行

次に、ミニCLIアプリケーションを実行します。

tsc cli.ts
node cli.js "HELLO"

これで、Claude / OpenAI APIを叩くミニCLIアプリケーションが作成されました。

まとめ

この記事では、TypeScriptで自作のミニCLIアプリケーションを作成して、Claude / OpenAI APIを叩く方法について説明しました。このアプリケーションは、自然言語処理タスクを実行するために使用できます。

詳しい手順はこちら → https://felixstudio0.gumroad.com/?utm_source=qiita&utm_medium=github_actions&utm_campaign=2026-q1&utm_content=qiita-footer

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