Skip to content

Commit ef0605b

Browse files
committed
feat: add simple ChatGPT clone
1 parent 8dabd41 commit ef0605b

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import openai"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 2,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from dotenv import dotenv_values\n",
19+
"config = dotenv_values(\".env\")"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 3,
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"openai.api_key = config[\"OPENAI_API_KEY\"]"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 5,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"completion = openai.ChatCompletion.create(\n",
38+
" model=\"gpt-3.5-turbo\",\n",
39+
" messages=[\n",
40+
" {\"role\": \"user\", \"content\": \"hello!\"}\n",
41+
" ],\n",
42+
" max_tokens=10\n",
43+
")"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 6,
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"name": "stdout",
53+
"output_type": "stream",
54+
"text": [
55+
"{\n",
56+
" \"content\": \"Hello! How can I assist you today?\",\n",
57+
" \"role\": \"assistant\"\n",
58+
"}\n"
59+
]
60+
}
61+
],
62+
"source": [
63+
"print(completion.choices[0].message)"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"chat_messages = []\n",
73+
"\n",
74+
"while True:\n",
75+
" user_message = input(\"You: \")\n",
76+
"\n",
77+
" chat_messages.append({\"role\": \"user\", \"content\": user_message})\n",
78+
"\n",
79+
" response = openai.ChatCompletion.create(\n",
80+
" model=\"gpt-3.5-turbo\",\n",
81+
" messages=chat_messages,\n",
82+
" max_tokens=50\n",
83+
" )\n",
84+
"\n",
85+
" ai_response = completion.choices[0].message.content\n",
86+
" chat_messages.append({\"role\": \"assistant\", \"content\": ai_response})\n",
87+
" print('AI: ', ai_response)"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"metadata": {},
94+
"outputs": [],
95+
"source": []
96+
}
97+
],
98+
"metadata": {
99+
"kernelspec": {
100+
"display_name": ".venv",
101+
"language": "python",
102+
"name": "python3"
103+
},
104+
"language_info": {
105+
"codemirror_mode": {
106+
"name": "ipython",
107+
"version": 3
108+
},
109+
"file_extension": ".py",
110+
"mimetype": "text/x-python",
111+
"name": "python",
112+
"nbconvert_exporter": "python",
113+
"pygments_lexer": "ipython3",
114+
"version": "3.10.5"
115+
},
116+
"orig_nbformat": 4
117+
},
118+
"nbformat": 4,
119+
"nbformat_minor": 2
120+
}

playground/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ For Stable Diffusion, you need to add your `STABILITY_AI_KEY`. You can find it i
118118
- creating a [Monster Slayer game](26-monster-slayer.py).
119119
- building a [NodeJS REST API](27-rest-api/app.js).
120120

121+
## Using GPT APIs
122+
123+
- recreating a [simple ChatGPT clone](28-simple-chatgpt-clone.ipynb).
124+
121125
These examples are based on two courses:
122126

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

0 commit comments

Comments
 (0)