Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing missing object field for regular completions. #2175

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 97 additions & 4 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,38 @@
}
}
},
"Chunk": {
"type": "object",
"required": [
"id",
"created",
"choices",
"model",
"system_fingerprint"
],
"properties": {
"choices": {
"type": "array",
"items": {
"$ref": "#/components/schemas/CompletionComplete"
}
},
"created": {
"type": "integer",
"format": "int64",
"minimum": 0
},
"id": {
"type": "string"
},
"model": {
"type": "string"
},
"system_fingerprint": {
"type": "string"
}
}
},
"CompatGenerateRequest": {
"type": "object",
"required": [
Expand All @@ -965,6 +997,55 @@
}
}
},
"Completion": {
"oneOf": [
{
"allOf": [
{
"$ref": "#/components/schemas/Chunk"
},
{
"type": "object",
"required": [
"object"
],
"properties": {
"object": {
"type": "string",
"enum": [
"text_completion"
]
}
}
}
]
},
{
"allOf": [
{
"$ref": "#/components/schemas/CompletionFinal"
},
{
"type": "object",
"required": [
"object"
],
"properties": {
"object": {
"type": "string",
"enum": [
"text_completion"
]
}
}
}
]
}
],
"discriminator": {
"propertyName": "object"
}
},
"CompletionComplete": {
"type": "object",
"required": [
Expand Down Expand Up @@ -994,14 +1075,15 @@
}
}
},
"CompletionCompleteChunk": {
"CompletionFinal": {
"type": "object",
"required": [
"id",
"created",
"choices",
"model",
"system_fingerprint"
"system_fingerprint",
"choices",
"usage"
],
"properties": {
"choices": {
Expand All @@ -1013,16 +1095,21 @@
"created": {
"type": "integer",
"format": "int64",
"example": "1706270835",
"minimum": 0
},
"id": {
"type": "string"
},
"model": {
"type": "string"
"type": "string",
"example": "mistralai/Mistral-7B-Instruct-v0.2"
},
"system_fingerprint": {
"type": "string"
},
"usage": {
"$ref": "#/components/schemas/Usage"
}
}
},
Expand Down Expand Up @@ -1647,6 +1734,12 @@
}
}
},
"Prompt": {
"type": "array",
"items": {
"type": "string"
}
},
"SimpleToken": {
"type": "object",
"required": [
Expand Down
29 changes: 19 additions & 10 deletions router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,17 @@ pub struct CompletionRequest {
pub stop: Option<Vec<String>>,
}

#[derive(Clone, Serialize, ToSchema)]
#[serde(tag = "object")]
enum Completion {
#[serde(rename = "text_completion")]
Chunk(Chunk),
#[serde(rename = "text_completion")]
Final(CompletionFinal),
}

#[derive(Clone, Deserialize, Serialize, ToSchema, Default)]
pub(crate) struct Completion {
pub(crate) struct CompletionFinal {
pub id: String,
#[schema(example = "1706270835")]
pub created: u64,
Expand All @@ -453,6 +462,15 @@ pub(crate) struct CompletionComplete {
pub finish_reason: String,
}

#[derive(Clone, Deserialize, Serialize, ToSchema)]
pub(crate) struct Chunk {
pub id: String,
pub created: u64,
pub choices: Vec<CompletionComplete>,
pub model: String,
pub system_fingerprint: String,
}

#[derive(Clone, Deserialize, Serialize, ToSchema)]
pub(crate) struct ChatCompletion {
pub id: String,
Expand Down Expand Up @@ -614,15 +632,6 @@ impl ChatCompletion {
}
}
}
#[derive(Clone, Deserialize, Serialize, ToSchema)]
pub(crate) struct CompletionCompleteChunk {
pub id: String,
pub created: u64,
pub choices: Vec<CompletionComplete>,
pub model: String,
pub system_fingerprint: String,
}

#[derive(Clone, Serialize, ToSchema)]
pub(crate) struct ChatCompletionChunk {
pub id: String,
Expand Down
17 changes: 10 additions & 7 deletions router/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::{
use crate::{
ChatCompletion, ChatCompletionChoice, ChatCompletionChunk, ChatCompletionComplete,
ChatCompletionDelta, ChatCompletionLogprob, ChatCompletionLogprobs, ChatCompletionTopLogprob,
ChatRequest, CompatGenerateRequest, Completion, CompletionComplete, CompletionCompleteChunk,
CompletionRequest, CompletionType, DeltaToolCall, Function, Tool, VertexRequest,
ChatRequest, Chunk, CompatGenerateRequest, Completion, CompletionComplete, CompletionFinal,
CompletionRequest, CompletionType, DeltaToolCall, Function, Prompt, Tool, VertexRequest,
VertexResponse,
};
use crate::{FunctionDefinition, HubPreprocessorConfig, ToolCall, ToolType};
Expand Down Expand Up @@ -705,7 +705,7 @@ async fn completions(
.as_secs();

event
.json_data(CompletionCompleteChunk {
.json_data(Completion::Chunk(Chunk {
id: "".to_string(),
created: current_time,

Expand All @@ -718,7 +718,7 @@ async fn completions(

model: model_id.clone(),
system_fingerprint: system_fingerprint.clone(),
})
}))
.unwrap_or_else(|_e| Event::default())
};

Expand Down Expand Up @@ -931,7 +931,7 @@ async fn completions(
.collect::<Result<Vec<_>, _>>()
.map_err(|(status, Json(err))| (status, Json(err)))?;

let response = Completion {
let response = Completion::Final(CompletionFinal {
id: "".to_string(),
created: current_time,
model: info.model_id.clone(),
Expand All @@ -946,7 +946,7 @@ async fn completions(
completion_tokens,
total_tokens,
},
};
});

// headers similar to `generate` but aggregated
let mut headers = HeaderMap::new();
Expand Down Expand Up @@ -1464,7 +1464,10 @@ pub async fn run(
ChatCompletion,
CompletionRequest,
CompletionComplete,
CompletionCompleteChunk,
Chunk,
Completion,
CompletionFinal,
Prompt,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one and Completion were missing on main.

GenerateParameters,
PrefillToken,
Token,
Expand Down
Loading