Skip to content

Commit 86249d5

Browse files
committed
Initial commit
0 parents  commit 86249d5

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

README.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Large Language Models with MATLAB®
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.
4+
5+
The functionality shown here simply serve as an interface to the ChatGPT API. You should be familiar with the limitations and risks associated with using this technology as well as with [OpenAI terms and policies](https://openai.com/policies). You are responsible for any fees OpenAI may charge for the use of their API.
6+
7+
## Setup
8+
1. Clone the repository to your local machine.
9+
10+
```bash
11+
git clone https://github.com/matlab-deep-learning/llms-with-matlab.git
12+
```
13+
14+
2. Open MATLAB and navigate to the directory where you cloned the repository.
15+
16+
3. Add the directory to the MATLAB path.
17+
18+
```matlab
19+
addpath('path/to/llms-with-matlab');
20+
```
21+
22+
4. Set up your OpenAI API key. You can either:
23+
- Pass it directly to the `openAIChat` class, using the nvp `ApiKey`
24+
- Or set it as an environment variable.
25+
```matlab
26+
setenv("OPENAI_API_KEY","your key here")
27+
```
28+
29+
### MathWorks Products (https://www.mathworks.com)
30+
31+
Requires MATLAB release R2023a or newer
32+
- Text Analytics Toolbox™
33+
34+
35+
### 3rd Party Products:
36+
3p:
37+
- An active OpenAI API subscription and API key.
38+
39+
40+
## Getting Started
41+
42+
To get started, you can either create an `openAIChat` object and use its methods or use it in a more complex setup, as needed.
43+
44+
### Simple call without preserving chat history
45+
46+
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 programatic way.
47+
48+
Here's a simple example of how to use the `openAIChat` for sentiment analysis:
49+
50+
```matlab
51+
% Initialize the OpenAI Chat object, passing a system prompt
52+
53+
% The system prompt tells the assistant how to behave, in this case, as a sentiment analyzer
54+
systemPrompt = "You are a sentiment analyser, you will look at a sentence and output a single word that classifier that sentence (positive or negative)."+....
55+
"Examples: \n"+...
56+
"The project was a complete failure.\n"+...
57+
"negative\n\n"+...
58+
"The team successfully completed the project ahead of schedule."+...
59+
"positive\n\n"+...
60+
"His attitude was terribly discouraging to the team.\n"+...
61+
"negative\n\n";
62+
63+
chat = openAIChat(systemPrompt);
64+
65+
% Generate a response, passing a new sentence for classification
66+
text = generate(chat, "The team is feeling very motivated")
67+
% Should output "positive"
68+
```
69+
70+
### Creating a chat system
71+
72+
If you want to create a chat system, you will have to create a history of the conversation and pass that to the `generate` function.
73+
74+
To start a conversation history, create a `openAIMessages` object:
75+
76+
```matlab
77+
history = openAIMessages;
78+
```
79+
80+
Then create the chat assistant:
81+
82+
```matlab
83+
chat = openAIChat("You are a helpful AI assistant.");
84+
```
85+
86+
Add a user message to the history and pass it to `generate`
87+
88+
```matlab
89+
history = addUserMessage(history, "What is an eigenvalue?");
90+
[text, response] = generate(chat, history)
91+
```
92+
93+
The output `text` will contain the answer and `response` will contain the full response, which you need to include in the hystory as follows
94+
```matlab
95+
history = addResponseMessage(history, response);
96+
```
97+
98+
You can keep interacting with the API and since we are saving the history, it will know about previous interactions.
99+
```matlab
100+
history = addUserMessage(history, "Generate MATLAB code that computes that");
101+
[text, response] = generate(chat, history);
102+
% Will generate code to compute the eigenvalue
103+
```
104+
105+
### Passing functions to API
106+
107+
You can define functions that the API is allowed to request calls using `openAIFunction`. For example, to define the `editDistance` function for string values, you can do as follows:
108+
109+
```matlab
110+
f = openAIFunction("editDistance", "Find edit distance between two strings or documents");
111+
```
112+
113+
You also have to define what parameters the function can take, providing details on the properties of each parameter. The properties can be `type`, `description` or `enum`.
114+
115+
```matlab
116+
f = addParameter(f, "str1", type="string", description="Source string.");
117+
f = addParameter(f, "str2", type="string", description="Target string.");
118+
```
119+
120+
Then you can pass the functions to the chat API as follows:
121+
122+
```matlab
123+
chat = openAIChat("You are a helpful assistant", Functions=f);
124+
````
125+
126+
The model will automatically determine if the function should be called based on the user input:
127+
128+
```matlab
129+
history = openAIMessages;
130+
history = addUserMessage(history, "What is the edit distance between MathWorks and MATLAB?");
131+
132+
[text, response] = generate(chat, history);
133+
```
134+
135+
If the model sends back an empty `text` and a response containing a field `function_call`, it means it's requesting that you call a function. The model is not able to automatically execute a function.
136+
137+
Once you have the result of the requested function, you can add the value to the history as a function message:
138+
139+
```
140+
history = addFunctionMessage(history, "editDistance", "8");
141+
```
142+
143+
Then the model can give a more precise answer based on the result of the function:
144+
```
145+
[text, response] = generate(chat, history);
146+
```
147+
148+
## Examples
149+
To learn how to use this in testing workflows, see [Examples](/examples/).
150+
151+
- [ExampleBasicUsage.mlx](/examples/ExampleBasicUsage.mlx): A beginner's guide to using ChatGPT with a focus on parameter settings like temperature for controlling text generation.
152+
- [ExampleSummarization.mlx](/examples/ExampleSummarization.mlx): Learn to create concise summaries of long texts with ChatGPT.
153+
- [ExampleChatBot.mlx](/examples/ExampleChatBot.mlx): Build a conversational chatbot capable of handling various dialogue scenarios using ChatGPT.
154+
- [ExampleRetrievalAugmentedGeneration.mlx](/examples/ExampleRetrievalAugmentedGeneration.mlx): Enhance ChatGPT responses by integrating data retrieved from a separate knowledge base.
155+
- [ExampleRobotControl.mlx](/examples/ExampleRobotControl.mlx): Translate natural language commands into robotic actions using ChatGPT.
156+
- [ExampleAgentCreation.mlx](/examples/ExampleAgentCreation.mlx): Learn how to create agents capable of execting MATLAB functions.
157+
158+
## License
159+
160+
The license is available in the license.txt file in this GitHub repository.
161+
162+
## Community Support
163+
[MATLAB Central](https://www.mathworks.com/matlabcentral)
164+
165+
Copyright 2023 The MathWorks, Inc.

0 commit comments

Comments
 (0)