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

Method parse_response

core/src/llm/huggingface.rs:73–100  ·  view source on GitHub ↗

Parse `HuggingFace` response to `GraphBit` response

(&self, response: HuggingFaceResponse)

Source from the content-addressed store, hash-verified

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]

Callers 1

completeMethod · 0.45

Calls 4

to_stringMethod · 0.80
lenMethod · 0.80
with_finish_reasonMethod · 0.45
with_usageMethod · 0.45

Tested by

no test coverage detected