File size: 4,932 Bytes
5fdb69e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "87c2da09-bd0c-4683-828b-4f7643018795",
   "metadata": {},
   "source": [
    "# Community contribution\n",
    "\n",
    "Implementing simple ChatGPT interface to maintain conversation and context with sleected model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "77a850ed-61f8-4a0d-9c41-45781eb60bc9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "API key looks good so far\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "import ipywidgets as widgets\n",
    "from IPython.display import Markdown, display, update_display, clear_output\n",
    "from openai import OpenAI\n",
    "\n",
    "load_dotenv()\n",
    "api_key = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "if api_key and api_key.startswith('sk-proj-') and len(api_key)>10:\n",
    "    print(\"API key looks good so far\")\n",
    "else:\n",
    "    print(\"There might be a problem with your API key? Please visit the troubleshooting notebook!\")\n",
    "    \n",
    "MODEL = 'gpt-4o-mini'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f7f16f0-6fec-4190-882a-3fe1f0e9704a",
   "metadata": {},
   "outputs": [],
   "source": [
    "class ChatGPTInterface:\n",
    "    def __init__(self, api_key, model, system_message=\"You are a helpful assistant. You can format your responses using Markdown.\"):\n",
    "        self.openai = OpenAI(api_key=api_key)\n",
    "        self.model = model\n",
    "        self.conversation_history = [{\"role\": \"system\", \"content\": system_message}]\n",
    "\n",
    "        self.chat_area = widgets.Output()\n",
    "        self.input_box = widgets.Text(placeholder=\"Enter your message here...\")\n",
    "        self.send_button = widgets.Button(description=\"Send\")\n",
    "        self.clear_button = widgets.Button(description=\"Clear\")\n",
    "\n",
    "        self.send_button.on_click(self.send_message)\n",
    "        self.clear_button.on_click(self.clear_chat)\n",
    "\n",
    "        self.layout = widgets.VBox([\n",
    "            self.chat_area,\n",
    "            widgets.HBox([self.input_box, self.send_button, self.clear_button])\n",
    "        ])\n",
    "\n",
    "    def display(self):\n",
    "        display(self.layout)\n",
    "\n",
    "    def send_message(self, _):\n",
    "        user_message = self.input_box.value.strip()\n",
    "        if user_message:\n",
    "            self.conversation_history.append({\"role\": \"user\", \"content\": user_message})\n",
    "            self.display_message(\"You\", user_message)\n",
    "            self.input_box.value = \"\"\n",
    "\n",
    "            try:\n",
    "                response = self.openai.chat.completions.create(\n",
    "                    model=self.model,\n",
    "                    messages=self.conversation_history\n",
    "                )\n",
    "                assistant_message = response.choices[0].message.content.strip()\n",
    "                self.conversation_history.append({\"role\": \"assistant\", \"content\": assistant_message})\n",
    "                self.display_message(\"ChatGPT\", assistant_message)\n",
    "            except Exception as e:\n",
    "                self.display_message(\"Error\", str(e))\n",
    "\n",
    "    def clear_chat(self, _):\n",
    "        self.conversation_history = [{\"role\": \"system\", \"content\": self.conversation_history[0][\"content\"]}]\n",
    "        self.chat_area.clear_output(wait=True)\n",
    "\n",
    "    def display_message(self, sender, message):\n",
    "        self.chat_area.append_display_data(Markdown(f\"**{sender}:**\\n{message}\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "78287e42-8964-4da6-bd48-a7dffd0ce7dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "54956535cb32419bbe38d2bee125992d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Output(), HBox(children=(Text(value='', placeholder='Enter your message here...'), Button(descr…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "chat_interface = ChatGPTInterface(api_key,MODEL)\n",
    "chat_interface.display()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}