Skip to content

Commit

Permalink
Split the retrieve_documents function in another cell for better expl…
Browse files Browse the repository at this point in the history
…anation of the example
  • Loading branch information
rogeriochaves committed Jul 30, 2023
1 parent 09cc56a commit 331f19f
Showing 1 changed file with 51 additions and 19 deletions.
70 changes: 51 additions & 19 deletions docs/docs/examples/qa-over-documents.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,53 @@
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Cool, now we can easily build a function that retrieves documents from our vector db:"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GPT4All LLMs\n",
"LLMs require a lot of GPU to run properly make it hard for the common folk to set one up locally. Fortunately, the folks at GPT4All are doing an excellent job in reall...\n",
"\n",
"LLMs\n",
"Large Language Models like GPT-4 is the whole reason LiteChain exists, we want to build on top of LLMs to construct an application. After learning the Chain Basics, it should ...\n",
"\n"
]
}
],
"source": [
"from typing import AsyncGenerator\n",
"from litechain import as_async_generator\n",
"\n",
"def retrieve_documents(query: str, n_results: int) -> AsyncGenerator[str, None]:\n",
" query_embedding = (\n",
" openai.Embedding.create(model=\"text-embedding-ada-002\", input=query)\n",
" .data[0] # type: ignore\n",
" .embedding\n",
" )\n",
"\n",
" results = collection.query(\n",
" query_embeddings=query_embedding,\n",
" n_results=n_results,\n",
" )[\"documents\"]\n",
" results = results[0] # type: ignore\n",
" return as_async_generator(*results)\n",
"\n",
"async for document in retrieve_documents(\"How to use GPT4All\", n_results=2):\n",
" print(document[20:200] + \"...\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -171,7 +218,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, with all our documents indexed, we can now write a chain that will reply the user questions about those documents"
"Finally, with all our documents indexed and our query function, we can now write a chain that will reply the user questions about those documents using an LLM:"
]
},
{
Expand Down Expand Up @@ -211,26 +258,11 @@
}
],
"source": [
"from typing import AsyncGenerator, Iterable, List\n",
"from litechain import Chain, filter_final_output, as_async_generator\n",
"from typing import Iterable\n",
"from litechain import Chain, filter_final_output\n",
"from litechain.contrib import OpenAIChatChain, OpenAIChatMessage, OpenAIChatDelta\n",
"\n",
"\n",
"def retrieve_documents(query: str, n_results: int) -> AsyncGenerator[str, None]:\n",
" query_embedding = (\n",
" openai.Embedding.create(model=\"text-embedding-ada-002\", input=query)\n",
" .data[0] # type: ignore\n",
" .embedding\n",
" )\n",
"\n",
" results = collection.query(\n",
" query_embeddings=query_embedding,\n",
" n_results=n_results,\n",
" )[\"documents\"]\n",
" results = results[0] # type: ignore\n",
" return as_async_generator(*results)\n",
"\n",
"\n",
"def chain(query):\n",
" return (\n",
" Chain[str, str](\n",
Expand Down Expand Up @@ -267,7 +299,7 @@
"source": [
"## Bonus: map-rerank document scoring\n",
"\n",
"In the previous example, we used chromadb to return the top-3 results according to embeddings, however, sometimes you have a lot more documents, and the top-scoring provided by the embeddings on the vector-db might not be the best, at the same time, we cannot just send all the documents to the LLM due to context length limit.\n",
"In the previous example, we used chromadb to return the top-3 results according to embeddings, however, sometimes you have a lot more documents, and the top-scoring provided by the embeddings on the vector db might not be the best, at the same time, we cannot just send all the documents to the LLM due to context length limit.\n",
"\n",
"What we can do then is to leverage the LLM inteligence for better scoring, by returning more results from the vectordb search at first, and then ask the LLM to score each of them in parallel, to then pick just the best ones for the final answer.\n",
"\n",
Expand Down

0 comments on commit 331f19f

Please sign in to comment.