Skip to content

Commit

Permalink
fix(cohere,langchain): handle exceptions in compressDocuments and for…
Browse files Browse the repository at this point in the history
…matDocuments methods due to empty documents (#7372)
  • Loading branch information
ahm750 authored Dec 24, 2024
1 parent a51bb4a commit 9039484
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions langchain/src/chains/combine_documents/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export async function formatDocuments({
documents: Document[];
config?: RunnableConfig;
}) {
if (documents == null || documents.length === 0) {
return "";
}
const formattedDocs = await Promise.all(
documents.map((document) =>
documentPrompt
Expand Down
3 changes: 3 additions & 0 deletions libs/langchain-cohere/src/rerank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export class CohereRerank extends BaseDocumentCompressor {
documents: Array<DocumentInterface>,
query: string
): Promise<Array<DocumentInterface>> {
if (documents == null || documents.length === 0) {
return [];
}
const _docs = documents.map((doc) => doc.pageContent);
const { results } = await this.client.rerank({
model: this.model,
Expand Down

0 comments on commit 9039484

Please sign in to comment.