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

Method parse_python_response

core/src/embeddings/python_bridge.rs:44–115  ·  view source on GitHub ↗

Parse Python response to EmbeddingResponse

(
        &self,
        py: Python<'_>,
        result: PyObject,
    )

Source from the content-addressed store, hash-verified

42
43 /// Parse Python response to EmbeddingResponse
44 fn parse_python_response(
45 &self,
46 py: Python<'_>,
47 result: PyObject,
48 ) -> GraphBitResult<EmbeddingResponse> {
49 // The Python embedding method should return a list of floats (single embedding)
50 // or a list of lists of floats (multiple embeddings)
51
52 let result_bound = result.bind(py);
53 let embeddings: Vec<Vec<f32>> = if let Ok(list) = result_bound.downcast::<PyList>() {
54 if list.is_empty() {
55 return Err(GraphBitError::llm("Empty embedding response from Python"));
56 }
57
58 // Check if it's a single embedding (list of floats) or multiple (list of lists)
59 let first_item = list.get_item(0).map_err(|e| {
60 GraphBitError::llm(format!(
61 "Failed to get first item from Python response: {e}"
62 ))
63 })?;
64
65 if first_item.downcast::<PyList>().is_ok() {
66 // Multiple embeddings (list of lists)
67 list.iter()
68 .map(|item| {
69 item.downcast::<PyList>()
70 .map_err(|e| {
71 GraphBitError::llm(format!("Invalid embedding format: {e}"))
72 })?
73 .iter()
74 .map(|v| {
75 v.extract::<f32>().map_err(|e| {
76 GraphBitError::llm(format!("Failed to extract float: {e}"))
77 })
78 })
79 .collect::<Result<Vec<f32>, _>>()
80 })
81 .collect::<Result<Vec<Vec<f32>>, _>>()?
82 } else {
83 // Single embedding (list of floats)
84 let embedding: Vec<f32> = list
85 .iter()
86 .map(|v| {
87 v.extract::<f32>().map_err(|e| {
88 GraphBitError::llm(format!("Failed to extract float: {e}"))
89 })
90 })
91 .collect::<Result<Vec<f32>, _>>()?;
92 vec![embedding]
93 }
94 } else {
95 return Err(GraphBitError::llm(
96 "Python embedding response must be a list",
97 ));
98 };
99
100 // Estimate token usage (Python providers typically don't provide this)
101 let total_chars: usize = embeddings.iter().map(|e| e.len()).sum();

Callers 1

generate_embeddingsMethod · 0.45

Calls 3

lenMethod · 0.80
cloneMethod · 0.80
is_emptyMethod · 0.45

Tested by

no test coverage detected