Stevenic / alphawave

AlphaWave is a very opinionated client for interfacing with Large Language Models.
MIT License
98 stars 9 forks source link

Calling OpenAI API with function setup fails with "400 Bad request" #17

Open iva2k opened 1 week ago

iva2k commented 1 week ago

Calling OpenAI API with functions setup (following example 04) fails with "400 Bad request"

iva2k commented 1 week ago

the POST to openAI backend looks like this:

const url = 'https://api.openai.com/v1/chat/completions';
const requestCfg = {
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': 5558,
    'User-Agent': 'AlphaWave',
    Authorization: 'Bearer <my-api-token>'
  }
};
const request = {
  messages: [
    {
      role: 'user',
      content: '<my_content>'
    }
  ],
  max_tokens: 1000,
  temperature: 0.11,
  functions: [
    {
      name: 'custom_schema',
      description: 'Use custom JSON schema for output.',
      parameters: {
        type: 'object',
        properties: {
          company: {
            type: 'string'
          },
          address: {
            type: 'string'
          },
        required: [
          'company',
          'address',
        ]
      }
    }
  ],
  model: 'gpt-3.5-turbo'
};

Per the OpenAI docs https://platform.openai.com/docs/guides/function-calling, the function calling should have these props in the request (copied from example in the doc):

  const 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"],
        },
      },
    },
  ];

  const response = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: messages,
    tools: tools,
    tool_choice: "auto", // auto is default, but we'll be explicit
  });

Analyzing the difference, request.messages prop is ok, but .functions prop is wrong, should be nested under .tools prop in a slightly different layout. Also there is a .tool_choice prop that would be needed, as I'd like to set it to "required".

To summarize, I see a disconnect between AlphaWave request and OpenAI docs, that .functions prop should be transformed into a similar .tools prop.