mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-23 16:34:27 +00:00
🔢 fix: Normalize PDF Citation Page Numbers (#15731)
This commit is contained in:
parent
af6e97f0ae
commit
fdaf4db938
2 changed files with 34 additions and 3 deletions
|
|
@ -167,7 +167,10 @@ const createFileSearchTool = async ({
|
|||
content: docInfo.page_content,
|
||||
distance,
|
||||
file_id: result.file_id,
|
||||
page: docInfo.metadata.page || null,
|
||||
page:
|
||||
Number.isInteger(docInfo.metadata.page) && docInfo.metadata.page >= 0
|
||||
? docInfo.metadata.page + 1
|
||||
: null,
|
||||
})),
|
||||
)
|
||||
.sort((a, b) => a.distance - b.distance)
|
||||
|
|
|
|||
|
|
@ -110,6 +110,34 @@ describe('fileSearch.js - tuple return validation', () => {
|
|||
});
|
||||
|
||||
describe('success cases should return tuple with artifact object', () => {
|
||||
it.each([
|
||||
[0, [1]],
|
||||
[2, [3]],
|
||||
[undefined, []],
|
||||
[null, []],
|
||||
[-1, []],
|
||||
[1.5, []],
|
||||
['2', []],
|
||||
])('maps RAG page index %s to citation pages %j', async (page, pages) => {
|
||||
generateShortLivedToken.mockReturnValue('mock-jwt-token');
|
||||
axios.post.mockResolvedValue({
|
||||
data: [
|
||||
[{ page_content: 'Synthetic passage', metadata: { source: '/test.pdf', page } }, 0.2],
|
||||
],
|
||||
});
|
||||
|
||||
const fileSearchTool = await createFileSearchTool({
|
||||
userId: 'user1',
|
||||
files: [{ file_id: 'file-123', filename: 'test.pdf' }],
|
||||
fileCitations: true,
|
||||
});
|
||||
const [, artifact] = await fileSearchTool.func({ query: 'Synthetic page check' });
|
||||
const source = artifact.file_search.sources[0];
|
||||
|
||||
expect(source.pages).toEqual(pages);
|
||||
expect(source.pageRelevance).toEqual(pages.length ? { [pages[0]]: expect.any(Number) } : {});
|
||||
});
|
||||
|
||||
it('should return tuple with formatted results and sources artifact', async () => {
|
||||
generateShortLivedToken.mockReturnValue('mock-jwt-token');
|
||||
|
||||
|
|
@ -118,14 +146,14 @@ describe('fileSearch.js - tuple return validation', () => {
|
|||
[
|
||||
{
|
||||
page_content: 'This is test content from the document',
|
||||
metadata: { source: '/path/to/test.pdf', page: 1 },
|
||||
metadata: { source: '/path/to/test.pdf', page: 0 },
|
||||
},
|
||||
0.2,
|
||||
],
|
||||
[
|
||||
{
|
||||
page_content: 'Additional relevant content',
|
||||
metadata: { source: '/path/to/test.pdf', page: 2 },
|
||||
metadata: { source: '/path/to/test.pdf', page: 1 },
|
||||
},
|
||||
0.35,
|
||||
],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue