GPT 3.5 turbo正在削减响应,而不会达到令牌限制

r7xajy2e  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(3002)

我尝试使用openAI API构建一个应用程序,当使用openAI API时,我在post请求中传递两条消息。第一条是作为系统角色,其中包含一系列练习,例如

const initialPrompt = '''
                    0001    3/4 Sit-up
                    0003    Air bike
                    0024    Barbell Bench Front Squat
                    0025    Barbell Bench Press
                    0026    Barbell Bench Squat
                    0027    Barbell Bent Over Row
                    0030    Barbell Close-Grip Bench Press
                    ''';

字符串
第二个消息是作为一个用户:

const userMessageString = """
                Give 3 days workout routine based on these exercises.
                Keep the ID of the exercises on the answer, 
                give me the answer in a complete json format as [days{[exercises{exerciseId, exerciseName, sets, reps}]}]
                """;


我想用这个方法得到一个JSON响应:

Future<String> sendOpenAIRequest() async {
final apiUrl = 'https://api.openai.com/v1/chat/completions';

final response = await http.post(
  Uri.parse(apiUrl),
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer $openaiApiKey',
  },
  body: json.encode({
    "model": "gpt-3.5-turbo",
    "messages": [
      {"role": "system", "content": initialPrompt},
      {"role": "user", "content": userMessageString}
    ],
    "max_tokens": 3000,
  }),
);

if (response.statusCode == 200) {
  final result = json.decode(response.body);
  print('usage >>> ${result['usage']}');
  return result['choices'][0]['message']['content'];
} else {
  throw Exception('Request failed with status: ${response.statusCode}');
}


然而,我得到的答案是cut,即使令牌的数量远远低于max_tokens

usage >>> {prompt_tokens: 143, completion_tokens: 324, total_tokens: 467}
{
  "days": [
    {
      "exercises": [
        {
          "exerciseId": "0026",
          "exerciseName": "Barbell Bench Squat",
          "sets": 3,
          "reps": 12
        },
        {
          "exerciseId": "0030",
          "exerciseName": "Barbell Close-Grip Bench Press",
          "sets": 3,
          "reps": 12
        }
      ]
    },
    {
      "exercises": [
        {
          "exerciseId": "0027",
          "exerciseName": "Barbell Bent Over Row",
          "sets": 3,
          "reps": 10
        },
        {
          "exerciseId": "0024",
          "exerciseName": "Barbell Bench Front Squat",
          "sets": 3,
          "reps": 10
        }
      ]
    },
    {
      "exercises": [
        {
          "exerciseId": "0025",
          "exerciseName": "Barbell Bench Press",
          "sets": 4,
          "reps": 8
        },
        {
          "exerciseId": "0003",
          "exerciseName": "Air bike",
          "sets": 4,
          "reps": 12
        },
        {
          "exercise

pcww981p

pcww981p1#

我在playground上尝试了你的提示,似乎很好。
它总是被切断还是只是有时?如果是这样,它可能是一个开放的一面不稳定。
API有时会失败,您的应用需要检查JSON是否无效,并在需要时重试。
你试过删除max_tokens键吗?除非你想强制简短的答案,否则它并不真正需要,因为对于gpt-3.5-turbo来说,它已经受到限制。

相关问题