gpt4 book ai didi

azure - Azure OpenAI API 的 Google Sheet Apps 脚本自定义函数

转载 作者:行者123 更新时间:2023-12-03 03:23:00 24 4
gpt4 key购买 nike

我在 Google App 脚本中创建了一个自定义函数来调用 OpenAI API。它运行正常,但我目前正在过渡到 Azure OpenAI API 服务,以提高性能和稳定性。但是,我在尝试根据 Azure OpenAI 说明重新编码 Apps 脚本时遇到了问题。当前脚本版本如下:

const api_key = "azure openai api key1";  
const max_tokens = 500;

function gpt(userprompt, userrole = "user", temperature = 1) {

const gptprompt = [{role:userrole,content:userprompt},
];

const url = "azure openai endpoint";
const payload = {
engine:"gpt-35-turbo",
messages: gptprompt,
temperature: temperature,
max_tokens: max_tokens,
};

// Use 'method' property with value 'POST' instead of default 'GET' method
const options = {
method: "POST",
contentType: "application/json",
headers: { Authorization: "Bearer " + api_key },
payload: JSON.stringify(payload),
muteHttpExceptions: true
};

try {
const response = UrlFetchApp.fetch(url, options);
console.log(response.getContentText());
const results = JSON.parse(response.getContentText());
console.log(results);
const text = results?.choices?.[0]?.message?.text?.trim() ?? "No text generated"; // Update this line to use 'choices' and 'message'
console.log(text);
return text;
} catch (error) {
// Log error message and return a custom error message
console.error("Error: ", error);
return "Error: Unable to fetch data.";
}
}

但根据日志返回错误:

6:52:23AM 通知执行开始

6:52:23AM Info { "statusCode": 401, "message": "未经授权。访问 token 丢失、无效、受众不正确 ( https://cognitiveservices.azure.com ) 或已过期。"}

6:52:23AM 信息 { statusCode: 401, message: '未经授权。访问 token 丢失、无效、受众不正确 ( https://cognitiveservices.azure.com ) 或已过期。 }

6:52:23AM 信息 未生成任何文本 6:52:24AM 通知执行已完成

我已经检查了 API key 和端点在 python 中是否工作我错过了应用程序脚本中的某些部分吗?

最佳答案

根据 Azure 指南,API key 的 header 已更改。

所以,尝试“api-key: YOUR_API_KEY”

而不是“授权:持有者 YOUR_API_KEY”

#Note: The openai-python library support for Azure OpenAI is in preview.

import os
import openai
openai.api_type = "azure"
openai.api_base = "YOUR_API_ENDPOINT"
openai.api_version = "2023-03-15-preview"
openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.ChatCompletion.create(
engine="YOUR_MODEL_NAME_DEPLOYED",
messages = [{"role":"system","content":"You are an AI assistant that helps people find information."},{"role":"user","content":"Hi"},{"role":"assistant","content":"Hi! What can I do for you?"}],
temperature=0.7,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None)

`

关于azure - Azure OpenAI API 的 Google Sheet Apps 脚本自定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76444286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com