Spaces:
Sleeping
Sleeping
File size: 14,999 Bytes
88db9ff |
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "25350da1",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"You are using the default legacy behaviour of the <class 'transformers.models.t5.tokenization_t5.T5Tokenizer'>. If you see this, DO NOT PANIC! This is expected, and simply means that the `legacy` (previous) behavior will be used so nothing changes for you. If you want to use the new behaviour, set `legacy=True`. This should only be set if you understand what it means, and thouroughly read the reason why this was added as explained in https://github.com/huggingface/transformers/pull/24565\n",
"C:\\Users\\SudheerRChinthala\\anaconda3\\Lib\\site-packages\\transformers\\utils\\generic.py:260: UserWarning: torch.utils._pytree._register_pytree_node is deprecated. Please use torch.utils._pytree.register_pytree_node instead.\n",
" torch.utils._pytree._register_pytree_node(\n",
"C:\\Users\\SudheerRChinthala\\anaconda3\\Lib\\site-packages\\transformers\\utils\\generic.py:260: UserWarning: torch.utils._pytree._register_pytree_node is deprecated. Please use torch.utils._pytree.register_pytree_node instead.\n",
" torch.utils._pytree._register_pytree_node(\n"
]
}
],
"source": [
"import transformers\n",
"from transformers import AutoTokenizer, AutoModelForSeq2SeqLM\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(\"lmsys/fastchat-t5-3b-v1.0\")\n",
"\n",
"model = AutoModelForSeq2SeqLM.from_pretrained(\"lmsys/fastchat-t5-3b-v1.0\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d012cfa8",
"metadata": {},
"outputs": [],
"source": [
"from transformers import pipeline\n",
"from langchain.llms import HuggingFacePipeline\n",
"import torch\n",
"\n",
"pipe = pipeline(\n",
" \"text2text-generation\",\n",
" model=model, \n",
" tokenizer=tokenizer, \n",
" max_length=256\n",
")\n",
"\n",
"local_llm = HuggingFacePipeline(pipeline=pipe)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f0493f3f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\SudheerRChinthala\\anaconda3\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:117: LangChainDeprecationWarning: The function `__call__` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n",
" warn_deprecated(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<pad> hyderabad\n"
]
}
],
"source": [
"print(local_llm('What is the capital of Telangana?'))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "71c47e16",
"metadata": {},
"outputs": [],
"source": [
"import os"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3bc5dcfa",
"metadata": {},
"outputs": [],
"source": [
"from langchain.vectorstores import Chroma\n",
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
"\n",
"from langchain.chains import RetrievalQA\n",
"from langchain.document_loaders import TextLoader\n",
"from langchain.document_loaders import PyPDFLoader\n",
"from langchain.document_loaders import DirectoryLoader\n",
"\n",
"\n",
"from InstructorEmbedding import INSTRUCTOR\n",
"from langchain.embeddings import HuggingFaceInstructEmbeddings"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "7d8598c8",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting SQLAlchemy==1.4.46\n",
" Using cached SQLAlchemy-1.4.46-cp311-cp311-win_amd64.whl (1.6 MB)\n",
"Requirement already satisfied: greenlet!=0.4.17 in c:\\users\\sudheerrchinthala\\anaconda3\\lib\\site-packages (from SQLAlchemy==1.4.46) (2.0.1)\n",
"Installing collected packages: SQLAlchemy\n",
" Attempting uninstall: SQLAlchemy\n",
" Found existing installation: SQLAlchemy 2.0.25\n",
" Uninstalling SQLAlchemy-2.0.25:\n",
" Successfully uninstalled SQLAlchemy-2.0.25\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR: Could not install packages due to an OSError: [WinError 5] Access is denied: 'C:\\\\Users\\\\SudheerRChinthala\\\\anaconda3\\\\Lib\\\\site-packages\\\\~-lalchemy\\\\cyextension\\\\collections.cp311-win_amd64.pyd'\n",
"Consider using the `--user` option or check the permissions.\n",
"\n"
]
}
],
"source": [
"#!pip install SQLAlchemy==1.4.46"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4eaf8c4c",
"metadata": {},
"outputs": [],
"source": [
"# Load and process the text files\n",
"# loader = TextLoader('single_text_file.txt')\n",
"loader = DirectoryLoader('C:/Users/SudheerRChinthala/sivallm/new_papers', glob=\"./*.pdf\", loader_cls=PyPDFLoader)\n",
"\n",
"#documents = loader.load()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f02c8c70",
"metadata": {},
"outputs": [],
"source": [
"documents = loader.load()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "483bc67a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"25"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(documents)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "11b461c5",
"metadata": {},
"outputs": [],
"source": [
"text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)\n",
"texts = text_splitter.split_documents(documents)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "7d3f6a0e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"43"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(texts)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "9bc9c5c2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Document(page_content='Release N otes for ICORE and INSTAR Release Dec 2023 Feature \\n3 \\n Purpose \\n \\nThe purpose of this document is to provide release notes for the ICORE and INSTAR portion of the Nov \\nand Dec 2023 release. \\n \\nDocument History \\nVersion Issue Date Author(s) Identification of Changes \\n .01', metadata={'source': 'C:\\\\Users\\\\SudheerRChinthala\\\\sivallm\\\\new_papers\\\\ICORE_INSTAR_Dec_2003_Release_Notes.pdf', 'page': 2})"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"texts[3]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f5c23330",
"metadata": {},
"outputs": [],
"source": [
"from langchain.embeddings import HuggingFaceInstructEmbeddings"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "0ff3dd0f",
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.embeddings import HuggingFaceInstructEmbeddings"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "10e390aa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: InstructorEmbedding in c:\\users\\sudheerrchinthala\\anaconda3\\lib\\site-packages (1.0.1)\n"
]
}
],
"source": [
"!pip install InstructorEmbedding"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "24977cc7",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"#from InstructorEmbedding import INSTRUCTOR\n",
"from langchain.embeddings import HuggingFaceInstructEmbeddings"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "5762c0bb",
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.vectorstores import Chroma"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "bedd0a78",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"load INSTRUCTOR_Transformer\n",
"max_seq_length 512\n"
]
}
],
"source": [
"instructor_embeddings = HuggingFaceInstructEmbeddings(model_name=\"hkunlp/instructor-base\") \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "3468b96f",
"metadata": {},
"outputs": [],
"source": [
"persist_directory = 'db'\n",
"## Here is the nmew embeddings being used\n",
"embedding = instructor_embeddings"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "c5a25bea",
"metadata": {},
"outputs": [],
"source": [
"vectordb = Chroma.from_documents(documents=texts, \n",
" embedding=embedding,\n",
" persist_directory=persist_directory)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "76fa58a0",
"metadata": {},
"outputs": [],
"source": [
"retriever = vectordb.as_retriever(search_kwargs={\"k\": 3})"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "8c07129e",
"metadata": {},
"outputs": [],
"source": [
"import textwrap\n",
"\n",
"def wrap_text_preserve_newlines(text, width=110):\n",
" # Split the input text into lines based on newline characters\n",
" lines = text.split('\\n')\n",
"\n",
" # Wrap each line individually\n",
" wrapped_lines = [textwrap.fill(line, width=width) for line in lines]\n",
"\n",
" # Join the wrapped lines back together using newline characters\n",
" wrapped_text = '\\n'.join(wrapped_lines)\n",
"\n",
" return wrapped_text\n",
"\n",
"def process_llm_response(llm_response):\n",
" print(wrap_text_preserve_newlines(llm_response['result']))\n",
" print('\\n\\nSources:')\n",
" for source in llm_response[\"source_documents\"]:\n",
" print(source.metadata['source'])"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "8c434e49",
"metadata": {},
"outputs": [],
"source": [
"qa_chain = RetrievalQA.from_chain_type(llm=local_llm, \n",
" chain_type=\"stuff\", \n",
" retriever=retriever, \n",
" return_source_documents=True)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "de1319cb",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\SudheerRChinthala\\anaconda3\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:117: LangChainDeprecationWarning: The function `__call__` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
" warn_deprecated(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<pad> No, Service Options are not ordered from USRP.\n",
"\n",
"\n",
"\n",
"Sources:\n",
"C:\\Users\\SudheerRChinthala\\sivallm\\new_papers\\ICORE_INSTAR_Release_Notes.pdf\n",
"C:\\Users\\SudheerRChinthala\\sivallm\\new_papers\\ICORE_INSTAR_Release_Notes.pdf\n",
"C:\\Users\\SudheerRChinthala\\sivallm\\new_papers\\ICORE_INSTAR_Release_Notes.pdf\n"
]
}
],
"source": [
"query = \"Are Service Options ordered from USRP\"\n",
"llm_response = qa_chain(query)\n",
"print(llm_response)\n",
"process_llm_response(llm_response)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "5e5f2f93",
"metadata": {},
"outputs": [],
"source": [
"vectordb.persist()\n",
"vectordb = None"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "e649ed71",
"metadata": {},
"outputs": [],
"source": [
"vectordb = Chroma(persist_directory=persist_directory, \n",
" embedding_function=embedding)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "e729fbba",
"metadata": {},
"outputs": [],
"source": [
"retriever = vectordb.as_retriever()"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "b9c687ea",
"metadata": {},
"outputs": [],
"source": [
"qa_chain = RetrievalQA.from_chain_type(llm=local_llm, \n",
" chain_type=\"stuff\", \n",
" retriever=retriever, \n",
" return_source_documents=True)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "8d40db5c",
"metadata": {
"scrolled": true
},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'qa_chain' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[21], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m query \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCan you give me information about Shared, Port tagging No\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m----> 2\u001b[0m llm_response \u001b[38;5;241m=\u001b[39m qa_chain(query)\n\u001b[0;32m 3\u001b[0m process_llm_response(llm_response)\n",
"\u001b[1;31mNameError\u001b[0m: name 'qa_chain' is not defined"
]
}
],
"source": [
"query = \"Can you give me information about Shared, Port tagging No\"\n",
"llm_response = qa_chain(query)\n",
"process_llm_response(llm_response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b31a07c",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|