forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Vespa retriever (langchain-ai#1087)
* Add Vespa retriever * Small fixes and code review changes * Fix formatting issues in examples * Fix docs * Add port to allow connecting to local instances, fix typos in docs * Just use the raw URL for VespaRetriever --------- Co-authored-by: Tat Dat Duong <[email protected]> Co-authored-by: jacoblee93 <[email protected]>
- Loading branch information
1 parent
8d774e0
commit d772368
Showing
14 changed files
with
148 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Vespa Retriever | ||
|
||
This shows how to use Vespa.ai as a LangChain retriever. | ||
Vespa.ai is a platform for highly efficient structured text and vector search. | ||
Please refer to [Vespa.ai](https://vespa.ai) for more information. | ||
|
||
The following sets up a retriever that fetches results from Vespa's documentation search: | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/retrievers/vespa.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> | ||
|
||
Here, up to 5 results are retrieved from the `content` field in the `paragraph` document type, | ||
using `documentation` as the ranking method. The `userQuery()` is replaced with the actual query | ||
passed from LangChain. | ||
|
||
Please refer to the [pyvespa documentation](https://pyvespa.readthedocs.io/en/latest/getting-started-pyvespa.html#Query) | ||
for more information. | ||
|
||
The URL is the endpoint of the Vespa application. | ||
You can connect to any Vespa endpoint, either a remote service or a local instance using Docker. | ||
However, most Vespa Cloud instances are protected with mTLS. | ||
If this is your case, you can, for instance set up a [CloudFlare Worker](https://cloud.vespa.ai/en/security/cloudflare-workers) | ||
that contains the necessary credentials to connect to the instance. | ||
|
||
Now you can return the results and continue using them in LangChain. |
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,22 @@ | ||
import { VespaRetriever } from "langchain/retrievers/vespa"; | ||
|
||
export const run = async () => { | ||
const url = "https://doc-search.vespa.oath.cloud"; | ||
const query_body = { | ||
yql: "select content from paragraph where userQuery()", | ||
hits: 5, | ||
ranking: "documentation", | ||
locale: "en-us", | ||
}; | ||
const content_field = "content"; | ||
|
||
const retriever = new VespaRetriever({ | ||
url, | ||
auth: false, | ||
query_body, | ||
content_field, | ||
}); | ||
|
||
const result = await retriever.getRelevantDocuments("what is vespa?"); | ||
console.log(result); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* eslint-disable no-process-env */ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { test, expect } from "@jest/globals"; | ||
|
||
import { VespaRetriever } from "../vespa.js"; | ||
|
||
test("VespaRetriever", async () => { | ||
const url = process.env.VESPA_URL!; | ||
const query_body = { | ||
yql: "select * from music where album contains 'head';", | ||
hits: 5, | ||
locale: "en-us", | ||
}; | ||
const content_field = "album"; | ||
|
||
const retriever = new VespaRetriever({ | ||
url, | ||
auth: false, | ||
query_body, | ||
content_field, | ||
}); | ||
|
||
const docs = await retriever.getRelevantDocuments("what is vespa?"); | ||
expect(docs.length).toBeGreaterThan(0); | ||
|
||
console.log(docs); | ||
}); |
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,53 @@ | ||
import { Document } from "../document.js"; | ||
import { | ||
RemoteRetriever, | ||
RemoteRetrieverValues, | ||
RemoteRetrieverParams, | ||
} from "./remote/base.js"; | ||
|
||
export interface VespaRetrieverParams extends RemoteRetrieverParams { | ||
/** | ||
* The body of the query to send to Vespa | ||
*/ | ||
query_body: object; | ||
/** | ||
* The name of the field the content resides in | ||
*/ | ||
content_field: string; | ||
} | ||
|
||
export class VespaRetriever extends RemoteRetriever { | ||
query_body: object; | ||
|
||
content_field: string; | ||
|
||
constructor({ query_body, content_field, ...rest }: VespaRetrieverParams) { | ||
super(rest); | ||
this.query_body = query_body; | ||
this.content_field = content_field; | ||
|
||
this.url = `${this.url}/search/?`; | ||
} | ||
|
||
createJsonBody(query: string): RemoteRetrieverValues { | ||
return { | ||
...this.query_body, | ||
query, | ||
}; | ||
} | ||
|
||
processJsonResponse(json: RemoteRetrieverValues): Document[] { | ||
return json.root.children.map( | ||
(doc: { | ||
id: string; | ||
relevance: number; | ||
source: string; | ||
fields: Record<string, unknown>; | ||
}) => | ||
new Document({ | ||
pageContent: doc.fields[this.content_field] as string, | ||
metadata: { id: doc.id }, | ||
}) | ||
); | ||
} | ||
} |
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
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