Repository navigation
VisionLanguageModel: ChatOptions, incomplete-reply check, reply decoding and page rendering - #102
Open
luisquintanilla wants to merge 2 commits into
Open
luisquintanilla wants to merge 2 commits into
luisquintanilla wants to merge 2 commits into
Conversation
…ecoding and page rendering Adds options to VisionLanguageModelDocumentExtractionClientOptions so document OCR models (OCRia-1, olmOCR, Granite Docling) work through any IChatClient: - Prompt = "" sends only the document; ChatOptions are copied per request. - ThrowOnIncompleteReply (default true): a non-Stop finish reason throws InvalidDataException; structured mode always throws. - ReplyDecoding: hand free-form replies to an app-supplied DocumentExtractionClient (for example a Markdown or HTML parser). - PageRendering + RenderedPage: send caller-rendered page images, one request per page, with the renderer's pages as source provenance. Refs #101 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Refs #101 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements #101. It adds four options to
VisionLanguageModelDocumentExtractionClientOptions, so document OCR models work through anyIChatClientwith no new dependencies. Examples are LlamaIndex OCRia-1, olmOCR and Granite Docling, run behind Ollama, llama.cpp, an OpenAI-compatible server or Azure OpenAI.The client stays sealed and keeps its existing types internal. The only new public types are two options groups and
RenderedPage. CTK ships no renderer and no parser; the app supplies both.API
Usage with an OCR model:
Behavior
Prompt = ""TextContentwas sent too.ChatOptionsExtractorName, when set, replacesModelId. Structured mode replacesResponseFormat.ThrowOnIncompleteReplyFinishReasonother thanStop(for exampleLengthorContentFilter) throwsInvalidDataException, naming the page and, forLength,MaxOutputTokens. Structured mode always throws. A null finish reason and an empty reply that finished withStopare accepted; see the deviations below.ReplyDecodingMediaType. Sections and elements the decoder returns without sources are attributed to the reply, andRawRepresentationis theChatResponse. The decoder is borrowed. Combining it withUseStructuredOutputthrowsArgumentExceptionat construction.PageRenderingMediaTypesare rendered. Each page is one request, in order, and sections reference the renderer'sPage(real page number and pixel size). The mergedRawRepresentationisList<ChatResponse>. Other media types are sent unchanged, and rendered images must beimage/*.Breaking changes (package is experimental,
CTDE0001)LengthorContentFilternow throws by default. SetThrowOnIncompleteReply = falseto restore the old behavior.Stop([DocumentExtraction] Vision language model: document models as settings, plus reply-decoding and page-rendering middleware #101 Q4).Prompt = ""sends no text part.ModelIdyou set inChatOptionsis now kept whenExtractorNameis null. Before this change there was noChatOptionsoption to set.Deviations from the #101 text, found while implementing
RenderedPage.Page, including in structured mode, where page indexes the model reports aren't used for provenance.RawRepresentationisList<ChatResponse>rather than a list of per-page documents. It matches the single-call case and keeps finish reasons and usage reachable.RenderedPageis a sealed class, not a record, so its constructor can validate both arguments.Out of scope, as a follow-up: #101 Q5, requiring
cellsin structured tables. The structured e2e run below shows why it matters.Validation
Unit tests:
VisionLanguageModel.UnitTests189/189 pass: 19 new tests, plus 2 updated for the intendedPrompt = ""and export-list changes. They cover:ChatOptionsper request;Stop/Length/ContentFilter/null, opt-out);DocumentExtraction.slnfbuilds with no warnings.End to end: I packed this branch as
1.0.0-preview.local1and ran it from a file-based app. The renderer is the README sample verbatim; the decoder is a community-style Markdig client. Inputs are a ParseBench page (financial_tables_infosys6…page1.pdf) and a 7-page scanned PDF.stop, 1 page at 1650×1275 px, 3 tables (4×4, 3×4, 6×12), 11 sstop; 7 sections each referencing its own page; 15 headings, 6 tables; 50 sMaxOutputTokens = 200InvalidDataException: '…page1.pdf' page 1: the model stopped with finish reason 'length', so the reply is incomplete. Increase ChatOptions.MaxOutputTokens.stop, 3 tables all returned as literal tables with nocells(Q5)llamaindex/ocria-1(OllamaSharp)Prompt = "",Temperature = 0, rendering + Markdown decodingstop, 2,525 tokens, correct page provenance. OCRia-1 replies with HTML<table>s inside a code fence, so a Markdown decoder keeps them as text. An HTML decoder (ReplyDecoding.MediaType = "text/html") is the right choice for this model, which is why the parser stays pluggable.qwen2.5vl:3b(OllamaSharp)MaxOutputTokens = 4096, renderinglengthnow throws, where before truncated text was returned silently.Docs
FinishReason.Lengthgate and lists the new options.