objectiveai_sdk/vector/completions/response/unary/
vector_completion.rs1use crate::{agent, vector::completions::response};
4use serde::{Deserialize, Serialize};
5use schemars::JsonSchema;
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.sort_by_cached_key(|c| serde_json::to_string(&c.inner).unwrap());
52
53 let mut i = 0;
55 for completion in &mut self.completions {
56 completion.index = i;
57 i += 1;
58 }
59 }
60
61 pub fn default_from_request_responses_len(
63 request_responses_len: usize,
64 ) -> Self {
65 let weights = vec![rust_decimal::Decimal::ZERO; request_responses_len];
66 let scores =
67 vec![
68 rust_decimal::Decimal::ONE
69 / rust_decimal::Decimal::from(request_responses_len);
70 request_responses_len
71 ];
72 Self {
73 id: String::new(),
74 completions: Vec::new(),
75 votes: Vec::new(),
76 scores,
77 weights,
78 created: 0,
79 swarm: String::new(),
80 object: super::Object::default(),
81 usage: agent::completions::response::Usage::default(),
82 }
83 }
84}
85
86impl From<response::streaming::VectorCompletionChunk> for VectorCompletion {
87 fn from(
88 response::streaming::VectorCompletionChunk {
89 id,
90 completions,
91 votes,
92 scores,
93 weights,
94 created,
95 swarm,
96 object,
97 usage,
98 }: response::streaming::VectorCompletionChunk,
99 ) -> Self {
100 Self {
101 id,
102 completions: completions
103 .into_iter()
104 .map(super::AgentCompletion::from)
105 .collect(),
106 votes,
107 scores,
108 weights,
109 created,
110 swarm,
111 object: object.into(),
112 usage: usage.unwrap_or_default(),
113 }
114 }
115}