-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(reliability): per-fallback API keys for custom endpoints #2571
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
244e68b
feat(reliability): support per-fallback API keys for custom endpoints
theonlyhennygod 0b06a70
ci: ensure rustfmt and rustdoc in toolchain setup
theonlyhennygod c6911f6
ci: scope rust tooling checks by job type
theonlyhennygod b1622d7
fix(ci): fail hard on required rust components
theonlyhennygod dd0cc10
docs(config): clarify fallback_api_keys contract
theonlyhennygod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| use wiremock::matchers::{header, method, path}; | ||
| use wiremock::{Mock, MockServer, ResponseTemplate}; | ||
| use zeroclaw::config::ReliabilityConfig; | ||
| use zeroclaw::providers::create_resilient_provider; | ||
|
|
||
| #[tokio::test] | ||
| async fn fallback_api_keys_support_multiple_custom_endpoints() { | ||
| let primary_server = MockServer::start().await; | ||
| let fallback_server_one = MockServer::start().await; | ||
| let fallback_server_two = MockServer::start().await; | ||
|
|
||
| Mock::given(method("POST")) | ||
| .and(path("/v1/chat/completions")) | ||
| .respond_with( | ||
| ResponseTemplate::new(500) | ||
| .set_body_json(serde_json::json!({ "error": "primary unavailable" })), | ||
| ) | ||
| .expect(1) | ||
| .mount(&primary_server) | ||
| .await; | ||
|
|
||
| Mock::given(method("POST")) | ||
| .and(path("/v1/chat/completions")) | ||
| .and(header("authorization", "Bearer fallback-key-1")) | ||
| .respond_with( | ||
| ResponseTemplate::new(500) | ||
| .set_body_json(serde_json::json!({ "error": "fallback one unavailable" })), | ||
| ) | ||
| .expect(1) | ||
| .mount(&fallback_server_one) | ||
| .await; | ||
|
|
||
| Mock::given(method("POST")) | ||
| .and(path("/v1/chat/completions")) | ||
| .and(header("authorization", "Bearer fallback-key-2")) | ||
| .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ | ||
| "id": "chatcmpl-1", | ||
| "object": "chat.completion", | ||
| "choices": [ | ||
| { | ||
| "index": 0, | ||
| "message": { | ||
| "role": "assistant", | ||
| "content": "response-from-fallback-two" | ||
| }, | ||
| "finish_reason": "stop" | ||
| } | ||
| ], | ||
| "usage": { | ||
| "prompt_tokens": 1, | ||
| "completion_tokens": 1, | ||
| "total_tokens": 2 | ||
| } | ||
| }))) | ||
| .expect(1) | ||
| .mount(&fallback_server_two) | ||
| .await; | ||
|
|
||
| let primary_provider = format!("custom:{}/v1", primary_server.uri()); | ||
| let fallback_provider_one = format!("custom:{}/v1", fallback_server_one.uri()); | ||
| let fallback_provider_two = format!("custom:{}/v1", fallback_server_two.uri()); | ||
|
|
||
| let mut fallback_api_keys = HashMap::new(); | ||
| fallback_api_keys.insert(fallback_provider_one.clone(), "fallback-key-1".to_string()); | ||
| fallback_api_keys.insert(fallback_provider_two.clone(), "fallback-key-2".to_string()); | ||
|
|
||
| let reliability = ReliabilityConfig { | ||
| provider_retries: 0, | ||
| provider_backoff_ms: 0, | ||
| fallback_providers: vec![fallback_provider_one.clone(), fallback_provider_two.clone()], | ||
| fallback_api_keys, | ||
| api_keys: Vec::new(), | ||
| model_fallbacks: HashMap::new(), | ||
| channel_initial_backoff_secs: 2, | ||
| channel_max_backoff_secs: 60, | ||
| scheduler_poll_secs: 15, | ||
| scheduler_retries: 2, | ||
| }; | ||
|
|
||
| let provider = | ||
| create_resilient_provider(&primary_provider, Some("primary-key"), None, &reliability) | ||
| .expect("resilient provider should initialize"); | ||
|
|
||
| let reply = provider | ||
| .chat_with_system(None, "hello", "gpt-4o-mini", 0.0) | ||
| .await | ||
| .expect("fallback chain should return final response"); | ||
|
|
||
| assert_eq!(reply, "response-from-fallback-two"); | ||
|
|
||
| fallback_server_one.verify().await; | ||
| fallback_server_two.verify().await; | ||
| } | ||
|
theonlyhennygod marked this conversation as resolved.
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.