-
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.
- Loading branch information
1 parent
d169dde
commit 58931bf
Showing
8 changed files
with
527 additions
and
522 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
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 |
---|---|---|
@@ -1,45 +1,51 @@ | ||
import { BaseRetriever, BaseRetrieverInput } from "@langchain/core/retrievers"; | ||
import { Document } from "@langchain/core/documents"; | ||
import { searchArxiv, loadDocsFromResults, getDocsFromSummaries } from '../utils/arxiv.js'; | ||
import { | ||
searchArxiv, | ||
loadDocsFromResults, | ||
getDocsFromSummaries, | ||
} from "../utils/arxiv.js"; | ||
|
||
export type ArxivRetrieverOptions = { | ||
getFullDocuments?: boolean; | ||
maxSearchResults?: number; | ||
returnFullDocuments?: boolean; | ||
maxSearchResults?: number; | ||
} & BaseRetrieverInput; | ||
|
||
/** | ||
* A retriever that searches arXiv for relevant articles based on a query. | ||
* It can retrieve either full documents (PDFs) or just summaries. | ||
*/ | ||
export class ArxivRetriever extends BaseRetriever { | ||
static lc_name() { | ||
return "ArxivRetriever"; | ||
} | ||
static lc_name() { | ||
return "ArxivRetriever"; | ||
} | ||
|
||
lc_namespace = ["langchain", "retrievers", "arxiv_retriever"]; | ||
lc_namespace = ["langchain", "retrievers", "arxiv_retriever"]; | ||
|
||
getFullDocuments: boolean; | ||
maxSearchResults: number; | ||
returnFullDocuments = false; | ||
|
||
constructor(options: ArxivRetrieverOptions = {}) { | ||
super(options); | ||
this.getFullDocuments = options.getFullDocuments ?? false; | ||
this.maxSearchResults = options.maxSearchResults ?? 10; | ||
} | ||
maxSearchResults = 10; | ||
|
||
constructor(options: ArxivRetrieverOptions = {}) { | ||
super(options); | ||
this.returnFullDocuments = | ||
options.returnFullDocuments ?? this.returnFullDocuments; | ||
this.maxSearchResults = options.maxSearchResults ?? this.maxSearchResults; | ||
} | ||
|
||
async _getRelevantDocuments(query: string): Promise<Document[]> { | ||
try { | ||
const results = await searchArxiv(query, this.maxSearchResults); | ||
|
||
async _getRelevantDocuments(query: string): Promise<Document[]> { | ||
try { | ||
const results = await searchArxiv(query, this.maxSearchResults); | ||
|
||
if (this.getFullDocuments) { | ||
// Fetch and parse PDFs to get full documents | ||
return await loadDocsFromResults(results); | ||
} else { | ||
// Use summaries as documents | ||
return getDocsFromSummaries(results); | ||
} | ||
} catch (error) { | ||
throw new Error(`Error retrieving documents from arXiv.`); | ||
} | ||
if (this.returnFullDocuments) { | ||
// Fetch and parse PDFs to get full documents | ||
return await loadDocsFromResults(results); | ||
} else { | ||
// Use summaries as documents | ||
return getDocsFromSummaries(results); | ||
} | ||
} catch (error) { | ||
throw new Error(`Error retrieving documents from arXiv.`); | ||
} | ||
} | ||
} |
Oops, something went wrong.