2023-11-06 06:38:29 +01:00
import json
import time
2025-05-08 17:30:27 +02:00
from typing import Dict , List , Optional
2023-11-06 06:38:29 +01:00
2025-08-11 21:39:18 +02:00
from pydantic import BaseModel , Field , model_validator , validator
2023-11-06 06:38:29 +01:00
class GenerationOptions ( BaseModel ) :
2025-04-26 13:56:54 +02:00
preset : str | None = Field ( default = None , description = " The name of a file under text-generation-webui/user_data/presets (without the .yaml extension). The sampling parameters that get overwritten by this option are the keys in the default_preset() function in modules/presets.py. " )
2024-01-09 03:28:35 +01:00
dynatemp_low : float = 1
dynatemp_high : float = 1
dynatemp_exponent : float = 1
2024-02-04 04:20:02 +01:00
smoothing_factor : float = 0
2024-03-03 17:22:21 +01:00
smoothing_curve : float = 1
2025-01-10 22:04:32 +01:00
min_p : float = 0
2023-11-06 06:38:29 +01:00
top_k : int = 0
typical_p : float = 1
2025-01-10 22:04:32 +01:00
xtc_threshold : float = 0.1
xtc_probability : float = 0
2023-11-06 06:38:29 +01:00
epsilon_cutoff : float = 0
eta_cutoff : float = 0
2025-01-10 22:04:32 +01:00
tfs : float = 1
top_a : float = 0
2025-03-14 20:45:11 +01:00
top_n_sigma : float = 0
2025-01-10 22:04:32 +01:00
dry_multiplier : float = 0
dry_allowed_length : int = 2
dry_base : float = 1.75
repetition_penalty : float = 1
encoder_repetition_penalty : float = 1
no_repeat_ngram_size : int = 0
repetition_penalty_range : int = 1024
2023-11-06 06:38:29 +01:00
penalty_alpha : float = 0
2025-01-10 22:04:32 +01:00
guidance_scale : float = 1
2023-11-06 06:38:29 +01:00
mirostat_mode : int = 0
mirostat_tau : float = 5
mirostat_eta : float = 0.1
2024-01-17 21:09:36 +01:00
prompt_lookup_num_tokens : int = 0
2025-01-10 22:04:32 +01:00
max_tokens_second : int = 0
do_sample : bool = True
dynamic_temperature : bool = False
temperature_last : bool = False
2023-11-06 06:38:29 +01:00
auto_max_new_tokens : bool = False
ban_eos_token : bool = False
add_bos_token : bool = True
2025-04-29 07:37:01 +02:00
enable_thinking : bool = True
2025-08-06 00:19:11 +02:00
reasoning_effort : str = " medium "
2023-11-06 06:38:29 +01:00
skip_special_tokens : bool = True
2025-01-10 22:04:32 +01:00
static_cache : bool = False
truncation_length : int = 0
seed : int = - 1
sampler_priority : List [ str ] | str | None = Field ( default = None , description = " List of samplers where the first items will appear first in the stack. Example: [ \" top_k \" , \" temperature \" , \" top_p \" ]. " )
custom_token_bans : str = " "
negative_prompt : str = ' '
dry_sequence_breakers : str = ' " \\ n " , " : " , " \\ " " , " * " '
2023-11-06 06:38:29 +01:00
grammar_string : str = " "
2025-05-08 17:30:27 +02:00
class ToolDefinition ( BaseModel ) :
function : ' ToolFunction '
type : str
class ToolFunction ( BaseModel ) :
description : str
name : str
parameters : ' ToolParameters '
class ToolParameters ( BaseModel ) :
properties : Optional [ Dict [ str , ' ToolProperty ' ] ] = None
required : Optional [ list [ str ] ] = None
type : str
description : Optional [ str ] = None
class ToolProperty ( BaseModel ) :
description : Optional [ str ] = None
type : Optional [ str ] = None # we are faced with definitions like anyOf, e.g. {'type': 'function', 'function': {'name': 'git_create_branch', 'description': 'Creates a new branch from an optional base branch', 'parameters': {'type': 'object', 'properties': {'repo_path': {'title': 'Repo Path', 'type': 'string'}, 'branch_name': {'title': 'Branch Name', 'type': 'string'}, 'base_branch': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'title': 'Base Branch'}}, 'required': ['repo_path', 'branch_name'], 'title': 'GitCreateBranch'}}}
class FunctionCall ( BaseModel ) :
name : str
arguments : Optional [ str ] = None
parameters : Optional [ str ] = None
@validator ( ' arguments ' , allow_reuse = True )
def checkPropertyArgsOrParams ( cls , v , values , * * kwargs ) :
if not v and not values . get ( ' parameters ' ) :
raise ValueError ( " At least one of ' arguments ' or ' parameters ' must be provided as property in FunctionCall type " )
return v
class ToolCall ( BaseModel ) :
id : str
index : int
type : str
function : FunctionCall
2023-11-06 18:55:36 +01:00
class CompletionRequestParams ( BaseModel ) :
2023-11-10 15:47:00 +01:00
model : str | None = Field ( default = None , description = " Unused parameter. To change the model, use the /v1/internal/model/load endpoint. " )
2025-08-09 04:31:16 +02:00
prompt : str | List [ str ] | None = Field ( default = None , description = " Text prompt for completion. Can also use ' messages ' format for multimodal. " )
messages : List [ dict ] | None = Field ( default = None , description = " OpenAI messages format for multimodal support. Alternative to ' prompt ' . " )
2023-11-07 17:56:09 +01:00
best_of : int | None = Field ( default = 1 , description = " Unused parameter. " )
2023-11-06 06:38:29 +01:00
echo : bool | None = False
frequency_penalty : float | None = 0
logit_bias : dict | None = None
logprobs : int | None = None
2025-08-10 16:21:55 +02:00
max_tokens : int | None = 512
2023-11-07 17:56:09 +01:00
n : int | None = Field ( default = 1 , description = " Unused parameter. " )
2023-11-08 15:23:51 +01:00
presence_penalty : float | None = 0
2023-11-06 06:38:29 +01:00
stop : str | List [ str ] | None = None
stream : bool | None = False
suffix : str | None = None
temperature : float | None = 1
top_p : float | None = 1
2023-11-07 17:43:45 +01:00
user : str | None = Field ( default = None , description = " Unused parameter. " )
2023-11-06 06:38:29 +01:00
2025-08-11 21:39:18 +02:00
@model_validator ( mode = ' after ' )
def validate_prompt_or_messages ( self ) :
if self . prompt is None and self . messages is None :
raise ValueError ( " Either ' prompt ' or ' messages ' must be provided " )
return self
2025-08-09 04:31:16 +02:00
2023-11-06 06:38:29 +01:00
2023-11-06 18:55:36 +01:00
class CompletionRequest ( GenerationOptions , CompletionRequestParams ) :
pass
2023-11-06 06:38:29 +01:00
class CompletionResponse ( BaseModel ) :
id : str
choices : List [ dict ]
created : int = int ( time . time ( ) )
model : str
object : str = " text_completion "
usage : dict
2023-11-06 18:55:36 +01:00
class ChatCompletionRequestParams ( BaseModel ) :
2023-11-06 06:38:29 +01:00
messages : List [ dict ]
2023-11-10 15:47:00 +01:00
model : str | None = Field ( default = None , description = " Unused parameter. To change the model, use the /v1/internal/model/load endpoint. " )
2023-11-06 06:38:29 +01:00
frequency_penalty : float | None = 0
2023-11-07 17:56:09 +01:00
function_call : str | dict | None = Field ( default = None , description = " Unused parameter. " )
functions : List [ dict ] | None = Field ( default = None , description = " Unused parameter. " )
2025-05-08 17:30:27 +02:00
tools : List [ dict ] | None = Field ( default = None , description = " Tools signatures passed via MCP. " )
2023-11-06 06:38:29 +01:00
logit_bias : dict | None = None
max_tokens : int | None = None
2023-11-07 17:56:09 +01:00
n : int | None = Field ( default = 1 , description = " Unused parameter. " )
2023-11-08 15:23:51 +01:00
presence_penalty : float | None = 0
2023-11-06 06:38:29 +01:00
stop : str | List [ str ] | None = None
stream : bool | None = False
temperature : float | None = 1
top_p : float | None = 1
2023-11-07 17:56:09 +01:00
user : str | None = Field ( default = None , description = " Unused parameter. " )
2023-11-06 06:38:29 +01:00
mode : str = Field ( default = ' instruct ' , description = " Valid options: instruct, chat, chat-instruct. " )
2025-04-26 13:56:54 +02:00
instruction_template : str | None = Field ( default = None , description = " An instruction template defined under text-generation-webui/user_data/instruction-templates. If not set, the correct template will be automatically obtained from the model metadata. " )
2023-12-12 21:23:14 +01:00
instruction_template_str : str | None = Field ( default = None , description = " A Jinja2 instruction template. If set, will take precedence over everything else. " )
2023-11-06 06:38:29 +01:00
2025-04-26 13:56:54 +02:00
character : str | None = Field ( default = None , description = " A character defined under text-generation-webui/user_data/characters. If not set, the default \" Assistant \" character will be used. " )
2024-01-14 17:30:36 +01:00
bot_name : str | None = Field ( default = None , description = " Overwrites the value set by character field. " , alias = " name2 " )
2024-01-10 04:06:11 +01:00
context : str | None = Field ( default = None , description = " Overwrites the value set by character field. " )
greeting : str | None = Field ( default = None , description = " Overwrites the value set by character field. " )
2024-03-12 03:41:57 +01:00
user_name : str | None = Field ( default = None , description = " Your name (the user). By default, it ' s \" You \" . " , alias = " name1 " )
2024-03-29 18:54:01 +01:00
user_bio : str | None = Field ( default = None , description = " The user description/personality. " )
2023-12-12 21:23:14 +01:00
chat_template_str : str | None = Field ( default = None , description = " Jinja2 template for chat. " )
2023-11-06 06:38:29 +01:00
2025-06-17 16:46:58 +02:00
chat_instruct_command : str | None = " Continue the chat dialogue below. Write a single reply for the character \" <|character|> \" . \n \n <|prompt|> "
2023-11-06 06:38:29 +01:00
continue_ : bool = Field ( default = False , description = " Makes the last bot message in the history be continued instead of starting a new message. " )
2023-11-06 18:55:36 +01:00
class ChatCompletionRequest ( GenerationOptions , ChatCompletionRequestParams ) :
pass
2023-11-06 06:38:29 +01:00
class ChatCompletionResponse ( BaseModel ) :
id : str
choices : List [ dict ]
created : int = int ( time . time ( ) )
model : str
object : str = " chat.completion "
usage : dict
2024-04-19 05:24:46 +02:00
class ChatPromptResponse ( BaseModel ) :
prompt : str
2023-11-19 04:35:22 +01:00
class EmbeddingsRequest ( BaseModel ) :
2023-12-15 04:26:16 +01:00
input : str | List [ str ] | List [ int ] | List [ List [ int ] ]
2023-11-19 04:35:22 +01:00
model : str | None = Field ( default = None , description = " Unused parameter. To change the model, set the OPENEDAI_EMBEDDING_MODEL and OPENEDAI_EMBEDDING_DEVICE environment variables before starting the server. " )
encoding_format : str = Field ( default = " float " , description = " Can be float or base64. " )
user : str | None = Field ( default = None , description = " Unused parameter. " )
class EmbeddingsResponse ( BaseModel ) :
index : int
embedding : List [ float ]
object : str = " embedding "
2023-11-08 04:05:36 +01:00
class EncodeRequest ( BaseModel ) :
text : str
2023-11-19 03:19:31 +01:00
class EncodeResponse ( BaseModel ) :
2023-11-08 04:05:36 +01:00
tokens : List [ int ]
2023-11-19 03:19:31 +01:00
length : int
2023-11-08 04:05:36 +01:00
2023-11-19 03:19:31 +01:00
class DecodeRequest ( BaseModel ) :
2023-11-08 04:05:36 +01:00
tokens : List [ int ]
class DecodeResponse ( BaseModel ) :
text : str
class TokenCountResponse ( BaseModel ) :
length : int
2023-11-19 03:19:31 +01:00
class LogitsRequestParams ( BaseModel ) :
prompt : str
use_samplers : bool = False
2023-12-15 04:22:43 +01:00
top_logits : int | None = 50
2023-11-19 03:19:31 +01:00
frequency_penalty : float | None = 0
2025-08-10 16:21:55 +02:00
max_tokens : int | None = 512
2023-11-19 03:19:31 +01:00
presence_penalty : float | None = 0
temperature : float | None = 1
top_p : float | None = 1
class LogitsRequest ( GenerationOptions , LogitsRequestParams ) :
pass
class LogitsResponse ( BaseModel ) :
2023-12-15 04:22:43 +01:00
logits : Dict [ str , float ]
2023-11-19 03:19:31 +01:00
2023-11-08 03:59:02 +01:00
class ModelInfoResponse ( BaseModel ) :
model_name : str
lora_names : List [ str ]
2023-11-19 04:35:22 +01:00
class ModelListResponse ( BaseModel ) :
model_names : List [ str ]
2023-11-08 05:58:06 +01:00
class LoadModelRequest ( BaseModel ) :
model_name : str
args : dict | None = None
settings : dict | None = None
2023-11-19 04:35:22 +01:00
class LoraListResponse ( BaseModel ) :
lora_names : List [ str ]
2023-11-10 16:34:27 +01:00
2023-11-19 04:35:22 +01:00
class LoadLorasRequest ( BaseModel ) :
lora_names : List [ str ]
2023-11-10 16:34:27 +01:00
2025-12-05 02:34:17 +01:00
class ImageGenerationRequest ( BaseModel ) :
2025-12-04 19:23:00 +01:00
""" Image-specific parameters for generation. """
2025-12-03 20:50:35 +01:00
prompt : str
negative_prompt : str = " "
size : str = Field ( default = " 1024x1024 " , description = " ' WIDTHxHEIGHT ' " )
steps : int = Field ( default = 9 , ge = 1 )
cfg_scale : float = Field ( default = 0.0 , ge = 0.0 )
2025-12-04 19:23:00 +01:00
image_seed : int = Field ( default = - 1 , description = " -1 for random " )
2025-12-03 20:50:35 +01:00
batch_size : int | None = Field ( default = None , ge = 1 , description = " Parallel batch size (VRAM heavy) " )
n : int = Field ( default = 1 , ge = 1 , description = " Alias for batch_size (OpenAI compatibility) " )
batch_count : int = Field ( default = 1 , ge = 1 , description = " Sequential batch count " )
# OpenAI compatibility (unused)
model : str | None = None
response_format : str = " b64_json "
user : str | None = None
@model_validator ( mode = ' after ' )
def resolve_batch_size ( self ) :
if self . batch_size is None :
self . batch_size = self . n
return self
def get_width_height ( self ) - > tuple [ int , int ] :
try :
parts = self . size . lower ( ) . split ( ' x ' )
return int ( parts [ 0 ] ) , int ( parts [ 1 ] )
except ( ValueError , IndexError ) :
return 1024 , 1024
class ImageGenerationResponse ( BaseModel ) :
created : int = int ( time . time ( ) )
data : List [ dict ]
2023-11-06 06:38:29 +01:00
def to_json ( obj ) :
return json . dumps ( obj . __dict__ , indent = 4 )
def to_dict ( obj ) :
return obj . __dict__