Using OpenAI API with Shell Scripting
Introduction
This tutorial demonstrates how to integrate and use the OpenAI API with Shell Scripting to leverage advanced AI capabilities in your applications.
1. Setting Up Your OpenAI API Key
Before starting, make sure you have your OpenAI API key ready. You can obtain it from the OpenAI website after signing up for an account.
2. Installing Necessary Tools
To use the OpenAI API in your shell scripts, you will need to have `curl` installed on your system. Most Unix-like systems have `curl` pre-installed. To check if `curl` is installed, run:
curl --version
If `curl` is not installed, you can install it using your package manager. For example, on Debian-based systems (like Ubuntu), you can install `curl` with:
sudo apt-get install curl
3. Making API Requests
To make requests to the OpenAI API using shell scripting, you'll use the `curl` command to send HTTP requests.
Text Completion
Here’s an example of making a request for text completion:
#!/bin/bash API_KEY="YOUR_API_KEY_HERE" PROMPT="Translate English to French: Hello, how are you?" MODEL="text-davinci-003" MAX_TOKENS=50 RESPONSE=$(curl -s https://api.openai.com/v1/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $API_KEY" \ -d '{ "model": "'"$MODEL"'", "prompt": "'"$PROMPT"'", "max_tokens": '$MAX_TOKENS' }') echo $RESPONSE | jq -r '.choices[0].text'
Output: Bonjour, comment ça va?
This shell script sends a POST request to the OpenAI API for text completion and prints the API response.
Code Generation
Here’s an example of making a request for code generation:
#!/bin/bash API_KEY="YOUR_API_KEY_HERE" PROMPT="Generate Python code to sort an array using bubble sort" MODEL="davinci-codex" MAX_TOKENS=150 RESPONSE=$(curl -s https://api.openai.com/v1/engines/$MODEL/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $API_KEY" \ -d '{ "prompt": "'"$PROMPT"'", "max_tokens": '$MAX_TOKENS', "stop": ["\n"] }') echo $RESPONSE | jq -r '.choices[0].text'
Output: ```python def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr ```
This shell script sends a POST request to the OpenAI Codex engine for code generation and prints the generated code.
4. Handling Responses
Once you receive a response from the API, you can handle it in your shell script.
Text Completion
Here’s how you might handle the completion response:
#!/bin/bash RESPONSE=$(curl -s https://api.openai.com/v1/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $API_KEY" \ -d '{ "model": "'"$MODEL"'", "prompt": "'"$PROMPT"'", "max_tokens": '$MAX_TOKENS' }') TRANSLATED_TEXT=$(echo $RESPONSE | jq -r '.choices[0].text') echo "Translated Text: $TRANSLATED_TEXT"
In this example, `RESPONSE` contains the JSON response from the OpenAI API, and `jq` is used to parse the response.
Code Generation
Here’s how you might handle the code generation response:
#!/bin/bash RESPONSE=$(curl -s https://api.openai.com/v1/engines/$MODEL/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $API_KEY" \ -d '{ "prompt": "'"$PROMPT"'", "max_tokens": '$MAX_TOKENS', "stop": ["\n"] }') GENERATED_CODE=$(echo $RESPONSE | jq -r '.choices[0].text') echo "Generated Code: $GENERATED_CODE"
In this example, `RESPONSE` contains the JSON response from the OpenAI API with the generated code, and `jq` is used to parse the response.
Conclusion
Integrating the OpenAI API with shell scripting allows you to enhance your applications with powerful AI capabilities. Explore more API endpoints and functionalities to innovate further.