Skip to content

Commit f48aee7

Browse files
committed
Update 603 Conversation between OpenAI ChatGPT and Itself in Python.ipynb
1 parent 968ea8b commit f48aee7

File tree

1 file changed

+94
-49
lines changed

1 file changed

+94
-49
lines changed

603 Conversation between OpenAI ChatGPT and Itself in Python.ipynb

Lines changed: 94 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Step #1 Imports and Setting the OpenAI Key"
8+
]
9+
},
310
{
411
"cell_type": "code",
5-
"execution_count": 3,
12+
"execution_count": 2,
613
"metadata": {},
714
"outputs": [
815
{
916
"name": "stdout",
1017
"output_type": "stream",
1118
"text": [
12-
"trying to get API key from azure keyvault\n",
13-
"Initializing conversation...\n",
14-
"James (Aristocrat): Good day, Sir. Wonderful day isn t it?\n",
15-
"Blackbeard (Pirate): Arrr, I'm on a quest for a valuable treasure. Have ye heard any tales or rumors of where it might be hidden?\n"
19+
"trying to get API key from azure keyvault\n"
1620
]
1721
}
1822
],
1923
"source": [
2024
"import openai\n",
2125
"import datetime as dt\n",
22-
"import logging\n",
2326
"from azure.identity import AzureCliCredential\n",
2427
"from azure.keyvault.secrets import SecretClient\n",
25-
"import tiktoken\n",
2628
"import time\n",
2729
"import os\n",
2830
"\n",
29-
"# Use environment variables for API key\n",
31+
"# use environment variables for API key\n",
3032
"timestamp = dt.datetime.now().strftime(\"%Y%m%d_%H%M%S\")\n",
3133
"\n",
32-
"# Set your OpenAI API key here\n",
34+
"# set your OpenAI API key here\n",
3335
"API_KEY = os.environ.get(\"OPENAI_API_KEY\")\n",
3436
"if API_KEY is None:\n",
3537
" print('trying to get API key from azure keyvault')\n",
@@ -38,44 +40,48 @@
3840
" API_KEY = client.get_secret('openai-api-key').value\n",
3941
"openai.api_key = API_KEY\n",
4042
"\n",
41-
"# save conversation to html file\n",
42-
"# create folder \"conversations\" if it does not exist\n",
43-
"path = 'GPT_conversations'\n",
43+
"# create a folder to store the conversations if it does not exist\n",
44+
"path = 'ChatGPT_conversations'\n",
4445
"if not os.path.exists(path):\n",
45-
" os.makedirs(path)\n",
46-
"\n",
47-
"def initialize_conversation(topic='', person=''):\n",
46+
" os.makedirs(path)"
47+
]
48+
},
49+
{
50+
"cell_type": "markdown",
51+
"metadata": {},
52+
"source": [
53+
"# Step #2: Prompts and OpenAI Completion"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 3,
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"# This function creates a prompt that initializes the conversation \n",
63+
"def initialize_conversation(topic='', character=''):\n",
4864
" instructions = f' You have a conversation on {topic}. You can bring up any topic that comes to your mind'\n",
49-
" instructions = person['description'] + instructions\n",
65+
" instructions = character['description'] + instructions\n",
5066
" task = f'Good day, Sir.'\n",
5167
" if topic != '':\n",
5268
" task = task + f' Wonderful day isn t it?'\n",
5369
" return instructions, task\n",
5470
"\n",
55-
"def respond_prompt(response, topic='', person=''): \n",
71+
"# This function creates a prompt that responds to the previous response\n",
72+
"def respond_prompt(response, topic='', character=''): \n",
5673
" instructions = f'You have a conversation with someone on {topic}. \\\n",
5774
" Reply to questions and bring up any topic that comes to your mind.\\\n",
5875
" Dont say more than 2 sentences at a time.'\n",
59-
" instructions = person['description'] + instructions\n",
76+
" instructions = character['description'] + instructions\n",
6077
" task = f'{response}' \n",
6178
" return instructions, task\n",
6279
"\n",
63-
"#### OpenAI Engine\n",
80+
"# OpenAI Engine using the turbo model\n",
6481
"def openai_request(instructions, task, model_engine='gpt-3.5-turbo'):\n",
6582
" prompt = [{\"role\": \"system\", \"content\": instructions }, \n",
6683
" {\"role\": \"user\", \"content\": task }]\n",
6784
"\n",
68-
" # Load an encoding object\n",
69-
" enc = tiktoken.get_encoding(\"cl100k_base\")\n",
70-
"\n",
71-
" # Tokenize the input text\n",
72-
" tokens = enc.encode(str(prompt))\n",
73-
"\n",
74-
" # Count the number of tokens\n",
75-
" token_count = len(tokens)\n",
76-
" max_tokens = 4097 - token_count\n",
77-
" #print(f'max tokens: {max_tokens}')\n",
78-
"\n",
7985
" #print('Generating response from OpenAI...')\n",
8086
" completion = openai.ChatCompletion.create(\n",
8187
" model=model_engine, \n",
@@ -85,62 +91,101 @@
8591
"\n",
8692
" response = completion.choices[0].message.content\n",
8793
"\n",
88-
" return response\n",
89-
"\n",
90-
"\n",
94+
" return response"
95+
]
96+
},
97+
{
98+
"attachments": {},
99+
"cell_type": "markdown",
100+
"metadata": {},
101+
"source": [
102+
"# Step #3 Configure the Characters"
103+
]
104+
},
105+
{
106+
"cell_type": "code",
107+
"execution_count": 4,
108+
"metadata": {},
109+
"outputs": [],
110+
"source": [
91111
"# initialize conversation on the following topic\n",
92112
"topic = 'The sense of life'\n",
93113
"conversation_rounds = 20\n",
94114
"\n",
95-
"# description of person 1\n",
115+
"# description of character 1\n",
96116
"color_1 = 'darkblue' \n",
97-
"person_1 = {\n",
117+
"character_1 = {\n",
98118
"\"name\": 'James (Aristocrat)',\n",
99119
"\"description\": 'You are a French nobleman from the 18th century. \\\n",
100120
" Your knowledge and wordlview corresponds to that of a common aristocrate from that time. \\\n",
101121
" You speak in a distinguished manner. \\\n",
102122
" You response in one or two sentences. \\\n",
103123
" You are afraid of pirates but also curious to meet one.'}\n",
104124
"\n",
105-
"# description of person 2 \n",
125+
"# description of character 2 \n",
106126
"color_2 = 'brown'\n",
107-
"person_2 = {\n",
127+
"character_2 = {\n",
108128
"\"name\": 'Blackbeard (Pirate)',\n",
109129
"\"description\": 'You are a devious pirate from the 18th century who tends to swear. \\\n",
110130
" Your knowledge and wordlview corresponds to that of a common pirate from that time. \\\n",
111131
" You response in one or two sentences. \\\n",
112132
" You are looking for a valuable treasure and trying to find where it is hidden. \\\n",
113-
" You try to steer the conversation back to the treasure no matter what.'}\n",
114-
"\n",
115-
"\n",
116-
"# start the conversation\n",
133+
" You try to steer the conversation back to the treasure no matter what.'}"
134+
]
135+
},
136+
{
137+
"attachments": {},
138+
"cell_type": "markdown",
139+
"metadata": {},
140+
"source": [
141+
"# Step #4 Start the Conversation"
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": 5,
147+
"metadata": {},
148+
"outputs": [
149+
{
150+
"name": "stdout",
151+
"output_type": "stream",
152+
"text": [
153+
"Initializing conversation...\n",
154+
"James (Aristocrat): Good day, Sir. Wonderful day isn t it?\n",
155+
"Blackbeard (Pirate): Aye, pirates be a scourge of the seas. But I be more interested in treasure, not tales of plunder. Have ye ever heard of a valuable booty hidden away around these parts?\n",
156+
"James (Aristocrat): Oh, my dear fellow, one's true treasure lies in the finer things of life: love, beauty, art, and culture. These are the things that bring joy and meaning to one's existence.\n"
157+
]
158+
}
159+
],
160+
"source": [
117161
"conversation = ''\n",
118162
"for i in range(conversation_rounds):\n",
119163
" # initialize conversation\n",
120164
" if i == 0:\n",
121165
" print('Initializing conversation...')\n",
122166
" text_color = color_1\n",
123-
" name = person_1['name']\n",
124-
" instructions, task = initialize_conversation(topic, person_1)\n",
167+
" name = character_1['name']\n",
168+
" instructions, task = initialize_conversation(topic, character_1)\n",
125169
" response = openai_request(instructions, task)\n",
126170
" print(f'{name}: {task}')\n",
127171
" conversation = f'<p style=\"color: {text_color};\"><b>{name}</b>: {task}</p> \\n'\n",
128-
" # alternate between person_1 and person_2\n",
172+
" # alternate between character_1 and character_2\n",
129173
" else:\n",
130174
" if i % 2 == 0:\n",
131175
" text_color = color_1\n",
132-
" name = person_1['name']\n",
133-
" instructions, task = respond_prompt(response, topic, person_1)\n",
176+
" name = character_1['name']\n",
177+
" instructions, task = respond_prompt(response, topic, character_1)\n",
134178
" else:\n",
135179
" text_color = color_2\n",
136-
" name = person_2['name']\n",
137-
" instructions, task = respond_prompt(response, topic, person_2)\n",
180+
" name = character_2['name']\n",
181+
" instructions, task = respond_prompt(response, topic, character_2)\n",
138182
"\n",
139183
" # OpenAI request\n",
140184
" response = openai_request(instructions, task)\n",
141185
"\n",
142-
" # wait some seconds\n",
186+
" # wait some seconds \n",
143187
" time.sleep(15)\n",
188+
"\n",
144189
" # add response to conversation after linebreak\n",
145190
" print(f'{name}: {response}')\n",
146191
" conversation += ' ' + f'<p style=\"color: {text_color};\"><b>{name}</b>: {response}</p> \\n'\n",

0 commit comments

Comments
 (0)