DEV Community

Cover image for OPENAI Function Calling API - A better way to use the power of Generative AI
Mudassir Ali
Mudassir Ali

Posted on

OPENAI Function Calling API - A better way to use the power of Generative AI

A few days back, I stumbled across the new Feature launched by OPENAI and it opens new doors for a developer like me and it can help business to minimize the customer need to look for each feature in the app and use it.
For example,
I am Fabric.js(it is a canvas library) Developer and I wanted to build a customize prompt for users where they can enter their query and based on that query I can perform functions on Canvas and this way users can have the option to customize their design templates very easily.

Practical Use Case:
I have an object which has many key value pairs and I want to get the key and value from users by providing an interactive prompt so if user types "Change the text to Hello World" then I can make a request like that and get results accordingly in a JSON formatted way.

curl https://api.openai.com/v1/chat/completions -u :$OPENAI_KEY -H 'Content-Type: application/json' -d '{
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {"role": "user", "content": "Change the text to Hello World"}
  ],
  "functions": [
    {
      "name": "change_value",
      "description": "Update value of object key",
      "parameters": {
        "type": "object",
        "properties": {

"key": {
            "type": "string",
            "description": "Key that needs to be changed"
          },
"value": {
            "type": "string",
            "description": "value that needs to be updated"
          }
        },
        "required": ["key","value"]
      }
    }
  ]
}'
Enter fullscreen mode Exit fullscreen mode

This is a curl command making a POST request to OpenAI's GPT-3.5-turbo model. It will return this JSON response

{
  "key": "text",
  "value": "Hello World"
}
Enter fullscreen mode Exit fullscreen mode

You can play around with it here.
http://langtale.ai/playground/p/fD10_ciAcM

If you enjoyed the article and have questions, make sure to:
🔔 Connect with me on mudassirali.com

Top comments (0)