anda_core/model/
completion.rs1use serde::{Deserialize, Serialize};
2use std::{collections::BTreeMap, fmt};
3
4use crate::{
5 AgentOutput, BoxError, ContentPart, Document, Documents, FunctionDefinition, Json, Message,
6 Resource,
7};
8
9pub trait CompletionFeatures: Sized {
11 fn completion(
13 &self,
14 req: CompletionRequest,
15 resources: Vec<Resource>,
16 ) -> impl Future<Output = Result<AgentOutput, BoxError>> + Send;
17
18 fn model_name(&self) -> String;
20}
21
22#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
24#[serde(rename_all = "lowercase")]
25pub enum ModelEffort {
26 Minimal,
27 Low,
28 Medium,
29 High,
30 Max,
31}
32
33impl ModelEffort {
34 pub fn as_str(self) -> &'static str {
35 match self {
36 Self::Minimal => "minimal",
37 Self::Low => "low",
38 Self::Medium => "medium",
39 Self::High => "high",
40 Self::Max => "max",
41 }
42 }
43}
44
45impl fmt::Display for ModelEffort {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 f.write_str(self.as_str())
48 }
49}
50
51#[derive(Debug, Clone, Default)]
53pub struct CompletionRequest {
54 pub instructions: String,
56
57 pub role: Option<String>,
59
60 pub chat_history: Vec<Message>,
62
63 pub raw_history: Vec<Json>,
65
66 pub documents: Documents,
68
69 pub prompt: String,
72
73 pub content: Vec<ContentPart>,
76
77 pub tools: Vec<FunctionDefinition>,
79
80 pub tool_choice_required: bool,
82
83 pub temperature: Option<f64>,
85
86 pub max_output_tokens: Option<usize>,
88
89 pub output_schema: Option<Json>,
91
92 pub stop: Option<Vec<String>>,
94
95 pub model: Option<String>,
97
98 pub effort: Option<ModelEffort>,
100}
101
102impl CompletionRequest {
103 pub fn context(mut self, id: String, text: String) -> Self {
105 self.documents.docs.push(Document {
106 content: text.into(),
107 metadata: BTreeMap::from([("id".to_string(), id.into())]),
108 });
109 self
110 }
111
112 pub fn append_documents(mut self, docs: Documents) -> Self {
114 self.documents.docs.extend(docs.docs);
115 self
116 }
117
118 pub fn append_tools(mut self, tools: Vec<FunctionDefinition>) -> Self {
120 self.tools.extend(tools);
121 self
122 }
123}