|
| 1 | + |
| 2 | +# addResponseMessage |
| 3 | + |
| 4 | +Add response message to message history |
| 5 | + |
| 6 | + |
| 7 | +`updatedMessages = addResponseMessage(messages,completeOutput)` |
| 8 | + |
| 9 | +# Description |
| 10 | + |
| 11 | +`updatedMessages = addResponseMessage(messages,completeOutput)` adds the generated output of a large language model to the `messageHistory` object `messages`. |
| 12 | + |
| 13 | +# Examples |
| 14 | +## Add Response Message to Message History |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +```matlab |
| 19 | +loadenv(".env") |
| 20 | +``` |
| 21 | + |
| 22 | +Connect to the OpenAI Chat Completion API. |
| 23 | + |
| 24 | +```matlab |
| 25 | +model = openAIChat("You are a helpful assistant."); |
| 26 | +``` |
| 27 | + |
| 28 | +Initialize the message history. |
| 29 | + |
| 30 | +```matlab |
| 31 | +messages = messageHistory; |
| 32 | +``` |
| 33 | + |
| 34 | +Add a user message to the message history. |
| 35 | + |
| 36 | +```matlab |
| 37 | +messages = addUserMessage(messages,"Why is a raven like a writing desk?"); |
| 38 | +
|
| 39 | +Generate a response. |
| 40 | +
|
| 41 | +```matlab |
| 42 | +[generatedText,completeOutput,httpResponse] = generate(model,messages); |
| 43 | +``` |
| 44 | + |
| 45 | +Add the response to the message history. |
| 46 | + |
| 47 | +```matlab |
| 48 | +messages = addResponseMessage(messages,completeOutput); |
| 49 | +messages.Messages{end} |
| 50 | +``` |
| 51 | + |
| 52 | +```matlabTextOutput |
| 53 | +ans = struct with fields: |
| 54 | + role: "assistant" |
| 55 | + 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." |
| 56 | +
|
| 57 | +``` |
| 58 | +## Compute Sine Using OpenAI Function Call |
| 59 | + |
| 60 | +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. |
| 61 | + |
| 62 | +```matlab |
| 63 | +loadenv(".env") |
| 64 | +``` |
| 65 | + |
| 66 | +Create an `openAIFunction` object that represents the [`sind`](https://www.mathworks.com/help/matlab/ref/sind.html) function. The `sind` function has a single input argument, `x`, representing the input angle in degrees. |
| 67 | + |
| 68 | +```matlab |
| 69 | +f = openAIFunction("sind","Sine of argument in degrees"); |
| 70 | +f = addParameter(f,"x",type="number",description="Angle in degrees"); |
| 71 | +``` |
| 72 | + |
| 73 | +Connect to the OpenAI Chat Completion API. Pass the `openAIFunction` object `f` as an input argument. |
| 74 | + |
| 75 | +```matlab |
| 76 | +model = openAIChat("You are a helpful assistant.",Tools=f); |
| 77 | +``` |
| 78 | + |
| 79 | +Initialize the message history. Add a user message to the message history. |
| 80 | + |
| 81 | +```matlab |
| 82 | +messages = messageHistory; |
| 83 | +messages = addUserMessage(messages,"What is the sine of thirty?"); |
| 84 | +``` |
| 85 | + |
| 86 | +Generate a response based on the message history. |
| 87 | + |
| 88 | +```matlab |
| 89 | +[~,completeOutput] = generate(model,messages) |
| 90 | +``` |
| 91 | + |
| 92 | +```matlabTextOutput |
| 93 | +completeOutput = struct with fields: |
| 94 | + role: 'assistant' |
| 95 | + content: [] |
| 96 | + tool_calls: [1x1 struct] |
| 97 | + refusal: [] |
| 98 | +
|
| 99 | +``` |
| 100 | + |
| 101 | +The model has not generated any text. Instead, it has detected a function call, `completeOutput.tool_calls`. |
| 102 | + |
| 103 | + |
| 104 | +Add the response to the message history. |
| 105 | + |
| 106 | +```matlab |
| 107 | +messages = addResponseMessage(messages,completeOutput); |
| 108 | +``` |
| 109 | + |
| 110 | +Extract the tool call ID and the name of the called function. |
| 111 | + |
| 112 | +```matlab |
| 113 | +toolCallID = string(completeOutput.tool_calls.id) |
| 114 | +``` |
| 115 | + |
| 116 | +```matlabTextOutput |
| 117 | +toolCallID = "call_VLRxaOUTDEyzCY4c8rDnq0jM" |
| 118 | +``` |
| 119 | + |
| 120 | +```matlab |
| 121 | +functionCalled = string(completeOutput.tool_calls.function.name) |
| 122 | +``` |
| 123 | + |
| 124 | +```matlabTextOutput |
| 125 | +functionCalled = "sind" |
| 126 | +``` |
| 127 | + |
| 128 | +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. |
| 129 | + |
| 130 | + |
| 131 | +Extract the input argument values from the complete output using the [`jsondecode`](https://www.mathworks.com/help/matlab/ref/jsondecode.html) function. Compute the sine of the generated argument value and add the result to the message history using the `addToolMessage` function. |
| 132 | + |
| 133 | +```matlab |
| 134 | +if functionCalled == "sind" |
| 135 | + args = jsondecode(completeOutput.tool_calls.function.arguments); |
| 136 | + result = sind(args.x) |
| 137 | + messages = addToolMessage(messages,toolCallID,functionCalled,"x="+result); |
| 138 | +end |
| 139 | +``` |
| 140 | + |
| 141 | +```matlabTextOutput |
| 142 | +result = 0.5000 |
| 143 | +``` |
| 144 | + |
| 145 | +Finally, generate a natural language response. |
| 146 | + |
| 147 | +```matlab |
| 148 | +generatedText = generate(model,messages) |
| 149 | +``` |
| 150 | + |
| 151 | +```matlabTextOutput |
| 152 | +generatedText = "The sine of thirty degrees is 0.5." |
| 153 | +``` |
| 154 | +# Input Arguments |
| 155 | +### `messages` — Message history |
| 156 | + |
| 157 | +`messageHistory` object |
| 158 | + |
| 159 | + |
| 160 | +Message history, specified as a [`messageHistory`](messageHistory.md) object. |
| 161 | + |
| 162 | +### completeOutput — Complete output |
| 163 | + |
| 164 | +structure array |
| 165 | + |
| 166 | + |
| 167 | +Complete output generated from a large language model using the [`generate`](generate.md) function, specified as a structure array. |
| 168 | + |
| 169 | + |
| 170 | +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. |
| 171 | + |
| 172 | +# Output Argument |
| 173 | +### `updatedMessages` — Updated message history |
| 174 | + |
| 175 | +`messageHistory` object |
| 176 | + |
| 177 | + |
| 178 | +Updated message history, specified as a [`messageHistory`](messageHistory.md) object. |
| 179 | + |
| 180 | + |
| 181 | +The updated message history includes a new structure array with these fields: |
| 182 | + |
| 183 | +- role —`"assistant"` |
| 184 | +- content — Set by the `content` input argument |
| 185 | + |
| 186 | +If the generated response includes a function call, then the updated message history also includes this field: |
| 187 | + |
| 188 | +- tool\_calls — `completeOutput.tool_calls` structure array |
| 189 | +# See Also |
| 190 | + |
| 191 | +[`generate`](generate.md) | [`messageHistory`](messageHistory.md) | [`openAIChat`](openAIChat.md) | [`ollamaChat`](ollamaChat.md) | [`azureChat`](azureChat.md) | [`addUserMessage`](addUserMessage.md) | [`addUserMessageWithImage`](addUserMessageWithImage.md) | [`addToolMessage`](addToolMessage.md) | [`addSystemMessage`](addSystemMessage.md) |
| 192 | + |
| 193 | +- [Analyze Text Data Using Parallel Function Calls with ChatGPT](../../examples/AnalyzeTextDataUsingParallelFunctionCallwithChatGPT.md) |
| 194 | + |
| 195 | +*Copyright 2024 The MathWorks, Inc.* |
| 196 | + |
0 commit comments