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<'p, M>(
27 &self,
28 params: ChatCompleteParameters<M>,
29 ) -> Pin<
30 Box<dyn Future<Output = Result<GenericChatCompletionResponse<GenericMessage>>> + Send + 'p>,
31 >
32 where
33 M: Into<Self::Message> + Send + Sync + 'p;
34}
35
36pub trait StreamingChatProvider: ChatCompletionProvider {
43 type Delta<'s>: Stream<Item = Result<String>> + Send + 's
46 where
47 Self: 's;
48
49 fn chat_complete_stream<'p, M>(&self, params: ChatCompleteParameters<M>) -> Self::Delta<'p>
51 where
52 M: Into<Self::Message> + Send + Sync + 'p;
53}
54
55#[derive(Debug, Clone)]
56pub struct ChatCompleteParameters<M> {
57 pub messages: Vec<M>,
58 pub model: Model,
59 pub tools: Option<Vec<GenericFunctionSpec>>,
60 pub temperature: Option<f64>,
61 pub response_format: Option<serde_json::Value>,
62}
63
64impl<M> ChatCompleteParameters<M> {
65 pub fn new(messages: Vec<M>, model: Model) -> Self {
66 Self {
67 messages,
68 model,
69 tools: None,
70 temperature: None,
71 response_format: None,
72 }
73 }
74
75 pub fn messages(&self) -> &Vec<M> {
76 &self.messages
77 }
78
79 pub fn model(&self) -> Model {
80 self.model.clone()
81 }
82
83 pub fn tools(&self) -> Option<&Vec<GenericFunctionSpec>> {
84 self.tools.as_ref()
85 }
86
87 pub fn with_temperature(mut self, temperature: f64) -> Self {
88 self.temperature = Some(temperature);
89 self
90 }
91
92 pub fn with_response_format(mut self, response_format: serde_json::Value) -> Self {
93 self.response_format = Some(response_format);
94 self
95 }
96
97 pub fn with_tools(mut self, tools: Vec<GenericFunctionSpec>) -> Self {
98 self.tools = Some(tools);
99 self
100 }
101}