1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
use super::*;
use anyhow::{anyhow, Context, Result};
use reqwest::{Client as ReqwestClient, RequestBuilder};
use serde::Deserialize;
use serde_json::{json, Value};
const API_BASE: &str = "https://api.cloudflare.com/client/v4";
#[derive(Debug, Clone, Deserialize, Default)]
pub struct CloudflareConfig {
pub name: Option<String>,
pub account_id: Option<String>,
pub api_key: Option<String>,
#[serde(default)]
pub models: Vec<ModelData>,
pub patch: Option<ModelPatch>,
pub extra: Option<ExtraConfig>,
}
impl CloudflareClient {
config_get_fn!(account_id, get_account_id);
config_get_fn!(api_key, get_api_key);
pub const PROMPTS: [PromptAction<'static>; 2] = [
("account_id", "Account ID:", true, PromptKind::String),
("api_key", "API Key:", true, PromptKind::String),
];
fn chat_completions_builder(
&self,
client: &ReqwestClient,
data: ChatCompletionsData,
) -> Result<RequestBuilder> {
let account_id = self.get_account_id()?;
let api_key = self.get_api_key()?;
let mut body = build_chat_completions_body(data, &self.model)?;
self.patch_chat_completions_body(&mut body);
let url = format!(
"{API_BASE}/accounts/{account_id}/ai/run/{}",
self.model.name()
);
debug!("Cloudflare Chat Completions Request: {url} {body}");
let builder = client.post(url).bearer_auth(api_key).json(&body);
Ok(builder)
}
fn embeddings_builder(
&self,
client: &ReqwestClient,
data: EmbeddingsData,
) -> Result<RequestBuilder> {
let account_id = self.get_account_id()?;
let api_key = self.get_api_key()?;
let body = json!({
"text": data.texts,
});
let url = format!(
"{API_BASE}/accounts/{account_id}/ai/run/{}",
self.model.name()
);
debug!("Cloudflare Embeddings Request: {url} {body}");
let builder = client.post(url).bearer_auth(api_key).json(&body);
Ok(builder)
}
}
impl_client_trait!(
CloudflareClient,
chat_completions,
chat_completions_streaming,
embeddings
);
async fn chat_completions(builder: RequestBuilder) -> Result<ChatCompletionsOutput> {
let res = builder.send().await?;
let status = res.status();
let data: Value = res.json().await?;
if !status.is_success() {
catch_error(&data, status.as_u16())?;
}
debug!("non-stream-data: {data}");
extract_chat_completions(&data)
}
async fn chat_completions_streaming(
builder: RequestBuilder,
handler: &mut SseHandler,
) -> Result<()> {
let handle = |message: SseMmessage| -> Result<bool> {
if message.data == "[DONE]" {
return Ok(true);
}
let data: Value = serde_json::from_str(&message.data)?;
debug!("stream-data: {data}");
if let Some(text) = data["response"].as_str() {
handler.text(text)?;
}
Ok(false)
};
sse_stream(builder, handle).await
}
async fn embeddings(builder: RequestBuilder) -> Result<EmbeddingsOutput> {
let res = builder.send().await?;
let status = res.status();
let data: Value = res.json().await?;
if !status.is_success() {
catch_error(&data, status.as_u16())?;
}
let res_body: EmbeddingsResBody =
serde_json::from_value(data).context("Invalid embeddings data")?;
Ok(res_body.result.data)
}
#[derive(Deserialize)]
struct EmbeddingsResBody {
result: EmbeddingsResBodyResult,
}
#[derive(Deserialize)]
struct EmbeddingsResBodyResult {
data: Vec<Vec<f32>>,
}
fn build_chat_completions_body(data: ChatCompletionsData, model: &Model) -> Result<Value> {
let ChatCompletionsData {
messages,
temperature,
top_p,
functions: _,
stream,
} = data;
let mut body = json!({
"model": &model.name(),
"messages": messages,
});
if let Some(v) = model.max_tokens_param() {
body["max_tokens"] = v.into();
}
if let Some(v) = temperature {
body["temperature"] = v.into();
}
if let Some(v) = top_p {
body["top_p"] = v.into();
}
if stream {
body["stream"] = true.into();
}
Ok(body)
}
fn extract_chat_completions(data: &Value) -> Result<ChatCompletionsOutput> {
let text = data["result"]["response"]
.as_str()
.ok_or_else(|| anyhow!("Invalid response data: {data}"))?;
Ok(ChatCompletionsOutput::new(text))
}
|