2023-11-06 00:38:29 -05:00
import json
import time
2023-12-14 22:22:43 -05:00
from typing import Dict , List
2023-11-06 00:38:29 -05:00
from pydantic import BaseModel , Field
class GenerationOptions ( BaseModel ) :
2023-11-07 11:23:17 -05:00
preset : str | None = Field ( default = None , description = " The name of a file under text-generation-webui/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. " )
2023-11-06 00:38:29 -05:00
min_p : float = 0
2024-01-07 15:03:47 -05:00
dynamic_temperature : bool = False
2024-01-08 21:28:35 -05:00
dynatemp_low : float = 1
dynatemp_high : float = 1
dynatemp_exponent : float = 1
2024-02-03 22:20:02 -05:00
smoothing_factor : float = 0
2024-03-03 11:22:21 -05:00
smoothing_curve : float = 1
2023-11-06 00:38:29 -05:00
top_k : int = 0
repetition_penalty : float = 1
2023-12-07 18:04:52 -05:00
repetition_penalty_range : int = 1024
2023-11-06 00:38:29 -05:00
typical_p : float = 1
tfs : float = 1
top_a : float = 0
epsilon_cutoff : float = 0
eta_cutoff : float = 0
guidance_scale : float = 1
negative_prompt : str = ' '
penalty_alpha : float = 0
mirostat_mode : int = 0
mirostat_tau : float = 5
mirostat_eta : float = 0.1
2023-11-06 10:11:49 -05:00
temperature_last : bool = False
2023-11-06 00:38:29 -05:00
do_sample : bool = True
seed : int = - 1
encoder_repetition_penalty : float = 1
no_repeat_ngram_size : int = 0
2024-05-19 22:53:47 -04:00
dry_multiplier : float = 0
dry_base : float = 1.75
dry_allowed_length : int = 2
dry_sequence_breakers : str = ' " \\ n " , " : " , " \\ " " , " * " '
2023-11-06 00:38:29 -05:00
truncation_length : int = 0
max_tokens_second : int = 0
2024-01-17 15:09:36 -05:00
prompt_lookup_num_tokens : int = 0
2023-11-06 00:38:29 -05:00
custom_token_bans : str = " "
2024-02-06 09:20:10 -05:00
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 \" ]. " )
2023-11-06 00:38:29 -05:00
auto_max_new_tokens : bool = False
ban_eos_token : bool = False
add_bos_token : bool = True
skip_special_tokens : bool = True
grammar_string : str = " "
2023-11-06 12:55:36 -05:00
class CompletionRequestParams ( BaseModel ) :
2023-11-10 09:47:00 -05:00
model : str | None = Field ( default = None , description = " Unused parameter. To change the model, use the /v1/internal/model/load endpoint. " )
2023-11-06 00:38:29 -05:00
prompt : str | List [ str ]
2023-11-07 11:56:09 -05:00
best_of : int | None = Field ( default = 1 , description = " Unused parameter. " )
2023-11-06 00:38:29 -05:00
echo : bool | None = False
frequency_penalty : float | None = 0
logit_bias : dict | None = None
logprobs : int | None = None
max_tokens : int | None = 16
2023-11-07 11:56:09 -05:00
n : int | None = Field ( default = 1 , description = " Unused parameter. " )
2023-11-08 09:23:51 -05:00
presence_penalty : float | None = 0
2023-11-06 00:38:29 -05: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 11:43:45 -05:00
user : str | None = Field ( default = None , description = " Unused parameter. " )
2023-11-06 00:38:29 -05:00
2023-11-06 12:55:36 -05:00
class CompletionRequest ( GenerationOptions , CompletionRequestParams ) :
pass
2023-11-06 00:38:29 -05: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 12:55:36 -05:00
class ChatCompletionRequestParams ( BaseModel ) :
2023-11-06 00:38:29 -05:00
messages : List [ dict ]
2023-11-10 09:47:00 -05:00
model : str | None = Field ( default = None , description = " Unused parameter. To change the model, use the /v1/internal/model/load endpoint. " )
2023-11-06 00:38:29 -05:00
frequency_penalty : float | None = 0
2023-11-07 11:56:09 -05:00
function_call : str | dict | None = Field ( default = None , description = " Unused parameter. " )
functions : List [ dict ] | None = Field ( default = None , description = " Unused parameter. " )
2023-11-06 00:38:29 -05:00
logit_bias : dict | None = None
max_tokens : int | None = None
2023-11-07 11:56:09 -05:00
n : int | None = Field ( default = 1 , description = " Unused parameter. " )
2023-11-08 09:23:51 -05:00
presence_penalty : float | None = 0
2023-11-06 00:38:29 -05:00
stop : str | List [ str ] | None = None
stream : bool | None = False
temperature : float | None = 1
top_p : float | None = 1
2023-11-07 11:56:09 -05:00
user : str | None = Field ( default = None , description = " Unused parameter. " )
2023-11-06 00:38:29 -05:00
mode : str = Field ( default = ' instruct ' , description = " Valid options: instruct, chat, chat-instruct. " )
2023-12-12 15:23:14 -05:00
instruction_template : str | None = Field ( default = None , description = " An instruction template defined under text-generation-webui/instruction-templates. If not set, the correct template will be automatically obtained from the model metadata. " )
instruction_template_str : str | None = Field ( default = None , description = " A Jinja2 instruction template. If set, will take precedence over everything else. " )
2023-11-06 00:38:29 -05:00
character : str | None = Field ( default = None , description = " A character defined under text-generation-webui/characters. If not set, the default \" Assistant \" character will be used. " )
2024-01-14 11:30:36 -05:00
bot_name : str | None = Field ( default = None , description = " Overwrites the value set by character field. " , alias = " name2 " )
2024-01-09 22:06:11 -05: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-11 22:41:57 -04:00
user_name : str | None = Field ( default = None , description = " Your name (the user). By default, it ' s \" You \" . " , alias = " name1 " )
2024-03-29 13:54:01 -04:00
user_bio : str | None = Field ( default = None , description = " The user description/personality. " )
2023-12-12 15:23:14 -05:00
chat_template_str : str | None = Field ( default = None , description = " Jinja2 template for chat. " )
2023-11-06 00:38:29 -05:00
chat_instruct_command : str | None = None
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 12:55:36 -05:00
class ChatCompletionRequest ( GenerationOptions , ChatCompletionRequestParams ) :
pass
2023-11-06 00:38:29 -05:00
class ChatCompletionResponse ( BaseModel ) :
id : str
choices : List [ dict ]
created : int = int ( time . time ( ) )
model : str
object : str = " chat.completion "
usage : dict
2024-04-18 23:24:46 -04:00
class ChatPromptResponse ( BaseModel ) :
prompt : str
2023-11-18 22:35:22 -05:00
class EmbeddingsRequest ( BaseModel ) :
2023-12-14 22:26:16 -05:00
input : str | List [ str ] | List [ int ] | List [ List [ int ] ]
2023-11-18 22:35:22 -05: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-07 22:05:36 -05:00
class EncodeRequest ( BaseModel ) :
text : str
2023-11-18 21:19:31 -05:00
class EncodeResponse ( BaseModel ) :
2023-11-07 22:05:36 -05:00
tokens : List [ int ]
2023-11-18 21:19:31 -05:00
length : int
2023-11-07 22:05:36 -05:00
2023-11-18 21:19:31 -05:00
class DecodeRequest ( BaseModel ) :
2023-11-07 22:05:36 -05:00
tokens : List [ int ]
class DecodeResponse ( BaseModel ) :
text : str
class TokenCountResponse ( BaseModel ) :
length : int
2023-11-18 21:19:31 -05:00
class LogitsRequestParams ( BaseModel ) :
prompt : str
use_samplers : bool = False
2023-12-14 22:22:43 -05:00
top_logits : int | None = 50
2023-11-18 21:19:31 -05:00
frequency_penalty : float | None = 0
max_tokens : int | None = 16
presence_penalty : float | None = 0
temperature : float | None = 1
top_p : float | None = 1
class LogitsRequest ( GenerationOptions , LogitsRequestParams ) :
pass
class LogitsResponse ( BaseModel ) :
2023-12-14 22:22:43 -05:00
logits : Dict [ str , float ]
2023-11-18 21:19:31 -05:00
2023-11-07 21:59:02 -05:00
class ModelInfoResponse ( BaseModel ) :
model_name : str
lora_names : List [ str ]
2023-11-18 22:35:22 -05:00
class ModelListResponse ( BaseModel ) :
model_names : List [ str ]
2023-11-07 23:58:06 -05:00
class LoadModelRequest ( BaseModel ) :
model_name : str
args : dict | None = None
settings : dict | None = None
2023-11-18 22:35:22 -05:00
class LoraListResponse ( BaseModel ) :
lora_names : List [ str ]
2023-11-10 10:34:27 -05:00
2023-11-18 22:35:22 -05:00
class LoadLorasRequest ( BaseModel ) :
lora_names : List [ str ]
2023-11-10 10:34:27 -05:00
2023-11-06 00:38:29 -05:00
def to_json ( obj ) :
return json . dumps ( obj . __dict__ , indent = 4 )
def to_dict ( obj ) :
return obj . __dict__