Browse Source

Add files via upload

Added additional tasks and fixed last function and Gradio call to use type='messages'
pull/6/head
Dorotea Poljak 7 months ago committed by GitHub
parent
commit
67070b855e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 231
      week2/day3.ipynb

231
week2/day3.ipynb

@ -10,7 +10,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "70e39cd8-ec79-4e3e-9c26-5659d42d0861",
"metadata": {},
"outputs": [],
@ -25,7 +25,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "231605aa-fccb-447e-89cf-8b187444536a",
"metadata": {},
"outputs": [],
@ -40,7 +40,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "6541d58e-2297-4de1-b1f7-77da1b98b8bb",
"metadata": {},
"outputs": [],
@ -53,7 +53,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "e16839b5-c03b-4d9d-add6-87a0f6f37575",
"metadata": {},
"outputs": [],
@ -93,7 +93,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"id": "1eacc8a4-4b48-4358-9e06-ce0020041bc1",
"metadata": {},
"outputs": [],
@ -128,17 +128,69 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "0866ca56-100a-44ab-8bd0-1568feaf6bf2",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\doroteap\\Documents\\Projects\\llm_engineering\\venv\\Lib\\site-packages\\gradio\\components\\chatbot.py:229: UserWarning: The 'tuples' format for chatbot messages is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead, which uses openai-style 'role' and 'content' keys.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7864\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"History is:\n",
"[]\n",
"And messages is:\n",
"[{'role': 'system', 'content': 'You are a helpful assistant'}, {'role': 'user', 'content': 'hey'}]\n",
"History is:\n",
"[['hey', 'Hello! How can I assist you today?']]\n",
"And messages is:\n",
"[{'role': 'system', 'content': 'You are a helpful assistant'}, {'role': 'user', 'content': 'hey'}, {'role': 'assistant', 'content': 'Hello! How can I assist you today?'}, {'role': 'user', 'content': \"why don't you like the new messages format?\"}]\n"
]
}
],
"source": [
"gr.ChatInterface(fn=chat).launch()"
"gr.ChatInterface(chat).launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"id": "1f91b414-8bab-472d-b9c9-3fa51259bdfe",
"metadata": {},
"outputs": [],
@ -152,7 +204,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"id": "4e5be3ec-c26c-42bc-ac16-c39d369883f6",
"metadata": {},
"outputs": [],
@ -174,17 +226,55 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "413e9e4e-7836-43ac-a0c3-e1ab5ed6b136",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\doroteap\\Documents\\Projects\\llm_engineering\\venv\\Lib\\site-packages\\gradio\\components\\chatbot.py:229: UserWarning: The 'tuples' format for chatbot messages is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead, which uses openai-style 'role' and 'content' keys.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7865\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7865/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gr.ChatInterface(fn=chat).launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"id": "d75f0ffa-55c8-4152-b451-945021676837",
"metadata": {},
"outputs": [],
@ -195,35 +285,74 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"id": "c602a8dd-2df7-4eb7-b539-4e01865a6351",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\doroteap\\Documents\\Projects\\llm_engineering\\venv\\Lib\\site-packages\\gradio\\components\\chatbot.py:229: UserWarning: The 'tuples' format for chatbot messages is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead, which uses openai-style 'role' and 'content' keys.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7866\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7866/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gr.ChatInterface(fn=chat).launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 38,
"id": "0a987a66-1061-46d6-a83a-a30859dc88bf",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}]\n",
" for user_message, assistant_message in history:\n",
" messages.append({\"role\": \"user\", \"content\": user_message})\n",
" messages.append({\"role\": \"assistant\", \"content\": assistant_message})\n",
"\n",
" if 'belt' in message:\n",
" messages.append({\"role\": \"system\", \"content\": \"For added context, the store does not sell belts, \\\n",
"but be sure to point out other items on sale\"})\n",
" \n",
" messages.append({\"role\": \"user\", \"content\": message})\n",
" content = [{\"role\": \"user\", \"content\": \"You are a helpful assistant in a clothes store. You should try to gently encourage \\\n",
"the customer to try items that are on sale. Hats are 60% off, and most other items are 50% off. \\\n",
"For example, if the customer says 'I'm looking to buy a hat', \\\n",
"you could reply something like, 'Wonderful - we have lots of hats - including several that are part of our sales evemt.'\\\n",
"Encourage the customer to buy hats if they are unsure what to get. \\\n",
"If the customer asks for a belt, be sure to say we don't sell them, but offer some hats instead.\"},\n",
" {\"role\": \"assistant\", \"content\": \"Hello, how can I help you today?\"}]\n",
" for messages in history: \n",
" content.append({\"role\": \"user\", \"content\": message})\n",
"\n",
" stream = openai.chat.completions.create(model=MODEL, messages=messages, stream=True)\n",
" stream = openai.chat.completions.create(model=MODEL, messages=content, stream=True)\n",
"\n",
" print(content)\n",
" \n",
" response = \"\"\n",
" for chunk in stream:\n",
" response += chunk.choices[0].delta.content or ''\n",
@ -232,12 +361,52 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 39,
"id": "20570de2-eaad-42cc-a92c-c779d71b48b6",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7874\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7874/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'role': 'user', 'content': \"You are a helpful assistant in a clothes store. You should try to gently encourage the customer to try items that are on sale. Hats are 60% off, and most other items are 50% off. For example, if the customer says 'I'm looking to buy a hat', you could reply something like, 'Wonderful - we have lots of hats - including several that are part of our sales evemt.'Encourage the customer to buy hats if they are unsure what to get. If the customer asks for a belt, be sure to say we don't sell them, but offer some hats instead.\"}, {'role': 'assistant', 'content': 'Hello, how can I help you today?'}]\n",
"[{'role': 'user', 'content': \"You are a helpful assistant in a clothes store. You should try to gently encourage the customer to try items that are on sale. Hats are 60% off, and most other items are 50% off. For example, if the customer says 'I'm looking to buy a hat', you could reply something like, 'Wonderful - we have lots of hats - including several that are part of our sales evemt.'Encourage the customer to buy hats if they are unsure what to get. If the customer asks for a belt, be sure to say we don't sell them, but offer some hats instead.\"}, {'role': 'assistant', 'content': 'Hello, how can I help you today?'}, {'role': 'user', 'content': 'what do you have?'}, {'role': 'user', 'content': 'what do you have?'}]\n",
"[{'role': 'user', 'content': \"You are a helpful assistant in a clothes store. You should try to gently encourage the customer to try items that are on sale. Hats are 60% off, and most other items are 50% off. For example, if the customer says 'I'm looking to buy a hat', you could reply something like, 'Wonderful - we have lots of hats - including several that are part of our sales evemt.'Encourage the customer to buy hats if they are unsure what to get. If the customer asks for a belt, be sure to say we don't sell them, but offer some hats instead.\"}, {'role': 'assistant', 'content': 'Hello, how can I help you today?'}, {'role': 'user', 'content': \"I'd like to but a belt\"}, {'role': 'user', 'content': \"I'd like to but a belt\"}, {'role': 'user', 'content': \"I'd like to but a belt\"}, {'role': 'user', 'content': \"I'd like to but a belt\"}]\n",
"[{'role': 'user', 'content': \"You are a helpful assistant in a clothes store. You should try to gently encourage the customer to try items that are on sale. Hats are 60% off, and most other items are 50% off. For example, if the customer says 'I'm looking to buy a hat', you could reply something like, 'Wonderful - we have lots of hats - including several that are part of our sales evemt.'Encourage the customer to buy hats if they are unsure what to get. If the customer asks for a belt, be sure to say we don't sell them, but offer some hats instead.\"}, {'role': 'assistant', 'content': 'Hello, how can I help you today?'}, {'role': 'user', 'content': \"you don't sell belts?\"}, {'role': 'user', 'content': \"you don't sell belts?\"}, {'role': 'user', 'content': \"you don't sell belts?\"}, {'role': 'user', 'content': \"you don't sell belts?\"}, {'role': 'user', 'content': \"you don't sell belts?\"}, {'role': 'user', 'content': \"you don't sell belts?\"}]\n"
]
}
],
"source": [
"gr.ChatInterface(fn=chat).launch()"
"gr.ChatInterface(fn=chat, type='messages').launch()"
]
},
{
@ -265,7 +434,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.10"
"version": "3.12.0"
}
},
"nbformat": 4,

Loading…
Cancel
Save