test03.py 1.65 KB
from openai import AzureOpenAI

# gets the API Key from environment variable AZURE_OPENAI_API_KEY
client = AzureOpenAI(
    # https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning
    api_version="2023-07-01-preview",
    # https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource
    azure_endpoint="https://glsk-openai-ce.openai.azure.com",
    api_key="8ba61036fe7847c7be4ebaaf58b85275",
)

completion = client.chat.completions.create(
    # model="gpt-35-turbo-1106",  # e.g. gpt-35-instant
    model="gpt-4-1106",  # e.g. gpt-35-instant
    # messages=[
    #     {
    #         "role": "user",
    #         "content": "帮我生成2条续保营销文案,每条不超过12个字,输出json格式",
    #     },
    # ],
    messages=[
        {"role": "system", "content": "你是一个能干的助手."},
        {"role": "user", "content": "谁赢得了2020年的世界职业棒球大赛?"},
        {"role": "assistant", "content": "洛杉矶道奇队在2020年赢得了世界职业棒球大赛冠军."},
        {"role": "user", "content": "它在哪里举办的?"}
    ],
    stream=True,  # 流式输出
)

# 普通输出
# print(completion.usage)
# print(completion.choices[0].message.content)
# print(completion.model_dump_json(indent=2))

for chunk in completion:
    # print(chunk)
    # continue
    if chunk.choices:
        choice = chunk.choices[0]
        if choice.finish_reason:
            # print(f"choice.finish reason: {choice.finish_reason}")
            break
        else:
            # yield choice.delta.content
            print(choice.delta.content)