Skip to content

Commit

Permalink
feat: add Chat api
Browse files Browse the repository at this point in the history
  • Loading branch information
solygambas committed Apr 30, 2023
1 parent 9badd2b commit d64e0a4
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ Follow these steps: [How to Work with Python Virtual Environments, Jupyter Noteb

[Check the playground](playground/05-important-api-parameters.ipynb)

### The Chat API & GPT-4

- writing a request to the Chat API.
- prompting with system, assistant, and user messages.
- rewriting a completion prompt in chat format.
- using Chat API parameters.

[Check the playground](playground/06-chat-api-gpt-4.ipynb)

Based on [Mastering OpenAI Python APIs: Unleash the Power of GPT4](https://www.udemy.com/course/mastering-openai/) by Colt Steele (2023).
177 changes: 177 additions & 0 deletions playground/06-chat-api-gpt-4.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import openai"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from dotenv import dotenv_values\n",
"config = dotenv_values(\".env\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"openai.api_key = config[\"OPENAI_API_KEY\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reply = openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" { \"role\": \"user\", \"content\": \"tell me a joke\" }\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(reply.choices[O].message.content)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# https://platform.openai.com/docs/guides/chat/chat-vs-completions\n",
"\n",
"openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" # {\"role\": \"system\", \"content\": \"You are a helpful assistant that translates English to French.\"},\n",
" # {\"role\": \"user\", \"content\": 'Translate the following English text to French: \"I want a dog\"'}\n",
" {\"role\": \"user\", \"content\": 'Translate the following English text to French: \"I want a dog\"'}\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant that classifies the sentiment in text as either positive, neutral, or negative.\"},\n",
" {\"role\": \"user\", \"content\": 'Classify the sentiment in the following text: \"I really love chicken biryani\"'},\n",
" {\"role\": \"assistant\", \"content\": \"Positive\" },\n",
" {\"role\": \"user\", \"content\": 'Classify the sentiment in the following text: \"I have a complicated relationship with cat\"'},\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import Markdown, display\n",
"import json\n",
"\n",
"def display_colors(colors):\n",
" display(Markdown(\" \".join(\n",
" f'<span style=\"color: {color}\">{chr(9608) * 4}</span>'\n",
" for color in colors\n",
" )))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def get_and_render_colors(query):\n",
" messages = [\n",
" { \"role\": \"system\", \"content\": \"You are a color palette generating assistant that responds to text prompts for color palettes. You should generate color palettes that fit the theme, mood, or instructions in the prompt. The palettes should be between 2 and 8 colors.\" },\n",
" { \"role\": \"user\", \"content\": \"Convert the following verbal description of a color palette into a list of colors: The Meditation\" },\n",
" { \"role\": \"assistant\", \"content\": '[\"#594F4F\", \"#A1887F\", \"#BCAAA4\", \"#81C784\", \"#455A64\"]' },\n",
" { \"role\": \"user\", \"content\": \"Convert the following verbal description of a color palette into a list of colors: The Mediterranean Sea\" },\n",
" { \"role\": \"assistant\", \"content\": '[\"#1A237E\", \"#1565C0\", \"#1E88E5\", \"#90CAF9\", \"#E1F5FE\"]' },\n",
" { \"role\": \"user\", \"content\": f\"Convert the following verbal description of a color palette into a list of colors: {query}\"}\n",
" ]\n",
"\n",
" response = openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=messages,\n",
" max_tokens=100 # default to infinite, provide it!\n",
" )\n",
"\n",
" colors = json.loads(response[\"choices\"][0].message.content)\n",
" display_colors(colors)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"<span style=\"color: #4285F4\">████</span> <span style=\"color: #DB4437\">████</span> <span style=\"color: #F4B400\">████</span> <span style=\"color: #0F9D58\">████</span>"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"get_and_render_colors(\"4 google brand colors\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.5"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
9 changes: 9 additions & 0 deletions playground/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,13 @@ Follow these steps: [How to Work with Python Virtual Environments, Jupyter Noteb

[Check the notebook](05-important-api-parameters.ipynb)

### The Chat API & GPT-4

- writing a request to the Chat API.
- prompting with system, assistant, and user messages.
- rewriting a completion prompt in chat format.
- using Chat API parameters.

[Check the notebook](06-chat-api-gpt-4.ipynb)

Based on [Mastering OpenAI Python APIs: Unleash the Power of GPT4](https://www.udemy.com/course/mastering-openai/) by Colt Steele (2023).

0 comments on commit d64e0a4

Please sign in to comment.