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?

OpenRouterを触ってみる

Posted at

はじめに

最近、LineBotとAIを組み合わせた機能を作りたいなと考えています。
OpenRouterがAIのモデルを色々仕え、さらに無料枠があるということなので触っていきたいと思います!

OpenRouterとは

複数のAIモデルを1つのAPI経由で利用できる。chatgptやgemini、claude等のモデルが異なる場合、apiのInterfaceが異なるため、それぞれのモデルの形式に沿ったAPIを叩く必要がある。これが面倒なので、OpenRouterは1つのInterfaceで複数のモデルをいい感じに利用できるよってやつ。
image.png

触ってみる

まずは、OpernRouterのサインアップからしていきます!!
下記URLからアカウント作成を実施してください。

・APIキーを発行する
では、APIのkeyを発行します。今回はtestというAPIkeyを発行します
image.png

実際にAPIを叩く

環境変数を設定する
~  $ # ① APIキーを環境変数にセット
     export OPENROUTER_API_KEY="...." //先ほど生成したAPIKeyを設定します
openrouterの実行コマンド
curl https://openrouter.ai/api/v1/chat/completions \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "google/gemini-2.0-flash-exp:free",
    "messages": [
      {"role": "system", "content": "あなたは親切なアシスタントです。"},
      {"role": "user", "content": "大阪の今日の天気を教えてください。"}
    ]
  }'
実行結果
{
	"id": "gen-1745715209-fgvOs1Rq3EqUPYULIiS7",
	"provider": "Google AI Studio",
	"model": "google/gemini-2.0-flash-exp:free",
	"object": "chat.completion",
	"created": 1745715209,
	"choices": [
		{
			"logprobs": null,
			"finish_reason": "stop",
			"native_finish_reason": "STOP",
			"index": 0,
			"message": {
				"role": "assistant",
				"content": "大阪の今日の天気は晴れ時々曇りです。最高気温は31℃、最低気温は23℃です。\n\n他に何か知りたいことはありますか?\n",
				"refusal": null,
				"reasoning": null
			}
		}
	],
	"usage": {
		"prompt_tokens": 16,
		"completion_tokens": 35,
		"total_tokens": 51
	}
}

→返答が返ってきましたね!!

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?