Skip to content

feat(dao): add shared ResolveModelContentLength helper - #18106

Merged
yuzhichang merged 3 commits into
infiniflow:mainfrom
jay77721:pr/5-model-context-length
Aug 12, 2026
Merged

yuzhichang merged 3 commits into
infiniflow:mainfrom
jay77721:pr/5-model-context-length

Conversation

@jay77721

@jay77721 jay77721 commented Aug 11, 2026 •

Copy link
Copy Markdown
Member

Summary

Add a shared dao.ResolveModelContentLength that resolves a chat model's context window (content_length) from a tenant_model UUID or a composite model@provider reference, with an optional driver + modelName catalog fallback for the no-database path.

Why

The agent LLM component (PR #18093) and the ingestion Extractor (PR #18095) both need this lookup, but they cannot import each other or internal/service (import cycles). Each previously carried an identical private copy; both callers now use this shared helper instead.

Changes

  • internal/dao/model_context.go: ResolveModelContentLength(ctx, db, modelRef, driver, modelName) int
    • tenant_model UUID → provider catalog
    • composite model@provider / model@instance@provider → provider catalog
    • driver + modelName fallback (no database needed)
    • returns 0 when unknown
  • internal/dao/model_context_test.go: covers composite, driver+modelName fallback, tenant UUID, inactive row, unknown and empty references.

Testing

  • bash build.sh --test ./internal/dao/... passes.
  • Rebased onto latest main (a0e091e75).

Known limitations (follow-up)

The resolver reads content_length from the provider catalog only. Python resolves a tenant model's context as model_extra.get("max_tokens") or 8192 (and the composite path honors the per-model max_tokens override first), so a per-model max_tokens override in tenant_model.extra — common for custom/local models — is currently ignored: such models resolve to 0 and callers fall back to the 8192 budget. A follow-up PR will honor TenantModel.Extra["max_tokens"] (mirroring tenantModelExtra in internal/ingestion/component/dispatch_model.go).

@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Aug 11, 2026
@coderabbitai

coderabbitai Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: af886fb1-69eb-44c0-a52f-6c3f44307b77

📥 Commits

Reviewing files that changed from the base of the PR and between 824dad7 and 547cde3.

📒 Files selected for processing (2)
  • internal/dao/model_context.go
  • internal/dao/model_context_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/dao/model_context_test.go

📝 Walkthrough

Walkthrough

Added ResolveModelContentLength to resolve model context length from tenant-model records, provider catalogs, and driver/model fallback values. Added parsing for composite references and DAO tests for resolved and unresolved cases.

Changes

Model context resolution

Layer / File(s) Summary
Resolver and reference parsing
internal/dao/model_context.go
The resolver parses model@provider and model@instance@provider references. It checks active tenant-model and provider records, applies driver/model fallback values, and returns zero when unresolved.
Resolver validation coverage
internal/dao/model_context_test.go
Tests cover composite references, excessive separators, fallback values, unknown and empty references, active tenant-model UUIDs, and inactive tenant-model behavior. The tests use an in-memory SQLite/GORM database.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested labels: 🧪 test

Poem

A rabbit checks each model name,
Then follows records through the chain.
Catalog values guide the way,
Unknown lengths return zero today.
Context limits now resolve.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the shared DAO helper added by this pull request.
Description check ✅ Passed The description includes the required summary and provides clear purpose, changes, testing, and known limitations.
Docstring Coverage ✅ Passed Docstring coverage is 90.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@jay77721 jay77721 added the ci Continue Integration label Aug 11, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
internal/dao/model_context.go (1)

40-55: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Parse composite references before the tenant-model query.

A composite reference contains @. It cannot be a tenant-model UUID under this API contract. The current order performs GetByID before the catalog lookup.

Resolve the composite form first. This removes one database query from each composite-reference resolution.

Proposed change
-	// 1. Tenant model UUID: read content_length from its provider catalog row.
-	if db != nil && modelRef != "" {
+	// 1. Composite reference: look up the provider catalog.
+	if pureName, _, providerName, ok := splitCompositeModelRef(modelRef); ok {
+		if mdl, err := GetModelProviderManager().GetModelByName(providerName, pureName); err == nil && mdl.ContentLength != nil {
+			return *mdl.ContentLength
+		}
+	}
+	// 2. Tenant model UUID: read content_length from its provider catalog row.
+	if db != nil && modelRef != "" {
 		if obj, err := NewTenantModelDAO().GetByID(ctx, db, modelRef); err == nil && obj != nil && obj.Status == "active" {
 			// ...
 		}
 	}
-	// 2. Composite "model@provider" reference: look up the provider catalog.
-	if pureName, _, providerName, ok := splitCompositeModelRef(modelRef); ok {
-		if mdl, err := GetModelProviderManager().GetModelByName(providerName, pureName); err == nil && mdl.ContentLength != nil {
-			return *mdl.ContentLength
-		}
-	}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/dao/model_context.go` around lines 40 - 55, Update the model
content-length resolution flow to call splitCompositeModelRef before the
tenant-model lookup. For valid composite references, resolve via
GetModelProviderManager().GetModelByName and return its ContentLength without
invoking NewTenantModelDAO().GetByID; only perform the tenant-model query for
non-composite references.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@internal/dao/model_context.go`:
- Around line 40-55: Update the model content-length resolution flow to call
splitCompositeModelRef before the tenant-model lookup. For valid composite
references, resolve via GetModelProviderManager().GetModelByName and return its
ContentLength without invoking NewTenantModelDAO().GetByID; only perform the
tenant-model query for non-composite references.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4dd9f6ab-7054-4164-a372-958f293024f4

📥 Commits

Reviewing files that changed from the base of the PR and between d7661b6 and 063d143.

📒 Files selected for processing (2)
  • internal/dao/model_context.go
  • internal/dao/model_context_test.go

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/dao/model_context_test.go`:
- Around line 46-57: Strengthen TestResolveModelContentLength_TooManyParts so it
directly verifies excessive-separator rejection instead of relying on the
unknown catalog input. Use a known catalog model prefix with an extra separator
in the no-fallback assertion, or add an explicit assertion that
splitCompositeModelRef rejects "a@b@c@d", while preserving the existing fallback
expectations.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f6abb33c-611f-40f9-80f4-2bde3972e745

📥 Commits

Reviewing files that changed from the base of the PR and between 063d143 and add6eee.

📒 Files selected for processing (1)
  • internal/dao/model_context_test.go

Comment thread internal/dao/model_context_test.go Resolved
Resolve a chat model's context window (content_length) from either a
tenant_model UUID or a composite model@provider reference, with an
optional driver+modelName catalog fallback for the no-database path.

The agent LLM component and the ingestion Extractor both need this
lookup but cannot import each other or internal/service (import
cycles), so each currently carries its own copy. This is the shared
implementation; follow-up PRs switch those callers onto it.

Adds unit tests for the composite, driver+modelName fallback, tenant
UUID, inactive-row, and unknown-reference paths.
Add coverage for the model@instance@provider reference form and document
that references with more than two "@" separators fall through to the
driver+modelName fallback (or 0), matching splitCompositeModelRef.
@jay77721
jay77721 force-pushed the pr/5-model-context-length branch from add6eee to 824dad7 Compare August 12, 2026 03:23
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/dao/model_context.go`:
- Around line 41-48: Update the model resolution flow around the tenant-model
lookup to parse valid composite model references such as “model@provider” before
querying the database. Resolve catalog-backed composite references directly, and
only execute NewTenantModelDAO().GetByID for non-composite references while
preserving the existing tenant-model status and provider checks.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: db75aa8a-92f0-4fbb-95bd-cccaa5f7f799

📥 Commits

Reviewing files that changed from the base of the PR and between b73aac3 and 824dad7.

📒 Files selected for processing (2)
  • internal/dao/model_context.go
  • internal/dao/model_context_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/dao/model_context_test.go

Comment thread internal/dao/model_context.go Resolved
Resolve 'model@provider' / 'model@instance@provider' references directly
from the provider catalog before querying the database — a composite ref
cannot be a tenant-model UUID, so the catalog-only path needs no DB.

Harden TestResolveModelContentLength_TooManyParts to use a known catalog
model with an extra separator and assert splitCompositeModelRef rejects
excessive separators, so a parser regression fails loudly.
jay77721 added a commit to jay77721/ragflow that referenced this pull request Aug 12, 2026
 review fixes

Resolve composite refs before the tenant-model lookup and harden the
too-many-parts test, matching the updated infiniflow#18106 helper so the stack stays
consistent until infiniflow#18106 merges.
jay77721 added a commit to jay77721/ragflow that referenced this pull request Aug 12, 2026
 review fixes

Resolve composite refs before the tenant-model lookup and harden the
too-many-parts test, matching the updated infiniflow#18106 helper so the stack stays
consistent until infiniflow#18106 merges.
@jay77721
jay77721 requested a review from xugangqiang August 12, 2026 04:01
@xugangqiang
xugangqiang requested a review from yuzhichang August 12, 2026 04:32
@yuzhichang
yuzhichang merged commit a912336 into infiniflow:main Aug 12, 2026
3 checks passed
@jay77721
jay77721 deleted the pr/5-model-context-length branch August 13, 2026 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants