objectiveai_sdk/vector/completions/response/unary/
vector_completion.rs1use crate::{agent, vector::completions::response};
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
12#[schemars(rename = "vector.completions.response.unary.VectorCompletion")]
13pub struct VectorCompletion {
14 pub id: String,
16 pub completions: Vec<super::AgentCompletion>,
18 pub votes: Vec<response::Vote>,
20 #[serde(deserialize_with = "crate::serde_util::vec_decimal")]
22 #[schemars(with = "Vec<f64>")]
23 pub scores: Vec<rust_decimal::Decimal>,
24 #[serde(deserialize_with = "crate::serde_util::vec_decimal")]
28 #[schemars(with = "Vec<f64>")]
29 pub weights: Vec<rust_decimal::Decimal>,
30 pub created: u64,
32 pub swarm: String,
34 pub object: super::Object,
36 pub usage: agent::completions::response::Usage,
38}
39
40impl VectorCompletion {
41 pub fn normalize_for_tests(&mut self) {
43 self.id = String::new();
44 self.created = 0;
45 for completion in &mut self.completions {
46 completion.inner.normalize_for_tests();
47 }
48 self.votes.sort_by_key(|v| v.flat_swarm_index);
49
50 self.completions
52 .sort_by_cached_key(|c| serde_json::to_string(&c.inner).unwrap());
53
54 let mut index_map = std::collections::HashMap::new();
60 let mut i = 0;
61 for completion in &mut self.completions {
62 index_map.insert(completion.index, i);
63 completion.index = i;
64 i += 1;
65 }
66 for vote in &mut self.votes {
67 if let Some(completion_index) = vote.completion_index {
68 if let Some(new_index) = index_map.get(&completion_index) {
71 vote.completion_index = Some(*new_index);
72 }
73 }
74 }
75 }
76
77 pub fn default_from_request_responses_len(
79 request_responses_len: usize,
80 ) -> Self {
81 let weights = vec![rust_decimal::Decimal::ZERO; request_responses_len];
82 let scores =
83 vec![
84 rust_decimal::Decimal::ONE
85 / rust_decimal::Decimal::from(request_responses_len);
86 request_responses_len
87 ];
88 Self {
89 id: String::new(),
90 completions: Vec::new(),
91 votes: Vec::new(),
92 scores,
93 weights,
94 created: 0,
95 swarm: String::new(),
96 object: super::Object::default(),
97 usage: agent::completions::response::Usage::default(),
98 }
99 }
100}
101
102impl From<response::streaming::VectorCompletionChunk> for VectorCompletion {
103 fn from(
104 response::streaming::VectorCompletionChunk {
105 id,
106 completions,
107 votes,
108 scores,
109 weights,
110 created,
111 swarm,
112 object,
113 usage,
114 }: response::streaming::VectorCompletionChunk,
115 ) -> Self {
116 Self {
117 id,
118 completions: completions
119 .into_iter()
120 .map(super::AgentCompletion::from)
121 .collect(),
122 votes,
123 scores,
124 weights,
125 created,
126 swarm,
127 object: object.into(),
128 usage: usage.unwrap_or_default(),
129 }
130 }
131}