|
@@ -63,6 +63,26 @@ class ResponseFormat(BaseModel):
|
|
|
type: str = Literal["text", "json_object"]
|
|
|
|
|
|
|
|
|
+class FunctionDefinition(BaseModel):
|
|
|
+ name: str
|
|
|
+ description: Optional[str] = None
|
|
|
+ parameters: Optional[Dict[str, Any]] = None
|
|
|
+
|
|
|
+
|
|
|
+class ChatCompletionToolsParam(BaseModel):
|
|
|
+ type: Literal["function"] = "function"
|
|
|
+ function: FunctionDefinition
|
|
|
+
|
|
|
+
|
|
|
+class ChatCompletionNamedFunction(BaseModel):
|
|
|
+ name: str
|
|
|
+
|
|
|
+
|
|
|
+class ChatCompletionNamedToolChoiceParam(BaseModel):
|
|
|
+ function: ChatCompletionNamedFunction
|
|
|
+ type: Literal["function"] = "function"
|
|
|
+
|
|
|
+
|
|
|
class ChatCompletionRequest(BaseModel):
|
|
|
model: str
|
|
|
# support list type in messages.content
|
|
@@ -88,6 +108,9 @@ class ChatCompletionRequest(BaseModel):
|
|
|
frequency_penalty: Optional[float] = 0.0
|
|
|
repetition_penalty: Optional[float] = 1.0
|
|
|
logit_bias: Optional[Dict[str, float]] = None
|
|
|
+ tools: Optional[List[ChatCompletionToolsParam]] = None
|
|
|
+ tool_choice: Optional[Union[Literal["none"],
|
|
|
+ ChatCompletionNamedToolChoiceParam]] = "none"
|
|
|
user: Optional[str] = None
|
|
|
best_of: Optional[int] = None
|
|
|
top_k: Optional[int] = -1
|
|
@@ -194,6 +217,22 @@ class ChatCompletionRequest(BaseModel):
|
|
|
raise ValueError(
|
|
|
"You can only use one kind of guided decoding "
|
|
|
"('guided_json', 'guided_regex' or 'guided_choice').")
|
|
|
+ # you can only either use guided decoding or tools, not both
|
|
|
+ if guide_count > 1 and "tool_choice" in data and data[
|
|
|
+ "tool_choice"] != "none":
|
|
|
+ raise ValueError(
|
|
|
+ "You can only either use guided decoding or tools, not both.")
|
|
|
+ return data
|
|
|
+
|
|
|
+ @model_validator(mode="before")
|
|
|
+ @classmethod
|
|
|
+ def check_tool_choice(cls, data):
|
|
|
+ if "tool_choice" in data and data["tool_choice"] != "none":
|
|
|
+ if not isinstance(data["tool_choice"], dict):
|
|
|
+ raise ValueError("Currently only named tools are supported.")
|
|
|
+ if "tools" not in data or data["tools"] is None:
|
|
|
+ raise ValueError(
|
|
|
+ "When using `tool_choice`, `tools` must be set.")
|
|
|
return data
|
|
|
|
|
|
|
|
@@ -407,6 +446,17 @@ class EmbeddingResponse(BaseModel):
|
|
|
usage: UsageInfo
|
|
|
|
|
|
|
|
|
+class FunctionCall(BaseModel):
|
|
|
+ name: str
|
|
|
+ arguments: str
|
|
|
+
|
|
|
+
|
|
|
+class ToolCall(BaseModel):
|
|
|
+ id: str = Field(default_factory=lambda: f"chatcmpl-tool-{random_uuid()}")
|
|
|
+ type: Literal["function"] = "function"
|
|
|
+ function: FunctionCall
|
|
|
+
|
|
|
+
|
|
|
class ChatMessage(BaseModel):
|
|
|
role: str
|
|
|
content: str
|
|
@@ -422,7 +472,7 @@ class ChatCompletionResponseChoice(BaseModel):
|
|
|
|
|
|
class ChatCompletionResponse(BaseModel):
|
|
|
id: str = Field(default_factory=lambda: f"chatcmpl-{random_uuid()}")
|
|
|
- object: str = "chat.completion"
|
|
|
+ object: Literal["chat.completion"] = "chat.completion"
|
|
|
created: int = Field(default_factory=lambda: int(time.time()))
|
|
|
model: str
|
|
|
choices: List[ChatCompletionResponseChoice]
|
|
@@ -432,6 +482,7 @@ class ChatCompletionResponse(BaseModel):
|
|
|
class DeltaMessage(BaseModel):
|
|
|
role: Optional[str] = None
|
|
|
content: Optional[str] = None
|
|
|
+ tool_calls: List[ToolCall] = Field(default_factory=list)
|
|
|
|
|
|
|
|
|
class ChatCompletionResponseStreamChoice(BaseModel):
|
|
@@ -444,7 +495,7 @@ class ChatCompletionResponseStreamChoice(BaseModel):
|
|
|
|
|
|
class ChatCompletionStreamResponse(BaseModel):
|
|
|
id: str = Field(default_factory=lambda: f"chatcmpl-{random_uuid()}")
|
|
|
- object: str = "chat.completion.chunk"
|
|
|
+ object: Literal["chat.completion.chunk"] = "chat.completion.chunk"
|
|
|
created: int = Field(default_factory=lambda: int(time.time()))
|
|
|
model: str
|
|
|
choices: List[ChatCompletionResponseStreamChoice]
|