OpenAI Function Call using Langchain returns inconsistent response

229 views Asked by At

I have a question for folks who have already tried out OpenAI function calling with Langchain. I am not getting a consistent response from Langchain.

Sometimes I get the below response, where the value is present in additional_kwargs.function_call attribute.

AIMessage {
  lc_serializable: true,
  lc_kwargs: {
    content: '',
    additional_kwargs: { function_call: [Object], tool_calls: undefined }
  },
  lc_namespace: [ 'langchain', 'schema' ],
  content: '',
  name: undefined,
  additional_kwargs: {
    function_call: {
      name: 'orderPizza',
      arguments: '{\n  "pizzaSize": "NA",\n  "pizzaType": "NA"\n}'
    },
    tool_calls: undefined
  }
}

And sometimes I get another response shared below, where additional_kwargs.function_call value is undefined.

AIMessage {
  lc_serializable: true,
  lc_kwargs: {
    content: '{\n  "function": "functions.welcomeUser",\n  "arguments": {}\n}',
    additional_kwargs: { function_call: undefined, tool_calls: undefined }
  },
  lc_namespace: [ 'langchain', 'schema' ],
  content: '{\n  "function": "functions.welcomeUser",\n  "arguments": {}\n}',
  name: undefined,
  additional_kwargs: { function_call: undefined, tool_calls: undefined }
}

The user message that generated the first response is I want to order pizza and the user message that generated the second response is Hey.

In both cases, it looks like OpenAI can identify the correct function to call. For the first case, the function to be called is orderPizza and for the second case the function to be called is welcomeUser. The only issue here is that the response is not consistent. In the second response, the function name is present in the JSON body of the content attribute instead of additional_kwargs.function_call.

Following are my function schemas:

const greetUserSchema = {
    name: "welcomeUser",
    description:
      "This function is used for greeting users. Acknowledging the user with a welcome message.",
    parameters: {},
};


const pizzaOrderFunctionSchema = {
    name: "orderPizza",
    description:
      "This function is called to accept the pizza orders. If the pizza size or the pizza type is not mentioned then pass the value as NA in the parameters.",
    parameters: {
      type: "object",
      properties: {
        pizzaSize: {
          type: "string",
          description: "The size of the pizza",
          enum: ["NA", "small", "medium", "large"],
        },
        pizzaType: {
          type: "string",
          description: "The type of pizza",
          enum: ["NA", "cheese", "pepperoni", "veggie"],
        },
      },
      required: ["pizzaSize", "pizzaType"],
    },
};

I wanted to know if this is something expected or if there is any bug in my code because of which I am getting inconsistent results. TIA

0

There are 0 answers