Launching refreshed version of LLM Engineering weeks 1-4 - see README
This commit is contained in:
194
week2/day3.ipynb
194
week2/day3.ipynb
@@ -35,23 +35,11 @@
|
||||
"\n",
|
||||
"load_dotenv(override=True)\n",
|
||||
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
|
||||
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
|
||||
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
|
||||
"\n",
|
||||
"if openai_api_key:\n",
|
||||
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
|
||||
"else:\n",
|
||||
" print(\"OpenAI API Key not set\")\n",
|
||||
" \n",
|
||||
"if anthropic_api_key:\n",
|
||||
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
|
||||
"else:\n",
|
||||
" print(\"Anthropic API Key not set\")\n",
|
||||
"\n",
|
||||
"if google_api_key:\n",
|
||||
" print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n",
|
||||
"else:\n",
|
||||
" print(\"Google API Key not set\")"
|
||||
" print(\"OpenAI API Key not set\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -64,7 +52,7 @@
|
||||
"# Initialize\n",
|
||||
"\n",
|
||||
"openai = OpenAI()\n",
|
||||
"MODEL = 'gpt-4o-mini'"
|
||||
"MODEL = 'gpt-4.1-mini'"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -74,6 +62,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Again, I'll be in scientist-mode and change this global during the lab\n",
|
||||
"\n",
|
||||
"system_message = \"You are a helpful assistant\""
|
||||
]
|
||||
},
|
||||
@@ -82,32 +72,75 @@
|
||||
"id": "98e97227-f162-4d1a-a0b2-345ff248cbe7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Please read this! A change from the video:\n",
|
||||
"## And now, writing a new callback\n",
|
||||
"\n",
|
||||
"In the video, I explain how we now need to write a function called:\n",
|
||||
"We now need to write a function called:\n",
|
||||
"\n",
|
||||
"`chat(message, history)`\n",
|
||||
"\n",
|
||||
"Which expects to receive `history` in a particular format, which we need to map to the OpenAI format before we call OpenAI:\n",
|
||||
"Which will be a callback function we will give gradio.\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"[\n",
|
||||
" {\"role\": \"system\", \"content\": \"system message here\"},\n",
|
||||
" {\"role\": \"user\", \"content\": \"first user prompt here\"},\n",
|
||||
" {\"role\": \"assistant\", \"content\": \"the assistant's response\"},\n",
|
||||
" {\"role\": \"user\", \"content\": \"the new user prompt\"},\n",
|
||||
"]\n",
|
||||
"```\n",
|
||||
"### The job of this function\n",
|
||||
"\n",
|
||||
"But Gradio has been upgraded! Now it will pass in `history` in the exact OpenAI format, perfect for us to send straight to OpenAI.\n",
|
||||
"\n",
|
||||
"So our work just got easier!\n",
|
||||
"\n",
|
||||
"We will write a function `chat(message, history)` where: \n",
|
||||
"**message** is the prompt to use \n",
|
||||
"**history** is the past conversation, in OpenAI format \n",
|
||||
"\n",
|
||||
"We will combine the system message, history and latest message, then call OpenAI."
|
||||
"Take a message, take the prior conversation, and return the response.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "354ce793",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def chat(message, history):\n",
|
||||
" return \"bananas\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e87f3417",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5d4996e8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def chat(message, history):\n",
|
||||
" return f\"You said {message} and the history is {history} but I still say bananas\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "434a0417",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "7890cac3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f7330d7f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## OK! Let's write a slightly better chat callback!"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -117,46 +150,61 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Simpler than in my video - we can easily create this function that calls OpenAI\n",
|
||||
"# It's now just 1 line of code to prepare the input to OpenAI!\n",
|
||||
"\n",
|
||||
"# Student Octavio O. has pointed out that this isn't quite as straightforward for Claude -\n",
|
||||
"# see the excellent contribution in community-contributions \"Gradio_issue_with_Claude\" that handles Claude.\n",
|
||||
"\n",
|
||||
"def chat(message, history):\n",
|
||||
" history = [{\"role\":h[\"role\"], \"content\":h[\"content\"]} for h in history]\n",
|
||||
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
||||
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
|
||||
" return response.choices[0].message.content\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0ab706f9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3bce145a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def chat(message, history):\n",
|
||||
" history = [{\"role\":h[\"role\"], \"content\":h[\"content\"]} for h in history]\n",
|
||||
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
||||
"\n",
|
||||
" print(\"History is:\")\n",
|
||||
" print(history)\n",
|
||||
" print(\"And messages is:\")\n",
|
||||
" print(messages)\n",
|
||||
"\n",
|
||||
" stream = openai.chat.completions.create(model=MODEL, messages=messages, stream=True)\n",
|
||||
"\n",
|
||||
" response = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" response += chunk.choices[0].delta.content or ''\n",
|
||||
" yield response"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1334422a-808f-4147-9c4c-57d63d9780d0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## And then enter Gradio's magic!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0866ca56-100a-44ab-8bd0-1568feaf6bf2",
|
||||
"id": "b8beeca6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1334422a-808f-4147-9c4c-57d63d9780d0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## OK let's keep going!\n",
|
||||
"\n",
|
||||
"Using a system message to add context, and to give an example answer.. this is \"one shot prompting\" again"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -171,24 +219,6 @@
|
||||
"Encourage the customer to buy hats if they are unsure what to get.\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4e5be3ec-c26c-42bc-ac16-c39d369883f6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def chat(message, history):\n",
|
||||
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
||||
"\n",
|
||||
" stream = openai.chat.completions.create(model=MODEL, messages=messages, stream=True)\n",
|
||||
"\n",
|
||||
" response = \"\"\n",
|
||||
" for chunk in stream:\n",
|
||||
" response += chunk.choices[0].delta.content or ''\n",
|
||||
" yield response"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -227,13 +257,11 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Fixed a bug in this function brilliantly identified by student Gabor M.!\n",
|
||||
"# I've also improved the structure of this function\n",
|
||||
"\n",
|
||||
"def chat(message, history):\n",
|
||||
"\n",
|
||||
" history = [{\"role\":h[\"role\"], \"content\":h[\"content\"]} for h in history]\n",
|
||||
" relevant_system_message = system_message\n",
|
||||
" if 'belt' in message:\n",
|
||||
" if 'belt' in message.lower():\n",
|
||||
" relevant_system_message += \" The store does not sell belts; if you are asked for belts, be sure to point out other items on sale.\"\n",
|
||||
" \n",
|
||||
" messages = [{\"role\": \"system\", \"content\": relevant_system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
||||
@@ -264,7 +292,7 @@
|
||||
"<table style=\"margin: 0; text-align: left;\">\n",
|
||||
" <tr>\n",
|
||||
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
|
||||
" <img src=\"../business.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
|
||||
" <img src=\"../assets/business.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <h2 style=\"color:#181;\">Business Applications</h2>\n",
|
||||
@@ -277,17 +305,15 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6dfb9e21-df67-4c2b-b952-5e7e7961b03d",
|
||||
"cell_type": "markdown",
|
||||
"id": "acc0e5a9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"display_name": ".venv",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
@@ -301,7 +327,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.13"
|
||||
"version": "3.12.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user