-
Notifications
You must be signed in to change notification settings - Fork 1
feat(provider): add openai chat format for new provider #18
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
+239
−0
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pub mod openai; | ||
|
|
||
| pub use openai::OpenAIChatFormat; |
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,233 @@ | ||
| use crate::gateway::{ | ||
| error::{GatewayError, Result}, | ||
| traits::{ChatFormat, NativeHandler, ProviderCapabilities}, | ||
| types::{ | ||
| common::BridgeContext, | ||
| openai::{ChatCompletionChunk, ChatCompletionRequest, ChatCompletionResponse}, | ||
| }, | ||
| }; | ||
|
|
||
| pub struct OpenAIChatFormat; | ||
|
|
||
| impl ChatFormat for OpenAIChatFormat { | ||
| type Request = ChatCompletionRequest; | ||
| type Response = ChatCompletionResponse; | ||
| type StreamChunk = ChatCompletionChunk; | ||
| type BridgeState = (); | ||
| type NativeStreamState = (); | ||
|
|
||
| fn name() -> &'static str { | ||
| "openai_chat" | ||
| } | ||
|
|
||
| fn is_stream(req: &Self::Request) -> bool { | ||
| req.stream.unwrap_or(false) | ||
| } | ||
|
|
||
| fn extract_model(req: &Self::Request) -> &str { | ||
| &req.model | ||
| } | ||
|
|
||
| fn to_hub(req: &Self::Request) -> Result<(ChatCompletionRequest, BridgeContext)> { | ||
| Ok((req.clone(), BridgeContext::default())) | ||
| } | ||
|
|
||
| fn from_hub(resp: &Self::Response, _ctx: &BridgeContext) -> Result<Self::Response> { | ||
| Ok(resp.clone()) | ||
| } | ||
|
|
||
| fn from_hub_stream( | ||
| chunk: &ChatCompletionChunk, | ||
| _state: &mut Self::BridgeState, | ||
| _ctx: &BridgeContext, | ||
| ) -> Result<Vec<Self::StreamChunk>> { | ||
| Ok(vec![chunk.clone()]) | ||
| } | ||
|
|
||
| fn native_support(_provider: &dyn ProviderCapabilities) -> Option<NativeHandler<'_>> | ||
| where | ||
| Self: Sized, | ||
| { | ||
| None | ||
| } | ||
|
|
||
| fn transform_native_stream_chunk( | ||
| provider: &dyn ProviderCapabilities, | ||
| _raw: &str, | ||
| _state: &mut Self::NativeStreamState, | ||
| ) -> Result<Vec<Self::StreamChunk>> { | ||
| Err(GatewayError::NativeNotSupported { | ||
| provider: provider.name().into(), | ||
| }) | ||
| } | ||
|
|
||
| fn serialize_chunk_payload(chunk: &Self::StreamChunk) -> String { | ||
| serde_json::to_string(chunk).expect("chat completion chunk should serialize") | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use serde_json::json; | ||
|
|
||
| use super::OpenAIChatFormat; | ||
| use crate::gateway::{ | ||
| error::GatewayError, | ||
| provider_instance::ProviderAuth, | ||
| traits::{ChatFormat, ProviderCapabilities, ProviderMeta, StreamReaderKind}, | ||
| types::{common::BridgeContext, openai::*}, | ||
| }; | ||
|
|
||
| struct DummyProvider; | ||
|
|
||
| impl ProviderMeta for DummyProvider { | ||
| fn name(&self) -> &'static str { | ||
| "dummy" | ||
| } | ||
|
|
||
| fn default_base_url(&self) -> &'static str { | ||
| "https://example.com" | ||
| } | ||
|
|
||
| fn stream_reader_kind(&self) -> StreamReaderKind { | ||
| StreamReaderKind::Sse | ||
| } | ||
|
|
||
| fn build_auth_headers( | ||
| &self, | ||
| _auth: &ProviderAuth, | ||
| ) -> crate::gateway::error::Result<http::HeaderMap> { | ||
| Ok(http::HeaderMap::new()) | ||
| } | ||
| } | ||
|
|
||
| impl crate::gateway::traits::ChatTransform for DummyProvider {} | ||
|
|
||
| impl ProviderCapabilities for DummyProvider {} | ||
|
|
||
| #[test] | ||
| fn request_round_trips_through_hub_identity() { | ||
| let request: ChatCompletionRequest = serde_json::from_value(json!({ | ||
| "model": "gpt-4", | ||
| "messages": [{"role": "user", "content": "Hello"}], | ||
| "stream": true, | ||
| "custom_provider_field": "value" | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| let (hub, ctx) = OpenAIChatFormat::to_hub(&request).unwrap(); | ||
|
|
||
| assert_eq!( | ||
| serde_json::to_value(&hub).unwrap(), | ||
| serde_json::to_value(&request).unwrap() | ||
| ); | ||
| assert!(ctx.anthropic_messages_extras.is_none()); | ||
| assert!(ctx.openai_responses_extras.is_none()); | ||
| assert!(ctx.passthrough.is_empty()); | ||
| } | ||
bzp2010 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #[test] | ||
| fn response_round_trips_from_hub_identity() { | ||
| let response: ChatCompletionResponse = serde_json::from_value(json!({ | ||
| "id": "chatcmpl-123", | ||
| "object": "chat.completion", | ||
| "created": 1677652288, | ||
| "model": "gpt-4", | ||
| "choices": [{ | ||
| "index": 0, | ||
| "message": { | ||
| "role": "assistant", | ||
| "content": "Hello!" | ||
| }, | ||
| "finish_reason": "stop" | ||
| }], | ||
| "usage": { | ||
| "prompt_tokens": 9, | ||
| "completion_tokens": 12, | ||
| "total_tokens": 21 | ||
| } | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| let bridged = OpenAIChatFormat::from_hub(&response, &BridgeContext::default()).unwrap(); | ||
|
|
||
| assert_eq!( | ||
| serde_json::to_value(&bridged).unwrap(), | ||
| serde_json::to_value(&response).unwrap() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn stream_chunk_round_trips_and_serializes_payload() { | ||
| let chunk: ChatCompletionChunk = serde_json::from_value(json!({ | ||
| "id": "chatcmpl-123", | ||
| "object": "chat.completion.chunk", | ||
| "created": 1677652288, | ||
| "model": "gpt-4", | ||
| "choices": [{ | ||
| "index": 0, | ||
| "delta": { | ||
| "tool_calls": [{ | ||
| "index": 0, | ||
| "id": "call_abc", | ||
| "type": "function", | ||
| "function": { | ||
| "name": "get_weather", | ||
| "arguments": "{\"loc" | ||
| } | ||
| }] | ||
| } | ||
| }] | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| let emitted = | ||
| OpenAIChatFormat::from_hub_stream(&chunk, &mut (), &BridgeContext::default()).unwrap(); | ||
|
|
||
| assert_eq!(emitted.len(), 1); | ||
| assert_eq!( | ||
| serde_json::to_value(&emitted[0]).unwrap(), | ||
| serde_json::to_value(&chunk).unwrap() | ||
| ); | ||
| assert_eq!( | ||
| OpenAIChatFormat::serialize_chunk_payload(&emitted[0]), | ||
| serde_json::to_string(&chunk).unwrap() | ||
| ); | ||
| assert!(OpenAIChatFormat::stream_end_events(&mut (), &BridgeContext::default()).is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn is_stream_and_extract_model_use_request_fields() { | ||
| let streaming_request: ChatCompletionRequest = serde_json::from_value(json!({ | ||
| "model": "gpt-4o-mini", | ||
| "messages": [{"role": "user", "content": "Hello"}], | ||
| "stream": true | ||
| })) | ||
| .unwrap(); | ||
| let non_streaming_request: ChatCompletionRequest = serde_json::from_value(json!({ | ||
| "model": "gpt-4.1", | ||
| "messages": [{"role": "user", "content": "Hello"}] | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| assert!(OpenAIChatFormat::is_stream(&streaming_request)); | ||
| assert!(!OpenAIChatFormat::is_stream(&non_streaming_request)); | ||
| assert_eq!( | ||
| OpenAIChatFormat::extract_model(&streaming_request), | ||
| "gpt-4o-mini" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn native_stream_path_returns_native_not_supported_error() { | ||
| let provider = DummyProvider; | ||
| let error = OpenAIChatFormat::transform_native_stream_chunk(&provider, "data: {}", &mut ()) | ||
| .unwrap_err(); | ||
|
|
||
| assert!(matches!( | ||
| error, | ||
| GatewayError::NativeNotSupported { provider } if provider == "dummy" | ||
| )); | ||
| assert!(OpenAIChatFormat::native_support(&provider).is_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pub mod error; | ||
| pub mod formats; | ||
| pub mod provider_instance; | ||
| pub mod traits; | ||
| pub mod types; |
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.