You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+89-27Lines changed: 89 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,14 @@
1
1
# Large Language Models (LLMs) with MATLAB® [](https://matlab.mathworks.com/open/github/v1?repo=matlab-deep-learning/llms-with-matlab)
2
2
3
-
This repository contains example code to demonstrate how to connect MATLAB to the OpenAI™ Chat Completions API (which powers ChatGPT™). This allows you to leverage the natural language processing capabilities of GPT models directly within your MATLAB environment.
3
+
This repository contains example code to demonstrate how to connect MATLAB to the OpenAI™ Chat Completions API (which powers ChatGPT™) as well as OpenAI Images API (which powers DALL-E™). This allows you to leverage the natural language processing capabilities of large language models directly within your MATLAB environment.
4
4
5
-
The functionality shown here serves as an interface to the ChatGPT API. To start using the ChatGPT API, you first need to obtain the OpenAI API keys. You are responsible for any fees OpenAI may charge for the use of their API. You should be familiar with the limitations and risks associated with using this technology, and you agree that you shall be solely responsible for full compliance with any terms that may apply to your use of the OpenAI Chat Completions API.
5
+
The functionality shown here serves as an interface to the ChatGPT and DALL-E APIs. To start using the OpenAI APIs, you first need to obtain the OpenAI API keys. You are responsible for any fees OpenAI may charge for the use of their APIs. You should be familiar with the limitations and risks associated with using this technology, and you agree that you shall be solely responsible for full compliance with any terms that may apply to your use of the OpenAI APIs.
6
6
7
7
Some of the current LLMs supported are:
8
-
- gpt-3.5-turbo
9
-
- gpt-4
8
+
- gpt-3.5-turbo, gpt-3.5-turbo-1106
9
+
- gpt-4, gpt-4-1106-preview
10
+
- gpt-4-vision-preview (a.k.a. GPT-4 Turbo with Vision)
11
+
- dall-e-2, dall-e-3
10
12
11
13
For details on the specification of each model, check the official [OpenAI documentation](https://platform.openai.com/docs/models).
12
14
@@ -51,13 +53,13 @@ If you would like to use it with MATLAB Desktop, proceed with the following step
51
53
- An active OpenAI API subscription and API key.
52
54
53
55
54
-
## Getting Started
56
+
## Getting Started with Chat Completion API
55
57
56
58
To get started, you can either create an `openAIChat` object and use its methods or use it in a more complex setup, as needed.
57
59
58
60
### Simple call without preserving chat history
59
61
60
-
In some situations, you will want to use GPT models without preserving chat history. For example, when you want to perform independent queries in a programmatic way.
62
+
In some situations, you will want to use chat completion models without preserving chat history. For example, when you want to perform independent queries in a programmatic way.
61
63
62
64
Here's a simple example of how to use the `openAIChat` for sentiment analysis:
63
65
@@ -78,7 +80,7 @@ systemPrompt = "You are a sentiment analyser. You will look at a sentence and ou
78
80
chat = openAIChat(systemPrompt);
79
81
80
82
% Generate a response, passing a new sentence for classification
81
-
text = generate(chat,"The team is feeling very motivated")
83
+
txt = generate(chat,"The team is feeling very motivated")
82
84
% Should output "positive"
83
85
```
84
86
@@ -102,24 +104,34 @@ Add a user message to the history and pass it to `generate`
102
104
103
105
```matlab
104
106
history = addUserMessage(history,"What is an eigenvalue?");
105
-
[text, response] = generate(chat, history)
107
+
[txt, response] = generate(chat, history)
106
108
```
107
109
108
-
The output `text` will contain the answer and `response` will contain the full response, which you need to include in the history as follows
110
+
The output `txt` will contain the answer and `response` will contain the full response, which you need to include in the history as follows
109
111
```matlab
110
112
history = addResponseMessage(history, response);
111
113
```
112
114
113
115
You can keep interacting with the API and since we are saving the history, it will know about previous interactions.
114
116
```matlab
115
117
history = addUserMessage(history,"Generate MATLAB code that computes that");
116
-
[text, response] = generate(chat,history);
118
+
[txt, response] = generate(chat,history);
117
119
% Will generate code to compute the eigenvalue
118
120
```
119
121
122
+
### Streaming the response
123
+
124
+
You can specifying the streaming function when you create the chat assistant. This will print the response to the command window.
125
+
```matlab
126
+
% streaming function
127
+
sf = @(x)fprintf("%s",x);
128
+
chat = openAIChat(StreamFun=sf);
129
+
txt = generate(chat,"What is Model-Based Design and how is it related to Digital Twin?")
130
+
```
131
+
120
132
### Calling MATLAB functions with the API
121
133
122
-
The optional parameter `functions` can be used to provide function specifications to the API. The purpose of this is to enable models to generate function arguments which adhere to the provided specifications.
134
+
The optional parameter `Tools` can be used to provide function specifications to the API. The purpose of this is to enable models to generate function arguments which adhere to the provided specifications.
123
135
Note that the API is not able to directly call any function, so you should call the function and pass the values to the API directly. This process can be automated as shown in [ExampleFunctionCalling.mlx](/examples/ExampleFunctionCalling.mlx), but it's important to consider that ChatGPT can hallucinate functionnames, so avoid executing any arbitrary generated functions and only allow the execution of functions that you have defined.
124
136
125
137
For example, if you want to use the API for mathematical operations such as `sind`, instead of letting the model generate the result and risk running into hallucinations, you can give the model direct access to the functionas follows:
@@ -128,14 +140,16 @@ For example, if you want to use the API for mathematical operations such as `sin
128
140
```matlab
129
141
f = openAIFunction("sind","Sine of argument in degrees");
130
142
f = addParameter(f,"x",type="number",description="Angle in degrees.");
131
-
chat = openAIChat("You are a helpful assistant.",Functions=f);
143
+
chat = openAIChat("You are a helpful assistant.",Tools=f);
132
144
```
133
145
134
-
When the model identifies that it could use the defined functions to answer a query, it will return a `function_call` request, instead of directly generating the response:
146
+
When the model identifies that it could use the defined functions to answer a query, it will return a `tool_calls` request, instead of directly generating the response:
135
147
136
148
```matlab
149
+
messages = openAIMessages;
137
150
messages = addUserMessage(messages, "What is the sine of 30?");
The resulting embedding is a vector that captures the semantics of your text and can be used on tasks such as retrieval augmented generation and clustering.
247
294
248
-
```
295
+
```matlab
249
296
>> size(emb)
250
297
251
298
ans =
252
299
253
300
1 1536
254
301
```
302
+
## Getting Started with Images API
303
+
304
+
To get started, you can either create an `openAIImage` object and use its methods or use it in a more complex setup, as needed.
305
+
306
+
```matlab
307
+
mdl = openAIImages(ModelName="dall-e-3");
308
+
images = generate(mdl,"Create a 3D avatar of a whimsical sushi on the beach. He is decorated with various sushi elements and is playfully interacting with the beach environment.");
309
+
figure
310
+
imshow(images{1})
311
+
```
255
312
256
313
## Examples
257
314
To learn how to use this in your workflows, see [Examples](/examples/).
258
315
259
-
- [ExampleSummarization.mlx](/examples/ExampleSummarization.mlx): Learn to create concise summaries of long texts with ChatGPT. (Requires Text Analytics Toolbox™)
316
+
- [ExampleStreaming.mlx](/examples/ExampleStreaming.mlx): Learn to implement a simple chat that stream the response.
317
+
- [ExampleSummarization.mlx](/examples/ExampleSummarization.mlx): Learn to create concise summaries of long texts with ChatGPT. (Requires Text Analytics Toolbox™)
260
318
- [ExampleChatBot.mlx](/examples/ExampleChatBot.mlx): Build a conversational chatbot capable of handling various dialogue scenarios using ChatGPT. (Requires Text Analytics Toolbox)
261
319
- [ExampleFunctionCalling.mlx](/examples/ExampleFunctionCalling.mlx): Learn how to create agents capable of executing MATLAB functions.
320
+
- [ExampleParallelFunctionCalls.mlx](/examples/ExampleParallelFunctionCalls.mlx): Learn how to take advantage of parallel functioncalling.
262
321
- [ExampleRetrievalAugmentedGeneration.mlx](/examples/ExampleRetrievalAugmentedGeneration.mlx): Learn about retrieval augmented generation with a simple use case. (Requires Text Analytics Toolbox™)
322
+
- [ExampleGPT4Vision.mlx](/examples/ExampleGPT4Vision.mlx): Learn how to use GPT-4 Turbo with Vision to understand the content of an image.
323
+
- [ExampleJSONMode.mlx](/examples/ExampleJSONMode.mlx): Learn how to use JSON mode in chat completions
324
+
- [ExampleDALLE.mlx](/examples/ExampleDALLE.mlx): Learn how to generate images, create variations and edit the images.
263
325
264
326
## License
265
327
@@ -268,4 +330,4 @@ The license is available in the license.txt file in this GitHub repository.
0 commit comments