-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding self query for vectara #3338
Merged
jacoblee93
merged 6 commits into
langchain-ai:main
from
adeelehsan:adding-self-query-for-vectara
Nov 20, 2023
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
27eefe9
added self query for vectara vector store
ef6187c
updated the docs
a96cc3f
skip the integration test
5cbb083
Updated the comments in the example
2a570ce
Merge branch 'main' into adding-self-query-for-vectara
adeelehsan cc1e289
Rename test, add linter warning
jacoblee93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...ocs/modules/data_connection/retrievers/how_to/self_query/vectara-self-query.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Vectara Self Query Retriever | ||
|
||
This example shows how to use a self query retriever with a [Vectara](https://vectara.com/) vector store. | ||
|
||
If you haven't already set up Vectara, please [follow the instructions here](/docs/integrations/vectorstores/vectara.mdx). | ||
|
||
## Usage | ||
|
||
This example shows how to intialize a `SelfQueryRetriever` with a vector store: | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/retrievers/vectara_self_query.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> | ||
|
||
You can also initialize the retriever with default search parameters that apply in | ||
addition to the generated query: | ||
|
||
```typescript | ||
const selfQueryRetriever = await SelfQueryRetriever.fromLLM({ | ||
llm, | ||
vectorStore, | ||
documentContents, | ||
attributeInfo, | ||
/** | ||
* We need to use a translator that translates the queries into a | ||
* filter format that the vector store can understand. LangChain provides one here. | ||
*/ | ||
structuredQueryTranslator: new VectaraTranslator()(), | ||
searchParams: { | ||
filter: { | ||
filter: "( doc.genre = 'science fiction' ) and ( doc.rating > 8.5 )", | ||
}, | ||
mergeFiltersOperator: "and", | ||
}, | ||
}); | ||
``` | ||
|
||
See the [official docs](https://docs.vectara.com/) for more on how to construct metadata filters. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { AttributeInfo } from "langchain/schema/query_constructor"; | ||
import { Document } from "langchain/document"; | ||
import { SelfQueryRetriever } from "langchain/retrievers/self_query"; | ||
|
||
import { OpenAI } from "langchain/llms/openai"; | ||
import { VectaraStore } from "langchain/vectorstores/vectara"; | ||
import { VectaraTranslator } from "langchain/retrievers/self_query/vectara"; | ||
import { FakeEmbeddings } from "langchain/embeddings/fake"; | ||
/** | ||
* First, we create a bunch of documents. You can load your own documents here instead. | ||
* Each document has a pageContent and a metadata field. Make sure your metadata matches the AttributeInfo below. | ||
*/ | ||
const docs = [ | ||
new Document({ | ||
pageContent: | ||
"A bunch of scientists bring back dinosaurs and mayhem breaks loose", | ||
metadata: { year: 1993, rating: 7.7, genre: "science fiction" }, | ||
}), | ||
new Document({ | ||
pageContent: | ||
"Leo DiCaprio gets lost in a dream within a dream within a dream within a ...", | ||
metadata: { year: 2010, director: "Christopher Nolan", rating: 8.2 }, | ||
}), | ||
new Document({ | ||
pageContent: | ||
"A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea", | ||
metadata: { year: 2006, director: "Satoshi Kon", rating: 8.6 }, | ||
}), | ||
new Document({ | ||
pageContent: | ||
"A bunch of normal-sized women are supremely wholesome and some men pine after them", | ||
metadata: { year: 2019, director: "Greta Gerwig", rating: 8.3 }, | ||
}), | ||
new Document({ | ||
pageContent: "Toys come alive and have a blast doing so", | ||
metadata: { year: 1995, genre: "animated" }, | ||
}), | ||
new Document({ | ||
pageContent: "Three men walk into the Zone, three men walk out of the Zone", | ||
metadata: { | ||
year: 1979, | ||
rating: 9.9, | ||
director: "Andrei Tarkovsky", | ||
genre: "science fiction", | ||
}, | ||
}), | ||
]; | ||
|
||
/** | ||
* Next, we define the attributes we want to be able to query on. | ||
* in this case, we want to be able to query on the genre, year, director, rating, and length of the movie. | ||
* We also provide a description of each attribute and the type of the attribute. | ||
* This is used to generate the query prompts. | ||
* | ||
* We need to setup the filters in the vectara as well otherwise filter won't work. | ||
* To setup the filter in vectara, go to Data -> {your_created_corpus} -> overview | ||
* In the overview section edit the filters section and all the following attributes in | ||
* the filters. | ||
*/ | ||
const attributeInfo: AttributeInfo[] = [ | ||
{ | ||
name: "genre", | ||
description: "The genre of the movie", | ||
type: "string or array of strings", | ||
}, | ||
{ | ||
name: "year", | ||
description: "The year the movie was released", | ||
type: "number", | ||
}, | ||
{ | ||
name: "director", | ||
description: "The director of the movie", | ||
type: "string", | ||
}, | ||
{ | ||
name: "rating", | ||
description: "The rating of the movie (1-10)", | ||
type: "number", | ||
}, | ||
]; | ||
|
||
/** | ||
* Next, we instantiate a vector store. This is where we store the embeddings of the documents. | ||
* We also need to provide an embeddings object. This is used to embed the documents. | ||
*/ | ||
|
||
const config = { | ||
customerId: Number(process.env.VECTARA_CUSTOMER_ID), | ||
corpusId: Number(process.env.VECTARA_CORPUS_ID), | ||
apiKey: String(process.env.VECTARA_API_KEY), | ||
verbose: true, | ||
}; | ||
|
||
const vectorStore = await VectaraStore.fromDocuments( | ||
docs, | ||
new FakeEmbeddings(), | ||
config | ||
); | ||
|
||
const llm = new OpenAI(); | ||
const documentContents = "Brief summary of a movie"; | ||
|
||
const selfQueryRetriever = await SelfQueryRetriever.fromLLM({ | ||
llm, | ||
vectorStore, | ||
documentContents, | ||
attributeInfo, | ||
/** | ||
* We need to create a basic translator that translates the queries into a | ||
* filter format that the vector store can understand. We provide a basic translator | ||
* here, but you can create your own translator by extending BaseTranslator | ||
* abstract class. Note that the vector store needs to support filtering on the metadata | ||
* attributes you want to query on. | ||
*/ | ||
structuredQueryTranslator: new VectaraTranslator(), | ||
}); | ||
|
||
/** | ||
* Now we can query the vector store. | ||
* We can ask questions like "Which movies are less than 90 minutes?" or "Which movies are rated higher than 8.5?". | ||
* We can also ask questions like "Which movies are either comedy or drama and are less than 90 minutes?". | ||
* The retriever will automatically convert these questions into queries that can be used to retrieve documents. | ||
*/ | ||
const query1 = await selfQueryRetriever.getRelevantDocuments( | ||
"What are some movies about dinosaurs" | ||
); | ||
const query2 = await selfQueryRetriever.getRelevantDocuments( | ||
"I want to watch a movie rated higher than 8.5" | ||
); | ||
const query3 = await selfQueryRetriever.getRelevantDocuments( | ||
"Which movies are directed by Greta Gerwig?" | ||
); | ||
const query4 = await selfQueryRetriever.getRelevantDocuments( | ||
"Which movies are either comedy or science fiction and are rated higher than 8.5?" | ||
); | ||
console.log(query1, query2, query3, query4); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
langchain/src/retrievers/self_query/tests/vectara_self_query.init.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* eslint-disable no-process-env */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR adds code that explicitly accesses environment variables via |
||
import { test } from "@jest/globals"; | ||
import { Document } from "../../../document.js"; | ||
import { AttributeInfo } from "../../../schema/query_constructor.js"; | ||
import { SelfQueryRetriever } from "../index.js"; | ||
import { OpenAI } from "../../../llms/openai.js"; | ||
import { VectaraTranslator } from "../vectara.js"; | ||
import { FakeEmbeddings } from "../../../embeddings/fake.js"; | ||
import { VectaraStore } from "../../../vectorstores/vectara.js"; | ||
|
||
test.skip("Vectara Self Query Retriever Test", async () => { | ||
const docs = [ | ||
new Document({ | ||
pageContent: | ||
"A bunch of scientists bring back dinosaurs and mayhem breaks loose", | ||
metadata: { year: 1993, rating: 7.7, genre: "science fiction" }, | ||
}), | ||
new Document({ | ||
pageContent: | ||
"Leo DiCaprio gets lost in a dream within a dream within a dream within a ...", | ||
metadata: { year: 2010, director: "Christopher Nolan", rating: 8.2 }, | ||
}), | ||
new Document({ | ||
pageContent: | ||
"A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea", | ||
metadata: { year: 2006, director: "Satoshi Kon", rating: 8.6 }, | ||
}), | ||
new Document({ | ||
pageContent: | ||
"A bunch of normal-sized women are supremely wholesome and some men pine after them", | ||
metadata: { year: 2019, director: "Greta Gerwig", rating: 8.3 }, | ||
}), | ||
new Document({ | ||
pageContent: "Toys come alive and have a blast doing so", | ||
metadata: { year: 1995, genre: "animated" }, | ||
}), | ||
new Document({ | ||
pageContent: | ||
"Three men walk into the Zone, three men walk out of the Zone", | ||
metadata: { | ||
year: 1979, | ||
rating: 9.9, | ||
director: "Andrei Tarkovsky", | ||
genre: "science fiction", | ||
}, | ||
}), | ||
]; | ||
|
||
const attributeInfo: AttributeInfo[] = [ | ||
{ | ||
name: "genre", | ||
description: "The genre of the movie", | ||
type: "string or array of strings", | ||
}, | ||
{ | ||
name: "year", | ||
description: "The year the movie was released", | ||
type: "number", | ||
}, | ||
{ | ||
name: "director", | ||
description: "The director of the movie", | ||
type: "string", | ||
}, | ||
{ | ||
name: "rating", | ||
description: "The rating of the movie (1-10)", | ||
type: "number", | ||
}, | ||
]; | ||
const config = { | ||
customerId: Number(process.env.VECTARA_CUSTOMER_ID), | ||
corpusId: Number(process.env.VECTARA_CORPUS_ID), | ||
apiKey: String(process.env.VECTARA_API_KEY), | ||
verbose: true, | ||
}; | ||
|
||
const vectorStore = await VectaraStore.fromDocuments( | ||
docs, | ||
new FakeEmbeddings(), | ||
config | ||
); | ||
|
||
const llm = new OpenAI(); | ||
const documentContents = "Brief summary of a movie"; | ||
|
||
const selfQueryRetriever = await SelfQueryRetriever.fromLLM({ | ||
llm, | ||
vectorStore, | ||
documentContents, | ||
attributeInfo, | ||
|
||
structuredQueryTranslator: new VectaraTranslator(), | ||
}); | ||
|
||
const query1 = await selfQueryRetriever.getRelevantDocuments( | ||
"I want to watch a movie rated higher than 8.5" | ||
); | ||
const query2 = await selfQueryRetriever.getRelevantDocuments( | ||
"Which movies are directed by Greta Gerwig?" | ||
); | ||
const query3 = await selfQueryRetriever.getRelevantDocuments( | ||
"Which movies are either comedy or science fiction and are rated higher than 8.5?" | ||
); | ||
const query4 = await selfQueryRetriever.getRelevantDocuments( | ||
"Wau wau wau wau hello gello hello?" | ||
); | ||
console.log(query1, query2, query3, query4); | ||
expect(query1.length).toBe(2); | ||
expect(query2.length).toBe(1); | ||
expect(query3.length).toBe(1); | ||
expect(query4.length).toBe(0); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR adds code that requires environment variables via
process.env
. Please review this change to ensure that the necessary environment variables are properly set.