Skip to content

Commit

Permalink
Adds GPT-4 support, etc.
Browse files Browse the repository at this point in the history
* adds support for GPT-4 models
* adds Connection Timeout settings. You can add proxy via Web Preferences in MATLAB
* adds support for stop sequences (max 4).
* improves the description of the API error messages
  • Loading branch information
toshiakit committed Jul 15, 2023
1 parent 2816072 commit 7814418
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
Binary file modified MatGPT.mlapp
Binary file not shown.
9 changes: 8 additions & 1 deletion 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,7 +33,7 @@ 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.
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
17 changes: 15 additions & 2 deletions helpers/chatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
arguments
obj
prompt string {mustBeTextScalar}
options.timeout double {mustBeScalarOrEmpty}
options.stop string {mustBeText,mustBeNonzeroLengthText}
end

Expand Down Expand Up @@ -53,8 +54,16 @@
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 @@ -73,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

0 comments on commit 7814418

Please sign in to comment.