Repository navigation
[FEAT] - Read scanned PDFs and images in knowledge uploads with OCR #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,40 @@ | |
| </SettingsRow> | ||
| </div> | ||
|
|
||
| <div class="mt-6 rounded-lg border border-border bg-card p-6" data-testid="knowledge-ocr"> | ||
| <h3 class="text-sm font-semibold text-foreground">{{ store.getTranslation('settings.knowledge_setup.ocr_title') }}</h3> | ||
| <p class="mb-4 mt-1 text-xs text-muted-foreground">{{ store.getTranslation('settings.knowledge_setup.ocr_description') }}</p> | ||
| <SettingsRow :label="store.getTranslation('settings.knowledge_setup.ocr_mode')" :hint="store.getTranslation(`settings.knowledge_setup.ocr_mode_hint.${form.ocr.mode}`)" stacked> | ||
| <ShadSelect v-model="form.ocr.mode" :disabled="!canEdit || settingsForm.saving"> | ||
| <ShadSelectTrigger data-testid="knowledge-ocr-mode"> | ||
| <ShadSelectValue /> | ||
| </ShadSelectTrigger> | ||
| <ShadSelectContent> | ||
| <ShadSelectItem v-for="mode in OCR_MODES" :key="mode" :value="mode" :data-testid="`knowledge-ocr-mode-${mode}`">{{ store.getTranslation(`settings.knowledge_setup.ocr_mode_option.${mode}`) }}</ShadSelectItem> | ||
| </ShadSelectContent> | ||
| </ShadSelect> | ||
| </SettingsRow> | ||
| <template v-if="form.ocr.mode !== 'off'"> | ||
| <SettingsRow :label="store.getTranslation('settings.knowledge_setup.ocr_model')" :hint="store.getTranslation('settings.knowledge_setup.ocr_model_hint')" stacked> | ||
| <DefaultModelPicker | ||
| v-model="form.ocr.model_id" | ||
| :disabled="!canEdit || settingsForm.saving" | ||
| :endpoint="adminModelsPath" | ||
| :selected-model-endpoint="adminModelsPath" | ||
| value-mode="uuid" | ||
| :placeholder="store.getTranslation('settings.knowledge_setup.ocr_model_none')" | ||
| :params="{type: 'document_reader'}" | ||
| /> | ||
| </SettingsRow> | ||
| <SettingsRow :label="store.getTranslation('settings.knowledge_setup.ocr_prompt')" :hint="store.getTranslation('settings.knowledge_setup.ocr_prompt_hint')" stacked> | ||
| <ShadTextarea v-model="ocrPrompt" rows="7" class="font-mono text-xs" :disabled="!canEdit || settingsForm.saving" data-testid="knowledge-ocr-prompt" /> | ||
| <ShadButton v-if="form.ocr.system_prompt !== null" variant="link" size="sm" class="mt-1 h-auto px-0" :disabled="!canEdit || settingsForm.saving" data-testid="knowledge-ocr-prompt-reset" @click="form.ocr.system_prompt = null"> | ||
| {{ store.getTranslation('settings.knowledge_setup.ocr_prompt_reset') }} | ||
| </ShadButton> | ||
| </SettingsRow> | ||
| </template> | ||
| </div> | ||
|
|
||
| <div v-if="setup.connections.length === 0" class="mt-6 rounded-lg border border-border bg-card p-6" data-testid="knowledge-setup-steps"> | ||
| <h3 class="text-sm font-semibold text-foreground">{{ store.getTranslation('settings.knowledge_setup.steps.title') }}</h3> | ||
| <ol class="mt-3 list-decimal space-y-1 pl-5 text-sm text-muted-foreground"> | ||
|
|
@@ -74,10 +108,12 @@ import {ArrowLeft} from 'lucide-vue-next'; | |
| import {computed, onBeforeUnmount, onMounted, reactive, ref, watch} from 'vue'; | ||
| import {useMainStore} from '@/stores'; | ||
| import DefaultModelPicker from '~/components/settings/DefaultModelPicker.vue'; | ||
| import type {OcrMode} from '~/types/generated/OcrMode'; | ||
| import type {RagSetupResponse} from '~/types/generated/RagSetupResponse'; | ||
|
|
||
| const NONE = 'none'; | ||
| const SECTIONS = ['reranker', 'defaults', 'chunking'] as const; | ||
| const SECTIONS = ['reranker', 'defaults', 'chunking', 'ocr'] as const; | ||
| const OCR_MODES = ['off', 'fallback', 'always'] as const; | ||
|
|
||
| const store = useMainStore(); | ||
| const canEdit = computed(() => store.hasPermission(ADMIN_RAG_EDIT)); | ||
|
|
@@ -87,6 +123,12 @@ const form = reactive({ | |
| reranker: {model_id: null as string | null, top_n: 6, min_score: null as number | null}, | ||
| defaults: {connection_id: null as string | null}, | ||
| chunking: {chunk_size: 400, overlap_percent: 15}, | ||
| ocr: {mode: 'off' as OcrMode, model_id: null as string | null, system_prompt: null as string | null}, | ||
| }); | ||
|
|
||
| const ocrPrompt = computed({ | ||
| get: () => form.ocr.system_prompt ?? setup.value?.ocr.default_system_prompt ?? '', | ||
| set: (value: string) => (form.ocr.system_prompt = value === setup.value?.ocr.default_system_prompt ? null : value), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalize an empty prompt to the default state. If the admin clears the editor, this setter stores Map empty or whitespace-only input to
|
||
| }); | ||
|
|
||
| const minScore = computed({ | ||
|
|
@@ -107,6 +149,7 @@ function fill(value: RagSetupResponse) { | |
| form.reranker = {...value.reranker}; | ||
| form.defaults = {...value.defaults}; | ||
| form.chunking = {...value.chunking}; | ||
| form.ocr = {mode: value.ocr.mode, model_id: value.ocr.model_id, system_prompt: value.ocr.system_prompt ?? null}; | ||
| } | ||
|
|
||
| function apply(value: RagSetupResponse) { | ||
|
|
@@ -138,11 +181,14 @@ const settingsForm = useSettingsForm({ | |
| if (form.reranker.min_score !== null && !Number.isFinite(form.reranker.min_score)) return 'rag.error.invalid_min_score'; | ||
| if (!Number.isInteger(form.chunking.chunk_size) || form.chunking.chunk_size < 100 || form.chunking.chunk_size > 2000) return 'rag.error.invalid_chunking'; | ||
| if (!Number.isInteger(form.chunking.overlap_percent) || form.chunking.overlap_percent < 0 || form.chunking.overlap_percent > 50) return 'rag.error.invalid_chunking'; | ||
| if (form.ocr.mode !== 'off' && !form.ocr.model_id) return 'rag.error.ocr_model_required'; | ||
| return null; | ||
| }, | ||
| save: async () => { | ||
| try { | ||
| setup.value = await saveRagSetup(form); | ||
| const saved = await saveRagSetup(form); | ||
| setup.value = saved; | ||
| fill(saved); | ||
| } catch (error: any) { | ||
| throw new Error(ragErrorMessage(error, '')); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
|
||
| /** | ||
| * When uploads are read by the OCR model: never, only for PDF pages without text (and images), or for every PDF page. | ||
| */ | ||
| export type OcrMode = "off" | "fallback" | "always"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
| import type { OcrMode } from "./OcrMode"; | ||
|
|
||
| /** | ||
| * Document reading (OCR) for knowledge uploads. | ||
| */ | ||
| export type OcrSettings = { mode: OcrMode, | ||
| /** | ||
| * An OCR model, or a chat model that takes images (and PDFs, for PDF pages). | ||
| */ | ||
| model_id: string | null, | ||
| /** | ||
| * Instructions for chat models; `None` uses the default ones. OCR models take none. | ||
| */ | ||
| system_prompt: string | null, }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
| import type { OcrMode } from "./OcrMode"; | ||
|
|
||
| /** | ||
| * Document reading settings with the instructions "Reset to default" restores. | ||
| */ | ||
| export type OcrSetup = { default_system_prompt: string, mode: OcrMode, | ||
| /** | ||
| * An OCR model, or a chat model that takes images (and PDFs, for PDF pages). | ||
| */ | ||
| model_id: string | null, | ||
| /** | ||
| * Instructions for chat models; `None` uses the default ones. OCR models take none. | ||
| */ | ||
| system_prompt: string | null, }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
| import type { ChunkingSettings } from "./ChunkingSettings"; | ||
| import type { ConnectionResponse } from "./ConnectionResponse"; | ||
| import type { OcrSetup } from "./OcrSetup"; | ||
| import type { RagDefaults } from "./RagDefaults"; | ||
| import type { RerankerSettings } from "./RerankerSettings"; | ||
|
|
||
| /** | ||
| * Everything the knowledge setup page shows. | ||
| */ | ||
| export type RagSetupResponse = { connections: Array<ConnectionResponse>, reranker: RerankerSettings, defaults: RagDefaults, chunking: ChunkingSettings, }; | ||
| export type RagSetupResponse = { connections: Array<ConnectionResponse>, reranker: RerankerSettings, defaults: RagDefaults, chunking: ChunkingSettings, ocr: OcrSetup, }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
| import type { ChunkingSettings } from "./ChunkingSettings"; | ||
| import type { OcrSettings } from "./OcrSettings"; | ||
| import type { RagDefaults } from "./RagDefaults"; | ||
| import type { RerankerSettings } from "./RerankerSettings"; | ||
|
|
||
| /** | ||
| * Saves the installation's knowledge settings; `GET` returns the same objects. | ||
| */ | ||
| export type UpdateRagSetupRequest = { reranker: RerankerSettings, defaults: RagDefaults, chunking?: ChunkingSettings, }; | ||
| export type UpdateRagSetupRequest = { reranker: RerankerSettings, defaults: RagDefaults, chunking?: ChunkingSettings, | ||
| /** | ||
| * `None` keeps the stored document reading settings. | ||
| */ | ||
| ocr?: OcrSettings, }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔍 Scanned PDF upload coverage gap
The new end-to-end test covers a PNG, not a scanned PDF. A mixed PDF fixture would exercise page selection, the child-process cutter, and search citations in one upload.
Was this helpful? React with 👍 or 👎 to provide feedback.