Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding OpenAI notebook for module 4 #429

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 242 additions & 0 deletions 04-prompt-engineering-fundamentals/python/oai-assignment.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following notebook was auto-generated by GitHub Copilot Chat and is meant for initial setup only"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to Prompt Engineering\n",
"Prompt engineering is the process of designing and optimizing prompts for natural language processing tasks. It involves selecting the right prompts, tuning their parameters, and evaluating their performance. Prompt engineering is crucial for achieving high accuracy and efficiency in NLP models. In this section, we will explore the basics of prompt engineering using the OpenAI models for exploration."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 1: Tokenization\n",
"Explore Tokenization using tiktoken, an open-source fast tokenizer from OpenAI\n",
"See [OpenAI Cookbook](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb?WT.mc_id=academic-105485-koreyst) for more examples.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# EXERCISE:\n",
"# 1. Run the exercise as is first\n",
"# 2. Change the text to any prompt input you want to use & re-run to see tokens\n",
"\n",
"import tiktoken\n",
"\n",
"# Define the prompt you want tokenized\n",
"text = f\"\"\"\n",
"Jupiter is the fifth planet from the Sun and the \\\n",
"largest in the Solar System. It is a gas giant with \\\n",
"a mass one-thousandth that of the Sun, but two-and-a-half \\\n",
"times that of all the other planets in the Solar System combined. \\\n",
"Jupiter is one of the brightest objects visible to the naked eye \\\n",
"in the night sky, and has been known to ancient civilizations since \\\n",
"before recorded history. It is named after the Roman god Jupiter.[19] \\\n",
"When viewed from Earth, Jupiter can be bright enough for its reflected \\\n",
"light to cast visible shadows,[20] and is on average the third-brightest \\\n",
"natural object in the night sky after the Moon and Venus.\n",
"\"\"\"\n",
"\n",
"# Set the model you want encoding for\n",
"encoding = tiktoken.encoding_for_model(\"gpt-3.5-turbo\")\n",
"\n",
"# Encode the text - gives you the tokens in integer form\n",
"tokens = encoding.encode(text)\n",
"print(tokens);\n",
"\n",
"# Decode the integers to see what the text versions look like\n",
"[encoding.decode_single_token_bytes(token) for token in tokens]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 2: Validate OpenAI API Key Setup\n",
"\n",
"Run the code below to verify that your OpenAI endpoint is set up correctly. The code just tries a simple basic prompt and validates the completion. Input `oh say can you see` should complete along the lines of `by the dawn's early light..`\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# The OpenAI SDK was updated on Nov 8, 2023 with new guidance for migration\n",
"# See: https://github.com/openai/openai-python/discussions/742\n",
"\n",
"## Updated\n",
"import os\n",
"from openai import OpenAI\n",
"from dotenv import load_dotenv\n",
"load_dotenv()\n",
"\n",
"client = OpenAI()\n",
"\n",
"deployment=\"gpt-3.5-turbo\"\n",
"\n",
"## Updated\n",
"def get_completion(prompt):\n",
" messages = [{\"role\": \"user\", \"content\": prompt}] \n",
" response = client.chat.completions.create( \n",
" model=deployment, \n",
" messages=messages,\n",
" temperature=0, # this is the degree of randomness of the model's output\n",
" max_tokens=1024\n",
" )\n",
" return response.choices[0].message.content\n",
"\n",
"## ---------- Call the helper method\n",
"\n",
"### 1. Set primary content or prompt text\n",
"text = f\"\"\"\n",
"oh say can you see\n",
"\"\"\"\n",
"\n",
"### 2. Use that in the prompt template below\n",
"prompt = f\"\"\"\n",
"```{text}```\n",
"\"\"\"\n",
"\n",
"## 3. Run the prompt\n",
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 3: Fabrications\n",
"Explore what happens when you ask the LLM to return completions for a prompt about a topic that may not exist, or about topics that it may not know about because it was outside it's pre-trained dataset (more recent). See how the response changes if you try a different prompt, or a different model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"## Set the text for simple prompt or primary content\n",
"## Prompt shows a template format with text in it - add cues, commands etc if needed\n",
"## Run the completion \n",
"text = f\"\"\"\n",
"generate a lesson plan on the Martian War of 2076.\n",
"\"\"\"\n",
"\n",
"prompt = f\"\"\"\n",
"```{text}```\n",
"\"\"\"\n",
"\n",
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 4: Instruction Based \n",
"Use the \"text\" variable to set the primary content \n",
"and the \"prompt\" variable to provide an instruction related to that primary content.\n",
"\n",
"Here we ask the model to summarize the text for a second-grade student"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Test Example\n",
"# https://platform.openai.com/playground/p/default-summarize\n",
"\n",
"## Example text\n",
"text = f\"\"\"\n",
"Jupiter is the fifth planet from the Sun and the \\\n",
"largest in the Solar System. It is a gas giant with \\\n",
"a mass one-thousandth that of the Sun, but two-and-a-half \\\n",
"times that of all the other planets in the Solar System combined. \\\n",
"Jupiter is one of the brightest objects visible to the naked eye \\\n",
"in the night sky, and has been known to ancient civilizations since \\\n",
"before recorded history. It is named after the Roman god Jupiter.[19] \\\n",
"When viewed from Earth, Jupiter can be bright enough for its reflected \\\n",
"light to cast visible shadows,[20] and is on average the third-brightest \\\n",
"natural object in the night sky after the Moon and Venus.\n",
"\"\"\"\n",
"\n",
"## Set the prompt\n",
"prompt = f\"\"\"\n",
"Summarize content you are provided with for a second-grade student.\n",
"```{text}```\n",
"\"\"\"\n",
"\n",
"## Run the prompt\n",
"response = get_completion(prompt)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 5: Complex Prompt \n",
"Try a request that has system, user and assistant messages \n",
"System sets assistant context\n",
"User & Assistant messages provide multi-turn conversation context\n",
"\n",
"Note how the assistant personality is set to \"sarcastic\" in the system context. \n",
"Try using a different personality context. Or try a different series of input/output messages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"response = client.chat.completions.create(\n",
" model=deployment,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a sarcastic assistant.\"},\n",
" {\"role\": \"user\", \"content\": \"Who won the world series in 2020?\"},\n",
" {\"role\": \"assistant\", \"content\": \"Who do you think won? The Los Angeles Dodgers of course.\"},\n",
" {\"role\": \"user\", \"content\": \"Where was it played?\"}\n",
" ]\n",
")\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise: Explore Your Intuition\n",
"The above examples give you patterns that you can use to create new prompts (simple, complex, instruction etc.) - try creating other exercises to explore some of the other ideas we've talked about like examples, cues and more."
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading