#Function Calling with Javascript

2 messages · Page 1 of 1 (latest)

urban swan
#

Code:


const configuration = new Configuration({
  apiKey: 'sk-XYZ',
});

const openai = new OpenAIApi(configuration);

//Method to getStaffInfo
const getStaffInfo = (staffPosition) => {
  switch (staffPosition) {
    case 'author':
      return {
        name: 'Rebecca',
        email: 'rebecca@company.com'
      };
    case 'owner':
      return {
        name: 'Josh',
        email: 'josh@company.com'
      };
     default:
      return {
        name: 'No name found',
        email: 'Not found'
      };
  }
}

const systemPrompt = `You are a helpful directory assistant. 
  Users will ask questions about staff and you will reply in a friendly tone
  with the name and email of the person.`;


const chat = openai.createChatCompletion({
  model: 'gpt-4-0613',
  // temperature: 0.2, // setting this lower to reduce hallucinations
  messages: [{
      "role": 'user', "content": "I want the author's information"
    },],
    
  functions: [
    {
      "name": "getStaffInfo", //Function name
      "description": "Get the contact info of a staff member", //What the function does
      "parameters": {
        "type": "object", //String, Int, etc.
        "properties": { //Parameters
          "staffPosition": {
            "type": "string",
            "description": 'The position of the desired staff member. E.g. "author" or "owner"',
          },
        },
        "required": ["staffPosition"],
      },
    },
  ],
  function_call: 'auto',})

console.log(chat.data.choices[0].message);```

Error:

```console.log(chat.data.choices[0].message);
                      ^

TypeError: Cannot read properties of undefined (reading 'choices')```

Any idea what is missing or what is causing this?
craggy vale
#

It's telling you why. Print the chat.data and you will see the error. It's not finding the key because it's sending you a string error reply