Parse `Perplexity` response to `GraphBit` response
(&self, response: PerplexityResponse)
| 115 | |
| 116 | /// Parse `Perplexity` response to `GraphBit` response |
| 117 | fn parse_response(&self, response: PerplexityResponse) -> GraphBitResult<LlmResponse> { |
| 118 | let choice = |
| 119 | response.choices.into_iter().next().ok_or_else(|| { |
| 120 | GraphBitError::llm_provider("perplexity", "No choices in response") |
| 121 | })?; |
| 122 | |
| 123 | let content = choice.message.content; |
| 124 | let tool_calls = choice |
| 125 | .message |
| 126 | .tool_calls |
| 127 | .unwrap_or_default() |
| 128 | .into_iter() |
| 129 | .map(|tc| LlmToolCall { |
| 130 | id: tc.id, |
| 131 | name: tc.function.name, |
| 132 | parameters: serde_json::from_str(&tc.function.arguments).unwrap_or_default(), |
| 133 | }) |
| 134 | .collect(); |
| 135 | |
| 136 | let finish_reason = match choice.finish_reason.as_deref() { |
| 137 | Some("stop") => FinishReason::Stop, |
| 138 | Some("length") => FinishReason::Length, |
| 139 | Some("tool_calls") => FinishReason::ToolCalls, |
| 140 | Some("content_filter") => FinishReason::ContentFilter, |
| 141 | Some(other) => FinishReason::Other(other.to_string()), |
| 142 | None => FinishReason::Stop, |
| 143 | }; |
| 144 | |
| 145 | let usage = LlmUsage::new( |
| 146 | response.usage.prompt_tokens, |
| 147 | response.usage.completion_tokens, |
| 148 | ); |
| 149 | |
| 150 | Ok(LlmResponse::new(content, &self.model) |
| 151 | .with_tool_calls(tool_calls) |
| 152 | .with_usage(usage) |
| 153 | .with_finish_reason(finish_reason) |
| 154 | .with_id(response.id)) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #[async_trait] |
no test coverage detected