|
111 | 111 | "source": [ |
112 | 112 | "with open(\"../sample_data/documents/hotd.txt\") as f:\n", |
113 | 113 | " lines = f.read().splitlines()\n", |
114 | | - " \n", |
| 114 | + "\n", |
115 | 115 | "lines[:3]" |
116 | 116 | ] |
117 | 117 | }, |
|
152 | 152 | "source": [ |
153 | 153 | "# create a new collection\n", |
154 | 154 | "collection = lx.create_collection(\n", |
155 | | - " collection_name=\"house_of_the_dragon\", \n", |
156 | | - " description=\"House of the Dragon characters\"\n", |
| 155 | + " collection_name=\"house_of_the_dragon\", description=\"House of the Dragon characters\"\n", |
157 | 156 | ")\n", |
158 | 157 | "collection" |
159 | 158 | ] |
|
177 | 176 | }, |
178 | 177 | "outputs": [], |
179 | 178 | "source": [ |
180 | | - "collection.add_documents([\n", |
181 | | - " {\"content\": line} for line in lines\n", |
182 | | - "])" |
| 179 | + "collection.add_documents([{\"content\": line} for line in lines])" |
183 | 180 | ] |
184 | 181 | }, |
185 | 182 | { |
|
205 | 202 | "source": [ |
206 | 203 | "# create an index\n", |
207 | 204 | "index_fields = {\n", |
208 | | - " \"embedding\": {\"type\": \"embedding\", \"extras\": {\"dims\": 1536, \"model\": \"text.embeddings.openai-3-small\"}}\n", |
| 205 | + " \"embedding\": {\n", |
| 206 | + " \"type\": \"embedding\",\n", |
| 207 | + " \"extras\": {\"dims\": 1536, \"model\": \"text.embeddings.openai-3-small\"},\n", |
| 208 | + " }\n", |
209 | 209 | "}\n", |
210 | 210 | "index = lx.create_index(\n", |
211 | | - " index_id=\"hotd_embeddings\", \n", |
| 211 | + " index_id=\"hotd_embeddings\",\n", |
212 | 212 | " description=\"Text embeddings for House of the Dragon collection\",\n", |
213 | | - " index_fields=index_fields\n", |
| 213 | + " index_fields=index_fields,\n", |
214 | 214 | ")" |
215 | 215 | ] |
216 | 216 | }, |
|
260 | 260 | "binding = lx.create_binding(\n", |
261 | 261 | " collection_name=\"house_of_the_dragon\",\n", |
262 | 262 | " index_id=\"hotd_embeddings\",\n", |
263 | | - " transformer_id=\"text.embeddings.openai-3-small\"\n", |
| 263 | + " transformer_id=\"text.embeddings.openai-3-small\",\n", |
264 | 264 | ")\n", |
265 | 265 | "binding" |
266 | 266 | ] |
|
409 | 409 | "results_ex = index.query(query_text=question_ex)\n", |
410 | 410 | "\n", |
411 | 411 | "# format results as context\n", |
412 | | - "context_ex = \"\\n\".join([\n", |
413 | | - " f'[doc_id: {er[\"document_id\"]}] {er[\"document.content\"]}' for er in results_ex\n", |
414 | | - "])\n", |
| 412 | + "context_ex = \"\\n\".join(\n", |
| 413 | + " [f'[doc_id: {er[\"document_id\"]}] {er[\"document.content\"]}' for er in results_ex]\n", |
| 414 | + ")\n", |
415 | 415 | "\n", |
416 | 416 | "# construct prompt\n", |
417 | 417 | "prompt_ex = question_template.format(question=question_ex, context=context_ex)\n", |
|
457 | 457 | " model=\"gpt-4\",\n", |
458 | 458 | " messages=[\n", |
459 | 459 | " {\"role\": \"system\", \"content\": system_prompt},\n", |
460 | | - " {\"role\": \"user\", \"content\": prompt_ex}\n", |
461 | | - " ]\n", |
| 460 | + " {\"role\": \"user\", \"content\": prompt_ex},\n", |
| 461 | + " ],\n", |
462 | 462 | ")\n", |
463 | 463 | "print(oai_response.choices[0].message.content)" |
464 | 464 | ] |
|
484 | 484 | }, |
485 | 485 | "outputs": [], |
486 | 486 | "source": [ |
487 | | - "def construct_prompt(question: str, \n", |
488 | | - " result_template: str = \"[doc_id: {r[document_id]}] {r[document.content]}\",\n", |
489 | | - " **query_kwargs):\n", |
| 487 | + "def construct_prompt(\n", |
| 488 | + " question: str,\n", |
| 489 | + " result_template: str = \"[doc_id: {r[document_id]}] {r[document.content]}\",\n", |
| 490 | + " **query_kwargs,\n", |
| 491 | + "):\n", |
490 | 492 | " # retrieve most relevant results\n", |
491 | 493 | " results = index.query(query_text=question, **query_kwargs)\n", |
492 | 494 | " # format results for context\n", |
493 | | - " context = \"\\n\".join([\n", |
494 | | - " result_template.format(r=r) for r in results\n", |
495 | | - " ])\n", |
| 495 | + " context = \"\\n\".join([result_template.format(r=r) for r in results])\n", |
496 | 496 | " # format prompt\n", |
497 | 497 | " return question_template.format(question=question, context=context)\n", |
498 | 498 | "\n", |
499 | | - "def chat_completion(message: str,\n", |
500 | | - " system: str = system_prompt, \n", |
501 | | - " **chat_kwargs):\n", |
| 499 | + "\n", |
| 500 | + "def chat_completion(message: str, system: str = system_prompt, **chat_kwargs):\n", |
502 | 501 | " # generate response\n", |
503 | 502 | " return openai_client.chat.completions.create(\n", |
504 | 503 | " model=\"gpt-4\",\n", |
505 | 504 | " messages=[\n", |
506 | 505 | " {\"role\": \"system\", \"content\": system},\n", |
507 | | - " {\"role\": \"user\", \"content\": message}\n", |
| 506 | + " {\"role\": \"user\", \"content\": message},\n", |
508 | 507 | " ],\n", |
509 | 508 | " **chat_kwargs,\n", |
510 | 509 | " )" |
|
626 | 625 | "outputs": [], |
627 | 626 | "source": [ |
628 | 627 | "# add a new document\n", |
629 | | - "collection.add_documents([\n", |
630 | | - " {\"content\": \"Lexy was by far the largest of the Targaryen dragons, and was ridden by AGI the Conqueror.\"}\n", |
631 | | - "])" |
| 628 | + "collection.add_documents(\n", |
| 629 | + " [\n", |
| 630 | + " {\n", |
| 631 | + " \"content\": \"Lexy was by far the largest of the Targaryen dragons, and was ridden by AGI the Conqueror.\"\n", |
| 632 | + " }\n", |
| 633 | + " ]\n", |
| 634 | + ")" |
632 | 635 | ] |
633 | 636 | }, |
634 | 637 | { |
|
652 | 655 | }, |
653 | 656 | "outputs": [], |
654 | 657 | "source": [ |
655 | | - "new_result_template = \\\n", |
656 | | - " \"[doc_id: {r[document_id]}, updated_at: {r[document.updated_at]}] {r[document.content]}\"\n", |
| 658 | + "new_result_template = \"[doc_id: {r[document_id]}, updated_at: {r[document.updated_at]}] {r[document.content]}\"\n", |
657 | 659 | "\n", |
658 | 660 | "new_prompt = construct_prompt(\n", |
659 | | - " question=\"which is the largest Targaryen dragon?\", \n", |
660 | | - " result_template=new_result_template, \n", |
661 | | - " return_fields=[\"document.content\", \"document.updated_at\"]\n", |
| 661 | + " question=\"which is the largest Targaryen dragon?\",\n", |
| 662 | + " result_template=new_result_template,\n", |
| 663 | + " return_fields=[\"document.content\", \"document.updated_at\"],\n", |
662 | 664 | ")\n", |
663 | 665 | "print(new_prompt)" |
664 | 666 | ] |
|
712 | 714 | "q = \"which is the largest Targaryen dragon?\"\n", |
713 | 715 | "oai_response = chat_completion(\n", |
714 | 716 | " message=construct_prompt(\n", |
715 | | - " question=q, \n", |
716 | | - " result_template=new_result_template, \n", |
717 | | - " return_fields=[\"document.content\", \"document.updated_at\"]\n", |
| 717 | + " question=q,\n", |
| 718 | + " result_template=new_result_template,\n", |
| 719 | + " return_fields=[\"document.content\", \"document.updated_at\"],\n", |
718 | 720 | " ),\n", |
719 | | - " system=new_system_prompt\n", |
| 721 | + " system=new_system_prompt,\n", |
720 | 722 | ")\n", |
721 | 723 | "print(oai_response.choices[0].message.content)" |
722 | 724 | ] |
|
0 commit comments