Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🔢 fix: Normalize PDF Citation Page Numbers (#15731)

easyicetrustyfdaf4db93882
diff --git a/api/app/clients/tools/util/fileSearch.js b/api/app/clients/tools/util/fileSearch.js
index d9f701376f..f18eef6bc3 100644
--- a/api/app/clients/tools/util/fileSearch.js
+++ b/api/app/clients/tools/util/fileSearch.js
@@ -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)
diff --git a/api/test/app/clients/tools/util/fileSearch.test.js b/api/test/app/clients/tools/util/fileSearch.test.js
index f160ba5448..27788b2124 100644
--- a/api/test/app/clients/tools/util/fileSearch.test.js
+++ b/api/test/app/clients/tools/util/fileSearch.test.js
@@ -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,
],