MCPcopy Create free account
hub / github.com/InfinitiBit/graphbit / parse_response

Method parse_response

core/src/llm/fireworks.rs:115–184  ·  view source on GitHub ↗

Parse `Fireworks AI` response to `GraphBit` response

(&self, response: FireworksResponse)

Source from the content-addressed store, hash-verified

113
114 /// Parse `Fireworks AI` response to `GraphBit` response
115 fn parse_response(&self, response: FireworksResponse) -> GraphBitResult<LlmResponse> {
116 let choice =
117 response.choices.into_iter().next().ok_or_else(|| {
118 GraphBitError::llm_provider("fireworks", "No choices in response")
119 })?;
120
121 let mut content = choice.message.content.unwrap_or_default();
122 if content.trim().is_empty()
123 && !choice
124 .message
125 .tool_calls
126 .as_ref()
127 .unwrap_or(&vec![])
128 .is_empty()
129 {
130 content = "I'll help you with that using the available tools.".to_string();
131 }
132
133 let tool_calls = choice
134 .message
135 .tool_calls
136 .unwrap_or_default()
137 .into_iter()
138 .map(|tc| {
139 // Production-grade argument parsing with error handling
140 let parameters = if tc.function.arguments.trim().is_empty() {
141 serde_json::Value::Object(serde_json::Map::new())
142 } else {
143 match serde_json::from_str(&tc.function.arguments) {
144 Ok(params) => params,
145 Err(e) => {
146 tracing::warn!(
147 "Failed to parse tool call arguments for {}: {e}. Arguments: '{}'",
148 tc.function.name,
149 tc.function.arguments
150 );
151 // Try to create a simple object with the raw arguments
152 serde_json::json!({ "raw_arguments": tc.function.arguments })
153 }
154 }
155 };
156
157 LlmToolCall {
158 id: tc.id,
159 name: tc.function.name,
160 parameters,
161 }
162 })
163 .collect();
164
165 let finish_reason = match choice.finish_reason.as_deref() {
166 Some("stop") => FinishReason::Stop,
167 Some("length") => FinishReason::Length,
168 Some("tool_calls") => FinishReason::ToolCalls,
169 Some("content_filter") => FinishReason::ContentFilter,
170 Some(other) => FinishReason::Other(other.to_string()),
171 None => FinishReason::Stop,
172 };

Callers 1

completeMethod · 0.45

Calls 6

to_stringMethod · 0.80
is_emptyMethod · 0.45
with_idMethod · 0.45
with_finish_reasonMethod · 0.45
with_usageMethod · 0.45
with_tool_callsMethod · 0.45

Tested by

no test coverage detected