Skip to content

Commit ccb0e6b

Browse files
NarsilErikKaum
authored andcommitted
Fixing missing object field for regular completions. (#2175)
* Fixing missing `object` field for regular completions. * Fixing docs by re-adding missing `Prompt`.
1 parent 8ffc018 commit ccb0e6b

File tree

3 files changed

+126
-21
lines changed

3 files changed

+126
-21
lines changed

docs/openapi.json

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,38 @@
946946
}
947947
}
948948
},
949+
"Chunk": {
950+
"type": "object",
951+
"required": [
952+
"id",
953+
"created",
954+
"choices",
955+
"model",
956+
"system_fingerprint"
957+
],
958+
"properties": {
959+
"choices": {
960+
"type": "array",
961+
"items": {
962+
"$ref": "#/components/schemas/CompletionComplete"
963+
}
964+
},
965+
"created": {
966+
"type": "integer",
967+
"format": "int64",
968+
"minimum": 0
969+
},
970+
"id": {
971+
"type": "string"
972+
},
973+
"model": {
974+
"type": "string"
975+
},
976+
"system_fingerprint": {
977+
"type": "string"
978+
}
979+
}
980+
},
949981
"CompatGenerateRequest": {
950982
"type": "object",
951983
"required": [
@@ -965,6 +997,55 @@
965997
}
966998
}
967999
},
1000+
"Completion": {
1001+
"oneOf": [
1002+
{
1003+
"allOf": [
1004+
{
1005+
"$ref": "#/components/schemas/Chunk"
1006+
},
1007+
{
1008+
"type": "object",
1009+
"required": [
1010+
"object"
1011+
],
1012+
"properties": {
1013+
"object": {
1014+
"type": "string",
1015+
"enum": [
1016+
"text_completion"
1017+
]
1018+
}
1019+
}
1020+
}
1021+
]
1022+
},
1023+
{
1024+
"allOf": [
1025+
{
1026+
"$ref": "#/components/schemas/CompletionFinal"
1027+
},
1028+
{
1029+
"type": "object",
1030+
"required": [
1031+
"object"
1032+
],
1033+
"properties": {
1034+
"object": {
1035+
"type": "string",
1036+
"enum": [
1037+
"text_completion"
1038+
]
1039+
}
1040+
}
1041+
}
1042+
]
1043+
}
1044+
],
1045+
"discriminator": {
1046+
"propertyName": "object"
1047+
}
1048+
},
9681049
"CompletionComplete": {
9691050
"type": "object",
9701051
"required": [
@@ -994,14 +1075,15 @@
9941075
}
9951076
}
9961077
},
997-
"CompletionCompleteChunk": {
1078+
"CompletionFinal": {
9981079
"type": "object",
9991080
"required": [
10001081
"id",
10011082
"created",
1002-
"choices",
10031083
"model",
1004-
"system_fingerprint"
1084+
"system_fingerprint",
1085+
"choices",
1086+
"usage"
10051087
],
10061088
"properties": {
10071089
"choices": {
@@ -1013,16 +1095,21 @@
10131095
"created": {
10141096
"type": "integer",
10151097
"format": "int64",
1098+
"example": "1706270835",
10161099
"minimum": 0
10171100
},
10181101
"id": {
10191102
"type": "string"
10201103
},
10211104
"model": {
1022-
"type": "string"
1105+
"type": "string",
1106+
"example": "mistralai/Mistral-7B-Instruct-v0.2"
10231107
},
10241108
"system_fingerprint": {
10251109
"type": "string"
1110+
},
1111+
"usage": {
1112+
"$ref": "#/components/schemas/Usage"
10261113
}
10271114
}
10281115
},
@@ -1647,6 +1734,12 @@
16471734
}
16481735
}
16491736
},
1737+
"Prompt": {
1738+
"type": "array",
1739+
"items": {
1740+
"type": "string"
1741+
}
1742+
},
16501743
"SimpleToken": {
16511744
"type": "object",
16521745
"required": [

router/src/lib.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,17 @@ pub struct CompletionRequest {
433433
pub stop: Option<Vec<String>>,
434434
}
435435

436+
#[derive(Clone, Serialize, ToSchema)]
437+
#[serde(tag = "object")]
438+
enum Completion {
439+
#[serde(rename = "text_completion")]
440+
Chunk(Chunk),
441+
#[serde(rename = "text_completion")]
442+
Final(CompletionFinal),
443+
}
444+
436445
#[derive(Clone, Deserialize, Serialize, ToSchema, Default)]
437-
pub(crate) struct Completion {
446+
pub(crate) struct CompletionFinal {
438447
pub id: String,
439448
#[schema(example = "1706270835")]
440449
pub created: u64,
@@ -453,6 +462,15 @@ pub(crate) struct CompletionComplete {
453462
pub finish_reason: String,
454463
}
455464

465+
#[derive(Clone, Deserialize, Serialize, ToSchema)]
466+
pub(crate) struct Chunk {
467+
pub id: String,
468+
pub created: u64,
469+
pub choices: Vec<CompletionComplete>,
470+
pub model: String,
471+
pub system_fingerprint: String,
472+
}
473+
456474
#[derive(Clone, Deserialize, Serialize, ToSchema)]
457475
pub(crate) struct ChatCompletion {
458476
pub id: String,
@@ -614,15 +632,6 @@ impl ChatCompletion {
614632
}
615633
}
616634
}
617-
#[derive(Clone, Deserialize, Serialize, ToSchema)]
618-
pub(crate) struct CompletionCompleteChunk {
619-
pub id: String,
620-
pub created: u64,
621-
pub choices: Vec<CompletionComplete>,
622-
pub model: String,
623-
pub system_fingerprint: String,
624-
}
625-
626635
#[derive(Clone, Serialize, ToSchema)]
627636
pub(crate) struct ChatCompletionChunk {
628637
pub id: String,

router/src/server.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use crate::{
1919
use crate::{
2020
ChatCompletion, ChatCompletionChoice, ChatCompletionChunk, ChatCompletionComplete,
2121
ChatCompletionDelta, ChatCompletionLogprob, ChatCompletionLogprobs, ChatCompletionTopLogprob,
22-
ChatRequest, CompatGenerateRequest, Completion, CompletionComplete, CompletionCompleteChunk,
23-
CompletionRequest, CompletionType, DeltaToolCall, Function, Tool, VertexRequest,
22+
ChatRequest, Chunk, CompatGenerateRequest, Completion, CompletionComplete, CompletionFinal,
23+
CompletionRequest, CompletionType, DeltaToolCall, Function, Prompt, Tool, VertexRequest,
2424
VertexResponse,
2525
};
2626
use crate::{FunctionDefinition, HubPreprocessorConfig, ToolCall, ToolType};
@@ -706,7 +706,7 @@ async fn completions(
706706
.as_secs();
707707

708708
event
709-
.json_data(CompletionCompleteChunk {
709+
.json_data(Completion::Chunk(Chunk {
710710
id: "".to_string(),
711711
created: current_time,
712712

@@ -719,7 +719,7 @@ async fn completions(
719719

720720
model: model_id.clone(),
721721
system_fingerprint: system_fingerprint.clone(),
722-
})
722+
}))
723723
.unwrap_or_else(|_e| Event::default())
724724
};
725725

@@ -932,7 +932,7 @@ async fn completions(
932932
.collect::<Result<Vec<_>, _>>()
933933
.map_err(|(status, Json(err))| (status, Json(err)))?;
934934

935-
let response = Completion {
935+
let response = Completion::Final(CompletionFinal {
936936
id: "".to_string(),
937937
created: current_time,
938938
model: info.model_id.clone(),
@@ -947,7 +947,7 @@ async fn completions(
947947
completion_tokens,
948948
total_tokens,
949949
},
950-
};
950+
});
951951

952952
// headers similar to `generate` but aggregated
953953
let mut headers = HeaderMap::new();
@@ -1466,7 +1466,10 @@ pub async fn run(
14661466
ChatCompletion,
14671467
CompletionRequest,
14681468
CompletionComplete,
1469-
CompletionCompleteChunk,
1469+
Chunk,
1470+
Completion,
1471+
CompletionFinal,
1472+
Prompt,
14701473
GenerateParameters,
14711474
PrefillToken,
14721475
Token,

0 commit comments

Comments
 (0)