-
Notifications
You must be signed in to change notification settings - Fork 3
feat(provider): add fireworks ai #76
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| use http::{HeaderMap, HeaderValue, header::AUTHORIZATION}; | ||
| use serde::{Deserialize, Serialize}; | ||
| use serde_json::Value; | ||
|
|
||
| use crate::gateway::{ | ||
| error::{GatewayError, Result}, | ||
| provider_instance::ProviderAuth, | ||
| traits::{ChatTransform, EmbedTransform, ProviderCapabilities, ProviderMeta}, | ||
| types::{ | ||
| embed::{EmbedRequestBody, EmbeddingRequest}, | ||
| openai::ChatCompletionRequest, | ||
| }, | ||
| }; | ||
|
|
||
| /// Fireworks AI currently uses its OpenAI-compatible inference API. | ||
| /// Docs: https://docs.fireworks.ai/tools-sdks/openai-compatibility | ||
| pub const IDENTIFIER: &str = "fireworks-ai"; | ||
|
|
||
| #[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)] | ||
| pub struct FireworksAiProviderConfig { | ||
| pub api_key: String, | ||
|
|
||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| pub api_base: Option<String>, | ||
| } | ||
|
|
||
| pub struct FireworksAi; | ||
|
|
||
| impl ProviderMeta for FireworksAi { | ||
| fn name(&self) -> &'static str { | ||
| IDENTIFIER | ||
| } | ||
|
|
||
| fn default_base_url(&self) -> &'static str { | ||
| "https://api.fireworks.ai/inference/v1" | ||
| } | ||
|
|
||
| fn build_auth_headers(&self, auth: &ProviderAuth) -> Result<HeaderMap> { | ||
| let mut headers = HeaderMap::new(); | ||
| let value = HeaderValue::from_str(&format!("Bearer {}", auth.api_key_for(self.name())?)) | ||
| .map_err(|error| GatewayError::Validation(error.to_string()))?; | ||
| headers.insert(AUTHORIZATION, value); | ||
| Ok(headers) | ||
| } | ||
| } | ||
|
|
||
| impl ChatTransform for FireworksAi { | ||
| fn transform_request(&self, request: &ChatCompletionRequest) -> Result<Value> { | ||
| let mut body = serde_json::to_value(request) | ||
| .map_err(|error| GatewayError::Transform(error.to_string()))?; | ||
|
|
||
| if let Value::Object(map) = &mut body { | ||
| // Fireworks defaults to truncating max_tokens on context overflow. | ||
| // Set the documented override so requests keep OpenAI-style error semantics. | ||
| map.entry("context_length_exceeded_behavior") | ||
| .or_insert_with(|| Value::String("error".into())); | ||
| } | ||
|
|
||
| Ok(body) | ||
| } | ||
| } | ||
|
|
||
| impl EmbedTransform for FireworksAi { | ||
| fn transform_embeddings_request(&self, request: &EmbeddingRequest) -> Result<EmbedRequestBody> { | ||
| let mut body = serde_json::to_value(request) | ||
| .map_err(|error| GatewayError::Transform(error.to_string()))?; | ||
|
|
||
| if let Value::Object(map) = &mut body { | ||
| // Fireworks documents dimensions, return_logits, normalize, and prompt_template, | ||
| // but not OpenAI's encoding_format or user fields on /embeddings. | ||
| map.remove("encoding_format"); | ||
| map.remove("user"); | ||
| } | ||
|
|
||
| Ok(EmbedRequestBody::Json(body)) | ||
| } | ||
| } | ||
|
|
||
| impl ProviderCapabilities for FireworksAi { | ||
| fn as_embed_transform(&self) -> Option<&dyn EmbedTransform> { | ||
| Some(self) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use pretty_assertions::assert_eq; | ||
| use serde_json::json; | ||
|
|
||
| use super::FireworksAi; | ||
| use crate::gateway::{ | ||
| provider_instance::ProviderAuth, | ||
| traits::{ChatTransform, EmbedTransform, ProviderCapabilities, ProviderMeta}, | ||
| types::{ | ||
| embed::{EmbedRequestBody, EmbeddingRequest}, | ||
| openai::ChatCompletionRequest, | ||
| }, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn provider_metadata_and_urls_are_correct() { | ||
| let provider = FireworksAi; | ||
| let headers = provider | ||
| .build_auth_headers(&ProviderAuth::ApiKey("fw-key".into())) | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(provider.name(), "fireworks-ai"); | ||
| assert_eq!( | ||
| provider.default_base_url(), | ||
| "https://api.fireworks.ai/inference/v1" | ||
| ); | ||
| assert_eq!(headers["authorization"], "Bearer fw-key"); | ||
| assert_eq!( | ||
| provider.build_url(provider.default_base_url(), "ignored"), | ||
| "https://api.fireworks.ai/inference/v1/chat/completions" | ||
| ); | ||
| assert!(provider.as_embed_transform().is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn transform_request_defaults_to_openai_context_length_behavior() { | ||
| let provider = FireworksAi; | ||
| let request: ChatCompletionRequest = serde_json::from_value(json!({ | ||
| "model": "accounts/fireworks/models/kimi-k2-instruct-0905", | ||
| "messages": [{"role": "user", "content": "hello"}] | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| let transformed = provider.transform_request(&request).unwrap(); | ||
|
|
||
| assert_eq!(transformed["context_length_exceeded_behavior"], "error"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn transform_request_preserves_explicit_context_length_behavior() { | ||
| let provider = FireworksAi; | ||
| let request: ChatCompletionRequest = serde_json::from_value(json!({ | ||
| "model": "accounts/fireworks/models/kimi-k2-instruct-0905", | ||
| "messages": [{"role": "user", "content": "hello"}], | ||
| "context_length_exceeded_behavior": "truncate" | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| let transformed = provider.transform_request(&request).unwrap(); | ||
|
|
||
| assert_eq!(transformed["context_length_exceeded_behavior"], "truncate"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn transform_embeddings_request_strips_unsupported_fields() { | ||
| let provider = FireworksAi; | ||
| let request: EmbeddingRequest = serde_json::from_value(json!({ | ||
| "model": "fireworks/qwen3-embedding-8b", | ||
| "input": ["hello"], | ||
| "dimensions": 128, | ||
| "encoding_format": "float", | ||
| "user": "user-123" | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| let body = provider.transform_embeddings_request(&request).unwrap(); | ||
|
|
||
| match body { | ||
| EmbedRequestBody::Json(value) => { | ||
| assert_eq!(value["dimensions"], 128); | ||
| assert_eq!(value.get("encoding_format"), None); | ||
| assert_eq!(value.get("user"), None); | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
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.