-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: Add NVIDIA provider support with OpenAI-compatible API #2847
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
Open
rohithmahesh3
wants to merge
3
commits into
antinomyhq:main
Choose a base branch
from
rohithmahesh3:feat/nvidia-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
199 changes: 199 additions & 0 deletions
199
crates/forge_app/src/dto/openai/transformers/ensure_system_first.rs
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,199 @@ | ||
| use forge_domain::Transformer; | ||
|
|
||
| use crate::dto::openai::{Message, MessageContent, Request, Role}; | ||
|
|
||
| /// Merges all system messages into a single system message at the beginning of | ||
| /// the messages array. | ||
| /// | ||
| /// Some providers (e.g. NVIDIA) reject requests with multiple system messages | ||
| /// or system messages that are not positioned at the start of the conversation. | ||
| pub struct MergeSystemMessages; | ||
|
|
||
| impl Transformer for MergeSystemMessages { | ||
| type Value = Request; | ||
|
|
||
| fn transform(&mut self, mut request: Self::Value) -> Self::Value { | ||
| if let Some(messages) = request.messages.take() { | ||
| let (system, rest): (Vec<_>, Vec<_>) = | ||
| messages.into_iter().partition(|m| m.role == Role::System); | ||
|
|
||
|
|
||
| let merged = if system.is_empty() { | ||
| rest | ||
| } else { | ||
| let combined_content = system | ||
| .iter() | ||
| .filter_map(|m| match &m.content { | ||
| Some(MessageContent::Text(text)) => Some(text.clone()), | ||
| Some(MessageContent::Parts(parts)) => Some( | ||
| parts | ||
| .iter() | ||
| .filter_map(|p| match p { | ||
| crate::dto::openai::ContentPart::Text { text, .. } => { | ||
| Some(text.clone()) | ||
| } | ||
| _ => None, | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .join(""), | ||
| ), | ||
| None => None, | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .join("\n\n"); | ||
|
|
||
| if combined_content.is_empty() { | ||
| // All system messages had no content, don't create empty system message | ||
| rest | ||
| } else { | ||
| let mut result = vec![Message { | ||
| role: Role::System, | ||
| content: Some(MessageContent::Text(combined_content)), | ||
| name: None, | ||
| tool_call_id: None, | ||
| tool_calls: None, | ||
| reasoning_details: None, | ||
| reasoning_text: None, | ||
| reasoning_opaque: None, | ||
| reasoning_content: None, | ||
| extra_content: None, | ||
| }]; | ||
| result.extend(rest); | ||
| result | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| request.messages = Some(merged); | ||
| } | ||
| request | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use pretty_assertions::assert_eq; | ||
|
|
||
| use super::*; | ||
| use crate::dto::openai::{Message, MessageContent, Role}; | ||
|
|
||
| fn system_msg(content: &str) -> Message { | ||
| Message { | ||
| role: Role::System, | ||
| content: Some(MessageContent::Text(content.to_string())), | ||
| name: None, | ||
| tool_call_id: None, | ||
| tool_calls: None, | ||
| reasoning_details: None, | ||
| reasoning_text: None, | ||
| reasoning_opaque: None, | ||
| reasoning_content: None, | ||
| extra_content: None, | ||
| } | ||
| } | ||
|
|
||
| fn user_msg(content: &str) -> Message { | ||
| Message { | ||
| role: Role::User, | ||
| content: Some(MessageContent::Text(content.to_string())), | ||
| name: None, | ||
| tool_call_id: None, | ||
| tool_calls: None, | ||
| reasoning_details: None, | ||
| reasoning_text: None, | ||
| reasoning_opaque: None, | ||
| reasoning_content: None, | ||
| extra_content: None, | ||
| } | ||
| } | ||
|
|
||
| fn assistant_msg(content: &str) -> Message { | ||
| Message { | ||
| role: Role::Assistant, | ||
| content: Some(MessageContent::Text(content.to_string())), | ||
| name: None, | ||
| tool_call_id: None, | ||
| tool_calls: None, | ||
| reasoning_details: None, | ||
| reasoning_text: None, | ||
| reasoning_opaque: None, | ||
| reasoning_content: None, | ||
| extra_content: None, | ||
| } | ||
| } | ||
|
|
||
| fn get_text_content(msg: &Message) -> Option<&str> { | ||
| match msg.content.as_ref() { | ||
| Some(MessageContent::Text(text)) => Some(text.as_str()), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_multiple_system_messages_merged() { | ||
| let fixture = Request::default().messages(vec![ | ||
| user_msg("hello"), | ||
| system_msg("you are helpful"), | ||
| assistant_msg("hi"), | ||
| system_msg("be concise"), | ||
| user_msg("how are you"), | ||
| ]); | ||
|
|
||
| let actual = MergeSystemMessages.transform(fixture); | ||
|
|
||
| let messages = actual.messages.unwrap(); | ||
| assert_eq!(messages.len(), 4); | ||
| assert_eq!(messages[0].role, Role::System); | ||
| assert_eq!( | ||
| get_text_content(&messages[0]), | ||
| Some("you are helpful\n\nbe concise") | ||
| ); | ||
| assert_eq!(messages[1].role, Role::User); | ||
| assert_eq!(messages[2].role, Role::Assistant); | ||
| assert_eq!(messages[3].role, Role::User); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_single_system_message_unchanged() { | ||
| let fixture = Request::default().messages(vec![ | ||
| system_msg("you are helpful"), | ||
| user_msg("hello"), | ||
| assistant_msg("hi"), | ||
| ]); | ||
|
|
||
| let actual = MergeSystemMessages.transform(fixture); | ||
|
|
||
| let messages = actual.messages.unwrap(); | ||
| assert_eq!(messages.len(), 3); | ||
| assert_eq!(messages[0].role, Role::System); | ||
| assert_eq!(get_text_content(&messages[0]), Some("you are helpful")); | ||
| assert_eq!(messages[1].role, Role::User); | ||
| assert_eq!(messages[2].role, Role::Assistant); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_no_system_messages_unchanged() { | ||
| let fixture = Request::default().messages(vec![ | ||
| user_msg("hello"), | ||
| assistant_msg("hi"), | ||
| user_msg("how are you"), | ||
| ]); | ||
|
|
||
| let actual = MergeSystemMessages.transform(fixture); | ||
|
|
||
| let messages = actual.messages.unwrap(); | ||
| assert_eq!(messages.len(), 3); | ||
| assert_eq!(messages[0].role, Role::User); | ||
| assert_eq!(messages[1].role, Role::Assistant); | ||
| assert_eq!(messages[2].role, Role::User); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_no_messages_unchanged() { | ||
| let fixture = Request::default(); | ||
|
|
||
| let actual = MergeSystemMessages.transform(fixture); | ||
|
|
||
| assert!(actual.messages.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
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
Oops, something went wrong.
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.