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.
The above was written using ChatGPT.
In this article we will use Ansible (Infrastructure as Code) to query ChatGPT and receive responses. We will use Elchico2007‘s collection and OpenAI‘s module 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
Install/Upgrade Ansible:
$ pip3 install ansible --upgrade --user && chmod 754 ~/.local/bin/ansible ~/.local/bin/ansible-playbook ~/.local/bin/ansible-galaxy
Install/Upgrade OpenAI’s module:
$ pip3 install openai --upgrade --user
Install/Upgrade JMESPath (so we may use json_query to parse output):
$ pip3 install jmespath --upgrade --user
Create a Ansible work folder and change in to the base path:
$ mkdir -p ansible/chatgpt/inventory && cd ansible/chatgpt
Create an Ansible configuration where we will target the collections install, in to this space:
$ cat << 'EOF' > ansible.cfg
> [defaults]
> collections_paths = ./collections
> EOF
Install Elchico2007’s ChatGPT collection:
$ ansible-galaxy collection install elchico2007.chatgpt
Create an Ansible inventory, which adds a local group, lists your local host and specifies the connection to be local:
$ cat << 'EOF' > inventory/static-hostname
> [local]
> localhost ansible_connection=local
> EOF
Create an Ansible playbook, which will query ChatGPT and print the response from it:
$ cat << 'EOF' > chatgpt.yml
> # Query ChatGPT and receive responses
> ---
> - hosts: local
>
> tasks:
> - name: Query ChatGPT
> elchico2007.chatgpt.gpt3:
> api_key: "{{ lookup('env', 'CHATGPT_API_KEY', default='') }}"
> model: "{{ lang_model | d('text-davinci-003', true) }}"
> input: "{{ chatgpt_query | d('What is ChatGPT?', true) }}"
> instruction: "{{ perform_action | d('', true) }}"
> register: chatgpt
>
> - name: Output ChatGPT's response
> debug:
> msg: "{{ chatgpt.output | json_query('choices[].text') }}"
> when: chatgpt
> EOF
‘Create a new secret key’ here and take note of it.
Run the default query (replace <API key> with the API key you received from ‘Create a new secret key’):
$ CHATGPT_API_KEY=<API key> ansible-playbook -i inventory/ chatgpt.yml
It should return similarly:
TASK [Output ChatGPT's response] ***************************************************************************************
ok: [localhost] => {
"msg": [
"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."
]
}
Ask it a question (replace <API key> with the API key you received from ‘Create a new secret key’):
$ CHATGPT_API_KEY=<API key> ansible-playbook -i inventory/ chatgpt.yml -e 'chatgpt_query="What is Droid Basement?"'
It should return similarly:
TASK [Output ChatGPT's response] ***************************************************************************************
ok: [localhost] => {
"msg": [
"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."
]
}
Prompt it to perform a correction for you (replace <API key> with the API key you received from ‘Create a new secret key’):
$ CHATGPT_API_KEY=<API key> ansible-playbook -i inventory/ chatgpt.yml -e 'lang_model=text-davinci-edit-001 chatgpt_query="I lick teeching." perform_action="Fix my grammar"'
It should return similarly:
TASK [Output ChatGPT's response] ***************************************************************************************
ok: [localhost] => {
"msg": [
"I like teaching."
]
}
You can set your API key in an environment variable so it (CHATGPT_API_KEY) does not need to be specified when executing ‘ansible-playbook’ (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:
« ChatGPT/Terraform – Send queries and receive responses using Infrastructure as Code Firmware – Asuswrt-Merlin (NG) – 386.11_alpha1 – RT-AC68 »