LMOutput
LMOutput
dataclass
¶
Source code in flexeval/core/language_model/base.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
raw_text
class-attribute
instance-attribute
¶
raw_text: str | None = None
The raw output text of the language model before post-processing.
finish_reason
class-attribute
instance-attribute
¶
finish_reason: str | None = None
The reason why the generation is finished. Typically, - 'stop': A stop sequence is generated. - 'length': The maximum length is reached.
__init__ ¶
__init__(
text: str,
raw_text: str | None = None,
finish_reason: str | None = None,
) -> None
LanguageModel ¶
LanguageModel is what you want to evaluate with this library.
It can generate text based on the input text, response to chat messages, and compute log probabilities.
Parameters:
-
string_processors
(StringProcessor | list[StringProcessor] | None
, default:None
) –A single or a list of StringProcessor objects to process the model's output.
Source code in flexeval/core/language_model/base.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
__init__ ¶
__init__(
string_processors: StringProcessor
| list[StringProcessor]
| None = None,
) -> None
Source code in flexeval/core/language_model/base.py
38 39 40 41 42 43 44 |
|
complete_text ¶
complete_text(
text: str | list[str],
stop_sequences: str | list[str] | None = None,
max_new_tokens: int | None = None,
**kwargs,
) -> LMOutput | list[LMOutput]
A wrapper for batch_complete_text
that accepts a single text or a list of texts.
This is a convenient method for end-users.
To implement generation logic, you should override batch_complete_text
method.
Source code in flexeval/core/language_model/base.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
generate_chat_response ¶
generate_chat_response(
chat_messages: list[dict[str, Any]]
| list[list[dict[str, Any]]],
**kwargs,
) -> LMOutput | list[LMOutput]
A wrapper for batch_generate_chat_response
that accepts a single chat message or a list of chat messages.
This is a convenient method for end-users.
To implement generation logic, you should override batch_generate_chat_response
method.
Source code in flexeval/core/language_model/base.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
compute_log_probs ¶
compute_log_probs(
text_list: str | list[str],
prefix_list: list[str] | None = None,
stride: int | None = None,
) -> float | list[float]
A wrapper for batch_compute_log_probs
that accepts a single text or a list of texts.
This is a convenient method for end-users.
To implement computation logic, you should override batch_compute_log_probs
method.
Source code in flexeval/core/language_model/base.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
compute_chat_log_probs ¶
compute_chat_log_probs(
prompt: list[dict[str, Any]]
| list[list[dict[str, Any]]],
response: dict[str, Any] | list[dict[str, Any]],
) -> float | list[float]
A wrapper for batch_compute_chat_log_probs
that accepts a single chat prompt or a list of chat prompts.
This is a convenient method for end-users.
To implement computation logic, you should override batch_compute_chat_log_probs
method.
Source code in flexeval/core/language_model/base.py
199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
HuggingFaceLM ¶
LanguageModel implementation using Hugging Face Transformers.
Parameters:
-
model
(str
) –The model name or path of the Hugging Face model.
-
model_kwargs
(dict[str, Any] | None
, default:None
) –Keyword arguments for the model instantiation by
from_pretrained()
. -
tokenizer
(str | None
, default:None
) –The tokenizer name or path of the Hugging Face tokenizer.
-
tokenizer_kwargs
(dict[str, Any] | None
, default:None
) –Keyword arguments for the tokenizer instantiation by `from_pretrained().
-
add_special_tokens
(bool
, default:False
) –Whether to add special tokens to the input. Note that whether BOS or EOS tokens are added depends on the tokenizer.
-
amp_dtype
(Literal['float16', 'bfloat16'] | None
, default:None
) –The dtype for automatic mixed precision.
-
random_seed
(int
, default:42
) –Random seed for the model.
-
load_peft
(bool
, default:False
) –Should be set to True when loading the model from PEFT weights.
-
custom_chat_template
(str | None
, default:None
) –A custom chat template for chatbot models. If specified, this overrides the default chat template of the tokenizer.
-
default_gen_kwargs
(dict[str, Any] | None
, default:None
) –Default generation kwargs to use when calling the API.
-
string_processors
(StringProcessor | list[StringProcessor] | None
, default:None
) –A single or a list of StringProcessor objects to process the model's output.
-
model_limit_tokens
(int | None | Literal['default']
, default:'default'
) –An upper limit on the number of tokens (input + output) the model can handle. If
max_new_tokens
exceeds this limit ingenerate_chat_response()
, it will be capped to this value. If this value is set to less than or equal to the model's capacity and the input exceeds it, an empty string is returned instead of raising an error. If set to “default”, the value will be automatically determined when possible.
Source code in flexeval/core/language_model/hf_lm.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
|
tokenizer
instance-attribute
¶
tokenizer: PreTrainedTokenizer = from_pretrained(
tokenizer, **tokenizer_kwargs
)
__init__ ¶
__init__(
model: str,
model_kwargs: dict[str, Any] | None = None,
tokenizer: str | None = None,
tokenizer_kwargs: dict[str, Any] | None = None,
add_special_tokens: bool = False,
amp_dtype: Literal["float16", "bfloat16"] | None = None,
random_seed: int = 42,
load_peft: bool = False,
custom_chat_template: str | None = None,
default_gen_kwargs: dict[str, Any] | None = None,
string_processors: StringProcessor
| list[StringProcessor]
| None = None,
model_limit_tokens: int
| None
| Literal["default"] = "default",
) -> None
Source code in flexeval/core/language_model/hf_lm.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
__repr__ ¶
__repr__() -> str
Source code in flexeval/core/language_model/hf_lm.py
482 483 |
|
LiteLLMChatAPI ¶
LanguageModel implementation using LiteLLM. Various APIs are available, such as OpenAI, Claude, Gemini, etc. See also: https://docs.litellm.ai/docs/providers
Parameters:
-
model
(str
, default:'openai/gpt-3.5-turbo'
) –The name of the model to use. e.g. 'openai/gpt-3.5-turbo',
-
default_gen_kwargs
(dict[str, Any] | None
, default:None
) –Default generation kwargs to use when calling the API.
-
developer_message
(str | None
, default:None
) –Instructions to the model that are prioritized ahead of user messages. Previously called the system prompt.
-
string_processors
(StringProcessor | list[StringProcessor] | None
, default:None
) –A single or a list of StringProcessor objects to process the model's output.
-
ignore_seed
(bool
, default:False
) –If True, ignore the seed specified in default_gen_kwargs. This is an option for models that do not support seed parameters such as anthropic/claude.
-
model_limit_completion_tokens
(int | None
, default:None
) –An upper limit on the number of tokens the model can generate. For example, if a too-large
max_new_tokens
is given to generate_chat_response(), this value will cap it.
Source code in flexeval/core/language_model/litellm_api.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
empty_response
instance-attribute
¶
empty_response = convert_to_model_response_object(
response_object=to_dict(),
model_response_object=ModelResponse(),
)
__init__ ¶
__init__(
model: str = "openai/gpt-3.5-turbo",
default_gen_kwargs: dict[str, Any] | None = None,
developer_message: str | None = None,
string_processors: StringProcessor
| list[StringProcessor]
| None = None,
ignore_seed: bool = False,
model_limit_completion_tokens: int | None = None,
) -> None
Source code in flexeval/core/language_model/litellm_api.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
__repr__ ¶
__repr__() -> str
Source code in flexeval/core/language_model/litellm_api.py
95 96 |
|
OpenAIChatAPI ¶
LanguageModel implementation using OpenAI's ChatGPT API. Note that this class is inherited by litellm_api.LiteLLMChatAPI, so be careful when making any modifications.
Parameters:
-
model
(str
, default:'gpt-3.5-turbo'
) –The name of the model to use.
-
api_headers
(dict[str, str] | None
, default:None
) –A dictionary of headers to use when making requests to the OpenAI API.
-
default_gen_kwargs
(dict[str, Any] | None
, default:None
) –Default generation kwargs to use when calling the API.
-
developer_message
(str | None
, default:None
) –Instructions to the model that are prioritized ahead of user messages. Previously called the system prompt.
-
string_processors
(StringProcessor | list[StringProcessor] | None
, default:None
) –A single or a list of StringProcessor objects to process the model's output.
-
model_limit_new_tokens
(int | None
, default:None
) –An upper limit on the number of tokens the model can generate. For example, if a too-large
max_new_tokens
is given to generate_chat_response(), this value will cap it.
Source code in flexeval/core/language_model/openai_api.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
__init__ ¶
__init__(
model: str = "gpt-3.5-turbo",
api_headers: dict[str, str] | None = None,
default_gen_kwargs: dict[str, Any] | None = None,
developer_message: str | None = None,
string_processors: StringProcessor
| list[StringProcessor]
| None = None,
model_limit_new_tokens: int | None = None,
) -> None
Source code in flexeval/core/language_model/openai_api.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
__repr__ ¶
__repr__() -> str
Source code in flexeval/core/language_model/openai_api.py
261 262 |
|
OpenAICompletionAPI ¶
LanguageModel implementation using OpenAI's Completion API.
Note that Completion API is a legacy API, with only a few models (such as gpt-3.5-turbo-instruct) supported by OpenAI. This LanguageModel implementation is primarily intended for use with on-premise VLLM servers, as described in the documentation: https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html
Parameters:
-
model
(str
, default:'gpt-3.5-turbo-instruct'
) –The name of the model to use.
-
api_headers
(dict[str, str] | None
, default:None
) –A dictionary of headers to use when making requests to the OpenAI API.
-
default_gen_kwargs
(dict[str, Any] | None
, default:None
) –Default generation kwargs to use when calling the API.
-
string_processors
(StringProcessor | list[StringProcessor] | None
, default:None
) –A single or a list of StringProcessor objects to process the model's output.
Source code in flexeval/core/language_model/openai_api.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|
__init__ ¶
__init__(
model: str = "gpt-3.5-turbo-instruct",
api_headers: dict[str, str] | None = None,
default_gen_kwargs: dict[str, Any] | None = None,
string_processors: StringProcessor
| list[StringProcessor]
| None = None,
) -> None
Source code in flexeval/core/language_model/openai_api.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
|
__repr__ ¶
__repr__() -> str
Source code in flexeval/core/language_model/openai_api.py
391 392 |
|
OpenAIChatBatchAPI ¶
LanguageModel implementation using OpenAI's ChatGPT API for Batch API. NOTE: Batch size should be more than or equal to the size of the given dataset for efficient generation.
Parameters:
-
model
(str
) –The name of the model to use.
-
api_headers
(dict[str, str] | None
, default:None
) –A dictionary of headers to use when making requests to the OpenAI API.
-
polling_interval_seconds
(int
, default:60
) –The interval in seconds to poll the batch status.
-
default_gen_kwargs
(dict[str, Any] | None
, default:None
) –Default generation kwargs to use when calling the API.
-
developer_message
(str | None
, default:None
) –Instructions to the model that are prioritized ahead of user messages. Previously called the system prompt.
-
string_processors
(StringProcessor | list[StringProcessor] | None
, default:None
) –A single or a list of StringProcessor objects to process the model's output.
-
model_limit_new_tokens
(int | None
, default:None
) –An upper limit on the number of tokens the model can generate. For example, if a too-large
max_new_tokens
is given to generate_chat_response(), this value will cap it.
Source code in flexeval/core/language_model/openai_batch_api.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
|
temp_jsonl_file
instance-attribute
¶
temp_jsonl_file = NamedTemporaryFile(
delete=False, suffix=".jsonl"
)
__init__ ¶
__init__(
model: str,
api_headers: dict[str, str] | None = None,
polling_interval_seconds: int = 60,
default_gen_kwargs: dict[str, Any] | None = None,
developer_message: str | None = None,
string_processors: StringProcessor
| list[StringProcessor]
| None = None,
model_limit_new_tokens: int | None = None,
) -> None
Source code in flexeval/core/language_model/openai_batch_api.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
create_batch_file ¶
create_batch_file(
custom_id_2_message: dict[str, list[dict[str, Any]]],
**kwargs,
) -> None
Source code in flexeval/core/language_model/openai_batch_api.py
86 87 88 89 90 91 92 93 94 95 |
|
poll_batch_status_until_completion
async
¶
poll_batch_status_until_completion(
batch_id: str, polling_interval_seconds: int
) -> tuple[Status, Batch]
Source code in flexeval/core/language_model/openai_batch_api.py
151 152 153 154 155 156 157 158 159 160 161 162 |
|
close ¶
close() -> None
Source code in flexeval/core/language_model/openai_batch_api.py
264 265 266 267 268 269 270 271 272 273 274 |
|
__del__ ¶
__del__() -> None
Source code in flexeval/core/language_model/openai_batch_api.py
317 318 |
|
__repr__ ¶
__repr__() -> str
Source code in flexeval/core/language_model/openai_batch_api.py
320 321 |
|
VLLM ¶
LanguageModel implementation using VLLM.
Parameters:
-
model
(str
) –The name of the model to use.
-
model_kwargs
(dict[str, Any] | None
, default:None
) –Additional keyword arguments to pass to the model.
-
tokenizer
(str | None
, default:None
) –The name of the tokenizer to use. Defaults to the model_name.
-
tokenizer_kwargs
(dict[str, Any] | None
, default:None
) –Keyword arguments for the tokenizer instantiation by `from_pretrained().
-
add_special_tokens
(bool
, default:False
) –Whether to add special tokens to the input. Note that whether BOS or EOS tokens are added depends on the tokenizer.
-
custom_chat_template
(str | None
, default:None
) –A custom chat template for chatbot models. If specified, this overrides the default chat template of the tokenizer.
-
default_gen_kwargs
(dict[str, Any] | None
, default:None
) –Default generation kwargs to use when calling the model.
-
string_processors
(StringProcessor | list[StringProcessor] | None
, default:None
) –A single or a list of StringProcessor objects to process the model's output.
-
model_limit_tokens
(int | None | Literal['default']
, default:'default'
) –An upper limit on the number of tokens (input + output) the model can handle. If
max_new_tokens
exceeds this limit ingenerate_chat_response()
, it will be capped to this value. If this value is set to less than or equal to the model's capacity and the input exceeds it, an empty string is returned instead of raising an error. If set to “default”, the value will be automatically determined when possible.
Source code in flexeval/core/language_model/vllm_model.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
|
tokenizer
instance-attribute
¶
tokenizer: PreTrainedTokenizer = from_pretrained(
tokenizer, **tokenizer_kwargs
)
default_gen_kwargs
instance-attribute
¶
default_gen_kwargs = default_gen_kwargs or {
"temperature": 0.0
}
__init__ ¶
__init__(
model: str,
model_kwargs: dict[str, Any] | None = None,
tokenizer: str | None = None,
tokenizer_kwargs: dict[str, Any] | None = None,
add_special_tokens: bool = False,
custom_chat_template: str | None = None,
default_gen_kwargs: dict[str, Any] | None = None,
string_processors: StringProcessor
| list[StringProcessor]
| None = None,
model_limit_tokens: int
| None
| Literal["default"] = "default",
) -> None
Source code in flexeval/core/language_model/vllm_model.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
__repr__ ¶
__repr__() -> str
Source code in flexeval/core/language_model/vllm_model.py
319 320 |
|