-
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.
Merge remote-tracking branch 'upstream/main' into nsc/firecrawl-loader
- Loading branch information
Showing
47 changed files
with
2,501 additions
and
908 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
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,30 @@ | ||
--- | ||
sidebar_position: 0 | ||
sidebar_class_name: hidden | ||
--- | ||
|
||
# Providers | ||
|
||
LangChain integrates with many providers. | ||
|
||
## Partner Packages | ||
|
||
These providers have standalone `@langchain/{provider}` packages for improved versioning, dependency management and testing. | ||
|
||
- [Anthropic](https://www.npmjs.com/package/@langchain/anthropic) | ||
- [Azure OpenAI](https://www.npmjs.com/package/@langchain/azure-openai) | ||
- [Cloudflare](https://www.npmjs.com/package/@langchain/cloudflare) | ||
- [Cohere](https://www.npmjs.com/package/@langchain/cohere) | ||
- [Exa](https://www.npmjs.com/package/@langchain/exa) | ||
- [Google GenAI](https://www.npmjs.com/package/@langchain/google-genai) | ||
- [Google VertexAI](https://www.npmjs.com/package/@langchain/google-vertexai) | ||
- [Google VertexAI Web](https://www.npmjs.com/package/@langchain/google-vertexai-web) | ||
- [Groq](https://www.npmjs.com/package/@langchain/groq) | ||
- [MistralAI](https://www.npmjs.com/package/@langchain/mistralai) | ||
- [MongoDB](https://www.npmjs.com/package/@langchain/mongodb) | ||
- [Nomic](https://www.npmjs.com/package/@langchain/nomic) | ||
- [OpenAI](https://www.npmjs.com/package/@langchain/openai) | ||
- [Pinecone](https://www.npmjs.com/package/@langchain/pinecone) | ||
- [Redis](https://www.npmjs.com/package/@langchain/redis) | ||
- [Weaviate](https://www.npmjs.com/package/@langchain/weaviate) | ||
- [Yandex](https://www.npmjs.com/package/@langchain/yandex) |
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
93 changes: 93 additions & 0 deletions
93
examples/src/retrievers/parent_document_retriever_rerank.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,93 @@ | ||
import { OpenAIEmbeddings } from "@langchain/openai"; | ||
import { CohereRerank } from "@langchain/cohere"; | ||
import { HNSWLib } from "@langchain/community/vectorstores/hnswlib"; | ||
import { InMemoryStore } from "langchain/storage/in_memory"; | ||
import { | ||
ParentDocumentRetriever, | ||
type SubDocs, | ||
} from "langchain/retrievers/parent_document"; | ||
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter"; | ||
|
||
// init Cohere Rerank. Remember to add COHERE_API_KEY to your .env | ||
const reranker = new CohereRerank({ | ||
topN: 50, | ||
model: "rerank-multilingual-v2.0", | ||
}); | ||
|
||
export function documentCompressorFiltering({ | ||
relevanceScore, | ||
}: { relevanceScore?: number } = {}) { | ||
return (docs: SubDocs) => { | ||
let outputDocs = docs; | ||
|
||
if (relevanceScore) { | ||
const docsRelevanceScoreValues = docs.map( | ||
(doc) => doc?.metadata?.relevanceScore | ||
); | ||
outputDocs = docs.filter( | ||
(_doc, index) => | ||
(docsRelevanceScoreValues?.[index] || 1) >= relevanceScore | ||
); | ||
} | ||
|
||
return outputDocs; | ||
}; | ||
} | ||
|
||
const splitter = new RecursiveCharacterTextSplitter({ | ||
chunkSize: 500, | ||
chunkOverlap: 0, | ||
}); | ||
|
||
const jimDocs = await splitter.createDocuments([`Jim favorite color is blue.`]); | ||
|
||
const pamDocs = await splitter.createDocuments([`Pam favorite color is red.`]); | ||
|
||
const vectorstore = await HNSWLib.fromDocuments([], new OpenAIEmbeddings()); | ||
const docstore = new InMemoryStore(); | ||
|
||
const retriever = new ParentDocumentRetriever({ | ||
vectorstore, | ||
docstore, | ||
// Very small chunks for demo purposes. | ||
// Use a bigger chunk size for serious use-cases. | ||
childSplitter: new RecursiveCharacterTextSplitter({ | ||
chunkSize: 10, | ||
chunkOverlap: 0, | ||
}), | ||
childK: 50, | ||
parentK: 5, | ||
// We add Reranker | ||
documentCompressor: reranker, | ||
documentCompressorFilteringFn: documentCompressorFiltering({ | ||
relevanceScore: 0.3, | ||
}), | ||
}); | ||
|
||
const docs = jimDocs.concat(pamDocs); | ||
await retriever.addDocuments(docs); | ||
|
||
// This will search for documents in vector store and return for LLM already reranked and sorted document | ||
// with appropriate minimum relevance score | ||
const retrievedDocs = await retriever.getRelevantDocuments( | ||
"What is Pam's favorite color?" | ||
); | ||
|
||
// Pam's favorite color is returned first! | ||
console.log(JSON.stringify(retrievedDocs, null, 2)); | ||
/* | ||
[ | ||
{ | ||
"pageContent": "My favorite color is red.", | ||
"metadata": { | ||
"relevanceScore": 0.9 | ||
"loc": { | ||
"lines": { | ||
"from": 1, | ||
"to": 1 | ||
} | ||
} | ||
} | ||
} | ||
] | ||
*/ |
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
Oops, something went wrong.