Skip to content

Commit

Permalink
Merge pull request #18 from toshiakit/dev
Browse files Browse the repository at this point in the history
Update: Token usage changes
  • Loading branch information
nothans authored Jul 20, 2023
2 parents 302f142 + 5dfe3b8 commit 92033fd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
Binary file modified MatGPT.mlapp
Binary file not shown.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ The app and class simply serve as an interface to the ChatGPT API. You should be

* 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).
* 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.

## Requirements

Expand Down Expand Up @@ -51,7 +53,8 @@ setenv("OPENAI_API_KEY","your key here")

## 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.
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](helpers/chatGPT.m) class is located in `helpers` folder.
Expand All @@ -70,7 +73,7 @@ You can also set `max_tokens` parameter, which determines the length of the resp
myBot = chatGPT(max_tokens=50);
```
You can also specify other parameters, such as `model` and `temperature`.
* models: gpt-3.5-turbo (default - stable release), gpt-3.5-turbo-0301 (latest release)
* 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.
Expand All @@ -89,16 +92,17 @@ answer = chat(myBot,"your prompt")
You will get an error if the API didn't return response successfully.

### Check the token usage
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.
Use `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](https://openai.com/pricing) for details.

```matlab
[tokens,cost] = usage(myBot)
[completion_tokens,prompt_tokens,total_tokens] = usage(myBot)
```
### Save the chat history
Use `saveChat` method to save the chat history to a file. The supported format includes .mat, .xlsx, and .json.

```matlab
[tokens,cost] = saveChat(myBot,format=".xlsx")
saveChat(myBot,format=".xlsx")
```

## chatGPT class example
Expand Down
21 changes: 13 additions & 8 deletions helpers/chatGPT.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
% Create an instance using your own API key, and optionally
% max_tokens that determine the length of the response
%
% ask method lets you send a prompt text to the API as HTTP request
% chat method lets you send a prompt text to the API as HTTP request
% and parses the response.
%
% Before using, set an environment variable with your OpenAI API key
Expand All @@ -14,7 +14,7 @@
properties (Access = public)
% the API endpoint
api_endpoint = "https://api.openai.com/v1/chat/completions";
% ChatGPT model to use - gpt-3.5-turbo or gpt-3.5-turbo-0301
% ChatGPT model to use - gpt-3.5-turbo, etc.
model;
% what role the bot should play
role;
Expand All @@ -25,6 +25,8 @@
% store chat log in messages object
messages;
% store usage
completion_tokens = 0;
prompt_tokens = 0;
total_tokens = 0;
end

Expand Down Expand Up @@ -106,6 +108,10 @@
obj.messages = [obj.messages, ...
struct('role',"assistant",'content',responseText)];
% add the tokens used
obj.completion_tokens = obj.completion_tokens + ...
response.Body.Data.usage.completion_tokens;
obj.prompt_tokens = obj.prompt_tokens + ...
response.Body.Data.usage.prompt_tokens;
obj.total_tokens = obj.total_tokens + ...
response.Body.Data.usage.total_tokens;
else
Expand All @@ -115,12 +121,11 @@
end
end

function [tokens_used, cost] = usage(obj)
%USAGE retunrs the number of tokens used and the cost
tokens_used = obj.total_tokens;
% as of March 1, 2023, it's priced at $0.002 per 1K tokens
price = 0.002/1000;
cost = round(tokens_used * price,4);
function [completion_tokensd, prompt_tokens, total_tokens] = usage(obj)
%USAGE retunrs the number of tokens used
completion_tokensd = obj.completion_tokens;
prompt_tokens = obj.prompt_tokens;
total_tokens = obj.total_tokens;
end

function saveChat(obj,options)
Expand Down
7 changes: 6 additions & 1 deletion helpers/chatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
obj.messages = [obj.messages, ...
struct('role',"assistant",'content',responseText)];
% add the numbers of tokens used
obj.completion_tokens = obj.completion_tokens + ...
response.Body.Data.usage.completion_tokens;
obj.prompt_tokens = obj.prompt_tokens + ...
response.Body.Data.usage.prompt_tokens;
obj.total_tokens = obj.total_tokens + ...
response.Body.Data.usage.total_tokens;
else
Expand All @@ -84,7 +88,8 @@
responseText = responseText + newline + "Check your API key.";
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.";
responseText = responseText + newline + "You may not have access to the model: " + ...
obj.model + ". Consider using another model.";
elseif string(response.StatusCode) == "429"
responseText = responseText + newline + "You exceeded the API limit. Your free trial for OpenAI API may have expired.";
end
Expand Down

0 comments on commit 92033fd

Please sign in to comment.