Truncated answers when querying chatgpt with Matlab
조회 수: 1 (최근 30일)
이전 댓글 표시
Some users (like here for example: https://www.mathworks.com/matlabcentral/answers/1894530-connecting-to-chatgpt-using-api ) 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
import matlab.net.*
import matlab.net.http.*
% Define the API endpoint Davinci
api_endpoint = "https://api.openai.com/v1/engines/text-davinci-003/completions";
% Define the API key from https://beta.openai.com/account/api-keys
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 = matlab.net.http.HeaderField('Content-Type', 'application/json');
headers(2) = matlab.net.http.HeaderField('Authorization', ['Bearer ' + api_key]);
% Define the request message
request = matlab.net.http.RequestMessage('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;
disp(response_text)
Its not immediately clear if the problem lies with the code above or with chatgpt. Has anyone experienced truncated answers from chatgpt ?
댓글 수: 0
답변 (2개)
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);
댓글 수: 0
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
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!