Get full article in Google Sheet using Openai
题意:将整篇文章导入Google表格中,使用OpenAI。
问题背景:
I'm trying to get full article in Google Sheet using Openai API. In column A I just mention the topic and want to get full article in column B.
我正在尝试使用 OpenAI API 将整篇文章导入 Google 表格。在 A 列中,我只提到主题,并希望在 B 列中获取完整的文章。
Here is what I'm trying
这是我正在尝试的内容。
/**
* Use GPT-3 to generate an article
*
* @param {string} topic - the topic for the article
* @return {string} the generated article
* @customfunction
*/
function getArticle(topic) {
// specify the API endpoint and API key
const api_endpoint = 'https://api.openai.com/v1/completions';
const api_key = 'YOUR_API_KEY';
// specify the API parameters
const api_params = {
prompt: topic,
max_tokens: 1024,
temperature: 0.7,
model: 'text-davinci-003',
};
// make the API request using UrlFetchApp
const response = UrlFetchApp.fetch(api_endpoint, {
method: 'post',
headers: {
Authorization: 'Bearer ' + api_key,
'Content-Type': 'application/json',
},
payload: JSON.stringify(api_params),
});
// retrieve the article from the API response
const json = JSON.parse(response.getContentText());
if (json.data && json.data.length > 0) {
const article = json.data[0].text;
return article;
} else {
return 'No article found for the given topic.';
}
}
How can I get the article?
我该如何获取这篇文章?
问题解决:
Modification points: 修改要点:
- When I saw the official document of OpenAI API, in your endpoint of
https://api.openai.com/v1/completions
, it seems that the following value is returned. Ref
当我查看OpenAI API的官方文档时,在你们的 `https://api.openai.com/v1/completions` 端点中,似乎返回了以下值。参考如下。
{
"id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
"object": "text_completion",
"created": 1589478378,
"model": "text-davinci-003",
"choices": [
{
"text": "\n\nThis is indeed a test",
"index": 0,
"logprobs": null,
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 5,
"completion_tokens": 7,
"total_tokens": 12
}
}
-
In the case of
json.data
, it seems that the endpoint ofhttps://api.openai.com/v1/models
might be required to be used. Ref And, there is no property ofjson.data[0].text
.
在 `json.data` 的情况下,似乎需要使用 `https://api.openai.com/v1/models` 这个端点。参考如下。此外,`json.data[0].text` 属性是不存在的。
I thought that this might be the reason for your current issue. If you want to retrieve the values of text
from the endpoint of https://api.openai.com/v1/completions
, how about the following modification?
我认为这可能是您当前问题的原因。如果您想从 `https://api.openai.com/v1/completions` 端点中检索 `text` 的值,那么以下修改如何?
From:
if (json.data && json.data.length > 0) {
const article = json.data[0].text;
return article;
} else {
return 'No article found for the given topic.';
}
To:
if (json.choices && json.choices.length > 0) {
const article = json.choices[0].text;
return article;
} else {
return 'No article found for the given topic.';
}
Note:
如果 `response.getContentText()` 的值不是您期望的,那么这个修改可能无法使用。请对此保持警惕。
-
- If the value of
response.getContentText()
is not your expected values, this modification might not be able to be used. Please be careful about this.
- If the value of
Reference:
- Completions of OpenAI API