Skip to main content

objectiveai_api/vector/completions/
vector_responses.rs

1//! Vector response transformation for LLM prompts.
2//!
3//! Converts vector response options into prompt content parts with labeled keys.
4
5/// Transforms vector responses into prompt content parts.
6///
7/// Formats responses as a JSON-like structure with prefix keys (e.g., `` `A` ``)
8/// as labels, suitable for inclusion in the user message prompt.
9pub fn into_parts_for_prompt(
10    vector_responses: &[objectiveai::chat::completions::request::RichContent],
11    vector_pfx_indices: &[(String, usize)],
12) -> Vec<objectiveai::chat::completions::request::RichContentPart> {
13    let mut parts = Vec::new();
14    for (i, (vector_pfx_key, vector_response_index)) in vector_pfx_indices.iter().enumerate() {
15        if i == 0 {
16            parts.push(
17                objectiveai::chat::completions::request::RichContentPart::Text {
18                    text: format!("{{\n    \"{}\": \"", vector_pfx_key),
19                },
20            );
21        } else {
22            parts.push(
23                objectiveai::chat::completions::request::RichContentPart::Text {
24                    text: format!("\",\n    \"{}\": \"", vector_pfx_key),
25                },
26            );
27        }
28        match &vector_responses[*vector_response_index] {
29            objectiveai::chat::completions::request::RichContent::Text(text) => {
30                parts.push(
31                    objectiveai::chat::completions::request::RichContentPart::Text {
32                        text: json_escape::escape_str(text).to_string(),
33                    },
34                );
35            }
36            objectiveai::chat::completions::request::RichContent::Parts(rich_parts) => {
37                for rich_part in rich_parts {
38                    match rich_part {
39                        objectiveai::chat::completions::request::RichContentPart::Text { text } => {
40                            parts.push(
41                                objectiveai::chat::completions::request::RichContentPart::Text {
42                                    text: json_escape::escape_str(text).to_string(),
43                                },
44                            );
45                        }
46                        part => {
47                            parts.push(part.clone());
48                        }
49                    }
50                }
51            }
52        }
53    }
54    if parts.len() > 0 {
55        parts.push(
56            objectiveai::chat::completions::request::RichContentPart::Text {
57                text: "\"\n}".to_string(),
58            },
59        );
60    }
61    parts
62}