Connecting to ChatGPT using API

991 ビュー (過去 30 日間)
John Adams
John Adams 2023 年 1 月 15 日
コメント済み: tony 2023 年 9 月 10 日 13:59
I tried connecting to Chat GPT with the following web instructions and get the error as reflected ...
prompt = 'What is the capital of France?';
api_key = 'sk-4Y8TmelxvsdfghfghhdT3BlbkFJepdojXzket1MmQpA9cov';
url = '';
options = weboptions('KeyName','Authorization','KeyValue',['Bearer ' api_key],'MediaType','application/json');
data = webwrite(url,'prompt',prompt,'max_tokens',2048,'model','text-davinci-003','stop','',options);
Error using webwrite
Expected options.MediaType to be 'application/x-www-form-urlencoded' for Name-Value pairs. Either set options.MediaType to 'application/x-www-form-urlencoded' or create a single encoded string from
the Name-Value pairs.
answer = loadjson(data);
answer = answer.choices{1}.text;
Does anyone know how to connect MATLAB to Chat GPT to send prompts and retrieve back the responses. Also how to send data over to fine tune the model and then to further prompt that model?
It would be good for Mathworks to double quick release a toolbox on FileExchange for this as Python already has an OpenAI library. Seems like Mathworks is always one step behind Python these days.
  3 件のコメント
Wii Che
Wii Che 2023 年 2 月 19 日
% Extract the response text
response_text = response.Body.Data;
response_text = response_text.choices(1).text;
There is an error, is it a version problem



Hans Scharler
Hans Scharler 2023 年 1 月 23 日
I have been researching the davinci/completions space and have working MATLAB code using net http. Get your API Key at OpenAPI:
% Define the API endpoint Davinci
api_endpoint = "";
% Define the API key from
api_key = "XXXYYYZZZ";
% Define the parameters for the API request
prompt = "How many tablespoons are in 2 cups?"
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;
  8 件のコメント
Georgi Nalbantov
Georgi Nalbantov 2023 年 6 月 18 日
Sometimes one gets a "10 sec" timeout error and therefore no response. One fix for this is:
% Send the request and store the response.
opt = 2;
if opt == 1 % org
response = send(request, URI(api_endpoint));
options = HTTPOptions;
options.ConnectTimeout = 60;
response = send(request, URI(api_endpoint),options);
The original value is: options.ConnectTimeout = 10;


その他の回答 (3 件)

Duncan Carlsmith
Duncan Carlsmith 2023 年 2 月 23 日
See Generate MATLAB code using ChatGPT API at for a Live Script that implements the great answers here. Thank you all so much!
You get different code for the same prompt each time. For the prompt used, there is often a minor bug. Great for teaching debugging skills. ;) I'll let you find the bugs in the example I provided.
  2 件のコメント
Duncan Carlsmith
Duncan Carlsmith 2023 年 2 月 24 日
Thanks for checking it out!


thijs buuron
thijs buuron 2023 年 1 月 20 日
If you have the curl command (get it from the network part of the console of you webbrowser). You could use: wich converts it to matlab code for you.
  2 件のコメント
tony 2023 年 9 月 10 日 13:59
thank you
not working for a longer quesion


henok 2023 年 6 月 1 日
write a matlab code EE vs no of users on EE analysis of 5g udn based massive MIMO using ZF


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