File size: 6,426 Bytes
406e6c0 |
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "TFu_ibC1eYrz"
},
"outputs": [],
"source": [
"!pip install torch transformers -q"
]
},
{
"cell_type": "code",
"source": [
"import torch\n",
"from transformers import pipeline\n",
"from IPython.display import clear_output\n",
"from google.colab import output"
],
"metadata": {
"id": "Zs7QNs0Tet6r"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"class ChatBot:\n",
" _instance = None\n",
" _current_model = None\n",
"\n",
" def __init__(self, model_slug=None):\n",
" if model_slug and model_slug != ChatBot._current_model:\n",
" self.load_model(model_slug)\n",
" ChatBot._current_model = model_slug\n",
"\n",
" self.messages = []\n",
" self.max_tokens = 2048\n",
" self.temperature = 0.01\n",
" self.top_k = 100\n",
" self.top_p = 0.95\n",
"\n",
" @classmethod\n",
" def get_instance(cls, model_slug=None):\n",
" if not cls._instance or (model_slug and model_slug != cls._current_model):\n",
" cls._instance = cls(model_slug)\n",
" return cls._instance\n",
"\n",
" def load_model(self, model_slug):\n",
" print(f\"Loading model {model_slug}...\")\n",
" self.pipeline = pipeline(\n",
" \"text-generation\",\n",
" model=model_slug,\n",
" device_map=\"auto\",\n",
" )\n",
" clear_output()\n",
" print(\"Model loaded successfully!\")\n",
"\n",
" def reset_conversation(self, system_message):\n",
" \"\"\"Reset the conversation with a new system message\"\"\"\n",
" self.messages = [{\"role\": \"system\", \"content\": system_message}]\n",
"\n",
" def get_response(self, user_input):\n",
" \"\"\"Get response with current parameters\"\"\"\n",
" self.messages.append({\"role\": \"user\", \"content\": user_input})\n",
" outputs = self.pipeline(\n",
" self.messages,\n",
" max_new_tokens=self.max_tokens,\n",
" do_sample=True,\n",
" temperature=self.temperature,\n",
" top_k=self.top_k,\n",
" top_p=self.top_p\n",
" )\n",
" response = outputs[0][\"generated_text\"][-1]\n",
" content = response.get('content', 'No content available')\n",
" self.messages.append({\"role\": \"assistant\", \"content\": content})\n",
" return content\n",
"\n",
" def update_params(self, max_tokens=None, temperature=None, top_k=None, top_p=None):\n",
" \"\"\"Update generation parameters\"\"\"\n",
" if max_tokens is not None:\n",
" self.max_tokens = max_tokens\n",
" if temperature is not None:\n",
" self.temperature = temperature\n",
" if top_k is not None:\n",
" self.top_k = top_k\n",
" if top_p is not None:\n",
" self.top_p = top_p"
],
"metadata": {
"id": "v4uIN6uIeyl3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def run_chatbot(\n",
" model=None,\n",
" system_message=\"You are Orca Mini, You are expert in following given instructions, Think step by step before coming up with final answer\",\n",
" max_tokens=None,\n",
" temperature=None,\n",
" top_k=None,\n",
" top_p=None,\n",
"):\n",
" try:\n",
" # Get or create chatbot instance\n",
" chatbot = ChatBot.get_instance(model)\n",
"\n",
" # Update parameters if provided\n",
" chatbot.update_params(max_tokens, temperature, top_k, top_p)\n",
"\n",
" # Reset conversation with new system message\n",
" chatbot.reset_conversation(system_message)\n",
"\n",
" print(\"Chatbot: Hi! Type 'quit' to exit.\")\n",
"\n",
" while True:\n",
" user_input = input(\"You: \").strip()\n",
" if user_input.lower() == 'quit':\n",
" break\n",
" try:\n",
" response = chatbot.get_response(user_input)\n",
" print(\"Chatbot:\", response)\n",
" except Exception as e:\n",
" print(f\"Chatbot: An error occurred: {str(e)}\")\n",
" print(\"Please try again.\")\n",
"\n",
" except Exception as e:\n",
" print(f\"Error in chatbot: {str(e)}\")"
],
"metadata": {
"id": "H2n_6Xcue3Vn"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"run_chatbot(model=\"pankajmathur/orca_mini_v9_6_1B-Instruct\")"
],
"metadata": {
"id": "JEqgoAH2fC6h"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# # change system message\n",
"# run_chatbot(\n",
"# system_message=\"You are Orca Mini, You are expert in logic, Think step by step before coming up with final answer\",\n",
"# max_tokens=1024,\n",
"# temperature=0.3\n",
"# )"
],
"metadata": {
"id": "tGW8wsfAfHDf"
},
"execution_count": null,
"outputs": []
}
]
} |