Skip to content

Latest commit

 

History

History
196 lines (124 loc) · 6.29 KB

addResponseMessage.md

File metadata and controls

196 lines (124 loc) · 6.29 KB

addResponseMessage

Add response message to message history

updatedMessages = addResponseMessage(messages,completeOutput)

Description

updatedMessages = addResponseMessage(messages,completeOutput) adds the generated output of a large language model to the messageHistory object messages.

Examples

Add Response Message to Message History

First, specify the OpenAI® API key as an environment variable and save it to a file called ".env". Next, load the environment file using the loadenv function.

loadenv(".env")

Connect to the OpenAI Chat Completion API.

model = openAIChat("You are a helpful assistant.");

Initialize the message history.

messages = messageHistory;

Add a user message to the message history.

messages = addUserMessage(messages,"Why is a raven like a writing desk?");

Generate a response.

```matlab
[generatedText,completeOutput,httpResponse] = generate(model,messages);

Add the response to the message history.

messages = addResponseMessage(messages,completeOutput);
messages.Messages{end}
ans = struct with fields:
       role: "assistant"
    content: "The question "Why is a raven like a writing desk?" is famously posed by the Mad Hatter in Lewis Carroll's "Alice's Adventures in Wonderland." Initially, it is presented as a riddle without a clear answer, contributing to the absurdity and nonsensical nature of the story. However, over time, various interpretations and answers have been suggested, such as:↵↵1. **Both can produce notes**: Ravens can "caw" or make sounds like a note, and writing desks are used for writing notes or letters.↵2. **Both are associated with writing**: Ravense have historically been linked to writers (like Edgar Allan Poe's famous poem "The Raven"), and desks are where writing is done.↵3. **The riddle is inherently nonsensical**: The whole point may be that some riddles don't have answers, fitting into the whimsical and illogical world of Wonderland.↵↵Carroll himself later suggested that the riddle was meant to be without an answer, thus adding to its charm and mystique in the context of his work."

Compute Sine Using OpenAI Function Call

First, specify the OpenAI® API key as an environment variable and save it to a file called ".env". Next, load the environment file using the loadenv function.

loadenv(".env")

Create an openAIFunction object that represents the sind function. The sind function has a single input argument, x, representing the input angle in degrees.

f = openAIFunction("sind","Sine of argument in degrees");
f = addParameter(f,"x",type="number",description="Angle in degrees");

Connect to the OpenAI Chat Completion API. Pass the openAIFunction object f as an input argument.

model = openAIChat("You are a helpful assistant.",Tools=f);

Initialize the message history. Add a user message to the message history.

messages = messageHistory;
messages = addUserMessage(messages,"What is the sine of thirty?");

Generate a response based on the message history.

[~,completeOutput] = generate(model,messages)
completeOutput = struct with fields:
          role: 'assistant'
       content: []
    tool_calls: [1x1 struct]
       refusal: []

The model has not generated any text. Instead, it has detected a function call, completeOutput.tool_calls.

Add the response to the message history.

messages = addResponseMessage(messages,completeOutput);

Extract the tool call ID and the name of the called function.

toolCallID = string(completeOutput.tool_calls.id)
toolCallID = "call_VLRxaOUTDEyzCY4c8rDnq0jM"
functionCalled = string(completeOutput.tool_calls.function.name)
functionCalled = "sind"

Make sure that the model is calling the correct function. Even with only a single function, large language models can hallucinate function calls to fictitious functions.

Extract the input argument values from the complete output using the jsondecode function. Compute the sine of the generated argument value and add the result to the message history using the addToolMessage function.

if functionCalled == "sind"
    args = jsondecode(completeOutput.tool_calls.function.arguments);
    result = sind(args.x)
    messages = addToolMessage(messages,toolCallID,functionCalled,"x="+result);
end
result = 0.5000

Finally, generate a natural language response.

generatedText = generate(model,messages)
generatedText = "The sine of thirty degrees is 0.5."

Input Arguments

messages — Message history

messageHistory object

Message history, specified as a messageHistory object.

completeOutput — Complete output

structure array

Complete output generated from a large language model using the generate function, specified as a structure array.

The type and name of the fields in the structure depend on the API, the model, whether you use function calls, and whether you stream the output.

Output Argument

updatedMessages — Updated message history

messageHistory object

Updated message history, specified as a messageHistory object.

The updated message history includes a new structure array with these fields:

  • role —"assistant"
  • content — Set by the content input argument

If the generated response includes a function call, then the updated message history also includes this field:

  • tool_calls — completeOutput.tool_calls structure array

See Also

generate | messageHistory | openAIChat | ollamaChat | azureChat | addUserMessage | addUserMessageWithImage | addToolMessage | addSystemMessage

Copyright 2024 The MathWorks, Inc.