artificial_core/provider/
chat_complete.rs1use std::{future::Future, pin::Pin};
2
3use crate::{
4 error::Result,
5 generic::{GenericChatCompletionResponse, GenericFunctionSpec, GenericMessage},
6 model::Model,
7};
8use futures_core::stream::Stream;
9
10pub trait ChatCompletionProvider: Send + Sync {
21 type Message: Send + Sync + 'static;
23
24 fn chat_complete<'s, M>(
27 &'s self,
28 params: ChatCompleteParameters<M>,
29 ) -> Pin<
30 Box<dyn Future<Output = Result<GenericChatCompletionResponse<GenericMessage>>> + Send + 's>,
31 >
32 where
33 M: Into<Self::Message> + Clone + Send + Sync + 's;
34}
35
36pub trait StreamingChatProvider: Send + Sync {
43 type Message: Send + Sync + 'static;
45
46 type Delta<'s>: Stream<Item = Result<String>> + Send + 's
49 where
50 Self: 's;
51
52 fn chat_complete_stream<'s, M>(&'s self, params: ChatCompleteParameters<M>) -> Self::Delta<'s>
54 where
55 M: Into<Self::Message> + Clone + Send + Sync + 's;
56}
57
58#[derive(Debug, Clone)]
59pub struct ChatCompleteParameters<M: Clone> {
60 pub messages: Vec<M>,
61 pub model: Model,
62 pub tools: Option<Vec<GenericFunctionSpec>>,
63 pub temperature: Option<f64>,
64 pub response_format: Option<serde_json::Value>,
65}
66
67impl<M: Clone> ChatCompleteParameters<M> {
68 pub fn new(messages: Vec<M>, model: Model) -> Self {
69 Self {
70 messages,
71 model,
72 tools: None,
73 temperature: None,
74 response_format: None,
75 }
76 }
77
78 pub fn messages(&self) -> &Vec<M> {
79 &self.messages
80 }
81
82 pub fn model(&self) -> Model {
83 self.model.clone()
84 }
85
86 pub fn tools(&self) -> Option<&Vec<GenericFunctionSpec>> {
87 self.tools.as_ref()
88 }
89
90 pub fn with_temperature(mut self, temperature: f64) -> Self {
91 self.temperature = Some(temperature);
92 self
93 }
94
95 pub fn with_response_format(mut self, response_format: serde_json::Value) -> Self {
96 self.response_format = Some(response_format);
97 self
98 }
99
100 pub fn with_tools(mut self, tools: Vec<GenericFunctionSpec>) -> Self {
101 self.tools = Some(tools);
102 self
103 }
104}