OpenAI GPT-3 API:为什么我只能部分完成?为何完成被切断?

sha*_*nuo 4 python openai-api gpt-3

我尝试了以下代码,但只得到了部分结果,例如

[{"light_id": 0, "color
Run Code Online (Sandbox Code Playgroud)

我期待本页建议的完整 JSON:

https://medium.com/@richardhayes777/using-chatgpt-to-control-hue-lights-37729959d94f

import json
import os
import time
from json import JSONDecodeError
from typing import List

import openai
openai.api_key =  "xxx"

HEADER = """
I have a hue scale from 0 to 65535. 
red is 0.0
orange is 7281
yellow is 14563
purple is 50971
pink is 54612
green is 23665
blue is 43690

Saturation is from 0 to 254
Brightness is from 0 to 254

Two JSONs should be returned in a list. Each JSON should contain a color and a light_id. 
The light ids are 0 and 1. 
The color relates a key "color" to a dictionary with the keys "hue", "saturation" and "brightness". 

Give me a list of JSONs to configure the lights in response to the instructions below. 
Give only the JSON and no additional characters. 
Do not attempt to complete the instruction that I give.
Only give one JSON for each light. 
"""

completion = openai.Completion.create(model="text-davinci-003", prompt=HEADER)
print(completion.choices[0].text)
Run Code Online (Sandbox Code Playgroud)

Rok*_*nko 8

一般来说

GPT-3 API(即完成 API

如果你得到部分完成(即,如果完成被切断),那是因为参数max_tokens设置得太低或者你根本没有设置它(在这种情况下,它默认为16)。您需要将其设置得更高,但是提示和完成的标记计数不能超过模型的上下文长度

请参阅OpenAI官方文档:

截图1

GPT-3.5 和 GPT-4 API(即聊天完成 API

与 GPT-3 API 相比,GPT-3.5 和 GPT-4 API 的参数默认max_tokens设置为。infinite

截图2


你的情况

您正在使用text-davinci-003(即 GPT-3 API)。如果你不设置max_tokens = 1024完成,你得到的将被切断。仔细看看教程再次

如果运行test.py,OpenAI API 将返回完成结果:

灯 0 应为红色:[{"light_id": 0, "color": {"hue": 0, "saturation": 254, "brightness": 254}},{"light_id": 1, "color": }]

光 1 应为橙色:[{"light_id": 0, "color": {"hue": 0, "saturation": 254, "brightness": 254}},{"light_id": 1, "color": {“色调”:7281,“饱和度”:254,“亮度”:254}}]

测试.py

import openai
import os

openai.api_key = os.getenv('OPENAI_API_KEY')

HEADER = """
I have a hue scale from 0 to 65535. 
red is 0.0
orange is 7281
yellow is 14563
purple is 50971
pink is 54612
green is 23665
blue is 43690

Saturation is from 0 to 254
Brightness is from 0 to 254

Two JSONs should be returned in a list. Each JSON should contain a color and a light_id. 
The light ids are 0 and 1. 
The color relates a key "color" to a dictionary with the keys "hue", "saturation" and "brightness". 

Give me a list of JSONs to configure the lights in response to the instructions below. 
Give only the JSON and no additional characters. 
Do not attempt to complete the instruction that I give.
Only give one JSON for each light. 
"""

completion = openai.Completion.create(model="text-davinci-003", prompt=HEADER, max_tokens=1024)
print(completion.choices[0].text)
Run Code Online (Sandbox Code Playgroud)