|
1 | 1 | {
|
2 | 2 | "cells": [
|
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Step #1 Imports and Setting the OpenAI Key" |
| 8 | + ] |
| 9 | + }, |
3 | 10 | {
|
4 | 11 | "cell_type": "code",
|
5 |
| - "execution_count": 3, |
| 12 | + "execution_count": 2, |
6 | 13 | "metadata": {},
|
7 | 14 | "outputs": [
|
8 | 15 | {
|
9 | 16 | "name": "stdout",
|
10 | 17 | "output_type": "stream",
|
11 | 18 | "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" |
16 | 20 | ]
|
17 | 21 | }
|
18 | 22 | ],
|
19 | 23 | "source": [
|
20 | 24 | "import openai\n",
|
21 | 25 | "import datetime as dt\n",
|
22 |
| - "import logging\n", |
23 | 26 | "from azure.identity import AzureCliCredential\n",
|
24 | 27 | "from azure.keyvault.secrets import SecretClient\n",
|
25 |
| - "import tiktoken\n", |
26 | 28 | "import time\n",
|
27 | 29 | "import os\n",
|
28 | 30 | "\n",
|
29 |
| - "# Use environment variables for API key\n", |
| 31 | + "# use environment variables for API key\n", |
30 | 32 | "timestamp = dt.datetime.now().strftime(\"%Y%m%d_%H%M%S\")\n",
|
31 | 33 | "\n",
|
32 |
| - "# Set your OpenAI API key here\n", |
| 34 | + "# set your OpenAI API key here\n", |
33 | 35 | "API_KEY = os.environ.get(\"OPENAI_API_KEY\")\n",
|
34 | 36 | "if API_KEY is None:\n",
|
35 | 37 | " print('trying to get API key from azure keyvault')\n",
|
|
38 | 40 | " API_KEY = client.get_secret('openai-api-key').value\n",
|
39 | 41 | "openai.api_key = API_KEY\n",
|
40 | 42 | "\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", |
44 | 45 | "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", |
48 | 64 | " 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", |
50 | 66 | " task = f'Good day, Sir.'\n",
|
51 | 67 | " if topic != '':\n",
|
52 | 68 | " task = task + f' Wonderful day isn t it?'\n",
|
53 | 69 | " return instructions, task\n",
|
54 | 70 | "\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", |
56 | 73 | " instructions = f'You have a conversation with someone on {topic}. \\\n",
|
57 | 74 | " Reply to questions and bring up any topic that comes to your mind.\\\n",
|
58 | 75 | " Dont say more than 2 sentences at a time.'\n",
|
59 |
| - " instructions = person['description'] + instructions\n", |
| 76 | + " instructions = character['description'] + instructions\n", |
60 | 77 | " task = f'{response}' \n",
|
61 | 78 | " return instructions, task\n",
|
62 | 79 | "\n",
|
63 |
| - "#### OpenAI Engine\n", |
| 80 | + "# OpenAI Engine using the turbo model\n", |
64 | 81 | "def openai_request(instructions, task, model_engine='gpt-3.5-turbo'):\n",
|
65 | 82 | " prompt = [{\"role\": \"system\", \"content\": instructions }, \n",
|
66 | 83 | " {\"role\": \"user\", \"content\": task }]\n",
|
67 | 84 | "\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", |
79 | 85 | " #print('Generating response from OpenAI...')\n",
|
80 | 86 | " completion = openai.ChatCompletion.create(\n",
|
81 | 87 | " model=model_engine, \n",
|
|
85 | 91 | "\n",
|
86 | 92 | " response = completion.choices[0].message.content\n",
|
87 | 93 | "\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": [ |
91 | 111 | "# initialize conversation on the following topic\n",
|
92 | 112 | "topic = 'The sense of life'\n",
|
93 | 113 | "conversation_rounds = 20\n",
|
94 | 114 | "\n",
|
95 |
| - "# description of person 1\n", |
| 115 | + "# description of character 1\n", |
96 | 116 | "color_1 = 'darkblue' \n",
|
97 |
| - "person_1 = {\n", |
| 117 | + "character_1 = {\n", |
98 | 118 | "\"name\": 'James (Aristocrat)',\n",
|
99 | 119 | "\"description\": 'You are a French nobleman from the 18th century. \\\n",
|
100 | 120 | " Your knowledge and wordlview corresponds to that of a common aristocrate from that time. \\\n",
|
101 | 121 | " You speak in a distinguished manner. \\\n",
|
102 | 122 | " You response in one or two sentences. \\\n",
|
103 | 123 | " You are afraid of pirates but also curious to meet one.'}\n",
|
104 | 124 | "\n",
|
105 |
| - "# description of person 2 \n", |
| 125 | + "# description of character 2 \n", |
106 | 126 | "color_2 = 'brown'\n",
|
107 |
| - "person_2 = {\n", |
| 127 | + "character_2 = {\n", |
108 | 128 | "\"name\": 'Blackbeard (Pirate)',\n",
|
109 | 129 | "\"description\": 'You are a devious pirate from the 18th century who tends to swear. \\\n",
|
110 | 130 | " Your knowledge and wordlview corresponds to that of a common pirate from that time. \\\n",
|
111 | 131 | " You response in one or two sentences. \\\n",
|
112 | 132 | " 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": [ |
117 | 161 | "conversation = ''\n",
|
118 | 162 | "for i in range(conversation_rounds):\n",
|
119 | 163 | " # initialize conversation\n",
|
120 | 164 | " if i == 0:\n",
|
121 | 165 | " print('Initializing conversation...')\n",
|
122 | 166 | " 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", |
125 | 169 | " response = openai_request(instructions, task)\n",
|
126 | 170 | " print(f'{name}: {task}')\n",
|
127 | 171 | " 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", |
129 | 173 | " else:\n",
|
130 | 174 | " if i % 2 == 0:\n",
|
131 | 175 | " 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", |
134 | 178 | " else:\n",
|
135 | 179 | " 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", |
138 | 182 | "\n",
|
139 | 183 | " # OpenAI request\n",
|
140 | 184 | " response = openai_request(instructions, task)\n",
|
141 | 185 | "\n",
|
142 |
| - " # wait some seconds\n", |
| 186 | + " # wait some seconds \n", |
143 | 187 | " time.sleep(15)\n",
|
| 188 | + "\n", |
144 | 189 | " # add response to conversation after linebreak\n",
|
145 | 190 | " print(f'{name}: {response}')\n",
|
146 | 191 | " conversation += ' ' + f'<p style=\"color: {text_color};\"><b>{name}</b>: {response}</p> \\n'\n",
|
|
0 commit comments