Skip to content

Commit

Permalink
docs[minor]: Query analysis (langchain-ai#4598)
Browse files Browse the repository at this point in the history
* cr

* quickstart in nb

* dropped mdx in favor of ipynb

* few shot

* cr

* expansion

* hyde

* routing

* nits and step back

* pull in how to from py

* high cardinality

* multi query

* Multi retriever

* no queries

* constructing filters

* cleanup and structure

* chore: lint files

* fix install deps

* fixes
  • Loading branch information
bracesproul authored Mar 6, 2024
1 parent fa1b07f commit dc6b883
Show file tree
Hide file tree
Showing 26 changed files with 5,134 additions and 1 deletion.
7 changes: 6 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"@langchain/anthropic": "npm:@langchain/anthropic",
"node-llama-cpp": "npm:/node-llama-cpp",
"readline": "https://deno.land/x/[email protected]/mod.ts",
"ml-distance": "npm:/ml-distance"
"ml-distance": "npm:/ml-distance",
"youtubei.js": "npm:/youtubei.js",
"youtube-transcript": "npm:/youtube-transcript",
"chromadb": "npm:/chromadb",
"uuid": "npm:/uuid",
"@faker-js/faker": "npm:@faker-js/faker"
}
}
28 changes: 28 additions & 0 deletions docs/core_docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ docs/use_cases/question_answering/citations.md
docs/use_cases/question_answering/citations.mdx
docs/use_cases/question_answering/chat_history.md
docs/use_cases/question_answering/chat_history.mdx
docs/use_cases/query_analysis/quickstart.md
docs/use_cases/query_analysis/quickstart.mdx
docs/use_cases/query_analysis/index.md
docs/use_cases/query_analysis/index.mdx
docs/use_cases/query_analysis/techniques/structuring.md
docs/use_cases/query_analysis/techniques/structuring.mdx
docs/use_cases/query_analysis/techniques/step_back.md
docs/use_cases/query_analysis/techniques/step_back.mdx
docs/use_cases/query_analysis/techniques/routing.md
docs/use_cases/query_analysis/techniques/routing.mdx
docs/use_cases/query_analysis/techniques/hyde.md
docs/use_cases/query_analysis/techniques/hyde.mdx
docs/use_cases/query_analysis/techniques/expansion.md
docs/use_cases/query_analysis/techniques/expansion.mdx
docs/use_cases/query_analysis/techniques/decomposition.md
docs/use_cases/query_analysis/techniques/decomposition.mdx
docs/use_cases/query_analysis/how_to/no_queries.md
docs/use_cases/query_analysis/how_to/no_queries.mdx
docs/use_cases/query_analysis/how_to/multiple_retrievers.md
docs/use_cases/query_analysis/how_to/multiple_retrievers.mdx
docs/use_cases/query_analysis/how_to/multiple_queries.md
docs/use_cases/query_analysis/how_to/multiple_queries.mdx
docs/use_cases/query_analysis/how_to/high_cardinality.md
docs/use_cases/query_analysis/how_to/high_cardinality.mdx
docs/use_cases/query_analysis/how_to/few_shot.md
docs/use_cases/query_analysis/how_to/few_shot.mdx
docs/use_cases/query_analysis/how_to/constructing_filters.md
docs/use_cases/query_analysis/how_to/constructing_filters.mdx
docs/modules/model_io/output_parsers/custom.md
docs/modules/model_io/output_parsers/custom.mdx
docs/modules/model_io/chat/function_calling.md
Expand Down
2 changes: 2 additions & 0 deletions docs/core_docs/docs/use_cases/query_analysis/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
position: 3
label: 'Query Analysis'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
position: 3
label: 'How-To Guides'
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
{
"cells": [
{
"cell_type": "raw",
"id": "df7d42b9-58a6-434c-a2d7-0b61142f6d3e",
"metadata": {},
"source": [
"---\n",
"sidebar_position: 6\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "f2195672-0cab-4967-ba8a-c6544635547d",
"metadata": {},
"source": [
"# Construct Filters\n",
"\n",
"We may want to do query analysis to extract filters to pass into retrievers. One way we ask the LLM to represent these filters is as a Zod schema. There is then the issue of converting that Zod schema into a filter that can be passed into a retriever. \n",
"\n",
"This can be done manually, but LangChain also provides some \"Translators\" that are able to translate from a common syntax into filters specific to each retriever. Here, we will cover how to use those translators."
]
},
{
"cell_type": "markdown",
"id": "bc1302ff",
"metadata": {},
"source": [
"## Setup\n",
"#### Install dependencies\n",
"\n",
"```{=mdx}\n",
"import Npm2Yarn from \"@theme/Npm2Yarn\";\n",
"\n",
"<Npm2Yarn>\n",
" langchain zod\n",
"</Npm2Yarn>\n",
"```\n"
]
},
{
"cell_type": "markdown",
"id": "70cd3b6d",
"metadata": {},
"source": [
"\n",
"In this example, `year` and `author` are both attributes to filter on."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "64055006",
"metadata": {},
"outputs": [],
"source": [
"import { z } from \"zod\";\n",
"\n",
"const searchSchema = z.object({\n",
" query: z.string(),\n",
" startYear: z.number().optional(),\n",
" author: z.string().optional(),\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "44eb6d98",
"metadata": {},
"outputs": [],
"source": [
"const searchQuery: z.infer<typeof searchSchema> = {\n",
" query: \"RAG\",\n",
" startYear: 2022,\n",
" author: \"LangChain\"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e8ba6705",
"metadata": {},
"outputs": [],
"source": [
"import { Comparison, Comparator } from \"langchain/chains/query_constructor/ir\";\n",
"\n",
"function constructComparisons(query: z.infer<typeof searchSchema>): Comparison[] {\n",
" const comparisons: Comparison[] = [];\n",
" if (query.startYear !== undefined) {\n",
" comparisons.push(\n",
" new Comparison(\n",
" \"gt\" as Comparator,\n",
" \"start_year\",\n",
" query.startYear,\n",
" )\n",
" );\n",
" }\n",
" if (query.author !== undefined) {\n",
" comparisons.push(\n",
" new Comparison(\n",
" \"eq\" as Comparator,\n",
" \"author\",\n",
" query.author,\n",
" )\n",
" );\n",
" }\n",
" return comparisons;\n",
" }\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6a79c9da",
"metadata": {},
"outputs": [],
"source": [
"const comparisons = constructComparisons(searchQuery);"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "2d0e9689",
"metadata": {},
"outputs": [],
"source": [
"import {\n",
" Operation,\n",
" Operator,\n",
"} from \"langchain/chains/query_constructor/ir\";\n",
"\n",
"const _filter = new Operation(\"and\" as Operator, comparisons)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d75455ae",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{\n",
" \u001b[32m\"$and\"\u001b[39m: [\n",
" { start_year: { \u001b[32m\"$gt\"\u001b[39m: \u001b[33m2022\u001b[39m } },\n",
" { author: { \u001b[32m\"$eq\"\u001b[39m: \u001b[32m\"LangChain\"\u001b[39m } }\n",
" ]\n",
"}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import { ChromaTranslator } from \"langchain/retrievers/self_query/chroma\";\n",
"\n",
"new ChromaTranslator().visitOperation(_filter)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Deno",
"language": "typescript",
"name": "deno"
},
"language_info": {
"file_extension": ".ts",
"mimetype": "text/x.typescript",
"name": "typescript",
"nb_converter": "script",
"pygments_lexer": "typescript",
"version": "5.3.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit dc6b883

Please sign in to comment.