matlab.net vs webwrite

12 vues (au cours des 30 derniers jours)
Sergio Oliveira
Sergio Oliveira le 22 Mai 2023
Modifié(e) : Hans Scharler le 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
%

Réponse acceptée

Hans Scharler
Hans Scharler le 15 Sep 2023
Modifié(e) : Hans Scharler le 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.

Plus de réponses (0)

Catégories

En savoir plus sur Call Web Services from MATLAB Using HTTP dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by