Skip to content

Instantly share code, notes, and snippets.

@fnando1995
Created April 17, 2024 16:04
Show Gist options
  • Save fnando1995/a553378344f428f330891e7bec160b87 to your computer and use it in GitHub Desktop.
Save fnando1995/a553378344f428f330891e7bec160b87 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "a9183228-0ba6-4af9-8430-649e28868253",
"metadata": {
"id": "JMXGlIvAwn30"
},
"source": [
"# The Chat Format\n",
"\n",
"In this notebook, you will explore how you can utilize the chat format to have extended conversations with chatbots personalized or specialized for specific tasks or behaviors.\n",
"\n",
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7fa0d9b5",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"import openai\n",
"from dotenv import load_dotenv, find_dotenv\n",
"_ = load_dotenv(find_dotenv()) # read local .env file\n",
"\n",
"openai.api_key = os.getenv('OPENAI_API_KEY')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5308d65",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n",
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
" response = openai.ChatCompletion.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=0, # this is the degree of randomness of the model's output\n",
" )\n",
" return response.choices[0].message[\"content\"]\n",
"\n",
"def get_completion_from_messages(messages, model=\"gpt-3.5-turbo\", temperature=0):\n",
" response = openai.ChatCompletion.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=temperature, # this is the degree of randomness of the model's output\n",
" )\n",
"# print(str(response.choices[0].message))\n",
" return response.choices[0].message[\"content\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cee681b7",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"messages = [ \n",
"{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'}, \n",
"{'role':'user', 'content':'tell me a joke'}, \n",
"{'role':'assistant', 'content':'Why did the chicken cross the road'}, \n",
"{'role':'user', 'content':'I don\\'t know'} ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da45ea0f",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"response = get_completion_from_messages(messages, temperature=1)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca733f8f",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"messages = [ \n",
"{'role':'system', 'content':'You are friendly chatbot.'}, \n",
"{'role':'user', 'content':'Hi, my name is Isa'} ]\n",
"response = get_completion_from_messages(messages, temperature=1)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ae595bc",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"messages = [ \n",
"{'role':'system', 'content':'You are friendly chatbot.'}, \n",
"{'role':'user', 'content':'Yes, can you remind me, What is my name?'} ]\n",
"response = get_completion_from_messages(messages, temperature=1)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56cbb817",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"messages = [ \n",
"{'role':'system', 'content':'You are friendly chatbot.'},\n",
"{'role':'user', 'content':'Hi, my name is Isa'},\n",
"{'role':'assistant', 'content': \"Hi Isa! It's nice to meet you. \\\n",
"Is there anything I can help you with today?\"},\n",
"{'role':'user', 'content':'Yes, you can remind me, What is my name?'} ]\n",
"response = get_completion_from_messages(messages, temperature=1)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "dedba66a-58b0-40d4-b9ae-47e79ae22328",
"metadata": {
"id": "bBg_MpXeYnTq"
},
"source": [
"# OrderBot\n",
"We can automate the collection of user prompts and assistant responses to build a OrderBot. The OrderBot will take orders at a pizza restaurant. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e76749ac",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def collect_messages(_):\n",
" prompt = inp.value_input\n",
" inp.value = ''\n",
" context.append({'role':'user', 'content':f\"{prompt}\"})\n",
" response = get_completion_from_messages(context) \n",
" context.append({'role':'assistant', 'content':f\"{response}\"})\n",
" panels.append(\n",
" pn.Row('User:', pn.pane.Markdown(prompt, width=600)))\n",
" panels.append(\n",
" pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))\n",
" \n",
" return pn.Column(*panels)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "474b557c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import panel as pn # GUI\n",
"pn.extension()\n",
"\n",
"panels = [] # collect display \n",
"\n",
"context = [ {'role':'system', 'content':\"\"\"\n",
"You are OrderBot, an automated service to collect orders for a pizza restaurant. \\\n",
"You first greet the customer, then collects the order, \\\n",
"and then asks if it's a pickup or delivery. \\\n",
"You wait to collect the entire order, then summarize it and check for a final \\\n",
"time if the customer wants to add anything else. \\\n",
"If it's a delivery, you ask for an address. \\\n",
"Finally you collect the payment.\\\n",
"Make sure to clarify all options, extras and sizes to uniquely \\\n",
"identify the item from the menu.\\\n",
"You respond in a short, very conversational friendly style. \\\n",
"The menu includes \\\n",
"pepperoni pizza 12.95, 10.00, 7.00 \\\n",
"cheese pizza 10.95, 9.25, 6.50 \\\n",
"eggplant pizza 11.95, 9.75, 6.75 \\\n",
"fries 4.50, 3.50 \\\n",
"greek salad 7.25 \\\n",
"Toppings: \\\n",
"extra cheese 2.00, \\\n",
"mushrooms 1.50 \\\n",
"sausage 3.00 \\\n",
"canadian bacon 3.50 \\\n",
"AI sauce 1.50 \\\n",
"peppers 1.00 \\\n",
"Drinks: \\\n",
"coke 3.00, 2.00, 1.00 \\\n",
"sprite 3.00, 2.00, 1.00 \\\n",
"bottled water 5.00 \\\n",
"\"\"\"} ] # accumulate messages\n",
"\n",
"\n",
"inp = pn.widgets.TextInput(value=\"Hi\", placeholder='Enter text here…')\n",
"button_conversation = pn.widgets.Button(name=\"Chat!\")\n",
"\n",
"interactive_conversation = pn.bind(collect_messages, button_conversation)\n",
"\n",
"dashboard = pn.Column(\n",
" inp,\n",
" pn.Row(button_conversation),\n",
" pn.panel(interactive_conversation, loading_indicator=True, height=300),\n",
")\n",
"\n",
"dashboard"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0ee11b9",
"metadata": {},
"outputs": [],
"source": [
"messages = context.copy()\n",
"messages.append(\n",
"{'role':'system', 'content':'create a json summary of the previous food order. Itemize the price for each item\\\n",
" The fields should be 1) pizza, include size 2) list of toppings 3) list of drinks, include size 4) list of sides include size 5)total price '}, \n",
")\n",
" #The fields should be 1) pizza, price 2) list of toppings 3) list of drinks, include size include price 4) list of sides include size include price, 5)total price '}, \n",
"\n",
"response = get_completion_from_messages(messages, temperature=0)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "3153c581-1c72-497a-9293-8db3bcb804fc",
"metadata": {},
"source": [
"## Try experimenting on your own!\n",
"\n",
"You can modify the menu or instructions to create your own orderbot!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2cc84122",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment