Chat using GenAI

This lesson describes how to use GenAI within Trill Workbench to receive a chat response

Code: /lessons/Gen_Ai

In this example, the function accepts messages in the below format, adds the answer for the last message, and returns the response. Sample input parameter -

{
 "messages" : [
   {
     "author": "user",
     "content": "What is the capital of USA?"
   }
 ]
}.

This function has another method to “summarizeText”.

Steps(chat method)

  1. Create a new function called GenAi.java.

  2. Implement its “chat” method as shown in the example code.

  3. Set parameters in its function details file. (server.json should be pointing to GenAi.chat.json).

  4. Execute code. Verify response. Return the Result object (if there is a failure then return an error message). Else, extract “messages” from the data.

  5. Sample response for above input is shown below.

 {
    "messages" : [
      {
        "author": "user",
        "content": "What is the capital of USA?"
      },
      {
        "content": " The capital of the United States of America is Washington, D.C.",
        "author": "WB"
      }
    ]
  }

Steps(SummarizeText Method)

  1. In the same file implement a method, “summarizeText”. It simply uses Trillo Workbench API, GCPGenApi.summarizeText and returns its response.

  2. Set parameters in its function details file. (server.json should be pointing to GenAi.summarizeText.json).

  3. Execute code and examine the response.

Last updated