-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add MetalRetriever * Add docs and example * Lint
- Loading branch information
Showing
10 changed files
with
126 additions
and
3 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,16 @@ | ||
# Remote Retriever | ||
|
||
This example shows how to use the Metal Retriever in a `RetrievalQAChain` to retrieve documents from Metal index. | ||
|
||
## Setup | ||
|
||
```bash npm2yarn | ||
npm i @getmetal/metal-sdk | ||
``` | ||
|
||
## Usage | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/retrievers/metal.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> |
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
File renamed without changes.
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,17 @@ | ||
import Metal from "@getmetal/metal-sdk"; | ||
import { MetalRetriever } from "langchain/retrievers"; | ||
|
||
export const run = async () => { | ||
const MetalSDK = Metal.default; | ||
|
||
const client = new MetalSDK( | ||
process.env.METAL_API_KEY!, | ||
process.env.METAL_CLIENT_ID!, | ||
process.env.METAL_APP_ID | ||
); | ||
const retriever = new MetalRetriever({ client }); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type MetalSDK from "@getmetal/metal-sdk"; | ||
|
||
import { BaseRetriever } from "../schema/index.js"; | ||
import { Document } from "../document.js"; | ||
|
||
export interface MetalRetrieverFields { | ||
client: MetalSDK.default; | ||
} | ||
|
||
interface ResponseItem { | ||
text: string; | ||
[key: string]: unknown; | ||
} | ||
|
||
export class MetalRetriever extends BaseRetriever { | ||
client: MetalSDK.default; | ||
|
||
constructor(fields: MetalRetrieverFields) { | ||
super(); | ||
|
||
this.client = fields.client; | ||
} | ||
|
||
async getRelevantDocuments(query: string): Promise<Document[]> { | ||
const res = await this.client.search({ text: query }); | ||
|
||
const items = ("data" in res ? res.data : res) as ResponseItem[]; | ||
return items.map( | ||
({ text, metadata }) => | ||
new Document({ | ||
pageContent: text, | ||
metadata: metadata as Record<string, unknown>, | ||
}) | ||
); | ||
} | ||
} |
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 @@ | ||
/* eslint-disable no-process-env */ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { test, expect } from "@jest/globals"; | ||
import Metal from "@getmetal/metal-sdk"; | ||
|
||
import { MetalRetriever } from "../metal.js"; | ||
|
||
test("MetalRetriever", async () => { | ||
const MetalSDK = Metal.default; | ||
const client = new MetalSDK( | ||
process.env.METAL_API_KEY!, | ||
process.env.METAL_CLIENT_ID!, | ||
process.env.METAL_APP_ID | ||
); | ||
const retriever = new MetalRetriever({ client }); | ||
|
||
const docs = await retriever.getRelevantDocuments("hello"); | ||
|
||
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
047691f
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.
Successfully deployed to the following URLs:
langchainjs-docs – ./
langchainjs-docs-langchain.vercel.app
langchainjs-docs-git-main-langchain.vercel.app
langchainjs-docs-ruddy.vercel.app
js.langchain.com