SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

Introduction to OpenAI Python API Library v1.0

Since the interface for the "OpenAI Python API Library" has been completely revamped, I have briefly summarized it again.

OpenAI Python API library v1.1.1


1. OpenAI Python API Library

To access the "OpenAI API" in Python, use the "OpenAI Python API Library".

2. Setup

The setup procedure for Colab is as follows.

(1) Package installation.

# パッケージのインストール
!pip install openai

(2) Preparation of environment variables.
In the code below, specify the <OpenAI_API_Key> with the API key that can be obtained from the OpenAI website. (Paid)

import os
os.environ["OPENAI_API_KEY"] = "<OpenAI_APIキー>"

(3) Preparation of the client.
The "client" serves as the interface for accessing the "OpenAI API".

from openai import OpenAI

# クライアントの準備
client = OpenAI()

3. Text Generation

3-1. Chat Completion

"Completion" is an API for text generation from "message list → message".

response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {
            "role": "user",
            "content": "日本一高い山は?",
        }
    ],
    temperature=0.2,
    max_tokens=500
)
print(response)
ChatCompletion(
    id='chatcmpl-8JTwaPXNYCyE9F9oK5WgiQUVYLZ3t', 
    choices=[
        Choice(
            finish_reason='stop', 
            index=0, 
            message=ChatCompletionMessage(
                content='日本一高い山は富士山です。', 
                role='assistant', 
                function_call=None, 
                tool_calls=None
            )
        )
    ], 
    created=1699654028, 
    model='gpt-3.5-turbo-0613', 
    object='chat.completion', 
    system_fingerprint=None, 
    usage=CompletionUsage(
        completion_tokens=14, 
        prompt_tokens=32, 
        total_tokens=46
    )
)

3-2. Completion

"Completion" is an API for text generation from "text → text".

response = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="""ユーザー: 日本一高い山は?
アシスタント: """,
    temperature=0.2,
    max_tokens=500
)
print(response)
Completion(
    id='cmpl-8JVzXJNr3KwUFhpSqg10swBko2k03', 
    choices=[
        CompletionChoice(
            finish_reason='stop', 
            index=0, 
            logprobs=None, 
            text='それは富士山です。標高は3,776メートルです。'
        )
    ], 
    created=1699661899, 
    model='gpt-3.5-turbo-instruct', 
    object='text_completion', 
    system_fingerprint=None, 
    usage=CompletionUsage(
        completion_tokens=22, 
        prompt_tokens=21, 
        total_tokens=43
    )
)

4. Streaming

4-1. Chat Completion

stream = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {
            "role": "user",
            "content": "日本一高い山は?",
        }
    ],
    temperature=0.2,
    max_tokens=500,
    stream=True,  # ストリーミングの有効化
)

for part in stream:
    print(part.choices[0].delta.content or "")
日
本
一
高
い
山
は
富
士
山
です
。

4-2. Completion

stream = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="""ユーザー: 日本一高い山は?
アシスタント: """,
    temperature=0.2,
    max_tokens=500,
    stream=True,  # ストリーミングの有効化
)

for part in stream:
    print(part.choices[0].text or "")
それ
は
富
士
山
です
。
標
高
は
3
,
776
メ
ート
ル
です
。

5. Asynchronous Processing

5-1. Setup for Asynchronous Processing

(1) Preprocessing for Colab.
To use "asyncio" in Colab, you must enable nesting before importing.

# Colab用の前処理
import nest_asyncio
nest_asyncio.apply()

(2) Preparing AsyncOpenAI
For asynchronous processing, use "AsyncOpenAI" instead of OpenAI, and use await for each API call.

import asyncio
from openai import AsyncOpenAI

# クライアントの準備
client = AsyncOpenAI()

5-1. Chat Completion

async def main() -> None:
    response = await client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {
                "role": "user",
                "content": "日本一高い山は?",
            }
        ],
        temperature=0.2,
        max_tokens=500,        
    )
    print(response.choices[0].message.content)

asyncio.run(main())

5-3. Completion

async def main() -> None:
    response = await client.completions.create(
        model="gpt-3.5-turbo-instruct",
        prompt="""ユーザー: 日本一高い山は?
アシスタント: """,
        temperature=0.2,
        max_tokens=500,
    )
    print(response.choices[0].text)

asyncio.run(main())

Related

Next time



いいなと思ったら応援しよう!