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
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"nbformat": 4,
|
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": [],
|
7 |
+
"gpuType": "T4"
|
8 |
+
},
|
9 |
+
"kernelspec": {
|
10 |
+
"name": "python3",
|
11 |
+
"display_name": "Python 3"
|
12 |
+
},
|
13 |
+
"language_info": {
|
14 |
+
"name": "python"
|
15 |
+
},
|
16 |
+
"accelerator": "GPU"
|
17 |
+
},
|
18 |
+
"cells": [
|
19 |
+
{
|
20 |
+
"cell_type": "code",
|
21 |
+
"execution_count": 1,
|
22 |
+
"metadata": {
|
23 |
+
"id": "TFu_ibC1eYrz"
|
24 |
+
},
|
25 |
+
"outputs": [],
|
26 |
+
"source": [
|
27 |
+
"!pip install torch transformers -q"
|
28 |
+
]
|
29 |
+
},
|
30 |
+
{
|
31 |
+
"cell_type": "code",
|
32 |
+
"source": [
|
33 |
+
"import torch\n",
|
34 |
+
"from transformers import pipeline\n",
|
35 |
+
"from IPython.display import clear_output\n",
|
36 |
+
"from google.colab import output"
|
37 |
+
],
|
38 |
+
"metadata": {
|
39 |
+
"id": "Zs7QNs0Tet6r"
|
40 |
+
},
|
41 |
+
"execution_count": 2,
|
42 |
+
"outputs": []
|
43 |
+
},
|
44 |
+
{
|
45 |
+
"cell_type": "code",
|
46 |
+
"source": [
|
47 |
+
"class ChatBot:\n",
|
48 |
+
" _instance = None\n",
|
49 |
+
" _current_model = None\n",
|
50 |
+
"\n",
|
51 |
+
" def __init__(self, model_slug=None):\n",
|
52 |
+
" if model_slug and model_slug != ChatBot._current_model:\n",
|
53 |
+
" self.load_model(model_slug)\n",
|
54 |
+
" ChatBot._current_model = model_slug\n",
|
55 |
+
"\n",
|
56 |
+
" self.messages = []\n",
|
57 |
+
" self.max_tokens = 2048\n",
|
58 |
+
" self.temperature = 0.01\n",
|
59 |
+
" self.top_k = 100\n",
|
60 |
+
" self.top_p = 0.95\n",
|
61 |
+
"\n",
|
62 |
+
" @classmethod\n",
|
63 |
+
" def get_instance(cls, model_slug=None):\n",
|
64 |
+
" if not cls._instance or (model_slug and model_slug != cls._current_model):\n",
|
65 |
+
" cls._instance = cls(model_slug)\n",
|
66 |
+
" return cls._instance\n",
|
67 |
+
"\n",
|
68 |
+
" def load_model(self, model_slug):\n",
|
69 |
+
" print(f\"Loading model {model_slug}...\")\n",
|
70 |
+
" self.pipeline = pipeline(\n",
|
71 |
+
" \"text-generation\",\n",
|
72 |
+
" model=model_slug,\n",
|
73 |
+
" device_map=\"auto\",\n",
|
74 |
+
" )\n",
|
75 |
+
" clear_output()\n",
|
76 |
+
" print(\"Model loaded successfully!\")\n",
|
77 |
+
"\n",
|
78 |
+
" def reset_conversation(self, system_message):\n",
|
79 |
+
" \"\"\"Reset the conversation with a new system message\"\"\"\n",
|
80 |
+
" self.messages = [{\"role\": \"system\", \"content\": system_message}]\n",
|
81 |
+
"\n",
|
82 |
+
" def get_response(self, user_input):\n",
|
83 |
+
" \"\"\"Get response with current parameters\"\"\"\n",
|
84 |
+
" self.messages.append({\"role\": \"user\", \"content\": user_input})\n",
|
85 |
+
" outputs = self.pipeline(\n",
|
86 |
+
" self.messages,\n",
|
87 |
+
" max_new_tokens=self.max_tokens,\n",
|
88 |
+
" do_sample=True,\n",
|
89 |
+
" temperature=self.temperature,\n",
|
90 |
+
" top_k=self.top_k,\n",
|
91 |
+
" top_p=self.top_p\n",
|
92 |
+
" )\n",
|
93 |
+
" response = outputs[0][\"generated_text\"][-1]\n",
|
94 |
+
" content = response.get('content', 'No content available')\n",
|
95 |
+
" self.messages.append({\"role\": \"assistant\", \"content\": content})\n",
|
96 |
+
" return content\n",
|
97 |
+
"\n",
|
98 |
+
" def update_params(self, max_tokens=None, temperature=None, top_k=None, top_p=None):\n",
|
99 |
+
" \"\"\"Update generation parameters\"\"\"\n",
|
100 |
+
" if max_tokens is not None:\n",
|
101 |
+
" self.max_tokens = max_tokens\n",
|
102 |
+
" if temperature is not None:\n",
|
103 |
+
" self.temperature = temperature\n",
|
104 |
+
" if top_k is not None:\n",
|
105 |
+
" self.top_k = top_k\n",
|
106 |
+
" if top_p is not None:\n",
|
107 |
+
" self.top_p = top_p"
|
108 |
+
],
|
109 |
+
"metadata": {
|
110 |
+
"id": "v4uIN6uIeyl3"
|
111 |
+
},
|
112 |
+
"execution_count": 3,
|
113 |
+
"outputs": []
|
114 |
+
},
|
115 |
+
{
|
116 |
+
"cell_type": "code",
|
117 |
+
"source": [
|
118 |
+
"def run_chatbot(\n",
|
119 |
+
" model=None,\n",
|
120 |
+
" system_message=\"You are Orca Mini, You are expert in python coding, Think step by step before coming up with final answer\",\n",
|
121 |
+
" max_tokens=None,\n",
|
122 |
+
" temperature=None,\n",
|
123 |
+
" top_k=None,\n",
|
124 |
+
" top_p=None,\n",
|
125 |
+
"):\n",
|
126 |
+
" try:\n",
|
127 |
+
" # Get or create chatbot instance\n",
|
128 |
+
" chatbot = ChatBot.get_instance(model)\n",
|
129 |
+
"\n",
|
130 |
+
" # Update parameters if provided\n",
|
131 |
+
" chatbot.update_params(max_tokens, temperature, top_k, top_p)\n",
|
132 |
+
"\n",
|
133 |
+
" # Reset conversation with new system message\n",
|
134 |
+
" chatbot.reset_conversation(system_message)\n",
|
135 |
+
"\n",
|
136 |
+
" print(\"Chatbot: Hi! Type 'quit' to exit.\")\n",
|
137 |
+
"\n",
|
138 |
+
" while True:\n",
|
139 |
+
" user_input = input(\"You: \").strip()\n",
|
140 |
+
" if user_input.lower() == 'quit':\n",
|
141 |
+
" break\n",
|
142 |
+
" try:\n",
|
143 |
+
" response = chatbot.get_response(user_input)\n",
|
144 |
+
" print(\"Chatbot:\", response)\n",
|
145 |
+
" except Exception as e:\n",
|
146 |
+
" print(f\"Chatbot: An error occurred: {str(e)}\")\n",
|
147 |
+
" print(\"Please try again.\")\n",
|
148 |
+
"\n",
|
149 |
+
" except Exception as e:\n",
|
150 |
+
" print(f\"Error in chatbot: {str(e)}\")"
|
151 |
+
],
|
152 |
+
"metadata": {
|
153 |
+
"id": "H2n_6Xcue3Vn"
|
154 |
+
},
|
155 |
+
"execution_count": 4,
|
156 |
+
"outputs": []
|
157 |
+
},
|
158 |
+
{
|
159 |
+
"cell_type": "code",
|
160 |
+
"source": [
|
161 |
+
"run_chatbot(model=\"pankajmathur/orca_mini_v9_5_1B-Instruct\")"
|
162 |
+
],
|
163 |
+
"metadata": {
|
164 |
+
"colab": {
|
165 |
+
"base_uri": "https://localhost:8080/"
|
166 |
+
},
|
167 |
+
"id": "JEqgoAH2fC6h",
|
168 |
+
"outputId": "1b46231a-a4a3-4dfb-8705-100a98a50aae"
|
169 |
+
},
|
170 |
+
"execution_count": null,
|
171 |
+
"outputs": [
|
172 |
+
{
|
173 |
+
"output_type": "stream",
|
174 |
+
"name": "stdout",
|
175 |
+
"text": [
|
176 |
+
"Model loaded successfully!\n",
|
177 |
+
"Chatbot: Hi! Type 'quit' to exit.\n",
|
178 |
+
"You: solve famous leetcode 2 sum problem\n",
|
179 |
+
"Chatbot: Here's a Python solution for the LeetCode 2 Sum problem:\n",
|
180 |
+
"\n",
|
181 |
+
"```python\n",
|
182 |
+
"def two_sum(nums, target):\n",
|
183 |
+
" num_set = set()\n",
|
184 |
+
" for num in nums:\n",
|
185 |
+
" complement = target - num\n",
|
186 |
+
" if complement in num_set:\n",
|
187 |
+
" return [num, complement]\n",
|
188 |
+
" num_set.add(num)\n",
|
189 |
+
" return None\n",
|
190 |
+
"```\n",
|
191 |
+
"\n",
|
192 |
+
"This solution works by iterating through the input list `nums` and for each number, it calculates its complement (the number that would be needed to reach the target). If the complement is already in the set, it means we've found a pair that sums to the target, so we return this pair. If no such pair is found, we add the current number to the set and continue with the next number. If the target is not found, the function returns `None`.\n",
|
193 |
+
"\n",
|
194 |
+
"Here's an example usage:\n",
|
195 |
+
"\n",
|
196 |
+
"```python\n",
|
197 |
+
"nums = [2, 7, 11, 15]\n",
|
198 |
+
"target = 9\n",
|
199 |
+
"result = two_sum(nums, target)\n",
|
200 |
+
"if result:\n",
|
201 |
+
" print(f\"Pair found: {result}\")\n",
|
202 |
+
"else:\n",
|
203 |
+
" print(\"No pair found\")\n",
|
204 |
+
"```\n",
|
205 |
+
"\n",
|
206 |
+
"This will output:\n",
|
207 |
+
"\n",
|
208 |
+
"```\n",
|
209 |
+
"Pair found: [2, 7]\n",
|
210 |
+
"```\n"
|
211 |
+
]
|
212 |
+
}
|
213 |
+
]
|
214 |
+
},
|
215 |
+
{
|
216 |
+
"cell_type": "code",
|
217 |
+
"source": [
|
218 |
+
"# # change system message\n",
|
219 |
+
"# run_chatbot(\n",
|
220 |
+
"# system_message=\"You are Orca Mini, You are expert in logic, Think step by step before coming up with final answer\",\n",
|
221 |
+
"# max_tokens=1024,\n",
|
222 |
+
"# temperature=0.3\n",
|
223 |
+
"# )"
|
224 |
+
],
|
225 |
+
"metadata": {
|
226 |
+
"id": "tGW8wsfAfHDf"
|
227 |
+
},
|
228 |
+
"execution_count": null,
|
229 |
+
"outputs": []
|
230 |
+
}
|
231 |
+
]
|
232 |
+
}
|