如何在Google Chrome扩展程序中调用ChatGPT API?

huangapple go评论122阅读模式
英文:

How can I call the chatgpt api in Google Chrome Extension?

问题

我想制作一个可以调用ChatGPT API的Chrome扩展程序。但我还没有看到OpenAI开放API的消息。

我看到一些相关的Chrome扩展程序实现了这样的技术,比如这篇博客
https://www.extspy.com/blog/List-of-10-Best-Chatgpt-Chrome-Extensions-for-2023

有人可以告诉我如何做吗?

英文:

I want to make a chrome extension that can call chatgpt api.
But I haven't seen openai open the api.

I see that the corresponding chrome extension implements such technology, such as this blog
https://www.extspy.com/blog/List-of-10-Best-Chatgpt-Chrome-Extensions-for-2023

Can anyone tell me how to do it?

答案1

得分: 0

使用Fetch API发送请求到OpenAI API。

首先,你需要先在OpenAI注册获取API密钥:
https://openai.com/api/

要向GPT发送问题,你可以使用以下的Fetch API调用。

const response = await fetch('https://api.openai.com/v1/completions', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization: Bearer YOUR_API_KEY'
  },
  body: {
    model: 'text-davinci-003',
    prompt: '如何在Chrome扩展程序中进行API调用到OpenAI?',
    max_tokens: 2000
  }
})

console.log(response.data.choices[0].text)

参考链接:https://platform.openai.com/docs/api-reference/completions

英文:

Use the fetch API to send a request to the OpenAI API.

You will need to signup for an API key with OpenAI first:
https://openai.com/api/

To send a question to GPT you would make the following Fetch API call.

const response = await fetch('https://api.openai.com/v1/completions', {
  method: 'POST',
  headers: {
    'Content-Type: application/json',
    'Authorization: Bearer YOUR_API_KEY'
  },
  body: {
    model: 'text-davinci-003',
    prompt: 'How do I make an API call to OpenAI inside a Chrome Extension?',
    max_tokens: 2000
  }
})

console.log(response.data.choices[0].text)

Ref: https://platform.openai.com/docs/api-reference/completions

huangapple
  • 本文由 发表于 2023年2月10日 15:38:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定