首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Openai获取Google中的完整文章

使用Openai获取Google中的完整文章
EN

Stack Overflow用户
提问于 2022-12-03 14:28:53
回答 2查看 38关注 0票数 1

我正在尝试使用Openai API在Google Sheet中获得完整的文章。在A栏中,我只提到这个主题,希望在B栏中得到完整的文章。

以下是我正在尝试的

代码语言:javascript
复制
    /**
 * 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.';
  }
}

我怎样才能拿到这篇文章?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74667621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档