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.
OPENAI_API_KEY
. You can also set an environmental variable using the Settings in the app.setenv("OPENAI_API_KEY","your key here")
Send
button, the response will be shown in the 'Chat' tab.Send
button is disabled until a chat is configured in the Settings tab.Test Generated MATLAB Code
checkbox to test the returned code.Send
.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.
chatGPT class is located in helpers
folder.
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 model
and temperature
.
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);
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.
Use 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)
Use saveChat
method to save the chat history to a file. The supported format includes .mat, .xlsx, and .json.
[tokens,cost] = saveChat(myBot,format=".xlsx")
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.