{"id":6478,"date":"2023-04-04T18:21:15","date_gmt":"2023-04-04T22:21:15","guid":{"rendered":"https:\/\/droidbasement.com\/db-blog\/?p=6478"},"modified":"2023-04-05T07:49:34","modified_gmt":"2023-04-05T11:49:34","slug":"chatgpt-ansible-send-queries-and-receive-responses-using-infrastructure-as-code","status":"publish","type":"post","link":"https:\/\/droidbasement.com\/db-blog\/chatgpt-ansible-send-queries-and-receive-responses-using-infrastructure-as-code\/","title":{"rendered":"ChatGPT\/Ansible \u2013 Send queries and receive responses using Infrastructure as Code"},"content":{"rendered":"\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/openai.com\/blog\/chatgpt\" target=\"_blank\">ChatGPT<\/a> is an AI-powered chatbot designed to provide natural language generations and follow-up questions to enable users to have natural, free-flowing conversations. It is powered by <a rel=\"noreferrer noopener\" href=\"https:\/\/openai.com\/\" target=\"_blank\">OpenAI<\/a>&#8216;s GPT-3 AI language model, and its goal is to enable people to have natural conversations with AI-driven chatbots.<\/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 rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/www.ansible.com\/\" target=\"_blank\">Ansible<\/a> (Infrastructure as Code) to query ChatGPT and receive responses.  We will use <a href=\"https:\/\/github.com\/elchico2007\" target=\"_blank\" rel=\"noreferrer noopener\">Elchico2007<\/a>&#8216;s collection and <a href=\"https:\/\/openai.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">OpenAI<\/a>&#8216;s module 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 rel=\"noreferrer noopener\" href=\"https:\/\/platform.openai.com\/account\" target=\"_blank\">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>Install\/Upgrade Ansible:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pip3 install ansible --upgrade --user &amp;&amp; chmod 754 ~\/.local\/bin\/ansible ~\/.local\/bin\/ansible-playbook ~\/.local\/bin\/ansible-galaxy<\/code><\/pre>\n\n\n\n<p>Install\/Upgrade OpenAI&#8217;s module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pip3 install openai --upgrade --user<\/code><\/pre>\n\n\n\n<p>Install\/Upgrade JMESPath (so we may use json_query to parse output):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pip3 install jmespath --upgrade --user<\/code><\/pre>\n\n\n\n<p>Create a Ansible work folder and change in to the base path:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mkdir -p ansible\/chatgpt\/inventory &amp;&amp; cd ansible\/chatgpt<\/code><\/pre>\n\n\n\n<p>Create an Ansible configuration where we will target the collections install, in to this space:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat &lt;&lt; 'EOF' &gt; ansible.cfg\n&gt; &#91;defaults]\n&gt; collections_paths = .\/collections\n&gt; EOF<\/code><\/pre>\n\n\n\n<p>Install Elchico2007&#8217;s ChatGPT collection: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ansible-galaxy collection install elchico2007.chatgpt<\/code><\/pre>\n\n\n\n<p>Create an Ansible inventory, which adds a local group, lists your local host and specifies the connection to be local:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat &lt;&lt; 'EOF' &gt; inventory\/static-hostname\n&gt; &#91;local]\n&gt; localhost ansible_connection=local\n&gt; EOF<\/code><\/pre>\n\n\n\n<p>Create an Ansible playbook, which will query ChatGPT and print the response from it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat &lt;&lt; 'EOF' &gt; chatgpt.yml\n&gt; # Query ChatGPT and receive responses\n&gt; ---\n&gt; - hosts: local\n&gt; \n&gt;   tasks:\n&gt;     - name: Query ChatGPT\n&gt;       elchico2007.chatgpt.gpt3:\n&gt;         api_key: \"{{ lookup('env', 'CHATGPT_API_KEY', default='') }}\"\n&gt;         model: \"{{ lang_model | d('text-davinci-003', true) }}\"\n&gt;         input: \"{{ chatgpt_query | d('What is ChatGPT?', true) }}\"\n&gt;         instruction: \"{{ perform_action | d('', true) }}\"\n&gt;       register: chatgpt\n&gt;\n&gt;     - name: Output ChatGPT's response\n&gt;       debug:\n&gt;         msg: \"{{ chatgpt.output | json_query('choices&#91;].text') }}\"\n&gt;       when: chatgpt\n&gt; EOF<\/code><\/pre>\n\n\n\n<p>&#8216;Create a new secret key&#8217; <a rel=\"noreferrer noopener\" href=\"https:\/\/platform.openai.com\/account\/api-keys\" target=\"_blank\">here<\/a> and take note of it.<\/p>\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; ansible-playbook -i inventory\/ chatgpt.yml<\/code><\/pre>\n\n\n\n<p>It should return similarly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TASK &#91;Output ChatGPT's response] ***************************************************************************************\nok: &#91;localhost] =&gt; {\n    \"msg\": &#91;\n        \"ChatGPT is an AI-powered chatbot designed to provide natural language generations and follow-up questions to enable users to have natural, free-flowing conversations. It is powered by OpenAI's GPT-3 AI language model, and its goal is to enable people to have natural conversations with AI-driven chatbots.\"\n    ]\n}<\/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&gt; ansible-playbook -i inventory\/ chatgpt.yml -e 'chatgpt_query=\"What is Droid Basement?\"'<\/code><\/pre>\n\n\n\n<p>It should return similarly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TASK &#91;Output ChatGPT's response] ***************************************************************************************\nok: &#91;localhost] =&gt; {\n    \"msg\": &#91;\n        \"Droid Basement is an Android enthusiast blog founded in 2012. It provides users with the latest news, reviews and information on Android devices, applications, and accessories. The blog includes tutorials and guides, development resources, and other Android-related content.\"\n    ]\n}<\/code><\/pre>\n\n\n\n<p>Prompt it to perform a correction for you (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; ansible-playbook -i inventory\/ chatgpt.yml -e 'lang_model=text-davinci-edit-001 chatgpt_query=\"I lick teeching.\" perform_action=\"Fix my grammar\"'<\/code><\/pre>\n\n\n\n<p>It should return similarly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TASK &#91;Output ChatGPT's response] ***************************************************************************************\nok: &#91;localhost] =&gt; {\n    \"msg\": &#91;\n        \"I like teaching.\"\n    ]\n}<\/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;ansible-playbook&#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\/elchico2007\/elchico2007.chatgpt\" target=\"_blank\" rel=\"noreferrer noopener\">elchico2007.chatgpt<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ChatGPT is an AI-powered chatbot designed to provide natural language generations and follow-up questions to enable users to have natural, free-flowing conversations. It is powered by OpenAI&#8216;s GPT-3 AI language model, and its goal is to enable people to have natural conversations with AI-driven chatbots. The above was written using ChatGPT. In this article we [&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-6478","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\/6478","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=6478"}],"version-history":[{"count":23,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/posts\/6478\/revisions"}],"predecessor-version":[{"id":6504,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/posts\/6478\/revisions\/6504"}],"wp:attachment":[{"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/media?parent=6478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/categories?post=6478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/droidbasement.com\/db-blog\/wp-json\/wp\/v2\/tags?post=6478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}