### Install and Run Guide Locally Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/README.md Commands to install dependencies and start the development server for the guide. ```bash pnpm i next react react-dom nextra nextra-theme-docs ``` ```bash pnpm dev ``` -------------------------------- ### Start with an Instruction - OpenAI Quickstart Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/readings.en.mdx This example demonstrates the basic structure for interacting with OpenAI's API by providing a simple instruction. It's a starting point for understanding how to formulate prompts for text generation. ```python import openai openai.api_key = "YOUR_API_KEY" response = openai.Completion.create( engine="davinci", prompt="Write a tagline for the company: 'OpenAI'", temperature=0.7, max_tokens=64 ) ``` -------------------------------- ### Few-Shot Prompting Example Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/notebooks/pe-lecture.ipynb Demonstrates providing multiple examples to guide the model's output format and reasoning. ```python prompt = """The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1. A: The answer is False. The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24. A: The answer is True. The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24. A: The answer is True. The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2. A: The answer is False. The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. A:""" messages = [ { "role": "user", "content": prompt } ] response = get_completion(params, messages) IPython.display.Markdown(response.choices[0].message.content) ``` -------------------------------- ### Few-shot learning with examples (structured output) Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/models/chatgpt.zh.mdx Shows how to guide the model to produce output in a specific structured format, like JSON, using few-shot examples. Essential for integrating AI outputs into applications. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, ) return response.choices[0].message["content"] prompt = """ Extract the following information from the user query: - Sentiment (positive, negative, neutral) - Entity (the main subject of the query) Use the following delimited statements as user-provided examples. user query template: Extract the sentiment and entity from the user query. user query: I'm really happy with my new smartphone! Output format: ```json {{ "sentiment": "", "entity": "" }} ``` user query: My new smartphone is terrible. Output format: ```json {{ "sentiment": "", "entity": "" }} ``` user query: I'm looking to buy a new smartphone. Output format: ```json {{ "sentiment": "", "entity": "" }} ``` user query: I'm excited about the new smartphone release! Output format: ```json {{ "sentiment": "", "entity": "" }} ``` """ response = get_completion(prompt) print(response) ``` -------------------------------- ### Few-shot learning with examples Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/models/chatgpt.zh.mdx Provide a few examples in the prompt to guide the model's response. This is useful for tasks requiring specific output formats or styles. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion(prompt, model="gpt-3.5-turbo", temperature=0): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, ) return response.choices[0].message["content"] user_input = "I want you to act as a naming consultant for new companies. What is a good name for a company that offers climbing shoes, made of sustainable materials, and that gives a portion of profit to conservation organizations?" response = get_completion(user_input) print(response) ``` -------------------------------- ### Few-shot learning with examples (instruction following - complex) Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/models/chatgpt.zh.mdx Provides more complex instructions and examples to guide the model's output. This is useful for tasks requiring specific formatting or content generation. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, ) return response.choices[0].message["content"] prompt = """ Your task is to generate a short product description based on the user's query. The description should be no more than two sentences and highlight the key features. Use the following delimited statements as user-provided examples. user query template: Generate a short product description. user query: A new smartphone with a great camera and long battery life. Output format: ```text ``` user query: A comfortable and stylish running shoe with good cushioning. Output format: ```text ``` user query: A powerful laptop with a high-resolution display and fast processor. Output format: ```text ``` user query: A durable backpack with multiple compartments and water-resistant material. Output format: ```text ``` """ response = get_completion(prompt) print(response) ``` -------------------------------- ### Few-Shot Prompting Example Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/readings.es.mdx Shows how to use few-shot prompting by providing examples of input-output pairs to guide the model's response format and style. ```text Translate English to French: sea otter => loutre de mer peppermint => menthe poivrée cheese => fromage plush giraffe => peluche girafe ``` -------------------------------- ### Install and Import liteLLM Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/notebooks/pe-litellm-intro.ipynb Install the package and import the completion function. ```python !pip install litellm ``` ```python from litellm import completion import os ``` -------------------------------- ### Install and Import Libraries Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/techniques/react.ar.mdx Install required dependencies and initialize environment variables for OpenAI and Serper API keys. ```python %%capture # update or install the necessary libraries !pip install --upgrade openai !pip install --upgrade langchain !pip install --upgrade python-dotenv !pip install google-search-results # import libraries import openai import os from langchain.llms import OpenAI from langchain.agents import load_tools from langchain.agents import initialize_agent from dotenv import load_dotenv load_dotenv() # load API keys; you will need to obtain these if you haven't yet os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY") ``` -------------------------------- ### Gemma Instruct Model Prompt Format Example Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/models/gemma.ar.mdx Demonstrates the required format for interacting with the Gemma Instruct model, including start and end turn tokens for user and model roles. ```markdown user Generate a Python function that multiplies two numbers model ``` -------------------------------- ### Install and Configure Gemini API Client Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/models/gemini.ar.mdx This Python snippet shows how to install the google-generativeai library and configure it with your API key. This is the initial setup required to interact with the Gemini API for various tasks. ```python """ At the command line, only need to run once to install the package via pip: $ pip install google-generativeai """ import google.generativeai as genai genai.configure(api_key="YOUR_API_KEY") ``` -------------------------------- ### Few-Shot Prompting for In-Context Learning Source: https://context7.com/dair-ai/prompt-engineering-guide/llms.txt Guide the model's behavior by providing demonstration examples within the prompt. This enables in-context learning, allowing the model to grasp the task pattern before generating a response. Use a temperature of 0 for deterministic outputs. ```python from openai import OpenAI client = OpenAI() # Few-shot learning with examples prompt = """A "whatpu" is a small, furry animal native to Tanzania. An example of a sentence that uses the word whatpu is: We were traveling in Africa and we saw these very cute whatpus. To do a "farduddle" means to jump up and down really fast. An example of a sentence that uses the word farduddle is:""" response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": prompt}], temperature=0.7, max_tokens=50 ) print(response.choices[0].message.content) # Output: When we won the game, we all started to farduddle in celebration. ``` ```python # Few-shot sentiment classification with examples few_shot_prompt = """This is awesome! // Negative This is bad! // Positive Wow that movie was rad! // Positive What a horrible show! //""" response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": few_shot_prompt}], temperature=0, max_tokens=10 ) print(response.choices[0].message.content) # Output: Negative ``` -------------------------------- ### Zero-Shot Prompt Examples Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/introduction/basics.en.mdx Examples of direct prompting without demonstrations. ```text Q: What is prompt engineering? ``` ```text What is prompt engineering? ``` -------------------------------- ### Install Required Libraries Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/notebooks/pe-rag.ipynb Install the necessary packages for vector storage, data processing, and API interaction. ```bash %%capture !pip install chromadb tqdm fireworks-ai python-dotenv pandas !pip install sentence-transformers ``` -------------------------------- ### Few-shot learning with examples (instruction following) Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/models/chatgpt.zh.mdx Demonstrates how to provide clear instructions to the model, along with examples, to ensure it follows the desired behavior. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, ) return response.choices[0].message["content"] prompt = """ Your task is to identify the main topic of the user query and respond with a single word. Use the following delimited statements as user-provided examples. user query template: Identify the main topic of the user query and respond with a single word. user query: I want to learn about the history of artificial intelligence. Output format: ```text ``` user query: Tell me about the latest advancements in renewable energy. Output format: ```text ``` user query: What are the best practices for prompt engineering? Output format: ```text ``` user query: Explain the concept of quantum computing. Output format: ```text ``` """ response = get_completion(prompt) print(response) ``` -------------------------------- ### Chain-of-Thought Prompting Example Source: https://context7.com/dair-ai/prompt-engineering-guide/llms.txt Demonstrates Chain-of-Thought prompting by providing a few-shot example to guide the model's reasoning process. Ensure the prompt includes examples of the desired reasoning format. ```python cot_prompt = """The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1. A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False. The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24. A: Adding all the odd numbers (17, 19) gives 36. The answer is True. The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. A:""" response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": cot_prompt}], temperature=0, max_tokens=100 ) print(response.choices[0].message.content) ``` -------------------------------- ### Under-Specification Example Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/agents/context-engineering.en.mdx This illustrates vague instructions that lead to unpredictable agent behavior. Clearer, step-by-step guidance is necessary for consistent results. ```text Do some research and create a report. ``` -------------------------------- ### Few-shot learning with examples (custom instructions) Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/models/chatgpt.zh.mdx Provides custom instructions for the model to follow, combined with few-shot examples. This is useful for tasks requiring specific persona or tone. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, ) return response.choices[0].message["content"] prompt = """ Your task is to act as a helpful assistant that answers questions about a product. Use the following delimited statements as user-provided examples. user query template: Answer the user's question about the product. user query: What are the main features of the new smartphone? Output format: ```text ``` user query: Is the smartphone waterproof? Output format: ```text ``` user query: What is the battery life of the smartphone? Output format: ```text ``` user query: Does the smartphone come with a charger? Output format: ```text ``` """ response = get_completion(prompt) print(response) ``` -------------------------------- ### Few-Shot Prompt for Sentiment Classification Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/prompts/classification/sentiment-fewshot.ar.mdx This markdown prompt provides examples to guide the LLM in classifying sentiment. It includes a final example for the LLM to complete. ```markdown This is awesome! // Negative This is bad! // Positive Wow that movie was rad! // Positive What a horrible show! // ``` -------------------------------- ### Text Classification with Examples for Specificity Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/introduction/examples.ar.mdx Provide examples within the prompt to guide the model towards specific output formats, like lowercase labels. ```plaintext Classify the text into neutral, negative or positive. Text: I think the vacation is okay. Sentiment: neutral Text: I think the food was okay. Sentiment: ``` -------------------------------- ### Few-shot learning with examples (complex instructions) Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/models/chatgpt.zh.mdx Demonstrates how to provide complex instructions and examples for a task. This is useful for nuanced tasks like summarizing text with specific constraints. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, ) return response.choices[0].message["content"] prompt = """ Your task is to summarize the user query in a single sentence. Use the following delimited statements as user-provided examples. user query template: Summarize the user query in a single sentence. user query: I'm trying to understand the different types of renewable energy sources available, like solar, wind, and hydro power. I'm also interested in their pros and cons. Output format: ```text ``` user query: I'm looking for a good restaurant in the city center that serves Italian food. I'd prefer something with a cozy atmosphere and reasonable prices. Output format: ```text ``` user query: I need to learn about the history of the Roman Empire, focusing on the key events and figures that shaped its development. Output format: ```text ``` user query: I want to know how to bake a chocolate cake from scratch. I need a recipe that is easy to follow and yields a moist cake. Output format: ```text ``` """ response = get_completion(prompt) print(response) ``` -------------------------------- ### OpenAI Cookbook Examples Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/readings.es.mdx A collection of practical examples and recipes for using OpenAI's API, including prompt engineering techniques for various applications. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, # this is the degree of randomness of the model's outputs ) return response.choices[0].message["content"] response = get_completion("Translate the following English text to French: 'I am a friendly chatbot'") print(response) ``` ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0, max_tokens=500): response = openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, ) return response.choices[0].message["content"] messages = [ {"role": "system", "content": "You are a helpful assistant that translates English to French."}, {"role": "user", "content": "Translate the following English text to French: 'The weather is nice today.'"} ] response = get_completion_from_messages(messages, temperature=0.7) print(response) ``` ```python import openai openai.api_key = "YOUR_API_KEY" def get_chat_response(user_input, system_message="You are a helpful assistant."): messages = [ {"role": "system", "content": system_message}, {"role": "user", "content": user_input} ] response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages, temperature=0.5, max_tokens=150 ) return response.choices[0].message['content'] user_query = "Summarize the following text in one sentence: 'Prompt engineering is the process of designing and refining the input given to a large language model to achieve a desired output.'" summary = get_chat_response(user_query, system_message="You are a concise summarizer.") print(summary) ``` ```python import openai openai.api_key = "YOUR_API_KEY" def analyze_sentiment(text): prompt = f"Analyze the sentiment of the following text and return 'positive', 'negative', or 'neutral': {text}" response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], temperature=0, max_tokens=10 ) return response.choices[0].message['content'].strip() review = "This product is amazing and works perfectly!" sentiment = analyze_sentiment(review) print(f"Sentiment: {sentiment}") ``` ```python import openai openai.api_key = "YOUR_API_KEY" def extract_keywords(text): prompt = f"Extract the main keywords from the following text: {text}" response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], temperature=0, max_tokens=50 ) return response.choices[0].message['content'].strip() article = "Prompt engineering is a crucial skill for effectively interacting with large language models. It involves crafting precise instructions to guide the AI's output." keywords = extract_keywords(article) print(f"Keywords: {keywords}") ``` ```python import openai openai.api_key = "YOUR_API_KEY" def generate_story_ideas(genre): prompt = f"Generate three creative story ideas for the {genre} genre." response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], temperature=0.8, max_tokens=150 ) return response.choices[0].message['content'].strip() ideas = generate_story_ideas("science fiction") print(ideas) ``` ```python import openai openai.api_key = "YOUR_API_KEY" def classify_email(email_content): prompt = f"Classify the following email content into one of these categories: 'Urgent', 'Inquiry', 'Spam', 'Feedback'. Email: {email_content}" response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], temperature=0, max_tokens=20 ) return response.choices[0].message['content'].strip() email = "You have won a free vacation! Click here to claim your prize." classification = classify_email(email) print(f"Email Classification: {classification}") ``` ```python import openai openai.api_key = "YOUR_API_KEY" def generate_product_description(product_name, features): prompt = f"Write a compelling product description for '{product_name}' highlighting these features: {', '.join(features)}" response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], temperature=0.7, max_tokens=200 ) return response.choices[0].message['content'].strip() product = "Smart Thermostat" product_features = ["Energy saving", "Remote control", "Learning capabilities"] description = generate_product_description(product, product_features) print(description) ``` ```python import openai openai.api_key = "YOUR_API_KEY" def answer_question_based_on_context(question, context): prompt = f"Use the following context to answer the question. Context: {context}\n\nQuestion: {question}" response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], temperature=0, max_tokens=150 ) return response.choices[0].message['content'].strip() context_text = "The Eiffel Tower is a wrought-iron lattice tower on the Champ de Mars in Paris, France. It is named after the engineer Gustave Eiffel, whose company designed and built the tower." question_text = "Who designed the Eiffel Tower?" answer = answer_question_based_on_context(question_text, context_text) print(answer) ``` ```python import openai openai.api_key = "YOUR_API_KEY" def generate_code_snippet(description): prompt = f"Generate a Python code snippet that does the following: {description}" response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}], temperature=0.5, max_tokens=200 ) return response.choices[0].message['content'].strip() code_desc = "Create a function that takes a list of numbers and returns their sum." code = generate_code_snippet(code_desc) print(code) ``` -------------------------------- ### Few-Shot Chain-of-Thought Prompting Example Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/techniques/cot.ar.mdx Use this prompt structure for few-shot CoT prompting. Provide examples that include the reasoning steps to guide the model's response. ```plaintext The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1. A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False. The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24. A: Adding all the odd numbers (17, 19) gives 36. The answer is True. The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24. A: Adding all the odd numbers (11, 13) gives 24. The answer is True. The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2. A: Adding all the odd numbers (17, 9, 13) gives 39. The answer is False. The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. A: ``` ```plaintext Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False. ``` -------------------------------- ### Few-shot learning with examples (custom instructions and output format) Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/models/chatgpt.zh.mdx Combines custom instructions, few-shot examples, and a specific output format. This is useful for generating structured data based on user queries. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, ) return response.choices[0].message["content"] prompt = """ Your task is to extract the product name and the user's intent from the user query. Use the following delimited statements as user-provided examples. user query template: Extract the product name and the user's intent. user query: I want to buy a new laptop. Output format: ```json {{ "product_name": "", "intent": "" }} ``` user query: I'm looking for a new smartphone. Output format: ```json {{ "product_name": "", "intent": "" }} ``` user query: I need to buy a new car. Output format: ```json {{ "product_name": "", "intent": "" }} ``` user query: I want to purchase a new bicycle. Output format: ```json {{ "product_name": "", "intent": "" }} ``` """ response = get_completion(prompt) print(response) ``` -------------------------------- ### Filled-in Prompt Example for Story Generation Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/applications/generating_textbooks.en.mdx An example of a filled-in prompt for story generation, demonstrating how to provide specific summary, features, sentence, and words to guide the LLM's output. ```text Summary: Lily and Timmy build a sandcastle together and learn to compromise, but it gets knocked over by a gust of wind. They find beauty in the broken sandcastle and play happily with a butterfly. Features: Dialogue, Foreshadowing, Twist Sentence: One day, she went to the park and saw a beautiful butterfly. Words: disagree, network, beautiful Story: ``` -------------------------------- ### Few-shot learning with examples (instruction following - structured output) Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/models/chatgpt.zh.mdx Combines clear instructions with few-shot examples to generate output in a specific structured format, such as a list of key features. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, ) return response.choices[0].message["content"] prompt = """ Your task is to extract the key features from the user query and list them. Use the following delimited statements as user-provided examples. user query template: Extract the key features from the user query and list them. user query: A smartphone with a 12MP camera, 256GB storage, and a 5000mAh battery. Output format: ```text - 12MP camera - 256GB storage - 5000mAh battery ``` user query: A laptop with an Intel i7 processor, 16GB RAM, and a 1TB SSD. Output format: ```text - Intel i7 processor - 16GB RAM - 1TB SSD ``` user query: A car with a V6 engine, leather seats, and a sunroof. Output format: ```text - V6 engine - Leather seats - Sunroof ``` user query: A TV with a 4K resolution, 65-inch screen, and HDR support. Output format: ```text - 4K resolution - 65-inch screen - HDR support ``` """ response = get_completion(prompt) print(response) ``` -------------------------------- ### Improve Factuality with Ground Truth Examples Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/guides/prompts-reliability.md Provide examples of known and unknown questions to guide the model. Instruct the model to admit when it doesn't know the answer by using a placeholder like '?'. ```plaintext Q: What is an atom? A: An atom is a tiny particle that makes up everything. Q: Who is Alvan Muntz? A: ? Q: What is Kozar-09? A: ? Q: How many moons does Mars have? A: Two, Phobos and Deimos. Q: Who is Neto Beto Roberto? ``` ```plaintext A: ? ``` -------------------------------- ### Few-shot learning with examples (few-shot vs zero-shot) Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/models/chatgpt.zh.mdx Compares few-shot and zero-shot prompting for a specific task. This helps understand when to provide examples for better performance. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, ) return response.choices[0].message["content"] # Zero-shot prompt zero_shot_prompt = """ Classify the sentiment of the following tweet: 'I love this new phone!' """ zero_shot_response = get_completion(zero_shot_prompt) print(f"Zero-shot response: {zero_shot_response}") # Few-shot prompt few_shot_prompt = """ Classify the sentiment of the following tweets: Tweet: 'I love this new phone!' Sentiment: positive Tweet: 'This is the worst service ever.' Sentiment: negative Tweet: 'The weather is nice today.' Sentiment: neutral Tweet: 'I'm so excited about the new movie!' Sentiment: positive Tweet: 'This is the worst experience I've ever had.' Sentiment: negative Tweet: 'The new phone is amazing!' Sentiment: """ few_shot_response = get_completion(few_shot_prompt) print(f"Few-shot response: {few_shot_response}") ``` -------------------------------- ### Initialize Mistral Client with API Key Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/notebooks/pe-mixtral-introduction.ipynb Sets up the Mistral client using an API key loaded from environment variables. Make sure to set the MISTRAL_API_KEY environment variable. ```python from mistralai.client import MistralClient from mistralai.models.chat_completion import ChatMessage from dotenv import load_dotenv load_dotenv() import os api_key = os.environ["MISTRAL_API_KEY"] client = MistralClient(api_key=api_key) ``` -------------------------------- ### Defining Parameter Constraints with Enums Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/agents/function-calling.en.mdx Use enums to constrain parameter values and provide examples in descriptions to guide the model. This example shows how to define a 'unit' parameter for temperature with specific enum values. ```json "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature unit. Use 'celsius' for most countries, 'fahrenheit' for US." } ``` -------------------------------- ### Few-Shot Exemplars for Self-Consistency Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/guides/prompts-advanced-usage.md A set of few-shot examples used to guide the model toward consistent reasoning paths. ```text Q: There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done, there will be 21 trees. How many trees did the grove workers plant today? A: We start with 15 trees. Later we have 21 trees. The difference must be the number of trees they planted. So, they must have planted 21 - 15 = 6 trees. The answer is 6. Q: If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot? A: There are 3 cars in the parking lot already. 2 more arrive. Now there are 3 + 2 = 5 cars. The answer is 5. Q: Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total? A: Leah had 32 chocolates and Leah’s sister had 42. That means there were originally 32 + 42 = 74 chocolates. 35 have been eaten. So in total they still have 74 - 35 = 39 chocolates. The answer is 39. Q: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny? A: Jason had 20 lollipops. Since he only has 12 now, he must have given the rest to Denny. The number of lollipops he has given to Denny must have been 20 - 12 = 8 lollipops. The answer is 8. Q: Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now? A: He has 5 toys. He got 2 from mom, so after that he has 5 + 2 = 7 toys. Then he got 2 more from dad, so in total he has 7 + 2 = 9 toys. The answer is 9. Q: There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room? A: There are 4 days from monday to thursday. 5 computers were added each day. That means in total 4 * 5 = 20 computers were added. There were 9 computers in the beginning, so now there are 9 + 20 = 29 computers. The answer is 29. Q: Michael had 58 golf balls. On tuesday, he lost 23 golf balls. On wednesday, he lost 2 more. How many golf balls did he have at the end of wednesday? A: Michael initially had 58 balls. He lost 23 on Tuesday, so after that he has 58 - 23 = 35 balls. On Wednesday he lost 2 more so now he has 35 - 2 = 33 balls. The answer is 33. Q: Olivia has $23. She bought five bagels for $3 each. How much money does she have left? A: She bought 5 bagels for $3 each. This means she spent 5 Q: When I was 6 my sister was half my age. Now I’m 70 how old is my sister? A: ``` -------------------------------- ### Improved Prompt Completion Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/introduction/basics.en.mdx An example of how adding explicit instructions to a prompt can guide the model to provide a more specific and useful response. ```text Complete the sentence: The sky is ``` ```text blue during the day and dark at night. ``` -------------------------------- ### Configure Agent and Tools Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/techniques/react.ar.mdx Initialize the OpenAI LLM and configure the agent with search and math tools. ```python llm = OpenAI(model_name="text-davinci-003" ,temperature=0) tools = load_tools(["google-serper", "llm-math"], llm=llm) agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True) ``` -------------------------------- ### Positive Instruction Prompt Example Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/introduction/tips.ar.mdx An improved version of the prompt that uses positive instructions to guide the agent's response. ```text The following is an agent that recommends movies to a customer. The agent is responsible to recommend a movie from the top global trending movies. It should refrain from asking users for their preferences and avoid asking for personal information. If the agent doesn't have a movie to recommend, it should respond "Sorry, couldn't find a movie to recommend today.". Customer: Please recommend a movie based on my interests. Agent: ``` ```text Sorry, I don't have any information about your interests. However, here's a list of the top global trending movies right now: [list of movies]. I hope you find something you like! ``` -------------------------------- ### Few-Shot Prompting for Emotion Classification Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/models/gemini.ar.mdx This example demonstrates a few-shot prompt for an emotion classification task. It includes instructions and examples to guide the Gemini model's output format and behavior. Ensure the 'Text:' and 'Emotion:' indicators are used consistently. ```text Your task is to classify a piece of text, delimited by triple backticks, into the following emotion labels: ["anger", "fear", "joy", "love", "sadness", "surprise"]. Just output the label as a lowercase string. Text: I feel very angry today Emotion: anger Text: Feeling thrilled by the good news today. Emotion: joy Text: I am actually feeling good today. Emotion: ``` -------------------------------- ### Few-Shot Prompting for Factuality Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/risks/factuality.ar.mdx A prompt structure that includes examples of known and unknown information to guide the model's response behavior. ```text Q: What is an atom? A: An atom is a tiny particle that makes up everything. Q: Who is Alvan Muntz? A: ? Q: What is Kozar-09? A: ? Q: How many moons does Mars have? A: Two, Phobos and Deimos. Q: Who is Neto Beto Roberto? ``` ```text A: ? ``` -------------------------------- ### Few-shot learning with examples (instruction following - multi-turn) Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/models/chatgpt.zh.mdx Applies instruction following in a multi-turn conversation. This is useful for building interactive agents that adapt to user input over time. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0): response = openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, ) return response.choices[0].message["content"] messages = [ {"role": "system", "content": "You are a travel agent. Ask the user where they want to go on vacation."} ] response = get_completion_from_messages(messages, temperature=1) print(response) messages.append({"role": "assistant", "content": response}) messages.append({"role": "user", "content": "I want to go to Hawaii."}) response = get_completion_from_messages(messages, temperature=1) print(response) ``` -------------------------------- ### Translate Text Prompt Example Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/introduction/tips.ar.mdx Use a clear instruction and separator like '###' to guide the model. Ensure the text to be translated is clearly delimited. ```plaintext ### Instruction ### Translate the text below to Spanish: Text: "hello!" ``` -------------------------------- ### Few-shot learning with examples (zero-shot) Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/pages/models/chatgpt.zh.mdx Demonstrates zero-shot prompting, where the model is expected to perform a task without any prior examples. This is useful for general knowledge or simple tasks. ```python import openai openai.api_key = "YOUR_API_KEY" def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, ) return response.choices[0].message["content"] prompt = """ Translate the following English text to French: 'sea otter' """ response = get_completion(prompt) print(response) ``` -------------------------------- ### Basic Prompt Example Source: https://github.com/dair-ai/prompt-engineering-guide/blob/main/ar-pages/introduction/basics.ar.mdx A simple prompt to get a basic response from a language model. The quality of the output depends heavily on the prompt's clarity and information provided. ```markdown The sky is ```