英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论