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.
- adds support for GPT-4 models available to all API users who have a history of successful payments
- adds Connection Timeout settings. You can add proxy via Web Preferences in MATLAB.
- adds support for stop sequences (max 4).
- adds support for 3 follow-up questions.
- improves the description of the API error messages
- removed fee calculation from the usage panel due to frequent updates to the API pricing.
- 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 you want suggestion for follow-up questions in the reponse, check
Suggest follow-up questionscheckbox.
- 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.
- Add stop sequences in
Advancedtab to specify the sequences where the API will stop generating further tokens
- Continue the conversation by keep adding more prompts and clicking
- You can right-click or double-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 updated on June 13, 2023.
Please note that this class doesn't support function calling.
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-4 (requires paid subsription to the API)
- 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 number of tokens used in the current session. It returns the completion tokens used, prompt tokens used, and total tokens used.
Different API rate applies depending on the model and type of tokens. Please consult OpenAI pricing page for details.
[completion_tokens,prompt_tokens,total_tokens] = usage(myBot)
Save the chat history
saveChat method to save the chat history to a file. The supported format includes .mat, .xlsx, and .json.
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.")
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/1.3.1), 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!
See release notes for this release on GitHub: https://github.com/toshiakit/MatGPT/releases/tag/1.3.1
See release notes for this release on GitHub: https://github.com/toshiakit/MatGPT/releases/tag/1.3.0
See release notes for this release on GitHub: https://github.com/toshiakit/MatGPT/releases/tag/188.8.131.52
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