Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Add stream_options.include_usage for OpenAI-compatible API token usage (#45812)

Elier905d28cc5406
diff --git a/crates/edit_prediction/src/mercury.rs b/crates/edit_prediction/src/mercury.rs
index 71362f4c873..df47a380623 100644
--- a/crates/edit_prediction/src/mercury.rs
+++ b/crates/edit_prediction/src/mercury.rs
@@ -137,6 +137,7 @@ impl Mercury {
content: open_ai::MessageContent::Plain(prompt),
}],
stream: false,
+ stream_options: None,
max_completion_tokens: None,
stop: vec![],
temperature: None,
diff --git a/crates/edit_prediction_cli/src/openai_client.rs b/crates/edit_prediction_cli/src/openai_client.rs
index c9947e16099..6bc9c2d77c0 100644
--- a/crates/edit_prediction_cli/src/openai_client.rs
+++ b/crates/edit_prediction_cli/src/openai_client.rs
@@ -40,6 +40,7 @@ impl PlainOpenAiClient {
model: model.to_string(),
messages,
stream: false,
+ stream_options: None,
max_completion_tokens: Some(max_tokens),
stop: Vec::new(),
temperature: None,
@@ -490,6 +491,7 @@ impl BatchingOpenAiClient {
model: serializable_request.model,
messages,
stream: false,
+ stream_options: None,
max_completion_tokens: Some(serializable_request.max_tokens),
stop: Vec::new(),
temperature: None,
diff --git a/crates/language_models/src/provider/open_ai.rs b/crates/language_models/src/provider/open_ai.rs
index c1ebf76e0b0..e033be8ee23 100644
--- a/crates/language_models/src/provider/open_ai.rs
+++ b/crates/language_models/src/provider/open_ai.rs
@@ -506,6 +506,11 @@ pub fn into_open_ai(
model: model_id.into(),
messages,
stream,
+ stream_options: if stream {
+ Some(open_ai::StreamOptions::default())
+ } else {
+ None
+ },
stop: request.stop,
temperature: request.temperature.or(Some(1.0)),
max_completion_tokens: max_output_tokens,
diff --git a/crates/open_ai/src/open_ai.rs b/crates/open_ai/src/open_ai.rs
index 25946591e32..c4a3e078d76 100644
--- a/crates/open_ai/src/open_ai.rs
+++ b/crates/open_ai/src/open_ai.rs
@@ -295,12 +295,27 @@ impl Model {
}
}
+#[derive(Debug, Serialize, Deserialize)]
+pub struct StreamOptions {
+ pub include_usage: bool,
+}
+
+impl Default for StreamOptions {
+ fn default() -> Self {
+ Self {
+ include_usage: true,
+ }
+ }
+}
+
#[derive(Debug, Serialize, Deserialize)]
pub struct Request {
pub model: String,
pub messages: Vec<RequestMessage>,
pub stream: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
+ pub stream_options: Option<StreamOptions>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
pub max_completion_tokens: Option<u64>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub stop: Vec<String>,