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?

WindowsでPLaMo PrimeのAPIを使用 python openaiライブラリにて

Last updated at Posted at 2025-01-01

PLaMo PrimeのAPIを使用したくてPythonでコードを作成していたのですが、少してこずったのでまとめておきます。

PLaMo PrimeのAPIの使用方法は以下のページに記載されています。

API 利用方法

しかしこのページではLinuxでのコマンドの入力方法しか記載されていない。

export PLAMO_API_KEY="your_api_key"
export OPENAI_API_KEY=$PLAMO_API_KEY

上記はPLAMO_API_KEYの環境変数を作成し、APIキーの値を入れる。
またOPENAI_API_KEYの環境変数を作成し、PLAMO_API_KEYの環境変数の値を代入するというもの。
your_api_keyの欄に取得したAPIキーを入れる。

上記のコマンドはWindowsのPowerShellでも実行できる。

[System.Environment]::SetEnvironmentVariable("PLAMO_API_KEY", "your_api_key", [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable("OPENAI_API_KEY", $env:PLAMO_API_KEY, [System.EnvironmentVariableTarget]::User)

また正しく代入されているかは以下で確認できる。
上のコマンドを入れたら一度、PowerShellを閉じて確認する必要がある。

echo $env:PLAMO_API_KEY
echo $env:OPENAI_API_KEY

もっと簡単な方法としてwindowsの検索欄から環境変数と検索して「環境変数を編集」を表示させる。 新規(N)を押して、変数名(OPENAI_API_KEY)と変数値(APIキー)を入れてOKを押すとういう方法もある。

またopenaiライブラリーで使用するために以下のサンプルが提供されている。

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://platform.preferredai.jp/api/completion/v1",
    # other params...,
)

completion = client.chat.completions.create(
    model="plamo-1.0-prime",
    messages=[
        {"role": "system", "content": "あなたは旅行アドバイザーです"},
        {"role": "user", "content": "金沢で朝から夕方まで1日のおすすめの観光ルートを教えて下さい"},
    ],
    stream=True,
)

for chunk in completion:
    if chunk.choices and chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="", flush=True)

このコードのclient.chat.completions.createの機能はopenaiライブラリのバージョンが1.*以上の系統でないと使用できない。

また以下の記事にrequestsを使用した方法が記述されています。

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?