Parse `HuggingFace` response to `GraphBit` response
(&self, response: HuggingFaceResponse)
| 71 | |
| 72 | /// Parse `HuggingFace` response to `GraphBit` response |
| 73 | fn parse_response(&self, response: HuggingFaceResponse) -> GraphBitResult<LlmResponse> { |
| 74 | let generated_text = response |
| 75 | .into_iter() |
| 76 | .next() |
| 77 | .ok_or_else(|| { |
| 78 | GraphBitError::llm_provider("huggingface", "No generated text in response") |
| 79 | })? |
| 80 | .generated_text; |
| 81 | |
| 82 | // Extract only the assistant's response (after the last "Assistant: ") |
| 83 | let content = if let Some(last_assistant) = generated_text.rfind("Assistant: ") { |
| 84 | generated_text[last_assistant + "Assistant: ".len()..] |
| 85 | .trim() |
| 86 | .to_string() |
| 87 | } else { |
| 88 | generated_text.trim().to_string() |
| 89 | }; |
| 90 | |
| 91 | // `HuggingFace` doesn't provide usage stats in the same way, so we estimate |
| 92 | let usage = LlmUsage::new( |
| 93 | (content.len() / 4) as u32, // Rough estimate: 4 chars per token |
| 94 | (content.len() / 4) as u32, |
| 95 | ); |
| 96 | |
| 97 | Ok(LlmResponse::new(content, &self.model) |
| 98 | .with_usage(usage) |
| 99 | .with_finish_reason(FinishReason::Stop)) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #[async_trait] |
no test coverage detected