This article introduces prompt engineering and its implementation using Azure OpenAI in Python.
To begin, let's explore the concept of a "prompt":
A prompt, within the realm of natural language processing and machine learning, is a precise input or directive provided to a language model such as GPT-3, GPT-3.5, GPT-4, and DALL-E. This input instructs the model to generate a specific desired response.
Now, let's delve into the concept of "prompt engineering":
Prompt engineering is the systematic process of crafting, refining, and optimizing input prompts to convey the user's intent to a language model effectively. It is crucial in guiding the model to generate accurate and relevant responses in various applications.
Structure of the Prompts
- Objective: This is the main goal or objective of the prompt. It can be a question, statement, command, or challenge you want the AI system to answer, generate, execute, or solve.
- Guidelines: These are the specific rules you want the AI system to follow when performing the task. They can include the format, style, tone, length, scope of the output and any constraints or limitations you want to impose on the AI system.
- Background: This is the background information or details that you want to provide to the AI system to help it understand the task and the domain. It can include definitions, examples, references, sources, or scenarios relevant to the task and the output.
- Settings: These are the optional settings or options that you can use to customize or fine-tune the AI system's behavior and performance. They can include parameters like temperature, top-k, top-p, frequency penalty, presence penalty, stop sequence, and best-of for generative models.
- Input Data: This is the optional data or information you want to give the AI system as part of the prompt. It can be text, images, audio, video, or any other type of input that the AI system can process and use for the task.
Prompting Techniques
This article focuses on Zero-Short Prompting Techniques
Zero-Short Prompting
The model is tasked with generating a response without the provision of sample outputs or additional context for the given task.
Benefits of zero-shot prompting:
- It conserves time and resources by eliminating the necessity for task-specific training or fine-tuning.
- It harnesses the extensive general knowledge and capabilities of large language models trained on vast and diverse datasets.
- It facilitates versatile and imaginative applications of natural language processing models across various tasks and domains.
Challenges of zero-shot prompting:
- Effective prompts require thoughtful and skillful design to clearly and efficiently convey intention and expectations to the model.
- It may not perform optimally for tasks or domains that are excessively specific, intricate, or novel, necessitating additional training or context.
- Results can be inconsistent, inaccurate, or inappropriate, contingent on the quality and appropriateness of the prompt.
Implementing Zero-Shot Prompting in Python
- Ensure you have Python 11 installed on your computer and install the OpenAI Python client library using the 'pip' command."
pip install openai
Please note that Python 12 is experiencing certain installation issues with the OpenAI package and its dependencies.
- Generate a key and endpoint for reference, checking the article below.
"Overview Of Azure OpenAI Modules With A Focus On Davinci Module (c-sharpcorner.com)"
- Your preferred Integrated Development Environment (IDE), I opted for Visual Studio Code.
Our objective of this implementation in Zero-Shot prompting
Objective: Determine the programming language based on a brief code snippet without providing examples or pre-trained data for each language.
Prompt: Identify the programming language used in the following code snippet
def hello_world():
print('Hello, world!')
Expected Response: Python
Let's begin writing a Python program.
import os
import requests
import json
import openai
from dotenv import load_dotenv
# Load the .env file
load_dotenv()
openai.api_key = os.getenv("AZURE_OPEN_KEY")
openai.api_base = os.getenv("AZURE_END_POINT")
openai.api_type = 'azure'
openai.api_version = '2023-07-01-preview'
deployment_name=os.getenv("DEPLOYMENT_NAME")
This code snippet configures the OpenAI API key and endpoint for the Azure platform. It relies on the `os` module to access the values of three environment variables: `AZURE_OPEN_KEY`, `AZURE_END_POINT`, and `DEPLOYMENT_NAME`. These variables are crucial in authenticating and establishing a connection with the OpenAI API.
- The `openai.api_key` variable is assigned the value of the `AZURE_OPEN_KEY` environment variable, serving as the secret key for authenticating API requests.
- The `openai.api_base` variable takes its value from the `AZURE_END_POINT` environment variable, which designates the endpoint URL for the OpenAI API.
- The `openai.api_type` variable is explicitly set to "azure", signaling the utilization of the OpenAI API within the Azure platform.
- The `openai.api_version` variable is configured with the value "2023-07-01-preview", indicating the specific version of the OpenAI API in use.
Additionally, the `deployment_name` variable obtains its value from the `DEPLOYMENT_NAME` environment variable. This variable assumes significance as it specifies the name of the deployment utilized for the OpenAI API. This name plays a role in connecting to the precise deployment instance of the API that is operational.
# Send a completion call to generate an answer
response = openai.ChatCompletion.create(
engine=os.getenv("DEPLOYMENT_NAME"),
messages=[
{"role": "system", "content": "Find the programming language:"},
{"role": "user", "content": "def hello_world(): print('Hello, world!')"},
]
)
print(response['choices'][0]['message']['content'])
The OpenAI API is employed here to generate chat responses. This process involves the utilization of the `openai.ChatCompletion.create()` method, which takes two essential parameters: `engine` and `messages`.
The `engine` parameter specifies the name of the OpenAI API deployment being utilized. In this instance, it adopts the value from the `DEPLOYMENT_NAME` environment variable as the engine name. This dynamic reference ensures a connection to the specific deployment of the OpenAI API currently in use.
Conversely, the `messages` parameter is constructed as a list of dictionaries, each representing a message within the chat conversation. Each dictionary comprises two keys: `role` and `content`. The `role` key indicates whether the message originates from the user or the system, while the `content` key carries the textual content of the message.
In the provided example, the chat conversation comprises two messages. The initial message assumes the "system" role and delivers the content "Find the programming language:". This message serves as a prompt from the system, urging the user to specify a programming language.
Subsequently, the second message takes on the "user" role and contains the content "def hello_world(): print('Hello, world!')". This user-sent message includes a Python function that prints "Hello, world!"—the basis for the OpenAI API's response generation.
Overall, this code leverages the OpenAI API to generate chat responses, with the `engine` parameter pinpointing the API deployment and the `messages` parameter delineating the chat conversation's content and structure.
Output
The programming language is Python.
Full source code
import os
import requests
import json
import openai
from dotenv import load_dotenv
# Load the .env file
load_dotenv()
openai.api_key = os.getenv("AZURE_OPEN_KEY")
openai.api_base = os.getenv("AZURE_END_POINT")
openai.api_type = 'azure'
openai.api_version = '2023-07-01-preview'
deployment_name=os.getenv("DEPLOYMENT_NAME")
# Send a completion call to generate an answer
response = openai.ChatCompletion.create(
engine=os.getenv("DEPLOYMENT_NAME"),
messages=[
{"role": "system", "content": "Find the programming language:"},
{"role": "user", "content": "def hello_world(): print('Hello, world!')"},
]
)
print(response['choices'][0]['message']['content'])
In the next article, we will explore additional prompt engineering techniques.