Skip to content

Commit

Permalink
Merge pull request #17 from toshiakit/dev
Browse files Browse the repository at this point in the history
GTP-4 and new model support
  • Loading branch information
nothans authored Jul 15, 2023
2 parents 6215eaf + 7814418 commit 302f142
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
Binary file modified MatGPT.mlapp
Binary file not shown.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ MatGPT is a MATLAB app powered by chatGPT class that allows you to easily access

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](https://openai.com/policies). You are responsible for any fees OpenAI may charge for the use of their API.

## What's New

* adds support for GPT-4 models [available to all API users who have a history of successful payments](https://openai.com/blog/gpt-4-api-general-availability)
* adds Connection Timeout settings. You can add proxy via [Web Preferences](https://www.mathworks.com/help/matlab/ref/preferences.html) in MATLAB.
* adds support for stop sequences (max 4).
* improves the description of the API error messages

## Requirements

* **MathWorks Products (https://www.mathworks.com)**: To use MatGPT, you need to have MATLAB R2021a or later installed on your computer.
Expand All @@ -26,18 +33,22 @@ setenv("OPENAI_API_KEY","your key here")

## How to use: MatGPT app

![MatGPT Chat Tab](images/MatGPT.png)
![MatGPT Chat Tab](images/MatGPT.gif)
1. Click on "+ New Chat" in the left nav to add a new chat. This opens the Settings tab.
2. 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](contents/presets.csv) - feel free to customize your prompts.
3. 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 `Send` button, the response will be shown in the 'Chat' tab.
* The `Send` button is disabled until a chat is configured in the Settings tab.
* If your prompt is intended to generate MATLAB code, Check `Test Generated MATLAB Code` checkbox to test the returned code.
* If you want suggestion for follow-up questions in the reponse, check `Suggest follow-up questions` checkbox.
* If your prompt is intended to generate MATLAB code, check `Test Generated MATLAB Code` checkbox 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 `Advanced` tab to specify the sequences where the API will stop generating further tokens
4. Continue the conversation by keep adding more prompts and clicking `Send`.
5. You can right-click a chat in the left nav to rename, delete, or save the chat to a text file.
5. You can right-click or double-click a chat in the left nav to rename, delete, or save the chat to a text file.
6. 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.
Expand Down
5 changes: 3 additions & 2 deletions helpers/chatGPT.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
arguments
options.model string {mustBeTextScalar, ...
mustBeMember(options.model, ...
["gpt-3.5-turbo","gpt-3.5-turbo-0301"])} = ...
"gpt-3.5-turbo";
["gpt-3.5-turbo","gpt-3.5-turbo-0613", ...
"gpt-4","gpt-4-0613", ...
"gpt-4-32k","gpt-4-32k-0613"])} = "gpt-3.5-turbo";
options.role string {mustBeTextScalar} = ...
"You are a helpful assistant.";
options.max_tokens (1,1) double {mustBeNumeric, ...
Expand Down
28 changes: 23 additions & 5 deletions helpers/chatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
% This subclass adds a new method 'injectChatLog'

methods
function responseText = chat(obj,prompt)
function responseText = chat(obj,prompt,options)
%CHAT This send http requests to the api
% Pass the prompt as input argument to send the request
% Only messages with valid roles are sent in messages object
arguments
obj
prompt string {mustBeTextScalar}
options.timeout double {mustBeScalarOrEmpty}
options.stop string {mustBeText,mustBeNonzeroLengthText}
end

% retrieve API key from the environment
Expand Down Expand Up @@ -37,19 +39,31 @@
m = obj.messages;
roles = arrayfun(@(x) string(x.role),m);
m(~ismember(roles,["system","user","assistant"])) = [];

% shorten calls to MATLAB HTTP interfaces
import matlab.net.*
import matlab.net.http.*
% construct http message content
query = struct('model',obj.model,'messages',m,'max_tokens',obj.max_tokens,'temperature',obj.temperature);
%
if isfield(options,'stop')
query = struct('model',obj.model,'messages',m,'max_tokens',obj.max_tokens,'temperature',obj.temperature,'stop',options.stop);
else
query = struct('model',obj.model,'messages',m,'max_tokens',obj.max_tokens,'temperature',obj.temperature);
end
% the headers for the API request
headers = HeaderField('Content-Type', 'application/json');
headers(2) = HeaderField('Authorization', "Bearer " + api_key);
% the request message
request = RequestMessage('post',headers,query);

% Create a HTTPOptions object; set proxy in MATLAB Web Preferences if needed
httpOpts = matlab.net.http.HTTPOptions;
% Set the ConnectTimeout option to 30 seconds
if isfield(options,'timeout') && options.timeout > 0
httpOpts.ConnectTimeout = options.timeout;
end
% send the request and store the response
response = send(request, URI(obj.api_endpoint));
response = send(request, URI(obj.api_endpoint),httpOpts);

% extract the response text
if response.StatusCode == "OK"
% extract text from the response
Expand All @@ -68,7 +82,11 @@
responseText = responseText + response.StatusLine.ReasonPhrase;
if string(response.StatusCode) == "401"
responseText = responseText + newline + "Check your API key.";
responseText = responseText + newline + "Your free trial for OpenAI API may have expired.";
responseText = responseText + newline + "You may have an invalid API key.";
elseif string(response.StatusCode) == "404"
responseText = responseText + newline + "You may not have access to the model.";
elseif string(response.StatusCode) == "429"
responseText = responseText + newline + "You exceeded the API limit. Your free trial for OpenAI API may have expired.";
end
id = "chatter:invalidKey";
ME = MException(id,responseText);
Expand Down
Binary file added images/MatGPT.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 302f142

Please sign in to comment.