mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
fix(copilot): use model policy for default reasoning
Use llm_adapter 0.4.2 to set low effort for built-in edit actions. Use minimal effort for BYOK probes on models that think by default.
This commit is contained in:
1 parent
af9e4b5b52
commit
123adb96eb
6 files changed
+95
-137
No files matched your search
Generated
+2
-2
@@ -4962,9 +4962,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "llm_adapter"
|
||||
version = "0.4.1"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "354989556fc4652502ad5b57507ebf7f2f761b2dfddb46573efa9598f8b651bd"
|
||||
checksum = "036650b1c0e4ee1d91863185417fdefaca1184ae08f167b398725c10cf1ba2e2"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"jsonschema",
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ resolver = "3"
|
||||
infer = { version = "0.19.0" }
|
||||
libc = "0.2"
|
||||
little_exif = "0.6.23"
|
||||
llm_adapter = { version = "0.4", default-features = false }
|
||||
llm_adapter = { version = "0.4.2", default-features = false }
|
||||
llm_runtime = { version = "0.4", default-features = false }
|
||||
lru = "0.16"
|
||||
matroska = "0.30"
|
||||
|
||||
@@ -112,7 +112,7 @@ mod tests {
|
||||
fn should_reject_ambiguous_alias_without_backend() {
|
||||
let error = llm_resolve_model_registry_variant(ModelRegistryResolveRequest {
|
||||
backend_kind: None,
|
||||
model_id: "claude-sonnet-4.5".to_string(),
|
||||
model_id: "claude-sonnet-4.6".to_string(),
|
||||
})
|
||||
.unwrap_err();
|
||||
|
||||
@@ -199,14 +199,14 @@ mod tests {
|
||||
fn should_resolve_opencode_go_prefixed_alias() {
|
||||
let response = llm_resolve_model_registry_variant(ModelRegistryResolveRequest {
|
||||
backend_kind: Some("opencode_go".to_string()),
|
||||
model_id: "opencode-go/kimi-k2.7-code".to_string(),
|
||||
model_id: "opencode-go/kimi-k3.1-pro".to_string(),
|
||||
})
|
||||
.unwrap();
|
||||
let variant = response.variant.unwrap();
|
||||
|
||||
assert_eq!(response.matched_by.as_deref(), Some("alias"));
|
||||
assert_eq!(variant.backend_kind, "opencode_go");
|
||||
assert_eq!(variant.raw_model_id, "kimi-k2.7-code");
|
||||
assert_eq!(variant.raw_model_id, "kimi-k3.1-pro");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -244,17 +244,17 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_keep_legacy_model_raw_id_across_backend_variants() {
|
||||
fn should_keep_gemini_model_raw_id_across_backend_variants() {
|
||||
let api_variant = llm_resolve_model_registry_variant(ModelRegistryResolveRequest {
|
||||
backend_kind: Some("gemini_api".to_string()),
|
||||
model_id: "gemini-2.5-flash".to_string(),
|
||||
model_id: "gemini-4-hydrogen".to_string(),
|
||||
})
|
||||
.unwrap()
|
||||
.variant
|
||||
.unwrap();
|
||||
let vertex_variant = llm_resolve_model_registry_variant(ModelRegistryResolveRequest {
|
||||
backend_kind: Some("gemini_vertex".to_string()),
|
||||
model_id: "gemini-2.5-flash".to_string(),
|
||||
model_id: "gemini-4-hydrogen".to_string(),
|
||||
})
|
||||
.unwrap()
|
||||
.variant
|
||||
|
||||
@@ -7,7 +7,8 @@ use llm_adapter::{
|
||||
},
|
||||
core::{
|
||||
CoreContent, CoreMessage, CoreRequest, CoreRole, CoreToolChoice, CoreToolDefinition, EmbeddingRequest,
|
||||
ImageOptions, ImageProviderOptions, ImageRequest, RerankCandidate, RerankRequest, StructuredRequest,
|
||||
ImageOptions, ImageProviderOptions, ImageRequest, ReasoningConfig, RerankCandidate, RerankRequest,
|
||||
StructuredRequest,
|
||||
},
|
||||
router::{ExecutableRequest, ExecutableResponse, dispatch_prepared_route},
|
||||
target::{BackendCredential, BackendOperation, EgressPolicy, prepare_backend_target},
|
||||
@@ -240,7 +241,7 @@ fn probe_request_for_operation(operation: &str) -> ExecutableRequest {
|
||||
name: "byok_probe".to_string(),
|
||||
}),
|
||||
include: None,
|
||||
reasoning: None,
|
||||
reasoning: Some(ReasoningConfig::default_only_effort("minimal").to_value()),
|
||||
response_schema: None,
|
||||
}),
|
||||
"structured" => ExecutableRequest::Structured(StructuredRequest {
|
||||
@@ -254,7 +255,7 @@ fn probe_request_for_operation(operation: &str) -> ExecutableRequest {
|
||||
}),
|
||||
max_tokens: Some(128),
|
||||
temperature: None,
|
||||
reasoning: None,
|
||||
reasoning: Some(ReasoningConfig::default_only_effort("minimal").to_value()),
|
||||
strict: Some(true),
|
||||
response_mime_type: Some("application/json".to_string()),
|
||||
}),
|
||||
@@ -597,6 +598,31 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chat_and_structured_probes_encode_minimal_effort_only_for_always_on_models() {
|
||||
use llm_adapter::protocol::{
|
||||
anthropic::request::encode_thinking, gemini::request::encode_thinking_config, openai::encode_chat_reasoning,
|
||||
};
|
||||
|
||||
for operation in ["chat", "tool_calling", "structured"] {
|
||||
let request = probe_request_for_operation(operation);
|
||||
let reasoning = match &request {
|
||||
ExecutableRequest::Chat(request) => request.reasoning.as_ref(),
|
||||
ExecutableRequest::Structured(request) => request.reasoning.as_ref(),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
assert_eq!(
|
||||
encode_thinking_config("gemini-4-hydrogen", reasoning, false),
|
||||
Some(json!({"thinkingLevel": "low"}))
|
||||
);
|
||||
assert!(encode_thinking("claude-sonnet-4-6", reasoning).is_none());
|
||||
assert_eq!(reasoning, Some(&json!({"effort": "minimal", "policy": "default_only"})));
|
||||
for model in ["gpt-4o-mini", "custom-chat-model"] {
|
||||
assert!(encode_chat_reasoning(model, reasoning).is_none(), "{model}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn openai_compatible_probe_smoke_uses_the_selected_dialect() {
|
||||
let operations = ["chat", "structured", "tool_calling", "embedding", "rerank", "image"];
|
||||
|
||||
@@ -5,9 +5,9 @@ use std::{
|
||||
|
||||
use gcp_auth::{CustomServiceAccount, TokenProvider};
|
||||
use llm_adapter::{
|
||||
backend::{BackendError, BackendProvider, BackendRequestLayer, CredentialPlan, DefaultHttpClient},
|
||||
backend::{BackendError, CredentialPlan, DefaultHttpClient},
|
||||
capability::{AttachmentKind, AttachmentSource},
|
||||
core::{CoreContent, ImageInput, ImageRequest},
|
||||
core::{CoreContent, ImageInput, ImageRequest, ReasoningConfig},
|
||||
router::{ExecutableRequest, ExecutableResponse},
|
||||
target::{BackendCredential, prepare_backend_target},
|
||||
};
|
||||
@@ -98,36 +98,19 @@ pub(super) fn request_and_slot(
|
||||
))
|
||||
}
|
||||
|
||||
pub(super) fn apply_prompt_reasoning_default(
|
||||
request: &mut ExecutableRequest,
|
||||
targets: &AuthorizedRouteTargets,
|
||||
prompt_name: Option<&str>,
|
||||
) {
|
||||
let AuthorizedRouteTargets::Managed(managed) = targets else {
|
||||
return;
|
||||
};
|
||||
if managed.target.wire_model() != "gemini-4-hydrogen"
|
||||
|| !matches!(
|
||||
(managed.provider, managed.target.request_layer()),
|
||||
(BackendProvider::Gemini, BackendRequestLayer::GeminiApi)
|
||||
| (BackendProvider::GeminiVertex, BackendRequestLayer::GeminiVertex)
|
||||
)
|
||||
{
|
||||
return;
|
||||
}
|
||||
pub(super) fn apply_prompt_reasoning_default(request: &mut ExecutableRequest, prompt_name: Option<&str>) {
|
||||
let reasoning = match request {
|
||||
ExecutableRequest::Chat(request) => &mut request.reasoning,
|
||||
ExecutableRequest::Structured(request) => &mut request.reasoning,
|
||||
_ => return,
|
||||
};
|
||||
if reasoning.is_none()
|
||||
&& prompt_name
|
||||
&& let Some(effort) = prompt_name
|
||||
.and_then(built_in_prompt_spec)
|
||||
.and_then(|prompt| prompt.config.as_ref())
|
||||
.and_then(|config| config["reasoningEffort"].as_str())
|
||||
== Some("low")
|
||||
{
|
||||
*reasoning = Some(serde_json::json!({ "effort": "low" }));
|
||||
*reasoning = Some(ReasoningConfig::default_only_effort(effort).to_value());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -461,9 +444,9 @@ mod tests {
|
||||
use crate::llm::route::slot;
|
||||
|
||||
#[test]
|
||||
fn prompt_reasoning_defaults_use_only_resolved_managed_gemini_and_preserve_explicit_effort() {
|
||||
fn prompt_reasoning_defaults_follow_model_policy_and_preserve_explicit_effort() {
|
||||
use llm_adapter::{
|
||||
backend::{HttpRequest, HttpResponse},
|
||||
backend::{BackendRequestLayer, HttpRequest, HttpResponse},
|
||||
router::dispatch_prepared_route,
|
||||
target::{BackendOperation, EgressPolicy},
|
||||
};
|
||||
@@ -487,84 +470,40 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
use BackendRequestLayer::{Anthropic, ChatCompletions, GeminiApi, GeminiVertex};
|
||||
|
||||
let gemini = "gemini-4-hydrogen";
|
||||
let edit = "Translate to";
|
||||
let cases = [
|
||||
(Some(BackendRequestLayer::GeminiApi), gemini, edit, None, Some("low")),
|
||||
(Some(BackendRequestLayer::GeminiVertex), gemini, edit, None, Some("low")),
|
||||
(
|
||||
Some(BackendRequestLayer::GeminiApi),
|
||||
gemini,
|
||||
edit,
|
||||
Some("medium"),
|
||||
Some("medium"),
|
||||
),
|
||||
(
|
||||
Some(BackendRequestLayer::GeminiApi),
|
||||
gemini,
|
||||
"Continue writing",
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
Some(BackendRequestLayer::GeminiApi),
|
||||
gemini,
|
||||
"Chat With AFFiNE AI",
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
Some(BackendRequestLayer::GeminiApi),
|
||||
"gemini-2.0-flash",
|
||||
edit,
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(
|
||||
Some(BackendRequestLayer::Anthropic),
|
||||
"claude-sonnet-5-5",
|
||||
edit,
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(Some(BackendRequestLayer::ChatCompletions), gemini, edit, None, None),
|
||||
(
|
||||
Some(BackendRequestLayer::ChatCompletions),
|
||||
"gpt-4o-mini",
|
||||
edit,
|
||||
None,
|
||||
None,
|
||||
),
|
||||
(None, gemini, edit, None, None),
|
||||
(None, gemini, edit, Some("medium"), Some("medium")),
|
||||
let actions = [
|
||||
("Improve writing for it", Some("low")),
|
||||
("Fix spelling for it", Some("low")),
|
||||
(edit, Some("low")),
|
||||
("Change tone to", Some("low")),
|
||||
("Make it shorter", Some("low")),
|
||||
("Create headings", Some("low")),
|
||||
("Generate a caption", Some("low")),
|
||||
("Write a twitter about this", Some("low")),
|
||||
("Explain this image", None),
|
||||
("Explain this code", None),
|
||||
("Write a poem about this", None),
|
||||
("Write outline", None),
|
||||
("Brainstorm ideas about this", None),
|
||||
("Make it longer", None),
|
||||
("Continue writing", None),
|
||||
];
|
||||
let cases = actions
|
||||
.into_iter()
|
||||
.map(|(prompt, expected)| (GeminiApi, gemini, prompt, None, expected))
|
||||
.chain([
|
||||
(GeminiVertex, gemini, edit, None, Some("low")),
|
||||
(GeminiApi, gemini, edit, Some("medium"), Some("medium")),
|
||||
(GeminiApi, gemini, "Chat With AFFiNE AI", None, None),
|
||||
(Anthropic, "claude-sonnet-4-6", edit, None, None),
|
||||
(ChatCompletions, "gpt-4o-mini", edit, None, None),
|
||||
(ChatCompletions, "custom-chat-model", edit, None, None),
|
||||
]);
|
||||
|
||||
for (layer, model, prompt, explicit, expected) in cases {
|
||||
let targets = if let Some(layer) = layer {
|
||||
AuthorizedRouteTargets::Managed(crate::llm::route::ResolvedManagedTarget {
|
||||
profile_id: "fixture".into(),
|
||||
provider: match layer {
|
||||
BackendRequestLayer::GeminiApi => BackendProvider::Gemini,
|
||||
BackendRequestLayer::GeminiVertex => BackendProvider::GeminiVertex,
|
||||
BackendRequestLayer::Anthropic => BackendProvider::Anthropic,
|
||||
_ => BackendProvider::OpenAi,
|
||||
},
|
||||
target: prepare_backend_target(
|
||||
layer,
|
||||
BackendOperation::Chat,
|
||||
model.into(),
|
||||
"https://provider.example.com",
|
||||
None,
|
||||
EgressPolicy::PublicOnly,
|
||||
)
|
||||
.unwrap(),
|
||||
capabilities: Vec::new(),
|
||||
credential_plan: Arc::new(CredentialPlan::Field { field: "apiKey".into() }),
|
||||
credential_config: Arc::new(json!({})),
|
||||
})
|
||||
} else {
|
||||
AuthorizedRouteTargets::Byok(Vec::new())
|
||||
};
|
||||
let (_, mut request) = request_and_slot(
|
||||
slot("chat.default").unwrap(),
|
||||
json!({
|
||||
@@ -574,35 +513,28 @@ mod tests {
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
apply_prompt_reasoning_default(&mut request, &targets, Some(prompt));
|
||||
let ExecutableRequest::Chat(chat) = &request else {
|
||||
panic!("expected chat request");
|
||||
};
|
||||
apply_prompt_reasoning_default(&mut request, Some(prompt));
|
||||
let client = CaptureBody(Default::default());
|
||||
let route = prepare_backend_target(
|
||||
layer,
|
||||
BackendOperation::Chat,
|
||||
model.into(),
|
||||
"https://provider.example.com",
|
||||
None,
|
||||
EgressPolicy::PublicOnly,
|
||||
)
|
||||
.unwrap()
|
||||
.bind(BackendCredential::new("fixture".into()), request)
|
||||
.unwrap();
|
||||
assert!(dispatch_prepared_route(&client, &route).is_err());
|
||||
let body = client.0.into_inner().expect("upstream request body");
|
||||
assert_eq!(
|
||||
chat
|
||||
.reasoning
|
||||
.as_ref()
|
||||
.and_then(|reasoning| reasoning["effort"].as_str()),
|
||||
expected,
|
||||
body.pointer("/generationConfig/thinkingConfig"),
|
||||
expected.map(|level| json!({"thinkingLevel": level})).as_ref(),
|
||||
"{layer:?} {model} {prompt}"
|
||||
);
|
||||
|
||||
if let AuthorizedRouteTargets::Managed(managed) = targets {
|
||||
let client = CaptureBody(Default::default());
|
||||
let route = managed
|
||||
.target
|
||||
.bind(BackendCredential::new("fixture".into()), request)
|
||||
.unwrap();
|
||||
assert!(dispatch_prepared_route(&client, &route).is_err());
|
||||
let body = client.0.into_inner().expect("upstream request body");
|
||||
assert_eq!(
|
||||
body
|
||||
.pointer("/generationConfig/thinkingConfig/thinkingLevel")
|
||||
.and_then(serde_json::Value::as_str),
|
||||
expected
|
||||
);
|
||||
assert!(body.get("reasoning_effort").is_none());
|
||||
assert!(body.get("thinking").is_none());
|
||||
for field in ["reasoning", "reasoning_effort", "thinking", "output_config", "policy"] {
|
||||
assert!(body.get(field).is_none(), "{layer:?} {model} {prompt}: {body}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -612,7 +544,7 @@ mod tests {
|
||||
let (_, request) = request_and_slot(
|
||||
slot("action.image.filter.pixel").unwrap(),
|
||||
json!({
|
||||
"model": "gpt-image-1",
|
||||
"model": "gpt-image-2",
|
||||
"prompt": "apply pixel filter",
|
||||
"operation": "edit",
|
||||
"images": [{
|
||||
|
||||
@@ -283,7 +283,7 @@ impl BackendRuntime {
|
||||
slot,
|
||||
)
|
||||
.await?;
|
||||
dispatch::apply_prompt_reasoning_default(&mut request, &authorized.targets, built_in_route_id.as_deref());
|
||||
dispatch::apply_prompt_reasoning_default(&mut request, built_in_route_id.as_deref());
|
||||
let managed_credential = resolve_managed_credential(&authorized.targets, &self.managed_token_providers).await?;
|
||||
Ok((
|
||||
authorized.config,
|
||||
|
||||
Reference in new issue
Block a user