Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v6.0.0
hooks:
- id: check-added-large-files
- id: check-toml
Expand All @@ -11,7 +11,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.10
rev: v0.14.3
hooks:
- id: ruff
- id: ruff-format
Expand Down
76 changes: 39 additions & 37 deletions examples/basic_rag.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"source": [
"with open(\"../sample_data/documents/hotd.txt\") as f:\n",
" lines = f.read().splitlines()\n",
" \n",
"\n",
"lines[:3]"
]
},
Expand Down Expand Up @@ -152,8 +152,7 @@
"source": [
"# create a new collection\n",
"collection = lx.create_collection(\n",
" collection_name=\"house_of_the_dragon\", \n",
" description=\"House of the Dragon characters\"\n",
" collection_name=\"house_of_the_dragon\", description=\"House of the Dragon characters\"\n",
")\n",
"collection"
]
Expand All @@ -177,9 +176,7 @@
},
"outputs": [],
"source": [
"collection.add_documents([\n",
" {\"content\": line} for line in lines\n",
"])"
"collection.add_documents([{\"content\": line} for line in lines])"
]
},
{
Expand All @@ -205,12 +202,15 @@
"source": [
"# create an index\n",
"index_fields = {\n",
" \"embedding\": {\"type\": \"embedding\", \"extras\": {\"dims\": 1536, \"model\": \"text.embeddings.openai-3-small\"}}\n",
" \"embedding\": {\n",
" \"type\": \"embedding\",\n",
" \"extras\": {\"dims\": 1536, \"model\": \"text.embeddings.openai-3-small\"},\n",
" }\n",
"}\n",
"index = lx.create_index(\n",
" index_id=\"hotd_embeddings\", \n",
" index_id=\"hotd_embeddings\",\n",
" description=\"Text embeddings for House of the Dragon collection\",\n",
" index_fields=index_fields\n",
" index_fields=index_fields,\n",
")"
]
},
Expand Down Expand Up @@ -260,7 +260,7 @@
"binding = lx.create_binding(\n",
" collection_name=\"house_of_the_dragon\",\n",
" index_id=\"hotd_embeddings\",\n",
" transformer_id=\"text.embeddings.openai-3-small\"\n",
" transformer_id=\"text.embeddings.openai-3-small\",\n",
")\n",
"binding"
]
Expand Down Expand Up @@ -409,9 +409,9 @@
"results_ex = index.query(query_text=question_ex)\n",
"\n",
"# format results as context\n",
"context_ex = \"\\n\".join([\n",
" f'[doc_id: {er[\"document_id\"]}] {er[\"document.content\"]}' for er in results_ex\n",
"])\n",
"context_ex = \"\\n\".join(\n",
" [f\"[doc_id: {er['document_id']}] {er['document.content']}\" for er in results_ex]\n",
")\n",
"\n",
"# construct prompt\n",
"prompt_ex = question_template.format(question=question_ex, context=context_ex)\n",
Expand Down Expand Up @@ -457,8 +457,8 @@
" model=\"gpt-4\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": prompt_ex}\n",
" ]\n",
" {\"role\": \"user\", \"content\": prompt_ex},\n",
" ],\n",
")\n",
"print(oai_response.choices[0].message.content)"
]
Expand All @@ -484,27 +484,26 @@
},
"outputs": [],
"source": [
"def construct_prompt(question: str, \n",
" result_template: str = \"[doc_id: {r[document_id]}] {r[document.content]}\",\n",
" **query_kwargs):\n",
"def construct_prompt(\n",
" question: str,\n",
" result_template: str = \"[doc_id: {r[document_id]}] {r[document.content]}\",\n",
" **query_kwargs,\n",
"):\n",
" # retrieve most relevant results\n",
" results = index.query(query_text=question, **query_kwargs)\n",
" # format results for context\n",
" context = \"\\n\".join([\n",
" result_template.format(r=r) for r in results\n",
" ])\n",
" context = \"\\n\".join([result_template.format(r=r) for r in results])\n",
" # format prompt\n",
" return question_template.format(question=question, context=context)\n",
"\n",
"def chat_completion(message: str,\n",
" system: str = system_prompt, \n",
" **chat_kwargs):\n",
"\n",
"def chat_completion(message: str, system: str = system_prompt, **chat_kwargs):\n",
" # generate response\n",
" return openai_client.chat.completions.create(\n",
" model=\"gpt-4\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system},\n",
" {\"role\": \"user\", \"content\": message}\n",
" {\"role\": \"user\", \"content\": message},\n",
" ],\n",
" **chat_kwargs,\n",
" )"
Expand Down Expand Up @@ -626,9 +625,13 @@
"outputs": [],
"source": [
"# add a new document\n",
"collection.add_documents([\n",
" {\"content\": \"Lexy was by far the largest of the Targaryen dragons, and was ridden by AGI the Conqueror.\"}\n",
"])"
"collection.add_documents(\n",
" [\n",
" {\n",
" \"content\": \"Lexy was by far the largest of the Targaryen dragons, and was ridden by AGI the Conqueror.\"\n",
" }\n",
" ]\n",
")"
]
},
{
Expand All @@ -652,13 +655,12 @@
},
"outputs": [],
"source": [
"new_result_template = \\\n",
" \"[doc_id: {r[document_id]}, updated_at: {r[document.updated_at]}] {r[document.content]}\"\n",
"new_result_template = \"[doc_id: {r[document_id]}, updated_at: {r[document.updated_at]}] {r[document.content]}\"\n",
"\n",
"new_prompt = construct_prompt(\n",
" question=\"which is the largest Targaryen dragon?\", \n",
" result_template=new_result_template, \n",
" return_fields=[\"document.content\", \"document.updated_at\"]\n",
" question=\"which is the largest Targaryen dragon?\",\n",
" result_template=new_result_template,\n",
" return_fields=[\"document.content\", \"document.updated_at\"],\n",
")\n",
"print(new_prompt)"
]
Expand Down Expand Up @@ -712,11 +714,11 @@
"q = \"which is the largest Targaryen dragon?\"\n",
"oai_response = chat_completion(\n",
" message=construct_prompt(\n",
" question=q, \n",
" result_template=new_result_template, \n",
" return_fields=[\"document.content\", \"document.updated_at\"]\n",
" question=q,\n",
" result_template=new_result_template,\n",
" return_fields=[\"document.content\", \"document.updated_at\"],\n",
" ),\n",
" system=new_system_prompt\n",
" system=new_system_prompt,\n",
")\n",
"print(oai_response.choices[0].message.content)"
]
Expand Down
Loading