|
1 | 1 | use std::{borrow::Cow, sync::OnceLock}; |
2 | 2 |
|
3 | | -use crate::tool::Tool; |
| 3 | +use crate::{providers::helpers_thinking_block::THINK_CHUNK_ID, tool::Tool}; |
4 | 4 | use futures::StreamExt; |
5 | 5 | use lazy_static::lazy_static; |
6 | 6 | use reqwest_eventsource::{Event, EventSource}; |
@@ -470,6 +470,8 @@ struct FireworksResponseMessage { |
470 | 470 | #[serde(skip_serializing_if = "Option::is_none")] |
471 | 471 | content: Option<String>, |
472 | 472 | #[serde(skip_serializing_if = "Option::is_none")] |
| 473 | + reasoning_content: Option<String>, |
| 474 | + #[serde(skip_serializing_if = "Option::is_none")] |
473 | 475 | tool_calls: Option<Vec<FireworksResponseToolCall>>, |
474 | 476 | } |
475 | 477 |
|
@@ -531,6 +533,8 @@ struct FireworksDelta { |
531 | 533 | #[serde(skip_serializing_if = "Option::is_none")] |
532 | 534 | content: Option<String>, |
533 | 535 | #[serde(skip_serializing_if = "Option::is_none")] |
| 536 | + reasoning_content: Option<String>, |
| 537 | + #[serde(skip_serializing_if = "Option::is_none")] |
534 | 538 | tool_calls: Option<Vec<FireworksToolCallChunk>>, |
535 | 539 | } |
536 | 540 |
|
@@ -630,6 +634,14 @@ fn fireworks_to_tensorzero_chunk( |
630 | 634 | if let Some(reason) = choice.finish_reason { |
631 | 635 | finish_reason = Some(reason.into()); |
632 | 636 | } |
| 637 | + if let Some(reasoning) = choice.delta.reasoning_content { |
| 638 | + content.push(ContentBlockChunk::Thought(ThoughtChunk { |
| 639 | + text: Some(reasoning), |
| 640 | + signature: None, |
| 641 | + id: THINK_CHUNK_ID.to_string(), |
| 642 | + provider_type: Some(PROVIDER_TYPE.to_string()), |
| 643 | + })); |
| 644 | + } |
633 | 645 | if let Some(text) = choice.delta.content { |
634 | 646 | if parse_think_blocks { |
635 | 647 | if !thinking_state.update(&text, PROVIDER_TYPE)? { |
@@ -745,6 +757,13 @@ impl<'a> TryFrom<FireworksResponseWithMetadata<'a>> for ProviderInferenceRespons |
745 | 757 | } |
746 | 758 | ))?; |
747 | 759 | let mut content: Vec<ContentBlockOutput> = Vec::new(); |
| 760 | + if let Some(reasoning) = message.reasoning_content { |
| 761 | + content.push(ContentBlockOutput::Thought(Thought { |
| 762 | + text: Some(reasoning), |
| 763 | + signature: None, |
| 764 | + provider_type: Some(PROVIDER_TYPE.to_string()), |
| 765 | + })); |
| 766 | + } |
748 | 767 | if let Some(raw_text) = message.content { |
749 | 768 | let (clean_text, extracted_reasoning) = |
750 | 769 | process_think_blocks(&raw_text, parse_think_blocks, PROVIDER_TYPE)?; |
@@ -826,6 +845,7 @@ mod tests { |
826 | 845 | index: 0, |
827 | 846 | finish_reason: Some(FireworksFinishReason::Stop), |
828 | 847 | message: FireworksResponseMessage { |
| 848 | + reasoning_content: None, |
829 | 849 | content: Some(test_response_with_thinking.to_string()), |
830 | 850 | tool_calls: None, |
831 | 851 | }, |
@@ -1008,6 +1028,7 @@ mod tests { |
1008 | 1028 | index: 0, |
1009 | 1029 | finish_reason: Some(FireworksFinishReason::Stop), |
1010 | 1030 | message: FireworksResponseMessage { |
| 1031 | + reasoning_content: None, |
1011 | 1032 | content: Some("Hello, world!".to_string()), |
1012 | 1033 | tool_calls: None, |
1013 | 1034 | }, |
@@ -1077,6 +1098,7 @@ mod tests { |
1077 | 1098 | choices: vec![FireworksChatChunkChoice { |
1078 | 1099 | delta: FireworksDelta { |
1079 | 1100 | content: Some("Hello".to_string()), |
| 1101 | + reasoning_content: None, |
1080 | 1102 | tool_calls: None, |
1081 | 1103 | }, |
1082 | 1104 | finish_reason: Some(FireworksFinishReason::Stop), |
@@ -1109,6 +1131,7 @@ mod tests { |
1109 | 1131 | choices: vec![FireworksChatChunkChoice { |
1110 | 1132 | delta: FireworksDelta { |
1111 | 1133 | content: None, |
| 1134 | + reasoning_content: None, |
1112 | 1135 | tool_calls: Some(vec![FireworksToolCallChunk { |
1113 | 1136 | index: 0, |
1114 | 1137 | id: None, |
@@ -1178,6 +1201,7 @@ mod tests { |
1178 | 1201 | choices: vec![FireworksChatChunkChoice { |
1179 | 1202 | delta: FireworksDelta { |
1180 | 1203 | content: Some("<think>".to_string()), |
| 1204 | + reasoning_content: None, |
1181 | 1205 | tool_calls: None, |
1182 | 1206 | }, |
1183 | 1207 | finish_reason: None, |
@@ -1209,6 +1233,7 @@ mod tests { |
1209 | 1233 | choices: vec![FireworksChatChunkChoice { |
1210 | 1234 | delta: FireworksDelta { |
1211 | 1235 | content: Some("reasoning".to_string()), |
| 1236 | + reasoning_content: None, |
1212 | 1237 | tool_calls: None, |
1213 | 1238 | }, |
1214 | 1239 | finish_reason: None, |
@@ -1241,6 +1266,7 @@ mod tests { |
1241 | 1266 | choices: vec![FireworksChatChunkChoice { |
1242 | 1267 | delta: FireworksDelta { |
1243 | 1268 | content: Some("</think>".to_string()), |
| 1269 | + reasoning_content: None, |
1244 | 1270 | tool_calls: None, |
1245 | 1271 | }, |
1246 | 1272 | finish_reason: None, |
@@ -1268,6 +1294,7 @@ mod tests { |
1268 | 1294 | choices: vec![FireworksChatChunkChoice { |
1269 | 1295 | delta: FireworksDelta { |
1270 | 1296 | content: Some("Final answer".to_string()), |
| 1297 | + reasoning_content: None, |
1271 | 1298 | tool_calls: None, |
1272 | 1299 | }, |
1273 | 1300 | finish_reason: None, |
@@ -1302,6 +1329,7 @@ mod tests { |
1302 | 1329 | choices: vec![FireworksChatChunkChoice { |
1303 | 1330 | delta: FireworksDelta { |
1304 | 1331 | content: Some("Hello <think>should not parse</think>".to_string()), |
| 1332 | + reasoning_content: None, |
1305 | 1333 | tool_calls: None, |
1306 | 1334 | }, |
1307 | 1335 | finish_reason: Some(FireworksFinishReason::Stop), |
@@ -1335,6 +1363,7 @@ mod tests { |
1335 | 1363 | choices: vec![FireworksChatChunkChoice { |
1336 | 1364 | delta: FireworksDelta { |
1337 | 1365 | content: None, |
| 1366 | + reasoning_content: None, |
1338 | 1367 | tool_calls: Some(vec![FireworksToolCallChunk { |
1339 | 1368 | index: 0, |
1340 | 1369 | id: Some("new_id".to_string()), |
@@ -1379,6 +1408,7 @@ mod tests { |
1379 | 1408 | choices: vec![FireworksChatChunkChoice { |
1380 | 1409 | delta: FireworksDelta { |
1381 | 1410 | content: None, |
| 1411 | + reasoning_content: None, |
1382 | 1412 | tool_calls: Some(vec![FireworksToolCallChunk { |
1383 | 1413 | index: 0, |
1384 | 1414 | id: None, |
|
0 commit comments