matlab.net vs webwrite

12 views (last 30 days)
Sergio Oliveira
Sergio Oliveira on 22 May 2023
Edited: Hans Scharler on 15 Sep 2023
Why the function not commented works perfectly, but the function commented always returns error 401 ?
Both use the same <API_KEY>
function [result,tokens] = getGPT4Response(content)
import matlab.net.*
import matlab.net.http.*
% Define the API endpoint
api_endpoint = "https://api.openai.com/v1/chat/completions";
% Define the API key from https://beta.openai.com/account/api-keys
api_key = "<API_KEY>";
data = jsondecode('{"model":"gpt-3.5-turbo","messages":[{"role":"system","content":"You are an assistant."},{"role":"user","content":"'+content+'"}]}');
% Define the headers for the API request
headers(1) = 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,data);
% Send the request and store the response
response = send(request, URI(api_endpoint));
% Extract the response text
result = response.Body.Data.choices.message.content;
tokens = response.Body.Data.usage;
end
%**********************************************************************************************************
% function response = getGPT4Response(prompt)
% % Define the API endpoint
% url = 'https://api.openai.com/v1/chat/completions'; % Replace with the actual GPT-4 API endpoint when available
%
% % Define the headers
% headers = ["Content-Type:", "application/json"; "Authorization:", "<API_KEY>"];
%
% % Define the data
% message = containers.Map({'role', 'content'}, {'user', prompt});
% data = containers.Map({'model', 'messages'}, {'gpt-3.5-turbo', {message}});
%
% % Convert the data to JSON
% data = jsonencode(data);
%
% % Send the POST request
% options = weboptions('RequestMethod', 'post', 'HeaderFields', headers, 'MediaType', 'application/json');
% response = webwrite(url, data, options);
%
% % Parse the response
% response = jsondecode(response);
%
% % Extract the text
% response = response.choices.text;
% end
%

Accepted Answer

Hans Scharler
Hans Scharler on 15 Sep 2023
Edited: Hans Scharler on 15 Sep 2023
There were three issues with the commented function.
  1. When you use headers for webwrite you do not have to include the ":" in the header label.
  2. The Authorization header needs the word "Bearer" infront of the API Key.
  3. webwrite returns an object not JSON.
Here's a working version of the function addressing the issues.
function response = getGPT4Response(prompt)
% Define the API endpoint
url = 'https://api.openai.com/v1/chat/completions';
% Define the headers
headers = ["Content-Type", "application/json"; "Authorization", "Bearer <API_KEY>"];
% Define the data
message = containers.Map({'role', 'content'}, {'user', prompt});
data = containers.Map({'model', 'messages'}, {'gpt-3.5-turbo', {message}});
% Convert the data to JSON
data = jsonencode(data);
% Send the POST request
options = weboptions('RequestMethod', 'post', 'HeaderFields', headers);
response = webwrite(url, data, options);
% Return the response
response = response.choices.message.content;
end
Let me know if you get tihs working.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!