Truncated answers when querying chatgpt with Matlab

6 ビュー (過去 30 日間)
012786534 2023 年 3 月 24 日
回答済み: Hans Scharler 2023 年 9 月 16 日
Some users (like here for example: ) have pointed out that we can ask chatgpt to write Matlab code.
I was curious and I have been playing with the code below a bit but I seem to get truncated annswers
% Define the API endpoint Davinci
api_endpoint = "";
% Define the API key from
api_key = "your_api_key";
prompt = "Matlab code to replace string in multiple text files";
parameters = struct('prompt', prompt, 'max_tokens', 100);
% Define the headers for the API request
headers ='Content-Type', 'application/json');
headers(2) ='Authorization', ['Bearer ' + api_key]);
% Define the request message
request ='post', headers, parameters);
% Send the request and store the response
response = send(request, URI(api_endpoint));
% Extract the response text
response_text = response.Body.Data;
response_text = response_text.choices(1).text;
Its not immediately clear if the problem lies with the code above or with chatgpt. Has anyone experienced truncated answers from chatgpt ?

回答 (2 件)

Hans Scharler
Hans Scharler 2023 年 9 月 16 日
The "max_tokens" parameter limits the response from OpenAI language models.
Try increasing it to 1000.
parameters = struct('prompt', prompt, 'max_tokens', 1000);

the cyclist
the cyclist 2023 年 3 月 24 日
My impression from searching the same topic is that there is an unpublished token limit (which will roughly translate to a character limit) for both input and output to ChatGPT. (This is not MATLAB specific.)
  2 件のコメント
012786534 2023 年 3 月 24 日
Perhaps but I have read that chatgpt has a character limit of 2048 characters per response, which is far more than I get when querying with Matlab. In the example above, I get a complete answer when I ask the same question directly on Introducing ChatGPT (
the cyclist
the cyclist 2023 年 3 月 24 日
Looking at your code a bit more closely ...
Did you realize that this line
parameters = struct('prompt', prompt, 'max_tokens', 100);
is setting the maximum token value that your code is asking for? Set that higher.



Help Center および File ExchangeCall Web Services from MATLAB Using HTTP についてさらに検索





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by