{"id":6455,"date":"2023-03-23T00:03:27","date_gmt":"2023-03-23T04:03:27","guid":{"rendered":"https:\/\/droidbasement.com\/db-blog\/?p=6455"},"modified":"2023-03-24T07:43:57","modified_gmt":"2023-03-24T11:43:57","slug":"chatgpt-terraform-send-queries-and-receive-responses-using-infrastructure-as-code","status":"publish","type":"post","link":"https:\/\/droidbasement.com\/db-blog\/chatgpt-terraform-send-queries-and-receive-responses-using-infrastructure-as-code\/","title":{"rendered":"ChatGPT\/Terraform \u2013 Send queries and receive responses using Infrastructure as Code"},"content":{"rendered":"\n<p><a href=\"https:\/\/openai.com\/blog\/chatgpt\" target=\"_blank\" rel=\"noreferrer noopener\">ChatGPT<\/a> is an AI-powered chatbot developed by <a href=\"https:\/\/openai.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">OpenAI<\/a>. 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.<\/p>\n\n\n\n<p><em>The above was written using ChatGPT.<\/em><\/p>\n\n\n\n<p>In this article we will use <a href=\"https:\/\/terraform.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">Terraform<\/a> (Infrastructure as Code) to query ChatGPT and receive responses.  We will use <a href=\"https:\/\/www.develeap.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Develeap<\/a>&#8216;s provider to accomplish this.<\/p>\n\n\n\n<p>We will use the same base path of &#8216;dev&#8217; that was <a rel=\"noreferrer noopener\" href=\"https:\/\/droidbasement.com\/db-blog\/?p=5346\" target=\"_blank\">previously created<\/a> and use ~\/.local\/bin for certain binaries.<\/p>\n\n\n\n<p>Please Sign up to OpenAI&#8217;s ChatGPT <a href=\"https:\/\/platform.openai.com\/account\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>&#8211;&gt;<br>Go in to the dev directory\/link located within your home directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cd ~\/dev<\/code><\/pre>\n\n\n\n<p id=\"block-5e1e46e2-6124-4268-bd36-7f75ffb18609\">Grab\/Update to the latest version of Terraform:<\/p>\n\n\n\n<pre id=\"block-ff05e76f-6735-4568-bf96-05ed4ad2d75d\" class=\"wp-block-code\"><code>$ wget https:\/\/releases.hashicorp.com\/terraform\/1.4.2\/terraform_1.4.2_linux_amd64.zip<\/code><\/pre>\n\n\n\n<p id=\"block-47aa1fa9-a924-4dc7-af0b-fda117d6453b\">Install Unzip if you do not have it installed:<\/p>\n\n\n\n<pre id=\"block-c1eba1a0-4d39-470d-83a3-c5b8bc6c7bcd\" class=\"wp-block-code\"><code>$ sudo apt update &amp;&amp; sudo apt -y install unzip<\/code><\/pre>\n\n\n\n<p id=\"block-e2cd1bf1-74bc-46e6-bd83-c42ea9635d67\">Unzip it to ~\/.local\/bin and set permissions accordingly on it (type y and hit enter to replace if upgrading, at the prompt):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ unzip terraform_1.4.2_linux_amd64.zip -d ~\/.local\/bin &amp;&amp; chmod 754 ~\/.local\/bin\/terraform<\/code><\/pre>\n\n\n\n<p>Create a Terraform work folder and change in to the base path:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mkdir -p terraform\/chatgpt &amp;&amp; cd terraform\/chatgpt<\/code><\/pre>\n\n\n\n<p>Pin the Terraform version to greater then or equal to 1.4:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat &lt;&lt; 'EOF' &gt; versions.tf\n&gt; terraform {\n&gt;   required_version = \"&gt;= 1.4.0\"\n&gt; }\n&gt; EOF<\/code><\/pre>\n\n\n\n<p>Set query as a variable and assign it a default value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat &lt;&lt; 'EOF' &gt; vars.tf\n&gt; variable \"query\" {\n&gt;   default = \"What is ChatGPT?\"\n&gt; }\n&gt; EOF<\/code><\/pre>\n\n\n\n<p>Add the ChatGPT provider from Develeap:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat &lt;&lt; 'EOF' &gt; provider.tf\n&gt; terraform {\n&gt;   required_providers {\n&gt;     chatgpt = {\n&gt;       version = \"0.0.1\"\n&gt;       source  = \"develeap\/chatgpt\"\n&gt;     }\n&gt;   }\n&gt; }\n&gt;\n&gt; provider \"chatgpt\" {\n&gt;   # CHATGPT_API_KEY=\"&lt;API key&gt;\" terraform apply -auto-approve\n&gt; }\n&gt; EOF<\/code><\/pre>\n\n\n\n<p>Add the ChatGPT resource:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat &lt;&lt; 'EOF' &gt; chatgpt.tf\n&gt; resource \"chatgpt_prompt\" \"query\" {\n&gt;   max_tokens = 256\n&gt;   query      = \"${var.query}\"\n&gt; }\n&gt; EOF<\/code><\/pre>\n\n\n\n<p>Output the response to our query:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat &lt;&lt; 'EOF' &gt; output.tf\n&gt; output \"query_result\" {\n&gt;   value = chatgpt_prompt.query.result\n&gt; }\n&gt; EOF<\/code><\/pre>\n\n\n\n<p>&#8216;Create a new secret key&#8217; <a href=\"https:\/\/platform.openai.com\/account\/api-keys\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> and take note of it.<\/p>\n\n\n\n<p>Initialize the Terraform directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ terraform init<\/code><\/pre>\n\n\n\n<p>Run the default query (replace &lt;API key&gt; with the API key you received from &#8216;Create a new secret key&#8217;):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ CHATGPT_API_KEY=\"&lt;API key&gt;\" terraform apply -auto-approve<\/code><\/pre>\n\n\n\n<p>It should return:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Outputs:\n\nquery_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.\"<\/code><\/pre>\n\n\n\n<p>Ask it a question (replace &lt;API key&gt; with the API key you received from &#8216;Create a new secret key&#8217;):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ CHATGPT_API_KEY=\"&lt;API key>\" terraform apply -var \"query=What is Droid Basement?\" -auto-approve<\/code><\/pre>\n\n\n\n<p>It should return:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Outputs:\n\nquery_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.\"<\/code><\/pre>\n\n\n\n<p>You can set your API key in an environment variable so it (CHATGPT_API_KEY) does not need to be specified when executing &#8216;terraform&#8217; (replace &lt;API key&gt; with the API key you received from &#8216;Create a new secret key&#8217;):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ export CHATGPT_API_KEY=\"&lt;API key&gt;\"<\/code><\/pre>\n\n\n\n<p>To unset the environment variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ unset CHATGPT_API_KEY<\/code><\/pre>\n\n\n\n<p>&lt;&#8211;<\/p>\n\n\n\n<p>Source:<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/develeap\/terraform-provider-chatgpt\" target=\"_blank\" rel=\"noreferrer noopener\">terraform-provider-chatgpt<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-6455","post","type-post","status-publish","format-standard","hentry","category-devops"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/posts\/6455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/comments?post=6455"}],"version-history":[{"count":18,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/posts\/6455\/revisions"}],"predecessor-version":[{"id":6476,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/posts\/6455\/revisions\/6476"}],"wp:attachment":[{"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/media?parent=6455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/categories?post=6455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/tags?post=6455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}