当前位置: 首页 > article >正文

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 of https://api.openai.com/v1/models might be required to be used. Ref And, there is no property of json.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.

Reference:

  • Completions of OpenAI API


http://www.kler.cn/news/290552.html

相关文章:

  • Python知识点:如何使用Mock库进行单元测试中的依赖模拟
  • Linux查看系统用户
  • 海康二次开发笔记10-独立Group导入、导出及执行
  • HTTP代理支持UDP协议吗?
  • ROS imu传感器节点
  • 第十二章节 xxjob, seata, zk, minio,activeMQ进行 helm化
  • 【boost库概述+应用场景】
  • vue 的diff算法原理
  • superMap mapboxgl初始化地图时,地图旋转api与设置地图中心api,同时进行无法完成实现效果
  • 【Unity案例】搭建射击系统与UI
  • C#预处理器指令
  • 1.Docker初探 —— 走进集装箱的世界
  • upload-labs通关详解
  • C#指针(内存地址)IntPtr
  • Windows中Git对文件名大小写不敏感的问题解决方法
  • Unity数据持久化 之 二进制存储法
  • 设计模式之适配器模式:软件世界的桥梁建筑师
  • 【系统架构设计师-2019年】综合知识-答案及详解
  • ts 类型分类
  • 在安卓和Windows下使用Vizario H264 RTSP
  • 数据库系统 第28节 数据库迁移 案例分析
  • 2011年
  • pr瘦脸怎么操作?
  • css设置让整个盒子的内容渐变透明(非颜色渐变透明)
  • Fine3399或rk3399\sw799刷armbian创建热点
  • 精益工程师资格证书:2024年CLMP报名指南
  • sql-labs51-55通关攻略
  • 【Pandas】Pandas日常工作的常用操作大全
  • Go 语言版本管理——Goenv
  • 探索淘宝拍立淘API:解锁以图搜图的购物新体验