sas - ChatGPT API 在 SAS 中集成时出错。无效的上下文类型 header

标签 sas openai-api chatgpt-api

我想将chatgpt集成到sas中,但是当我执行代码时,出现如图所示的错误。

显示的错误消息是> Invalid-COntent Type header。扩展的 application/json。

enter image description here

%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);

尝试了不同的解决方案

最佳答案

您检查过您创建的有效负载吗?

put 语句上下文中宏变量的解析不会加倍,而不是 json 选项 prompt 的值。

尝试

%let chat_prompt = '"Hello, how can I assist you?"';

关于sas - ChatGPT API 在 SAS 中集成时出错。无效的上下文类型 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76297924/

相关文章:

SAS - 在同一数据集的不同 ID 变量中逐行比较并删除所有重复项

next.js - OpenAI GPT-3 API 错误 : 'text' does not exist TS(2339) & 'prompt' does not exist on type 'CreateChatCompletion' TS(2345)

python - 无效请求错误 : Must provide an 'engine' parameter while invoking openAI API for text generation

token - 聊天GPT : How to use long texts of unknown content in a prompt?

openai-api - 属性错误: module ‘openai’ has no attribute ‘FineTuneingJob’

python - LangChain python - 在提交给法学硕士之前提取大量 secret 文本的能力

csv - 如何在 SAS 中导入多行 CSV

sas - 输入 YYYY-mm-dd hh :mm:ss' in SAS 的正确命令

sas - 在 SAS EG 中计算工作日

discord.js - Discord GPT-3.5-Turbo 抛出未定义的错误