ChatGPT is an AI-powered chatbot developed by OpenAI. It uses natural language processing technology to generate intelligent, personalized responses to user queries in real-time. It combines the power of a neural network with the natural conversational techniques used by real people.

The above was written using ChatGPT.

In this article we will use Terraform (Infrastructure as Code) to query ChatGPT and receive responses. We will use Develeap‘s provider to accomplish this.

We will use the same base path of ‘dev’ that was previously created and use ~/.local/bin for certain binaries.

Please Sign up to OpenAI’s ChatGPT here.

–>
Go in to the dev directory/link located within your home directory:

$ cd ~/dev

Grab/Update to the latest version of Terraform:

$ wget https://releases.hashicorp.com/terraform/1.4.2/terraform_1.4.2_linux_amd64.zip

Install Unzip if you do not have it installed:

$ sudo apt update && sudo apt -y install unzip

Unzip it to ~/.local/bin and set permissions accordingly on it (type y and hit enter to replace if upgrading, at the prompt):

$ unzip terraform_1.4.2_linux_amd64.zip -d ~/.local/bin && chmod 754 ~/.local/bin/terraform

Create a Terraform work folder and change in to the base path:

$ mkdir -p terraform/chatgpt && cd terraform/chatgpt

Pin the Terraform version to greater then or equal to 1.4:

$ cat << 'EOF' > versions.tf
> terraform {
>   required_version = ">= 1.4.0"
> }
> EOF

Set query as a variable and assign it a default value:

$ cat << 'EOF' > vars.tf
> variable "query" {
>   default = "What is ChatGPT?"
> }
> EOF

Add the ChatGPT provider from Develeap:

$ cat << 'EOF' > provider.tf
> terraform {
>   required_providers {
>     chatgpt = {
>       version = "0.0.1"
>       source  = "develeap/chatgpt"
>     }
>   }
> }
>
> provider "chatgpt" {
>   # CHATGPT_API_KEY="<API key>" terraform apply -auto-approve
> }
> EOF

Add the ChatGPT resource:

$ cat << 'EOF' > chatgpt.tf
> resource "chatgpt_prompt" "query" {
>   max_tokens = 256
>   query      = "${var.query}"
> }
> EOF

Output the response to our query:

$ cat << 'EOF' > output.tf
> output "query_result" {
>   value = chatgpt_prompt.query.result
> }
> EOF

‘Create a new secret key’ here and take note of it.

Initialize the Terraform directory:

$ terraform init

Run the default query (replace <API key> with the API key you received from ‘Create a new secret key’):

$ CHATGPT_API_KEY="<API key>" terraform apply -auto-approve

It should return:

Outputs:

query_result = "ChatGPT is an AI-powered chatbot developed by OpenAI. It uses natural language processing technology to generate intelligent, personalized responses to user queries in real-time. It combines the power of a neural network with the natural conversational techniques used by real people."

Ask it a question (replace <API key> with the API key you received from ‘Create a new secret key’):

$ CHATGPT_API_KEY="<API key>" terraform apply -var "query=What is Droid Basement?" -auto-approve

It should return:

Outputs:

query_result = "Droid Basement is a website dedicated to providing Android users with tutorials on rooting, ROMs and other custom development tasks. The site also offers popular downloads, forums, and articles related to Android development."

You can set your API key in an environment variable so it (CHATGPT_API_KEY) does not need to be specified when executing ‘terraform’ (replace <API key> with the API key you received from ‘Create a new secret key’):

$ export CHATGPT_API_KEY="<API key>"

To unset the environment variable:

$ unset CHATGPT_API_KEY

<–

Source:

terraform-provider-chatgpt

« »