Using ChatGPT Chat FunctionCall from Java

Using ChatGPT Chat FunctionCall from Java


Introduction

As artificial intelligence and chatbots continue to gain popularity, integrating functions into chat conversations is becoming increasingly important. Functions are small units of code that can be reused and embedded into larger programs to perform a specific task. In this blog post, we will discuss how to implement and integrate functions into ChatGPT conversations using JAI, a Java Open AI API client. This guide will cover defining a function, handling function callbacks, and mixing function results with content/context returned from the function. Also, we'll be able to provide an example of a weather-related function and its integration into a larger program using a function map.

This developer notebook guides implementing and integrating functions into a ChatGPT conversation with JAI, a Java Open AI API client. It covers everything from defining a function to handling function callbacks and mixing the results with the content/context returned. In addition, the guide includes an example implementation of a weather-related function and its integration into a larger program using a function map.

The latest models (gpt-3.5-turbo-0613 and gpt-4-0613) have been fine-tuned to detect when a function should be called based on the input and to respond with JSON that follows the function signature. However, this power also comes with great responsibility due to potential risks. Users must build some user confirmation before taking actions on behalf of users that could impact the world (such as sending an email, posting political views online, buying something, etc.).

The notebook begins by explaining how to define a function by creating a dictionary that maps function names to their respective implementations. It then covers handling function callbacks, which occur when ChatGPT calls a function and returns a "function call" object containing the function's name and its arguments. The guide also explains how to call ChatGPT again by appending the function response as a new message and returning the updated chat request to the ChatGPT model.

The model will intelligently select and output a JSON object, called a functionCall, containing names of the functions and arguments to call those functions. Note that the Chat Completions API does not call the function but instead generates JSON that the user can use to call the function in their code.

Why it is important

This is a powerful concept and allows you a way to combine up-to-date data from your business domain with ChatGPT.

Function callbacks get used a lot by ChatGPT plugins.

Function calling enables you to add more context to the output from ChatGPT and allows you to get structured data from a model from ChatGPT. This can be used to:

  • Create chatbots that answer questions by calling external APIs (e.g., ChatGPT Plugins).
  • For example, you can define functions like send_email(to: string, body: string) or get_current_weather(location: string, unit: 'celsius' | 'fahrenheit').
  • Convert natural language into function calls.
  • For example, you can convert "Who are my top followers on IG?" to get_top_IG_followers(min_revenue: int, created_before: string, limit: int) and call an internal function that could hit an internal service.
  • Extract structured data from text.
  • For example, you can define a function called extract_data(name: string, birthday: string) or sql_query(query: string).

Basic steps

The basic steps for function calls is:

  1. Call the ChatGPT chat with the user query and a set of functions defined in the functions parameter.
  2. ChatGPT can choose to call a function. ChatGPT selects this function by returning a functionCall from a message (from a choice). The functionaCall arguments is in a JSON object that you must deserialize.
  3. From the functionaCall you call a function in your code with the arguments provided, and get a function response.
  4. Call the ChatGPT again by appending the function response as a new message, and let the model summarize the results back to the user.

Code breakdown

Let’s look at some example code.

Here is the complete code listing of our example

package com.cloudurable.jai.examples;

import com.cloudurable.jai.OpenAIClient;
import com.cloudurable.jai.model.text.completion.chat.ChatRequest;
import com.cloudurable.jai.model.text.completion.chat.ChatResponse;
import com.cloudurable.jai.model.text.completion.chat.Message;
import com.cloudurable.jai.model.text.completion.chat.Role;
import com.cloudurable.jai.model.text.completion.chat.function.*;
import com.cloudurable.jai.util.JsonSerializer;
import io.nats.jparse.node.ObjectNode;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

public class WeatherFunctionCallExample {

    private final OpenAIClient client;

    /**
     * Holds the function mappings.
     */
    private final Map<String, Function<ObjectNode, String>> functionMap = new HashMap<>();

    public WeatherFunctionCallExample(OpenAIClient client) {
        this.client = client;
        functionMap.put("get_current_weather", this::getCurrentWeather);
    }

    /**
     *
     # Example dummy function hard coded to return the same weather for two cities and a default for other cities.
     # In production, this could be your backend API or an external API
     * @param objectNode arguments from chat GPT.
     * @return JSON string
     */
    public  String getCurrentWeather(final ObjectNode objectNode) {
        final String location = objectNode.getString("location");
        final String unit = Optional.ofNullable(objectNode.get("unit"))
                .map(node->node.asScalar().stringValue()).orElse("fahrenheit");
        final JsonSerializer json = new JsonSerializer();
        json.startObject();
        json.addAttribute("location", location);
        json.addAttribute("unit", unit);
        switch (location)  {
            case "Austin, TX":
                json.addAttribute("temperature", 92);
                json.endObject();
                return json.toString();
            case "Boston, MA":
                json.addAttribute("temperature", 72);
                json.endObject();
                return json.toString();
            default:
                json.addAttribute("temperature", 70);
                json.endObject();
                return json.toString();
        }
    }

    public static void main(final String... args) {
        try {
            final var client = OpenAIClient.builder().setApiKey(System.getenv("OPENAI_API_KEY")).build();
            new WeatherFunctionCallExample(client).runConversation();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void runConversation() {
        final var getCurrentWeatherFunc = getFunctionDefinition();
        final var message = Message.builder().role(Role.USER)
                .content("What's the weather like in Boston in fahrenheit?").build();
        final var chatBuilder = ChatRequest.builder()
                .model("gpt-3.5-turbo-0613")
                .addMessage(message)
                .addFunction(getCurrentWeatherFunc)
                .functionalCall(ChatRequest.AUTO);
        final var chatRequest = chatBuilder.build();
        final var chatResponse = client.chat(chatRequest);

        chatResponse.getResponse().ifPresent(
               response -> handleFunctionCallback(chatBuilder, response));

        System.out.println(chatResponse.getStatusCode().orElse(666));
        System.out.println(chatResponse.getStatusMessage().orElse(""));
        chatResponse.getException().ifPresent(e-> e.printStackTrace());
    }

    private void handleFunctionCallback(final ChatRequest.Builder chatBuilder, final ChatResponse chatResponse) {
        var responseMessage = chatResponse.getChoices().get(0).getMessage();
        var functionCall = responseMessage.getFunctionCall();

        var functionResponse = getFunctionResponse(functionCall);

        chatBuilder.addMessage(Message.builder().name(functionCall.getName()).role(Role.FUNCTION)
                    .content(functionResponse)
                    .build());

       var response = client.chat(chatBuilder.build());
       
        response.getResponse().ifPresent(chatResponse1 ->
                System.out.println(chatResponse1.getChoices().get(0).getMessage().getContent()));

        if (response.getStatusMessage().isPresent()) {
            System.out.println(response.getStatusMessage().orElse(""));
        }
    }

    private String getFunctionResponse(FunctionalCall functionCall) {
        String functionResponse = "";
        if (functionCall !=null && functionMap.containsKey(functionCall.getName())) {
            functionResponse = functionMap.get(functionCall.getName()).apply(functionCall.getArguments());
        }
        return functionResponse;
    }

    private static FunctionDef getFunctionDefinition() {
        return FunctionDef.builder().name("get_current_weather")
                .description("Get the current weather in a given location")
                .setParameters(
                    ObjectParameter.objectParamBuilder()
                        .addParameter(Parameter.builder().name("location")
                                .description("The city and state, e.g. Austin, TX").build())
                        .addParameter(
                                EnumParameter.enumBuilder()
                                        .name("unit").enumValues("celsius", "fahrenheit").build())
                            .required("location")
                        .build()
        ).build();
    }
}        

If that is unclear, let me break it down based on our defined steps. Then we will show the messages flying across the wire between the client application and Chat GPT.

1.1 Call the ChatGPT chat with the user query

public void runConversation() {

        // Call the ChatGPT chat with the user query.
        final var message = Message.builder().role(Role.USER)
                .content("What's the weather like in Boston in fahrenheit?").build();

        final var chatBuilder = ChatRequest.builder()
                .model("gpt-3.5-turbo-0613")
                .addMessage(message) // user query 
                ...
                .functionalCall(ChatRequest.AUTO);        

The runConversation() establishes a conversation with the ChatGPT model by providing a user query and configuring the chat request. Next, it prepares the conversation to handle functional calls, allowing the model to invoke specific functions as needed during the conversation.

This **runConversation()**demonstrates how to use the ChatGPT model to run a conversation and make a function call. Let's break it down step by step:

  1. The runConversation() method is the entry point for executing the conversation.
  2. The code begins by creating a user query message using the Message.builder() method. The query is set to "What's the weather like in Boston in Fahrenheit?" and is assigned the user's role.
  3. A ChatRequest builder object, chatBuilder, is created to configure the chat request to be sent to the ChatGPT model.
  4. The model for the chat is specified as "gpt-3.5-turbo-0613" using the model() method on the chatBuilder.
  5. The user query message is added to the chatBuilder using the addMessage() method. This message represents the initial user input for the conversation.
  6. Additional configuration and functionality can be added to the chatBuilder, although the snippet provided does not show these details.
  7. Finally, the functionalCall() method is called on the chatBuilder with the parameter ChatRequest.AUTO. This indicates that the ChatGPT model should automatically handle any functional calls made during the conversation.

1.2 And include the set of functions defined in the functions parameter.

private static FunctionDef getFunctionDefinition() {
        return FunctionDef.builder().name("get_current_weather")
                .description("Get the current weather in a given location")
                .setParameters(
                    ObjectParameter.objectParamBuilder()
                        .addParameter(Parameter.builder().name("location")
                                .description("The city and state, e.g. Austin, TX")
                                .build())
                        .addParameter(
                                EnumParameter.enumBuilder()
                                        .name("unit")
                                        .enumValues("celsius", "fahrenheit")
                                        .build())
                            .required("location")
                        .build()
        ).build();
}

public void runConversation() {

        ...

        // And include the set of functions defined in the functions parameter.
        final var getCurrentWeatherFunc = getFunctionDefinition();// defined above
        final var chatBuilder = ChatRequest.builder()
                .model("gpt-3.5-turbo-0613")
                .addMessage(message) 
                .addFunction(getCurrentWeatherFunc) //function definitions
                .functionalCall(ChatRequest.AUTO);        

The runConversation() method serves as an entry point for executing a conversation with the ChatGPT model.

  1. The runConversation creates a Message object representing a user query. The query is set to "What's the weather like in Boston in Fahrenheit?" and is assigned the role of the user.
  2. The code defines a function called getCurrentWeatherFunc (not shown in the provided code) and retrieves its definition.
  3. A ChatRequest builder object, chatBuilder, is created. It specifies the model to be used as "gpt-3.5-turbo-0613".
  4. The user query message is added to the chatBuilder using the addMessage() method.
  5. The getCurrentWeatherFunc function definition is added to the chatBuilder using the addFunction() method.
  6. Finally, the functionalCall() method is called on the chatBuilder with the parameter ChatRequest.AUTO. Specifying auto means you want ChatGPT to decide the best method to call based on the message.

1.2 And include the set of functions defined getFunctionDefinition breakdown

private static FunctionDef getFunctionDefinition() {
        return FunctionDef.builder().name("get_current_weather")
                .description("Get the current weather in a given location")
                .setParameters(
                    ObjectParameter.objectParamBuilder()
                        .addParameter(Parameter.builder().name("location")
                                .description("The city and state, e.g. Austin, TX")
                                .build())
                        .addParameter(
                                EnumParameter.enumBuilder()
                                        .name("unit")
                                        .enumValues("celsius", "fahrenheit")
                                        .build())
                            .required("location")
                        .build()
        ).build();
}        

The method getFunctionDefinition(), which returns a FunctionDef object. This is a function definition for use in the ChatGPT conversation when we pass the menu of functions it can call.

Here's a breakdown of what the code does:

  1. The getFunctionDefinition() method returns an instance of the FunctionDef class.
  2. The FunctionDef builder is used to construct the FunctionDef object.
  3. The name property of the function is set to "get_current_weather". This defines the function's name that can be invoked during the conversation.
  4. The description property describes the function which retrieves the current weather in a given location.
  5. The setParameters() method is called to specify the parameters for the function.
  6. Inside setParameters(), two parameters are defined:

  • The first parameter, "location", represents the city and state for which the weather is requested. It has a description explaining its purpose.
  • The second parameter is an EnumParameter named "unit". It represents the unit in which the weather should be returned (either "celsius" or "fahrenheit"). It is defined as an enumeration with two possible values.

  1. The required() method is called on the ObjectParameter to specify that the "location" parameter is required for the function.
  2. The build() method is called to finalize the construction of the FunctionDef object.

This getFunctionDefinition defines a function named "get_current_weather" that takes two parameters: "location" and "unit". It represents a function that retrieves the current weather for a given location in Celsius or Fahrenheit. This function can be used in the ChatGPT conversation to provide weather information based on user queries.

2.1 ChatGPT can choose to call a function.

public void runConversation() {

        final var chatRequest = ...
        final var chatResponse = client.chat(chatRequest);

        chatResponse.getResponse().ifPresent(
                 response -> handleFunctionCallback(chatBuilder, response));
...
}

private void handleFunctionCallback(final ChatRequest.Builder chatBuilder, final ChatResponse chatResponse) {
        var responseMessage = chatResponse.getChoices().get(0).getMessage();
        var functionCall = responseMessage.getFunctionCall();

        var functionResponse = getFunctionResponse(functionCall);
....        

The above code demonstrates how to handle a function callback within the runConversation() method. Let's break it down:

  1. final var chatRequest = ...: This line represents the creation of the chatRequest object, which contains the necessary configuration for the chat request to be sent to the ChatGPT model, including the function definition that we defined earlier.
  2. final var chatResponse = client.chat(chatRequest): This line sends the chatRequest to the ChatGPT model using the chat() method of the client. It returns a chatResponse object that contains the response generated by the model.
  3. chatResponse.getResponse().ifPresent(response -> handleFunctionCallback(chatBuilder, response)): This line checks if the chatResponse contains a response by using the getResponse() method. If a response is present, it invokes the handleFunctionCallback() method and passes the chatBuilder and response as arguments. This is done to handle any function callbacks received from the ChatGPT model during the conversation.
  4. handleFunctionCallback(final ChatRequest.Builder chatBuilder, final ChatResponse chatResponse): This method receives the chatBuilder and chatResponse as parameters. It handles the function callback received from the ChatGPT model.
  5. var responseMessage = chatResponse.getChoices().get(0).getMessage(): This line retrieves the first message choice from the chatResponse. It assumes that the model provides multiple response choices and focuses on the first one.
  6. var functionCall = responseMessage.getFunctionCall(): This line extracts the FunctionCall object from the responseMessage. It represents a function call made by the ChatGPT model during the conversation.
  7. var functionResponse = getFunctionResponse(functionCall): This line invokes the getFunctionResponse() method, passing the functionCall object as an argument, to retrieve the response generated by the function call.

This code demonstrates how to handle function callbacks within the runConversation() method. It sends a chat request to the ChatGPT model, receives the response, and if a response is present, it invokes the handleFunctionCallback() method to handle the function callback. The function callback is extracted from the response message, and the getFunctionResponse() method is called to retrieve the function's response.

2.2 ChatGPT selects this function by returning a functionCall from a message


private final Map<String, Function<ObjectNode, String>> functionMap = 
                                                           new HashMap<>();

/**
 *
 * Example dummy function hard coded to return the same weather for two cities and a default for other cities.
 * In production, this could be your backend API or an external API
 * @param objectNode arguments from chat GPT.
 * @return JSON string
 */
public  String getCurrentWeather(final ObjectNode objectNode) {
        final String location = objectNode.getString("location");
        final String unit = Optional.ofNullable(objectNode.get("unit"))
                .map(node->node.asScalar().stringValue()).orElse("fahrenheit");
        final JsonSerializer json = new JsonSerializer();
        json.startObject();
        json.addAttribute("location", location);
        json.addAttribute("unit", unit);
        switch (location)  {
            case "Austin, TX":
                json.addAttribute("temperature", 92);
                json.endObject();
                return json.toString();
            case "Boston, MA":
                json.addAttribute("temperature", 72);
                json.endObject();
                return json.toString();
            default:
                json.addAttribute("temperature", 70);
                json.endObject();
                return json.toString();
        }
}

public WeatherFunctionCallExample(OpenAIClient client) {
        this.client = client;
        functionMap.put("get_current_weather", this::getCurrentWeather); //register
}

private String getFunctionResponse(FunctionalCall functionCall) {
        String functionResponse = "";
        if (functionCall !=null && functionMap.containsKey(functionCall.getName())) {
            functionResponse = functionMap
									.get(functionCall.getName())
									.apply(functionCall.getArguments());
        }
        return functionResponse;
}        

The provided code snippet is an example of a weather-related function implementation within a larger ChatGPT conversation. Let's break it down:

  1. functionMap = new HashMap<>(): This line declares and initializes a Map named functionMap that maps function names (as strings) to functions that accept an ObjectNode parameter and return a String. This map is used to associate function names with their respective implementations. The ObjectNode is the arguments to the function call that ChatGPT decided to call.
  2. getCurrentWeather(final ObjectNode objectNode): This method represents implementing the get_current_weather function. It takes an ObjectNode as input, which contains arguments passed from the ChatGPT model. It retrieves the location and unit values from the ObjectNode and generates a JSON response based on the specified location. The temperature values for "Austin, TX", "Boston, MA", and the default location are hard coded. The JSON response is constructed using the JsonSerializer class, and the function returns the generated JSON as a string.
  3. WeatherFunctionCallExample(OpenAIClient client): This is a constructor for the WeatherFunctionCallExample class, which takes an OpenAIClient object as a parameter. It initializes the client field with the provided OpenAIClient and registers the getCurrentWeather function by adding it to the functionMap with the key "get_current_weather".
  4. getFunctionResponse(FunctionalCall functionCall): This method retrieves the function response based on a FunctionalCall object. It first checks if the functionCall object is not null and if the functionMap contains the function name specified in functionCall. If both conditions are met, it invokes the associated function from functionMap using the function name as the key and passes the function call arguments to it. The result is then returned as the function response. This is how we map the function ChatGPT requested to call and the results to a message for our next chat.

Overall, this code demonstrates an example implementation of a weather-related function and its integration into a larger program using a functionMap to associate function names with their respective implementations for easy access to a ChatGPT conversation.

3 Call the ChatGPT again by appending the function response as a new message, and let the model summarize the results to the user.

private void handleFunctionCallback(final ChatRequest.Builder chatBuilder, 
                                    final ChatResponse chatResponse) {
        var responseMessage = chatResponse.getChoices().get(0).getMessage();
        var functionCall = responseMessage.getFunctionCall();

        var functionResponse = getFunctionResponse(functionCall);

        chatBuilder.addMessage(Message.builder().name(functionCall.getName()).role(Role.FUNCTION)
                    .content(functionResponse)
                    .build());

       var response = client.chat(chatBuilder.build());

        response.getResponse().ifPresent(chatResponse1 ->
                System.out.println(chatResponse1.getChoices().get(0).getMessage().getContent()));

       
}        

The handleFunctionCallback handles the function callback response received from the ChatGPT model. It retrieves the function call from the response, invokes the appropriate function to generate a response, adds the function response as a message in the chatBuilder sends the updated chatBuilder back to the model, and prints the subsequent response generated by the model to the console.Here's a breakdown of what the code does:

  1. var responseMessage = chatResponse.getChoices().get(0).getMessage(): This line retrieves the first message from the choices available in the chatResponse object. The chatResponse likely contains multiple possible responses generated by the ChatGPT model, and here we are focusing on the first one.
  2. var functionCall = responseMessage.getFunctionCall(): This line retrieves the FunctionCall object from the responseMessage. The FunctionCall represents a function call made by the ChatGPT model during the conversation.
  3. var functionResponse = getFunctionResponse(functionCall): This line invokes the getFunctionResponse() method, passing the functionCall object as an argument, to retrieve the response generated by the function call.
  4. chatBuilder.addMessage(Message.builder().name(functionCall.getName()).role(Role.FUNCTION).content(functionResponse).build()): This line creates a new Message object representing the response from the function call. It sets the name of the message to the name of the function, assigns the role as Role.FUNCTION, and sets the content of the message as the functionResponse. The message is then added to the chatBuilder. This enables ChatGPT model to mix in the response from our message.
  5. var response = client.chat(chatBuilder.build()): This line sends the updated chatBuilder with the function response message to the ChatGPT model by invoking the chat() method of the client. It likely initiates the next iteration of the conversation, including the function response.
  6. response.getResponse().ifPresent(chatResponse1 -> System.out.println(chatResponse1.getChoices().get(0).getMessage().getContent())): This line retrieves the response generated by the ChatGPT model in the response object. It then extracts the first message choice from the response and prints its content to the console.

JSON TO/FRO

Let's look at the messages going to and fro ChatGPT to grok this example fully.

Call the ChatGPT chat with the user query and a set of functions defined in the function's parameter.

Here is the initial message that we send to Chat GPT:

{
   "model":"gpt-3.5-turbo-0613",
   "messages":[
      {
         "role":"user",
         "content":"What's the weather like in Boston in fahrenheit?"
      }
   ],
   "function_call":"auto",
   "functions":[
      {
         "name":"get_current_weather",
         "parameters":{
            "type":"object",
            "properties":{
               "location":{
                  "type":"string"
               },
               "unit":{
                  "type":"string",
                  "enum":[
                     "celsius",
                     "fahrenheit"
                  ]
               }
            },
            "required":[
               "location"
            ]
         }
      }
   ]
}        

  1. ChatGPT can choose to call a function. ChatGPT selects this function by returning a functionCall from a message (from a choice). The functionaCall arguments is in a JSON object that you must deserialize.

Here is the response form ChatGPT

{
  "id": "chatcmpl-7aWQG4KfBi7zLXkQXkGv0lp7YHGuu",
  "object": "chat.completion",
  "created": 1688938796,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "function_call": {
          "name": "get_current_weather",
          "arguments": "{\\n  \\"location\\": \\"Boston\\",\\n  \\"unit\\": \\"fahrenheit\\"\\n}"
        }
      },
      "finish_reason": "function_call"
    }
  ],
  "usage": {
    "prompt_tokens": 61,
    "completion_tokens": 24,
    "total_tokens": 85
  }
}        

Notice that ChatGPT returned message that has a function_call defined. This means it wants you to invoke your function called get_current_weather.

From the functionaCall you call a function in your code with the arguments provided, and get a function response.

Call the ChatGPT again by appending the function response as a new message, and let the model summarize the results back to the user.

Here is the original ask with the function response mixed in.

{
   "model":"gpt-3.5-turbo-0613",
   "messages":[
      {
         "role":"user",
         "content":"What's the weather like in Boston in fahrenheit?"
      },
      {
         "role":"function",
         "content":"{\\"location\\":\\"Boston\\",\\"unit\\":\\"fahrenheit\\",\\"temperature\\":70}",
         "name":"get_current_weather"
      }
   ]
...
}        

Then lastly, ChatGPT mixes the results with the content/context returned from your weather function.

The final response from ChatGPT mixes our get_current_weather response.

{
  "id": "chatcmpl-7aWQIWMzyaPDNtxJtmBQ43PYfEueh",
  "object": "chat.completion",
  "created": 1688938798,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The weather in Boston is currently 70 degrees Fahrenheit."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 81,
    "completion_tokens": 12,
    "total_tokens": 93
  }
}         

This developer notebook provides a comprehensive guide on implementing and integrating functions into a ChatGPT conversation with JAI. The first step is defining a function. This can be done by creating a dictionary that maps function names to their respective implementation. The implementation can be a simple function with arguments or a more complex function, such as one that makes API calls to retrieve data.

Next, we covered handling function callbacks. When ChatGPT calls a function, it returns a "function call" object containing the function's name and its arguments. The function call is extracted from the response message, and the appropriate function is invoked with the provided arguments. The function then generates a response, which is sent back to ChatGPT.

Once the function generates the response, we explain how to call the ChatGPT again by appending the function response as a new message. This allows ChatGPT to mix the results with the content/context returned from the function. The updated chat request is then sent back to the ChatGPT model, and the process continues until a final response is generated.

We also included an example implementation of a weather-related function and its integration into a larger program using a function map. The function map associates function names with their respective implementations for easy access in a ChatGPT conversation.

Overall, this developer notebook explains how to implement and integrate functions into a ChatGPT conversation, from defining a function to handling function callbacks and mixing the results with the content/context returned.

As artificial intelligence and chatbots continue to gain popularity, integrating functions into chat conversations is becoming increasingly important. Functions are essentially small units of code that can be reused and embedded into larger programs to perform a specific task. In this blog post, we will discuss how to implement and integrate functions into ChatGPT conversations using JAI, a Java Open AI API client. This guide will define a function, handle function callbacks, and mix function results with content/context returned from the function. Also, we'll be able to provide an example of a weather-related function and its integration into a larger program using a function map.

Related content:



Svetozar Radoj?in

Java Solutions Architect/Computer Vision Developer at Energosoft ITSS

10 个月

Nice article! Just to ask, not related to the tool you presented here, but a question of principle... I understand that, to let the model know it can call functions, we need to add a list of them to the payload (ie, JSON containing 'functions' array (with name, parameters etc...) Now, I guess that the model will decide to call function (ie, return the 'choice' with the 'function_call' property etc...) ONLY if the model cannot provide an answer in another way, that is, if a question is asked that is not in his knowledge base. I am right? if so, now let's imagine a realistic scenario, an application where an end user sends requests to GPT. Since we can't know what the user will ask, does this mean that we always have to send a list of functions that the model can decide to use? Thanks in advace,...

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了