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.
feat: DataberryRetriever (langchain-ai#643) (langchain-ai#732)
* feat: DataberryRetriever (langchain-ai#643) * cr * cr * Small fixes for new paths --------- Co-authored-by: Georges Petrov <[email protected]> Co-authored-by: Nuno Campos <[email protected]>
- Loading branch information
1 parent
f975656
commit 86ac005
Showing
15 changed files
with
145 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,40 @@ | ||
# Databerry | ||
|
||
This page covers how to use the [Databerry](https://databerry.ai) within LangChain. | ||
|
||
## What is Databerry? | ||
|
||
Databerry is an [open source](https://github.com/gmpetrov/databerry) document retrievial platform that helps to connect your personal data with Large Language Models. | ||
|
||
![Databerry](/img/DataberryDashboard.png) | ||
|
||
## Quick start | ||
|
||
Retrieving documents stored in Databerry from LangChain is very easy! | ||
|
||
```typescript | ||
import { DataberryRetriever } from "langchain/retrievers/databerry"; | ||
|
||
const retriever = new DataberryRetriever({ | ||
datastoreUrl: "https://api.databerry.ai/query/clg1xg2h80000l708dymr0fxc", | ||
apiKey: "DATABERRY_API_KEY", // optional: needed for private datastores | ||
topK: 8, // optional: default value is 3 | ||
}); | ||
|
||
// Create a chain that uses the OpenAI LLM and Databerry retriever. | ||
const chain = RetrievalQAChain.fromLLM(model, retriever); | ||
|
||
// Call the chain with a query. | ||
const res = await chain.call({ | ||
query: "What's Databerry?", | ||
}); | ||
|
||
console.log({ res }); | ||
/* | ||
{ | ||
res: { | ||
text: 'Databerry provides a user-friendly solution to quickly setup a semantic search system over your personal data without any technical knowledge.' | ||
} | ||
} | ||
*/ | ||
``` |
10 changes: 10 additions & 0 deletions
10
docs/docs/modules/indexes/retrievers/databerry-retriever.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,10 @@ | ||
# Remote Retriever | ||
|
||
This example shows how to use the Databerry Retriever in a `RetrievalQAChain` to retrieve documents from a Databerry.ai datastore. | ||
|
||
## Usage | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/retrievers/databerry.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,13 @@ | ||
import { DataberryRetriever } from "langchain/retrievers/databerry"; | ||
|
||
export const run = async () => { | ||
const retriever = new DataberryRetriever({ | ||
datastoreUrl: "https://api.databerry.ai/query/clg1xg2h80000l708dymr0fxc", | ||
apiKey: "DATABERRY_API_KEY", // optional: needed for private datastores | ||
topK: 8, // optional: default value is 3 | ||
}); | ||
|
||
const docs = await retriever.getRelevantDocuments("hello"); | ||
|
||
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
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,63 @@ | ||
import { BaseRetriever } from "../schema/index.js"; | ||
import { Document } from "../document.js"; | ||
import { AsyncCaller, AsyncCallerParams } from "../util/async_caller.js"; | ||
|
||
interface DataberryRetrieverArgs extends AsyncCallerParams { | ||
datastoreUrl: string; | ||
topK?: number; | ||
apiKey?: string; | ||
} | ||
|
||
interface Berry { | ||
text: string; | ||
score: number; | ||
source?: string; | ||
[key: string]: unknown; | ||
} | ||
|
||
export class DataberryRetriever extends BaseRetriever { | ||
caller: AsyncCaller; | ||
|
||
datastoreUrl: string; | ||
|
||
topK?: number; | ||
|
||
apiKey?: string; | ||
|
||
constructor({ datastoreUrl, apiKey, topK, ...rest }: DataberryRetrieverArgs) { | ||
super(); | ||
|
||
this.caller = new AsyncCaller(rest); | ||
this.datastoreUrl = datastoreUrl; | ||
this.apiKey = apiKey; | ||
this.topK = topK; | ||
} | ||
|
||
async getRelevantDocuments(query: string): Promise<Document[]> { | ||
const r = await this.caller.call(fetch, this.datastoreUrl, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
query, | ||
...(this.topK ? { topK: this.topK } : {}), | ||
}), | ||
headers: { | ||
"Content-Type": "application/json", | ||
...(this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {}), | ||
}, | ||
}); | ||
|
||
const { results } = (await r.json()) as { results: Berry[] }; | ||
|
||
return results.map( | ||
({ text, score, source, ...rest }) => | ||
new Document({ | ||
pageContent: text, | ||
metadata: { | ||
score, | ||
source, | ||
...rest, | ||
}, | ||
}) | ||
); | ||
} | ||
} |
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