### OpenAI Quickstart: Start with an Instruction Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/pages/readings.zh.mdx This section from the OpenAI documentation provides a basic example of how to start interacting with their models by providing a simple instruction. It demonstrates the fundamental input required for many AI model interactions. ```APIDOC OpenAI API Quickstart: Start with an Instruction This guide shows you how to get started with the OpenAI API by sending a simple instruction. Example: Send a POST request to the completions endpoint: POST https://api.openai.com/v1/completions Headers: Authorization: Bearer YOUR_API_KEY Content-Type: application/json Body: { "model": "text-davinci-003", "prompt": "Say this is a test", "max_tokens": 7, "temperature": 0 } Response: { "id": "cmpl-XXXXXXXXXXXXXX", "object": "text_completion", "created": 1677652287, "model": "text-davinci-003", "choices": [ { "text": "\n\nThis is a test", "index": 0, "logprobs": null, "finish_reason": "length" } ], "usage": { "prompt_tokens": 5, "completion_tokens": 7, "total_tokens": 12 } } Parameters: - model: The ID of the model to use for completion. (e.g., "text-davinci-003") - prompt: The prompt(s) to generate completions for. (string or array of strings) - max_tokens: The maximum number of tokens to generate in the completion. (integer, default 16) - temperature: Controls randomness. Lower values make output more focused and deterministic. (float, 0 to 2, default 1) Error Handling: - Invalid API Key: Authentication error. - Rate Limiting: Exceeding usage limits. - Invalid Model: Model not found or unavailable. ``` -------------------------------- ### Install and Configure Gemini Python Library Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/pages/models/gemini.en.mdx Demonstrates how to install the google-generativeai library using pip and configure it with an API key for accessing Gemini models. This is the initial setup required to use the Gemini API in Python. ```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") ``` -------------------------------- ### Install Libraries Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/notebooks/pe-lecture.ipynb Installs or upgrades essential Python libraries required for prompt engineering, including openai, langchain, and python-dotenv. ```python %%capture # update or install the necessary libraries !pip install --upgrade openai !pip install --upgrade langchain !pip install --upgrade python-dotenv ``` -------------------------------- ### Install and Configure Gemini Python Library Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/ar-pages/models/gemini.ar.mdx Demonstrates how to install the google-generativeai library using pip and configure it with an API key for accessing Gemini models. This is the initial setup required to use the Gemini API in Python. ```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") ``` -------------------------------- ### Basic Prompt Example Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/notebooks/pe-lecture.ipynb Demonstrates a basic prompt engineering interaction by setting default parameters, defining a user prompt, formatting it into messages, and calling the get_completion function. ```python # basic example params = set_open_params() prompt = "The sky is" messages = [ { "role": "user", "content": prompt } ] response = get_completion(params, messages) ``` -------------------------------- ### Install Required Libraries Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/notebooks/pe-code-llama.ipynb Installs the necessary Python libraries, `openai` for API interaction and `pandas` for data manipulation, to get started with the Code Llama guide. ```python %%capture !pip install openai !pip install pandas ``` -------------------------------- ### Run Prompt Engineering Guide Locally Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/README.md Instructions to set up and run the prompt engineering guide locally using pnpm. This involves installing dependencies and then booting the development server. ```shell pnpm i next react react-dom nextra nextra-theme-docs pnpm dev ``` -------------------------------- ### Configure and Generate with Gemini-Pro Model Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/pages/models/gemini.en.mdx Demonstrates how to initialize the `gemini-pro` model with custom generation configuration and safety settings. It then shows how to generate content using a prompt and prints the response. ```Python generation_config = { "temperature": 0, "top_p": 1, "top_k": 1, "max_output_tokens": 2048, } safety_settings = [ { "category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" } ] model = genai.GenerativeModel(model_name="gemini-pro", generation_config=generation_config, safety_settings=safety_settings) prompt_parts = [ "Your task is to extract model names from machine learning paper abstracts. Your response is an array of the model names in the format [\"model_name\"]. If you don't find model names in the abstract or you are not sure, return [\"NA\"]\n\nAbstract: Large Language Models (LLMs), such as ChatGPT and GPT-4, have revolutionized natural language processing research and demonstrated potential in Artificial General Intelligence (AGI). However, the expensive training and deployment of LLMs present challenges to transparent and open academic research. To address these issues, this project open-sources the Chinese LLaMA and Alpaca…", ] response = model.generate_content(prompt_parts) print(response.text) ``` -------------------------------- ### Few-Shot Prompting Examples Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/guides/prompts-intro.md Illustrates few-shot prompting techniques using both direct question-answer pairs and a classification task format. This method leverages examples to guide the model's learning. ```text ? ? ? ? ``` ```text Q: ? A: Q: ? A: Q: ? A: Q: ? A: ``` ```text This is awesome! // Positive This is bad! // Negative Wow that movie was rad! // Positive What a horrible show! // ``` -------------------------------- ### Install liteLLM Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/notebooks/pe-litellm-intro.ipynb Installs the liteLLM Python package using pip. This is the first step to start using the library. ```python !pip install litellm ``` -------------------------------- ### Text Summarization Example Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/notebooks/pe-lecture.ipynb Provides an example of text summarization using the OpenAI API. A longer text about antibiotics is given, with an instruction to explain it in one sentence. ```python params = set_open_params(temperature=0.7) prompt = """Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body's immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance. Explain the above in one sentence:""" messages = [ { "role": "user", "content": prompt } ] response = get_completion(params, messages) IPython.display.Markdown(response.choices[0].message.content) ``` -------------------------------- ### OpenAI API Setup with Environment Variables Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/notebooks/pe-chatgpt-intro.ipynb Imports required libraries, loads the OpenAI API key from a .env file, and sets it for subsequent API calls. Ensure you have a .env file with OPENAI_API_KEY. ```python import openai import os import IPython from dotenv import load_dotenv load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") ``` -------------------------------- ### Gemini Few-Shot Prompt Example for Emotion Classification Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/pages/models/gemini.en.mdx Illustrates a few-shot prompt structure for an emotion classifier using Gemini. It includes task instructions, example input/output pairs, and the final input for the model to predict the emotion. ```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: ``` -------------------------------- ### Setup OpenAI Environment Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/notebooks/pe-chatgpt-adversarial.ipynb Imports necessary libraries including openai, os, IPython, and dotenv. It then loads environment variables and sets the OpenAI API key, which is essential for making API calls. ```python import openai import os import IPython from dotenv import load_dotenv load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") ``` -------------------------------- ### Gemini Few-Shot Prompt Example for Emotion Classification Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/ar-pages/models/gemini.ar.mdx Illustrates a few-shot prompt structure for an emotion classifier using Gemini. It includes task instructions, example input/output pairs, and the final input for the model to predict the emotion. ```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: ``` -------------------------------- ### Code Infilling Setup: Install Fireworks AI Client Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/notebooks/pe-code-llama.ipynb Installs the fireworks-ai Python client, which is necessary for using the Fireworks AI platform for tasks like code infilling. The `%%capture` magic command suppresses output. ```python %%capture !pip install fireworks-ai ``` -------------------------------- ### Basic Prompt Example Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/pages/introduction/basics.pt.mdx Demonstrates a very simple prompt and its output, highlighting the need for more context and instruction. ```text O céu é ``` -------------------------------- ### Few-Shot Prompting Example Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/notebooks/pe-lecture.ipynb Illustrates few-shot prompting by providing the model with several examples of a task (determining if the sum of odd numbers is even) before presenting the final query. This helps the model understand the desired output format and logic. ```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) ``` -------------------------------- ### Specific Prompt Engineering Explanation Example Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/pages/introduction/tips.kr.mdx 이 예시는 프롬프트 엔지니어링 개념을 고등학생에게 설명하도록 요청하는 구체적인 프롬프트를 보여줍니다. '2~3개의 문장으로'와 같이 명확한 제약 조건을 제공하여 모델이 원하는 길이와 대상에 맞는 답변을 생성하도록 합니다. 이는 구체적인 지시의 효과를 강조합니다. ```Korean 고등학생에게 프롬프트 엔지니어링의 개념을 2~3개의 문장으로 설명해 줘. ``` -------------------------------- ### Configure and Generate with Gemini-Pro Model Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/ar-pages/models/gemini.ar.mdx Demonstrates how to initialize the `gemini-pro` model with custom generation configuration and safety settings. It then shows how to generate content using a prompt and prints the response. ```Python generation_config = { "temperature": 0, "top_p": 1, "top_k": 1, "max_output_tokens": 2048, } safety_settings = [ { "category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" } ] model = genai.GenerativeModel(model_name="gemini-pro", generation_config=generation_config, safety_settings=safety_settings) prompt_parts = [ "Your task is to extract model names from machine learning paper abstracts. Your response is an array of the model names in the format [\"model_name\"]. If you don't find model names in the abstract or you are not sure, return [\"NA\"]\n\nAbstract: Large Language Models (LLMs), such as ChatGPT and GPT-4, have revolutionized natural language processing research and demonstrated potential in Artificial General Intelligence (AGI). However, the expensive training and deployment of LLMs present challenges to transparent and open academic research. To address these issues, this project open-sources the Chinese LLaMA and Alpaca…", ] response = model.generate_content(prompt_parts) print(response.text) ``` -------------------------------- ### Question Answering Example Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/notebooks/pe-lecture.ipynb Demonstrates a question-answering task using the OpenAI API. It provides context about Teplizumab and OKT3, then asks a specific question based on the context. ```python prompt = """Answer the question based on the context below. Keep the answer short and concise. Respond \"Unsure about answer\" if not sure about the answer. Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use. Question: What was OKT3 originally sourced from? Answer:""" messages = [ { "role": "user", "content": prompt } ] response = get_completion(params, messages) IPython.display.Markdown(response.choices[0].message.content) ``` -------------------------------- ### Temperature Effect Example Source: https://github.com/hungbv1/prompt-engineering-guide/blob/main/notebooks/pe-lecture.ipynb Shows how changing the 'temperature' parameter affects the output of the OpenAI API. A lower temperature (e.g., 0) results in more deterministic output. ```python params = set_open_params(temperature=0) response = get_completion(params, messages) IPython.display.Markdown(response.choices[0].message.content) ```