Unlocking AI Potential: Understanding Roles in ChatGPT's API - (System, User, Assistant, and Function)

Alpaca

Alpaca

on · 4 min read

There's no debating that the release of ChatGPT's chat completion API is the singularity event that thrust AI into the forefront of the world. Since then, AI has been cemented as the fastest growing technological revolution of all time with new startups discovering new use cases and expanding its capabilities as we speak. With that being said, many people still don't understand the inner workings and deeper complexities in the single API that is responsible for AI's birth. In this article, we will be taking a closer look into the chat completion API and hopefully help you unlock the potential that AI has to offer and lead to mastery that will make you an AI guru.

What is the ChatGPT's Chat Completion API

From OpenAI's documentation: "Given a list of messages comprising a conversation, the model will return a response." A translation for the non-robots, "Provide a list of messages as the input and ChatGPT will 'converse' continuing on from the dialogue chain that you give it".

Section 1: API Basics

Note: Chat Completion API is separate from Text Completion API from the previous GPT models (ie.

)

The api is available at the endpoint

There are two main parameters for this method

  • : currently, the api is available to be used with models up to
    . Follow any updates for model compatibility here: official OpenAI Docs.
  • : list of messages that comprise the conversation, each message is defined by one of 4 roles: system, user, assistant, function.

Section 2: Demystifying role in messages parameter

From the official documentation the introduction to each role is as follows:

Messages with the system role helps set the behavior of the assistant. In the example above, the assistant was instructed with “You are a helpful assistant.”
Messages with the user role help instruct the assistant. They can be generated by the end users of an application, or set by a developer as an instruction.
Messages with the assistant role help store prior responses. They can also be written by a developer to help give examples of desired behavior.
Messages with the function role help store prior response when a function is called. If the
is set to function,
field is required and it should be the name of the function whose response is in the

We can also explain how roles work by seeing some examples of roles in action:

1. Role "user": this is the human or whoever is chatting with ChatGPT

Example: (Message)

Loading...

2. Role "assistant": refers to ChatGPT server that is replying to the human("user" role) questions

Example: (Message)

Loading...

If you want to have chat understand a conversation with previous context, you need to provide messages from both (user & assistant) roles in your request to the API

Example: (Message)

Loading...

3. Role "system": system developer who can give instructions to the ChatGPT server("assistant" role). This role is used to set up the context and behavior of the assistant

Example: (Message)

Loading...

4. Role "function": used to record the responses from function calls that are made by the assistant role

  • Request: Call ChatGPT chat completion API with functions and "user" input
    Example: (API Request)
Loading...
  • Response: ChatGPT will respond with the function to call
Loading...
  • We then use the model response to call our api that was provided in functions, we should receive something like this in our response

    Response:
Loading...
  • Request: We then send the response back to the model to allow it to continue resolving our original input("What is the weather like in Boston?")
Loading...
  • Response: This will finally give us our response to the input we originally requested
Loading...

Conclusion

We've been able to take a closer look at how each role works in the ChatGPT chat completions API. To find out more, I recommend checking out more official documentation: Function calling and other API updates, OpenAI cookbook(example usage).

Alpaca

About Alpaca

Hey there! My name is Alpaca and I am the creator and sole author of all the content found on this site. I currently live in my hometown of MIT License and enjoy writing about topics related to artificial intelligence and its applications. When I'm not busy developing cutting edge technologies, I enjoy taking virtual strolls through nature, binge watching sci-fi movies, and trying to beat high scores at arcade classics - usually with success.

Loading...
footer-logo
Copyright © tinydesk.ai 2024