Reputation: 23
I would like to integrate chatgpt into sas, but when I execute the code I get the error shown in the image.
The error message that is shown is> Invalid-COntent Type header. Expexted application/json.
%let chatgpt_api_token = sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;
%let chatgpt_api_url = "https://api.openai.com/v1/chat/completions";
%let chat_prompt = "Hello, how can I assist you?";
/* Define the SAS macro to interact with the ChatGPT API */
%macro chat_with_gpt(prompt);
/* Prepare the JSON payload for the API request */
filename payload temp;
data null;
file payload;
put '{ "prompt": "' &prompt '", "max_tokens": 50, "model": "gpt-3.5-turbo" }';
run;
/* Submit the API request using PROC HTTP */
filename response temp;
proc http
method="POST"
url=&chatgpt_api_url
in=payload
out=response
headerout=header;
headers "Authorization" = "Bearer &chatgpt_api_token"
"Content-Type" = "application/json";
run;
/* Read the API response and save it to a SAS dataset */
data chatgpt_response;
infile response;
input;
put infile;
/* Parse the JSON response and store it in a variable */
response = infile;
run;
/* Display the API response in the SAS log */
proc print data=chatgpt_response;
run;
%mend;
/* Call the macro to initiate a chat with ChatGPT */
%chat_with_gpt(&chat_prompt);
Tried different solution
Upvotes: 1
Views: 190
Reputation: 27508
There are a variety of issues. In particular, the chat/completion endpoint should be delivered the 'prompt' via a "messages" item.
Try this complete example:
Your double quoting of the prompt has been replaced with macro encapslating %str()
.
%include "D:\GPTKeyForSAS.txt";
/* Api documentation for chat/completions:
* https://platform.openai.com/docs/api-reference/chat/create
*/
/* Define the SAS macro to interact with the ChatGPT API */
%macro chat_with_gpt(prompt, completer="https://api.openai.com/v1/chat/completions");
/* Prepare the JSON payload for the API request */
filename payload temp;
data null;
file payload;
prompt = symget('prompt');
put '{ "messages": [{"role":"user", "content":' prompt: $quote5000. '}]'
/ ', "max_tokens": 100'
/ ', "model": "gpt-3.5-turbo"'
/ '}';
run;
data _null_;
infile payload lrecl=32767;
input;
putlog _infile_;
run;
%local options;
%let options = %sysfunc(getoption(mprint)) %sysfunc(getoption(symbolgen));
options nosymbolgen nomprint;
/* Submit the API request using PROC HTTP */
filename response 'c:\temp\chatgpt-response.json';
filename resphdr temp;
proc http
method="POST"
url=&completer
in=payload
out=response
headerout=resphdr
;
headers "Authorization" = "Bearer &chatgpt_api_key"
"Content-Type" = "application/json";
run;
options &options;
/* Log the response header */
data _null_;
put 'NOTE: ---------------';
put 'NOTE: Response header';
put 'NOTE: ---------------';
do while (not done);
infile resphdr end=done;
input;
putlog _infile_;
end;
stop;
run;
/* Log and store the API response in a data set */
proc delete data=chatgpt_response;
data chatgpt_response;
timestamp = datetime(); format timestamp datetime19.;
putlog 'NOTE: --------';
putlog 'NOTE: Response @' timestamp;
putlog 'NOTE: --------';
do while (not done);
infile response end=done lrecl=32767;
input;
putlog _infile_;
response = _infile_;
output;
end;
stop;
run;
libname parse json "%sysfunc(pathname(response))";
proc copy inlib=parse outlib=work;
run;
ods html file='response-content.html';
proc print data=choices_message;
var content / style=[asis=yes];
run;
ods html close;
%mend;
%chat_with_gpt( %str(Write me a haiku about SAS programming) )
Upvotes: 0
Reputation: 27508
Did you inspect the payload you created?
The resolution of the macro variable in the context of the put
statement will not double not the value of the json option prompt
.
Try
%let chat_prompt = '"Hello, how can I assist you?"';
Upvotes: 0