How to integrate ChatGPT into your website
Ping Pong Labs GmbH
Web & app development / development of cloud-based platforms (with AWS)
Project
The following steps describe how we can access the OpenAI API (GPT-3 engine) with a simple HTTP request.
The example uses Node.js and is written in JavaScript (ECMAScript 6). It does not require any OpenAI specific dependencies and can be easily transferred into other languages like Java, Python or C#. Other examples, using language specific dependencies from OpenAI, can be found here: https://beta.openai.com/docs/quickstart/build-your-application
The example uses AWS resources, but can also be tested without AWS (see last section).
1. Create OpenAI API key:
Log in or sign up at https://beta.openai.com
Create a new OpenAI API key at https://beta.openai.com/account/api-keys
Don’t reveal keys (and other secrets) in your code or store them in your code repository. Secrets should always be stored encrypted in a key vault. There are many options to store keys securely. Some very popular vaults are HashiCorp Vault, Azure Key Vault and AWS Secrets Manager.
In the following example we use AWS Secrets Manager to store the OpenAI API key. To follow this step you need an AWS account. If you don’t have or want to use AWS, you can remove the AWS dependency as described in Step 3. Log in to the AWS Web Console, go to Secrets Manager and click “Store a new Secret”. Choose “Other type of secret”, and type “Key” and your API Key in the Key/Value Fields. Save the Secret with the name “Test”.
2. The API Request
The OpenAI API accepts a POST request to https://api.openai.com/v1/completions. In our example we set four properties in the request body:
A complete list of available properties can be found here: https://beta.openai.com/docs/api-reference/completions/create#completions/create-model
Sample Request Body:
{
? ? ? ?"model": "text-davinci-003",
? ? ? ?"prompt": "Tell me a nice story",
? ? ? ?"max_tokens": 64,
? ? ? ?"temperature": 0.2
? ?}
The actual answer from ChatGPT is included in the API response body as plain text (text property):
?
{
??
"id": "cmpl-6UZ...",
???"object": "text_completion",
???"created": 1672745240,
???"model": "text-davinci-003",
???"choices": [
???????{
???????????"text": "\n\nSome nice answer text from GPT",
???????????"index": 0,
???????????"logprobs": null,
???????????"finish_reason": "length"
???????}
???],
???"usage": {
???????"prompt_tokens": 6,
???????"completion_tokens": 64,
???????"total_tokens": 70
???}
}
领英推荐
3. Working example (Node.js)
The following example fetches the API key from AWS Secrets Manager and calls the OpenAI API using axios library:
import { SecretsManagerClient, GetSecretValueCommand } from "@aws-sdk/client-secrets-manager"
import axios from 'axios';
?
// Get decrypted API key from AWS Secrets Manager
const getSecret = async () => {
?
???const secretId = "Test";
???const secretKey = "Key";
?
???const client = new SecretsManagerClient({
???????region: "eu-central-1",
???});
?
???try {
???????const response = await client.send(
???????????new GetSecretValueCommand({
???????????????SecretId: secretId,
???????????????VersionStage: "AWSCURRENT"
???????????})
???????);
???????return JSON.parse(response.SecretString)[secretKey];
???} catch (err) {
???????console.log(err);
???????throw err;
???}
}
?
// Call OpenAI API using API key and question
// Returns the answer from OpenAI as a string
const callGPT = async (key, question) => {
?
???var data = JSON.stringify({
???????"model": "text-davinci-003",
???????"prompt": question,
???????"max_tokens": 64,
???????"temperature": 0.2
???});
?
???const url = 'https://api.openai.com/v1/completions';
?
???try {
???????const result = await axios
???????????.post(url, data, {
???????????????headers: {
???????????????????'Authorization': 'Bearer ' + key,
???????????????????'Content-Type': 'application/json'
???????????????}
???????????});
?
???????return JSON.stringify(result.data.choices[0].text);
???} catch (err) {
???????console.log(err);
???????throw err;
???}
};
?
// Entry point
export const handler = async (event) => {
?
???const question = "Tell me a nice story";
?
???return {
???????statusCode: 200,
???????body: JSON.stringify({ answer: await callGPT(await getSecret(), question) }),
???};
};
The code above should be hosted on AWS (e.g. on EC2 or Lambda) to use the benefits of Secrets Manager.
For local testing without using AWS, you can remove the dependency to AWS Secrets Manager:
import { SecretsManagerClient, GetSecretValueCommand } from "@aws-sdk/client-secrets-manager";
and replace the content of getSecret() function to just return your OpenAI API key:
const getSecret = async () => {
???return "[YOUR_API_KEY]";
}
The example is just a basic implementation without proper error handling and protection against abuse.
It only demonstrates how easily ChatGPT can be used in a custom application.
About Ping Pong Labs GmbH
Ping Pong Labs is a digital agency in Berin.
Our Services:
Software Expert and Innovator | Building the future one line of code at at time.
1 年If you prefer an out-of-the-box solution, EmbedGPT.chat (https://EmbedGPT.chat) is a great option for embedding customized AI chats into websites. It allows you to have unlimited chats with different setups for different purposes. It can also transfer customer requests to a human rep when needed, acting as an intelligent “Contact Us” form. The setup, including the chat prompt, is completely automatic and it can refresh itself with new data periodically. You can create and customize your chat in seconds and embed it on your website with a simple code snippet. It also provides Wordpress plugin, which makes the setup even easier for Wordpress sites. You can also have standalone Chat Pages - it's like instant Web sites with embedded AI chat.
Senior R&D Manager at QNAP Systems
1 年Hi! I came across your article and wanted to let you know that Bing cited it as a source for my question about embedding.
Business Intelligence Analyst at Provincial Health Services Authority
1 年thank you for demonstrating the ChatGPT API. Looking forward to integrate more Cloud service other than AWS and Azure ( https://www.udemy.com/course/chatgpt-build-solutions-and-apps-with-chatgpt-and-openai/ )
Senior Software Developer at Mediengruppe RTL Deutschland
1 年Pretty good tutorial. If you use https://www.npmjs.com/package/openai it's even a bit more convenient.