Workflows Send HTTP Request and ChatGPT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lj4353
    Senior Member
    • Nov 2023
    • 114

    Workflows Send HTTP Request and ChatGPT

    I am trying to use Workflows Send HTTP Request to send a message to ChatGPT. How can I get the response from ChatGPT?

    Here is an example link for GTP.
    HTML Code:
    var response = UrlFetchApp.fetch('https://api.openai.com/v1/chat/completions', {
    method: 'post',
    headers: {
    'Authorization': 'Bearer ' + OPENAI_API_KEY,
    'Content-Type': 'application/json'
    },
    payload: JSON.stringify({
    'model': 'gpt-3.5-turbo',
    'messages': [
    {'role': 'system', 'content': '以下の文章に「外国籍可」を示す内容が含まれていますか?'},
    {'role': 'user', 'content': text},
    {'role': 'system', 'content': '回答は「はい」または「いいえ」で答えてください。'}
    ],
    'max_tokens': 10,
    'temperature': 0.2
    }),
    muteHttpExceptions: true
    });
    var json = JSON.parse(response.getContentText());
    Logger.log(json);
    return json.choices && json.choices[0] && json.choices[0].message.content.trim() === 'はい' && !text.includes('外国籍:不可');​
    Click image for larger version  Name:	image.png Views:	0 Size:	61.3 KB ID:	108242
  • lj4353
    Senior Member
    • Nov 2023
    • 114

    #2
    I tried using the following prompts:
    Hello friends, I have a JSON file with this information. When I received the file, I wanted to create leads with the information from this JSON content, but I see that it only created a single line with the first ID json [ { "userId": "4", "lastname": "TEST", "firstname"


    $response is equal to the following value:
    HTML Code:
    {
        "id": "chatcmpl-9iwcaaaGbwtDtmfho6rVtY0VziK",
        "object": "chat.completion",
        "created": 1720498811,
        "model": "gpt-3.5-turbo-0125",
        "choices": [
            {
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": "可"
                },
                "logprobs": null,
                "finish_reason": "stop"
            }
        ],
        "usage": {
            "prompt_tokens": 233,
            "completion_tokens": 1,
            "total_tokens": 234
        },
        "system_fingerprint": null
    }​
    However, $choices = json\retrieve($response, 'choices'); is empty. json\retrieve does not seem to work. My version is Version 8.3.4.​
    Click image for larger version

Name:	image.png
Views:	175
Size:	57.7 KB
ID:	108244

    Comment

    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #3
      Choices is an array, you need to access values within the array. try something as below:

      PHP Code:
       $choices = json\retrieve($response, 'choices.0.message.0.content') 
      

      The message is a collection of objects so you need to access first / last etc element then access value.
      Rabii
      Web Dev

      Comment


      • lj4353
        lj4353 commented
        Editing a comment
        Thanks, he's working normally now

        $response = workflow\lastHttpResponseBody();

        // 获取第一个choice中的message.content
        $choices = json\retrieve($response, 'choices');

        if (array\length($choices) > 0) {
        $firstChoice = array\at($choices, 0);
        $message = object\get($firstChoice, 'message');
        $content = object\get($message, 'content');
        entity\setAttribute('body', $content);
        }
    Working...