Skip to content

Commit 1e72598

Browse files
🧿 [pre-commit.ci] auto fixes from pre-commit hooks
1 parent e871380 commit 1e72598

File tree

5 files changed

+1030
-970
lines changed

5 files changed

+1030
-970
lines changed

‎examples/basic_rag.ipynb‎

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
"source": [
112112
"with open(\"../sample_data/documents/hotd.txt\") as f:\n",
113113
" lines = f.read().splitlines()\n",
114-
" \n",
114+
"\n",
115115
"lines[:3]"
116116
]
117117
},
@@ -152,8 +152,7 @@
152152
"source": [
153153
"# create a new collection\n",
154154
"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",
157156
")\n",
158157
"collection"
159158
]
@@ -177,9 +176,7 @@
177176
},
178177
"outputs": [],
179178
"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])"
183180
]
184181
},
185182
{
@@ -205,12 +202,15 @@
205202
"source": [
206203
"# create an index\n",
207204
"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",
209209
"}\n",
210210
"index = lx.create_index(\n",
211-
" index_id=\"hotd_embeddings\", \n",
211+
" index_id=\"hotd_embeddings\",\n",
212212
" description=\"Text embeddings for House of the Dragon collection\",\n",
213-
" index_fields=index_fields\n",
213+
" index_fields=index_fields,\n",
214214
")"
215215
]
216216
},
@@ -260,7 +260,7 @@
260260
"binding = lx.create_binding(\n",
261261
" collection_name=\"house_of_the_dragon\",\n",
262262
" index_id=\"hotd_embeddings\",\n",
263-
" transformer_id=\"text.embeddings.openai-3-small\"\n",
263+
" transformer_id=\"text.embeddings.openai-3-small\",\n",
264264
")\n",
265265
"binding"
266266
]
@@ -409,9 +409,9 @@
409409
"results_ex = index.query(query_text=question_ex)\n",
410410
"\n",
411411
"# 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",
415415
"\n",
416416
"# construct prompt\n",
417417
"prompt_ex = question_template.format(question=question_ex, context=context_ex)\n",
@@ -457,8 +457,8 @@
457457
" model=\"gpt-4\",\n",
458458
" messages=[\n",
459459
" {\"role\": \"system\", \"content\": system_prompt},\n",
460-
" {\"role\": \"user\", \"content\": prompt_ex}\n",
461-
" ]\n",
460+
" {\"role\": \"user\", \"content\": prompt_ex},\n",
461+
" ],\n",
462462
")\n",
463463
"print(oai_response.choices[0].message.content)"
464464
]
@@ -484,27 +484,26 @@
484484
},
485485
"outputs": [],
486486
"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",
490492
" # retrieve most relevant results\n",
491493
" results = index.query(query_text=question, **query_kwargs)\n",
492494
" # 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",
496496
" # format prompt\n",
497497
" return question_template.format(question=question, context=context)\n",
498498
"\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",
502501
" # generate response\n",
503502
" return openai_client.chat.completions.create(\n",
504503
" model=\"gpt-4\",\n",
505504
" messages=[\n",
506505
" {\"role\": \"system\", \"content\": system},\n",
507-
" {\"role\": \"user\", \"content\": message}\n",
506+
" {\"role\": \"user\", \"content\": message},\n",
508507
" ],\n",
509508
" **chat_kwargs,\n",
510509
" )"
@@ -626,9 +625,13 @@
626625
"outputs": [],
627626
"source": [
628627
"# 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+
")"
632635
]
633636
},
634637
{
@@ -652,13 +655,12 @@
652655
},
653656
"outputs": [],
654657
"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",
657659
"\n",
658660
"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",
662664
")\n",
663665
"print(new_prompt)"
664666
]
@@ -712,11 +714,11 @@
712714
"q = \"which is the largest Targaryen dragon?\"\n",
713715
"oai_response = chat_completion(\n",
714716
" 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",
718720
" ),\n",
719-
" system=new_system_prompt\n",
721+
" system=new_system_prompt,\n",
720722
")\n",
721723
"print(oai_response.choices[0].message.content)"
722724
]

0 commit comments

Comments
 (0)