Lambdua / openai4j

Java client library for OpenAI API.Full support for all OpenAI API models including Completions, Chat, Edits, Embeddings, Audio, Files, Assistants-v2, Images, Moderations, Batch, and Fine-tuning.
MIT License
322 stars 27 forks source link

反序列化ChatCompletionRequest对象失败 #33

Closed egan-mw closed 5 months ago

egan-mw commented 5 months ago

我在尝试将接收到的JSON请求体反序列化为ChatCompletionRequest对象时遇到了问题。尽管我发送的请求数据格式看起来是正确的,但是反序列化过程没有成功。

请求示例 { "topP": 1, "temperature": 1, "maxTokens": 2500, "messages": [ { "role": "system", "content": "你好" }, { "role": "user", "content": [ { "type": "text", "text": "识别这张图片" }, { "type": "image_url", "imageUrl": { "url": "https://img.baigonglian.com/open_platform/generate/1ad71539c74715cfceb1c6b2634e9bf1.png" } } ] } ], "model": "gpt-4o", "frequencyPenalty": 0.6299999952316284 }

Lambdua commented 5 months ago

openai-api返回的数据对象不是驼峰命名的. 因该返回类似下面的结构:

{
  "model": "gpt-4-turbo",
  "messages": [
    {
      "role": "system",
      "content": "Hello! I'm a weather bot. I can help you with the weather. Just ask me for the current weather in a specific location."
    },
    {
      "role": "assistant",
      "content": "这是一个assistant的回复模拟"
    },
    {
      "role": "user",
      "content": "文本内容测试"
    },
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "What's in this image?"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
          }
        }
      ]
    },
    {
      "role": "tool",
      "tool_call_id": "123",
      "content": "这是一个tool的回复模拟"
    },
    {
      "role": "function",
      "content": "这是一个system的回复模拟",
      "name": "get_current_weather"
    }
  ],
  "max_tokens": 300,
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
              "type": "string",
              "enum": [
                "celsius",
                "fahrenheit"
              ]
            }
          },
          "required": [
            "location"
          ]
        }
      }
    }
  ],
  "tool_choice": "auto",
  "functions": [
    {
      "name": "get_current_weather",
      "description": "Get the current weather in a given location",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA"
          },
          "unit": {
            "type": "string",
            "enum": [
              "celsius",
              "fahrenheit"
            ]
          }
        },
        "required": [
          "location"
        ]
      }
    }
  ],
  "function_call": {
    "name": "get_current_weather"
  },
  "logprobs": true,
  "top_logprobs": 2,
  "stop": [
    "test",
    "other"
  ],
  "stream_options": {
    "include_usage": true
  }
}
egan-mw commented 5 months ago

openai-api返回的数据对象不是驼峰命名的. 因该返回类似下面的结构:

{
  "model": "gpt-4-turbo",
  "messages": [
    {
      "role": "system",
      "content": "Hello! I'm a weather bot. I can help you with the weather. Just ask me for the current weather in a specific location."
    },
    {
      "role": "assistant",
      "content": "这是一个assistant的回复模拟"
    },
    {
      "role": "user",
      "content": "文本内容测试"
    },
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "What's in this image?"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
          }
        }
      ]
    },
    {
      "role": "tool",
      "tool_call_id": "123",
      "content": "这是一个tool的回复模拟"
    },
    {
      "role": "function",
      "content": "这是一个system的回复模拟",
      "name": "get_current_weather"
    }
  ],
  "max_tokens": 300,
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
              "type": "string",
              "enum": [
                "celsius",
                "fahrenheit"
              ]
            }
          },
          "required": [
            "location"
          ]
        }
      }
    }
  ],
  "tool_choice": "auto",
  "functions": [
    {
      "name": "get_current_weather",
      "description": "Get the current weather in a given location",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA"
          },
          "unit": {
            "type": "string",
            "enum": [
              "celsius",
              "fahrenheit"
            ]
          }
        },
        "required": [
          "location"
        ]
      }
    }
  ],
  "function_call": {
    "name": "get_current_weather"
  },
  "logprobs": true,
  "top_logprobs": 2,
  "stop": [
    "test",
    "other"
  ],
  "stream_options": {
    "include_usage": true
  }
}

我是在接口中反序列化ChatCompletionRequest类型的参数不成功 @PostMapping("/chat-dialog") public ChatCompletionChoice chatCompletions(@requestbody ChatCompletionRequest chatCompletionRequest) { return customerService.chat(chatCompletionRequest); }

Lambdua commented 5 months ago

你应该按照snake_case的参数命名风格来设置你的参数

Mr-LiuDC commented 3 months ago

这个问题似乎还是存在 #52