fix: prevent truncated Ollama answers
This commit is contained in:
@@ -51,6 +51,7 @@ class Settings(BaseSettings):
|
||||
ollama_default_model: str = Field(default="qwen3.5:9b", validation_alias="OLLAMA_DEFAULT_MODEL")
|
||||
ollama_timeout_seconds: int = Field(default=120, ge=5, le=600, validation_alias="OLLAMA_TIMEOUT_SECONDS")
|
||||
ollama_max_output_tokens: int = Field(default=700, ge=100, le=4_000, validation_alias="OLLAMA_MAX_OUTPUT_TOKENS")
|
||||
ollama_context_tokens: int = Field(default=16_384, ge=4_096, le=131_072, validation_alias="OLLAMA_CONTEXT_TOKENS")
|
||||
cors_origins: list[str] | str = Field(
|
||||
default=["http://localhost:5173", "http://127.0.0.1:5173"],
|
||||
validation_alias="CORS_ORIGINS",
|
||||
|
||||
@@ -367,9 +367,23 @@ class GeoAssistantService:
|
||||
"stream": False,
|
||||
"think": False,
|
||||
"keep_alive": "10m",
|
||||
"options": {"temperature": 0.1, "num_predict": self.settings.ollama_max_output_tokens},
|
||||
"options": {
|
||||
"temperature": 0.1,
|
||||
"num_ctx": self.settings.ollama_context_tokens,
|
||||
"num_predict": self.settings.ollama_max_output_tokens,
|
||||
},
|
||||
},
|
||||
)
|
||||
if response.get("done_reason") == "length":
|
||||
raise AppError(
|
||||
code="OLLAMA_RESPONSE_TRUNCATED",
|
||||
message="Ollama kon geen volledig antwoord binnen de ingestelde contextlimiet genereren.",
|
||||
details={
|
||||
"context_tokens": self.settings.ollama_context_tokens,
|
||||
"max_output_tokens": self.settings.ollama_max_output_tokens,
|
||||
},
|
||||
status_code=502,
|
||||
)
|
||||
message = response.get("message") if isinstance(response.get("message"), dict) else {}
|
||||
answer = str(message.get("content") or "").strip()
|
||||
if not answer:
|
||||
|
||||
Reference in New Issue
Block a user