Updated 19 Apr 2023
MatGPT - MATLAB® app and class to access ChatGPT API from OpenAI™
MatGPT is a MATLAB app powered by chatGPT class that allows you to easily access the ChatGPT API provided by OpenAI. With the app, you can load a list of prompts for specific use cases and engage in conversations with ease. If you're new to ChatGPT and prompt engineering, MatGPT is a great way to learn. If you're already familiar with ChatGPT, you can directly use the class.
The app and class 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. You are responsible for any fees OpenAI may charge for the use of their API.
- MathWorks Products (https://www.mathworks.com): To use MatGPT, you need to have MATLAB R2021a or later installed on your computer.
- OpenAI API Key: Additionally, you will need your own API key from https://platform.openai.com/account/api-keys.
- Download the content of this repository into a MATLAB path.
- Launch MATLAB
- Before using, set an environment variable with your OpenAI API key named
OPENAI_API_KEY. You can also set an environmental variable using the Settings in the app.
setenv("OPENAI_API_KEY","your key here")
- Open MatGPT.mlapp in App Designer and click 'Run'
How to use: MatGPT app
- Click on "+ New Chat" in the left nav to add a new chat. This opens the Settings tab.
- In the Settings tab, either choose a preset to populate the settings or customize on your own. Once you have completed the settings, click "Start New Chat" to initiate a chat. This will take you back to the Main tab.
- Presets are loaded from Presets.csv - feel free to customize your prompts.
- In the Main tab, a sample prompt is already provided based on the preset you selected, but feel free to replace it with your own. When you click
Sendbutton, the response will be shown in the 'Chat' tab.
Sendbutton is disabled until a chat is configured in the Settings tab.
- If your prompt is intended to generate MATLAB code, Check
Test Generated MATLAB Codecheckbox to test the returned code.
- The Usage tab shows number of tokens used and the cost incurred in the current chat session.
- Continue the conversation by keep adding more prompts and clicking
- You can right-click a chat in the left nav to rename, delete, or save the chat to a text file.
- When you close the app, the chat will be saved and will be reloaded into the left nav when you relaunch the app.
How to use: chatGPT class
chatGPT class creates an instance to access OpenAI API using your own API key, and optionally
max_tokens that determine the length of the response. Once you create an instance, then you can use its chat method to post prompt via OpenAPI ChatGPT API released on March 1, 2023.
Where to find it
chatGPT class is located in
Set up the bot
First, make sure you set your OpenAI API key in the environment variable as shown above. Create an instance using the default parameters. Your API key will be loaded from the OS environment variable.
myBot = chatGPT();
You can also set
max_tokens parameter, which determines the length of the response. If you don't specify
max_tokens, the response will be limited to up to 1000 tokens. The
max_tokens must be less than 4096.
myBot = chatGPT(max_tokens=50);
You can also specify other parameters, such as
- models: gpt-3.5-turbo (default - stable release), gpt-3.5-turbo-0301 (latest release)
- temperature: 0 = more strict, 1 = balanced, 2 = more creative
You can pass a
role to prime the chatbot for a specific use case. By default the chatbot is primed to act as an AI assistant, using the prompt "You are a helpful assistant." You can customize this for specific use cases.
role = "You define the role the chatbot should play."; myBot = chatGPT(model="gpt-3.5-turbo", max_tokens=1000, temperature=0.9,role=role);
Chat with the bot
Use the instance you created to send your prompt to the API.
answer = chat(myBot,"your prompt")
You will get an error if the API didn't return response successfully.
Check the token usage
usage method to obtain the total number of tokens used in the current session. It returns both the number of tokens as well as the cost.
[tokens,cost] = usage(myBot)
Save the chat history
saveChat method to save the chat history to a file. The supported format includes .mat, .xlsx, and .json.
[tokens,cost] = saveChat(myBot,format=".xlsx")
chatGPT class example
role = "You are a MATLAB expert who only answers with code with no explanation."; myBot = chatGPT(model="gpt-3.5-turbo", max_tokens=1000, temperature=0.9, role=role); answer = chat(myBot, "Generate 10x10 matrix using integers from 0 to 9.")
This code is adapted from this MATLAB Answers comment by Hans Scharler and uses Brian Buechel's CodeChecker and other great contributions.
The license for MatGPT is available in the LICENSE.txt file in this GitHub repository.
Toshiaki Takeuchi (2023). MatGPT (https://github.com/toshiakit/MatGPT/releases/tag/188.8.131.52), GitHub. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
See release notes for this release on GitHub: https://github.com/toshiakit/MatGPT/releases/tag/184.108.40.206
See release notes for this release on GitHub: https://github.com/toshiakit/MatGPT/releases/tag/1.2.1
See release notes for this release on GitHub: https://github.com/toshiakit/MatGPT/releases/tag/1.2.0
See release notes for this release on GitHub: https://github.com/toshiakit/MatGPT/releases/tag/1.1.0