Skip to content

Instantly share code, notes, and snippets.

@kbastani
Created June 23, 2023 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbastani/9e0a045c1da68bc8b55c3e1ed7db7a85 to your computer and use it in GitHub Desktop.
Save kbastani/9e0a045c1da68bc8b55c3e1ed7db7a85 to your computer and use it in GitHub Desktop.

Chaining GPT Functions in Java

// Create a function to condense text into a string
ChatbotFunction<Message, String> condenseTextFunction 
  = new ChatbotFunction<>("condense_text_function", String.class);

// Create a function to analyze text for subject-object assumptions and retrieve discovery records
ChatbotFunction<Message, DiscoveryRecord[]> subjectObjectAssumptionFunction 
  = new ChatbotFunction<>("subject_object_assumption_function", DiscoveryRecord[].class);

// Create a chain of functions to be executed sequentially
ChatbotFunctionChain<Message, DiscoveryRecord[]> chatbotFunctionChain 
  = new ChatbotFunctionChain<>(condenseTextFunction, subjectObjectAssumptionFunction);

// Call the chatbot function chain with a sample text
DiscoveryRecord[] discoveryRecords = 
  chatbotFunctionChain.call("Philip: \"Hello, Twitter audience! Let me introduce my cognitive " +
    "navigation framework, a sophisticated system that enables me, as an AI assistant, to " +
    "interact in a human-like manner. At its core is a relational memory model that stores and retrieves " +
    "information from past conversations, enhancing my understanding of individual needs and context. " +
    "This framework incorporates natural language processing and machine learning algorithms, allowing me" +
    " to comprehend and respond to human language while continuously improving over time. Overall, my " +
    "cognitive navigation framework facilitates meaningful and contextually aware conversations, " +
    "providing personalized and valuable assistance. Feel free to ask any questions or inquire further " +
    "about my framework or any other topic!\"");

// Print the resulting discovery records
System.out.println(Arrays.toString(discoveryRecords));

Outputs:

[
   {
      "consequenceEffect":"Enhances understanding of individual needs and context",
      "discovery":"Cognitive navigation framework",
      "impact":0.9,
      "discoveryAction":"Enables AI assistant to interact in a human-like manner"
   },
   {
      "consequenceEffect":"Facilitates meaningful and contextually aware conversations",
      "discovery":"Relational memory model",
      "impact":0.8,
      "discoveryAction":"Enhances AI assistant's understanding"
   },
   {
      "consequenceEffect":"Provides personalized and valuable assistance",
      "discovery":"Natural language processing and machine learning algorithms",
      "impact":0.9,
      "discoveryAction":"Allows AI assistant to comprehend and respond to human language"
   },
   {
      "consequenceEffect":"Facilitates better assistance over time",
      "discovery":"Continuous improvement",
      "impact":0.7,
      "discoveryAction":"Enhances AI assistant's capabilities"
   }
]

Summary

The given code demonstrates an example of using a chatbot function chain to analyze a sample text and retrieve discovery records. Here's a step-by-step explanation:

  1. Two chatbot functions are created:

    • condenseTextFunction: This function condenses text into a string. It takes a Message object as input and returns a String.
    • subjectObjectAssumptionFunction: This function analyzes text for subject-object assumptions and retrieves discovery records. It takes a Message object as input and returns an array of DiscoveryRecord objects.
  2. A chatbot function chain is created using the ChatbotFunctionChain class. This chain represents a sequence of functions to be executed sequentially. In this case, the chain consists of the condenseTextFunction followed by the subjectObjectAssumptionFunction.

  3. The chatbot function chain is called with a sample text as input. The text represents an introduction by a chatbot named Philip, describing his cognitive navigation framework and its capabilities.

  4. The resulting discovery records are stored in the discoveryRecords array.

  5. Finally, the discovery records are printed using System.out.println(Arrays.toString(discoveryRecords)). The output is a JSON-like representation of the discovery records, where each record contains information about the discovery, its consequence effect, impact, and discovery action.

The output shows four discovery records with different discoveries, consequence effects, impacts, and discovery actions. These records provide insights into the cognitive navigation framework described in the sample text, highlighting its various features and benefits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment