API Reference¶
Documentation for ChatHPC Application¶
ChatHPC Application: A Python package for interacting with Kokkos-based applications.
This module provides the main components for creating and configuring Kokkos-based applications using a chat-like interface.
App
¶
Main application class for ChatHPC Application.
This class handles the initialization, loading, and management of models, datasets, and training processes for the ChatHPC application. It provides methods for loading different types of models, evaluating prompts, and fine-tuning the model.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
AppConfig
|
Configuration settings for the application. |
tokenizer |
AppConfig
|
Tokenizer for processing input text. |
model |
AppConfig
|
The language model used for text generation and fine-tuning. |
train_dataset |
AppConfig
|
Dataset used for training. |
eval_dataset |
AppConfig
|
Dataset used for evaluation. |
tokenized_train_dataset |
AppConfig
|
Tokenized version of training dataset. |
tokenized_val_dataset |
AppConfig
|
Tokenized version of validation dataset. |
peft_config |
AppConfig
|
Configuration for LoRA fine-tuning. |
training_args |
AppConfig
|
Arguments for model training. |
Methods:
| Name | Description |
|---|---|
load_base_model |
Loads the base LLM model. |
load_finetuned_model |
Loads a model with fine-tuned layers. |
load_merged_model |
Loads a complete merged model. |
load_datasets |
Loads training and evaluation datasets. |
evaluate_model |
Generates responses for given prompts. |
chat_prompt |
Creates formatted prompts for questions. |
chat_evaluate |
Evaluates questions with context. |
tokenize_training_set |
Prepares datasets for training. |
train |
Executes model fine-tuning process. |
interactive |
Starts interactive chat session. |
print_config |
Displays current configuration settings. |
Source code in src/chathpc/app/app.py
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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 | |
__init__(app_config=None)
¶
Initialize the ChatHPC application instance.
This method sets up a new application instance with configuration settings and initializes the Jinja2 environment for template processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_config
|
AppConfig
|
Application configuration settings. If None, creates default AppConfig instance. |
None
|
Sets
- self.config: Application configuration settings
- self.jinja: Jinja2 environment for template processing
Example
# With default settings
app = App()
# With custom settings
config = AppConfig(base_model_path="/path/to/model")
app = App(app_config=config)
Note
Model loading and other initializations must be performed explicitly by calling the appropriate methods after initialization.
Source code in src/chathpc/app/app.py
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 | |
chat_evaluate(**kwargs)
¶
Evaluate a question with provided context using the model.
This method processes a question-context pair through the model by: 1. Formatting the input using the inference template 2. Generating a response using the model 3. Returning both the response and original prompt
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The question to be answered by the model. |
required |
**kwargs
|
Additional keyword arguments passed to evaluate_model(). Common arguments include: - max_new_tokens (int): Override default token generation limit - Other template variables defined in prompt template |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Generated model response. |
Requires
- Initialized model via one of load methods:
- load_base_model()
- load_finetuned_model()
- load_merged_model()
- Initialized tokenizer and templates
Example
app = App()
app.load_merged_model()
response = app.chat_evaluate(
question="What is Kokkos?",
context="Kokkos is a performance portable programming model...",
max_new_tokens=200,
)
print(response) # Prints model's explanation of Kokkos
Note
- Uses chat_prompt() for template-based input formatting
- Uses evaluate_model() for response generation
- Response format follows inference template structure
- Template variables can be passed via kwargs
Source code in src/chathpc/app/app.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
chat_evaluate_extract(**kwargs)
¶
Extract the model's answer from a chat evaluation response.
This method combines chat_evaluate() with answer extraction, removing template formatting and returning only the model's direct response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Keyword arguments passed to chat_evaluate(). Common arguments include: - question (str): The question to be answered - context (str): Supporting context or documentation - max_new_tokens (int): Override default token generation limit Additional arguments can be used if defined in the template. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The extracted answer from the model's response, without template formatting. |
Example
app = App()
app.load_merged_model()
answer = app.chat_evaluate_extract(
question="What is Kokkos?", context="Kokkos is a programming model..."
)
print(answer) # Prints just the model's answer without template
Note
- Uses chat_evaluate() to generate the full response
- Automatically extracts the answer portion using template structure
- More concise than chat_evaluate() for direct answer retrieval
Source code in src/chathpc/app/app.py
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | |
chat_prompt(**kwargs)
¶
Create a formatted prompt for chat questions.
This method generates a structured prompt using the inference template by combining provided keyword arguments according to the template defined in the application configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Keyword arguments to be passed to the template. Common arguments include: - question (str): The question to be answered - context (str): Supporting context or documentation Additional arguments can be used if defined in the template. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A formatted prompt string following the inference template. |
Requires
- Initialized inference_template via _load_templates()
- Template must be properly formatted with expected variables
Example
app = App()
prompt = app.chat_prompt(
question="How do I use Views?",
context="Views are memory spaces in Kokkos...",
)
print(prompt) # Returns formatted prompt based on template
Note
- The actual prompt format is determined by the inference template loaded during initialization
- Keywords are automatically mapped using template_utils.map_keywords()
- This method is typically used internally by chat_evaluate()
Source code in src/chathpc/app/app.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 | |
evaluate_model(prompt, max_new_tokens=None)
¶
Generate a model response for a given input prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Input text prompt for model evaluation. |
required |
max_new_tokens
|
int | None
|
Maximum tokens to generate. Defaults to config.max_response_tokens. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Generated text response from the model. |
Requires
- Initialized model via one of:
- load_base_model()
- load_finetuned_model()
- load_merged_model()
- Initialized tokenizer
Example
app = App()
app.load_base_model()
response = app.evaluate_model("What is Kokkos?", max_new_tokens=100)
print(response) # "Kokkos is a programming model..."
Note
Uses evaluation mode and torch.no_grad() for inference. Input is processed on CUDA if available.
Source code in src/chathpc/app/app.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | |
extract_answer(chat_response, **kwargs)
¶
Extract the model's answer from a complete response string.
This method processes the full model response to extract just the answer portion, removing any template formatting or context that was part of the prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
str
|
The complete response string from the model evaluation |
required |
**kwargs
|
Additional keyword arguments that may be used for template-specific extraction |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
The extracted answer portion of the response |
Example
app = App()
response = app.chat_evaluate("What is Kokkos?")
answer = app.extract_answer(response)
print(answer) # Prints just the model's answer without template formatting
Note
The exact extraction logic depends on the prompt template structure defined in the application configuration.
Source code in src/chathpc/app/app.py
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 | |
from_json(json_or_file, extra_params=None)
classmethod
¶
Create an App instance from JSON configuration sources.
This class method creates an App instance by combining settings from a primary JSON source and optional additional parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_or_file
|
Union[str, Path, dict]
|
Primary configuration source - either a path to a JSON file or a dictionary with configuration values. |
required |
extra_params
|
Union[str, Path, dict]
|
Additional configuration source to override or supplement primary settings. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
App |
App
|
A new App instance initialized with combined settings. |
Example
# From JSON file
app = App.from_json("config.json")
# With extra parameters
app = App.from_json("config.json", {"max_response_tokens": 800})
# From dictionary
app = App.from_json({"data_file": "data.json"})
Note
When both sources are provided, settings from extra_params override corresponding values from the primary source.
Source code in src/chathpc/app/app.py
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 | |
interactive(args, prompt='chathpc')
¶
Start an interactive chat session with the model.
This method provides a command-line interface for interacting with the model. It maintains a command history and supports context setting for conversations.
Commands
/bye: Exit the interactive session /context: Set a new context for subsequent questions
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt prefix to display. Defaults to "chathpc". |
'chathpc'
|
Requires
- A model must be loaded via one of:
- load_base_model()
- load_finetuned_model()
- load_merged_model()
- The tokenizer must be initialized
Example
```python
app = App() app.load_merged_model() app.interactive() chathpc ()> What is Kokkos?
Source code in src/chathpc/app/app.py
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 | |
load_base_model()
¶
Load and initialize the base Large Language Model.
This method initializes both the tokenizer and model from the base model path specified in the application preferences. The model is loaded with specific configurations for optimal performance.
Requires
- preferences.base_model_path must be set to a valid model path
Sets
- self.tokenizer: Initialized AutoTokenizer for text processing
- self.model: Initialized AutoModelForCausalLM in float16 precision
Example
>>> app = App()
>>> app.preferences.base_model_path = "path/to/model"
>>> app.load_base_model()
Note
The model is loaded with float16 precision and automatic device mapping for optimal performance on available hardware.
Source code in src/chathpc/app/app.py
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 | |
load_datasets()
¶
Load training and evaluation datasets from a JSON file.
This method loads datasets from the JSON file specified in the application preferences. The datasets are loaded using the Hugging Face datasets library and split into training and evaluation sets.
Config
preferences.data_file (str): Path to the JSON file containing the datasets.
Sets
self.train_dataset: Dataset object for training self.eval_dataset: Dataset object for evaluation
Requires
- The data file must be in JSON format
- The data file path must be set in preferences.data_file
Source code in src/chathpc/app/app.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | |
load_finetuned_model()
¶
Load and initialize the finetuned Large Language Model.
This method loads a finetuned model by first initializing the base model and tokenizer, then loading the finetuned layers on top of it using PeftModel.
Requires
- preferences.base_model_path must be set to a valid base model path
- preferences.finetuned_model_path must be set to a valid finetuned model path
Sets
- self.tokenizer: Initialized AutoTokenizer for text processing
- self.model: Initialized PeftModel with finetuned layers
Example
>>> app = App()
>>> app.preferences.base_model_path = "path/to/base/model"
>>> app.preferences.finetuned_model_path = "path/to/finetuned/model"
>>> app.load_finetuned_model()
Note
This method first calls load_base_model() to initialize the foundation model before applying the finetuned layers.
Source code in src/chathpc/app/app.py
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 | |
load_merged_model()
¶
Load and initialize the merged Large Language Model.
This method loads a complete merged model that combines the base model with finetuned layers into a single model file. The tokenizer is initialized from the base model path while the full model is loaded from the merged model path.
Requires
- preferences.base_model_path must be set to a valid base model path for tokenizer
- preferences.merged_model_path must be set to a valid merged model path
Sets
- self.tokenizer: Initialized AutoTokenizer for text processing
- self.model: Initialized AutoModelForCausalLM with merged weights
Example
>>> app = App()
>>> app.preferences.base_model_path = "path/to/base/model"
>>> app.preferences.merged_model_path = "path/to/merged/model"
>>> app.load_merged_model()
Note
The model is loaded with float16 precision and automatic device mapping for optimal performance on available hardware.
Source code in src/chathpc/app/app.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |
print_config()
¶
Print the current configurations of the application in a formatted table.
This method displays all configuration settings from self.config in a formatted table using the tabulate library.
The table includes all configuration parameters like: - File paths (data, models, checkpoints) - Model parameters - Training settings - Other application settings
Example
app = App()
app.print_config()
# Outputs:
# ===================== ==========================
# Setting Value
# ===================== ==========================
# data_file /path/to/data.json
# base_model_path /path/to/base/model
# max_response_tokens 600
# use_wandb False
# ===================== ==========================
Note
The output format uses the 'simple' table format from the tabulate library for clean and readable presentation of settings.
Source code in src/chathpc/app/app.py
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 | |
test(test_dataset, save_test_data_path=None, ollama_model=None, openai_model=None)
¶
Test model against provided testing dataset.
This method evaluates the model on the test file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_test_data_path
|
Union[str, Path, None]
|
Optional path to save test results. |
None
|
ollama_model
|
str
|
Name of Ollama model, if using Ollama instead of app's model |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
list[dict[str, Any]]
|
result of the test. |
Example
app = App()
test_results = app.test(
test_dataset="test_data.json", save_test_data_path="test_results.json"
)
print(f"Test completed with {len(test_results)} cases")
Source code in src/chathpc/app/app.py
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 | |
tokenize_training_set()
¶
Tokenize the training and validation datasets.
This method processes the loaded datasets by tokenizing text data for model training. It handles padding configuration and EOS token management during tokenization.
Requires
- Initialized datasets via load_datasets()
- Initialized tokenizer via loading a model
Sets
- self.tokenized_train_dataset: Processed training dataset
- self.tokenized_val_dataset: Processed validation dataset
Example
app = App()
app.load_base_model()
app.load_datasets()
app.tokenize_training_set()
Note
- The method uses the training_prompt template from config to format inputs before tokenization.
- This method also handles padding token configuration and adds/removes EOS tokens as needed for the tokenization process.
Source code in src/chathpc/app/app.py
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 | |
train()
¶
Train the model using fine-tuning layers.
This method performs fine-tuning of the base model using LoRA (Low-Rank Adaptation) configuration. It prepares the model for training, sets up training arguments, and executes the training process.
Requires
- App.load_datasets() must be called first to load training data
- App.load_base_model() must be called first to load the base model
- Tokenizer and model must be properly initialized
Sets
- self.peft_config: LoRA configuration for fine-tuning
- self.training_args: Training arguments for the Trainer
- self.model: Updated model after training
Saves
- Finetuned model layers to preferences.finetuned_model_path
- Complete merged model to preferences.merged_model_path
Note
This method uses Hugging Face's Trainer for the training process and supports multi-GPU training when available. It also integrates with Weights & Biases (wandb) for experiment tracking.
Source code in src/chathpc/app/app.py
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 | |
training_prompt(**kwargs)
¶
Create a formatted prompt for training data.
This method generates a structured prompt using the training template by combining provided keyword arguments according to the template defined in the application configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Keyword arguments to be passed to the template. Common arguments include: - question (str): The question to be used in training - context (str): Supporting context or documentation - answer (str): The expected answer or response Additional arguments can be used if defined in the template. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A formatted prompt string following the training template. |
Requires
- Initialized training_template via _load_templates()
- Template must be properly formatted with expected variables
Example
app = App()
prompt = app.training_prompt(
question="How do I use Views?",
context="Views are memory spaces in Kokkos...",
answer="To use Views in Kokkos...",
)
print(prompt) # Returns formatted prompt based on template
Note
- The actual prompt format is determined by the training template loaded during initialization
- Keywords are automatically mapped using template_utils.map_keywords()
- This method is typically used internally by tokenize_training_set()
Source code in src/chathpc/app/app.py
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 | |
verify(save_verify_data_path=None, ollama_model=None, openai_model=None)
¶
Verify model outputs against the training dataset.
This method runs verification tests on the model by comparing its outputs against the training set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_verify_data_path
|
Union[str, Path, None]
|
Optional path to save verification results. |
None
|
ollama_model
|
str
|
Name of Ollama model, if using Ollama instead of app's model |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of errors. |
Example
app = App()
app.load_merged_model()
tests_failed = app.verify(save_verify_data_path="verify_results.json")
Source code in src/chathpc/app/app.py
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 | |
AppConfig
¶
Bases: BaseSettings
Configuration settings for the ChatHPC application.
This class inherits from Pydantic BaseSettings to manage application configuration through multiple sources with a defined priority order.
Attributes:
| Name | Type | Description |
|---|---|---|
data_file |
Path
|
Training data JSON file path. |
base_model_path |
Path
|
Pre-trained base LLM model directory. |
finetuned_model_path |
Path
|
Directory for fine-tuned model layers. |
merged_model_path |
Path
|
Directory for complete merged model. |
training_output_dir |
Path
|
Directory for training output and checkpoints. |
max_training_tokens |
int
|
Maximum tokens for training set tokenization. |
max_response_tokens |
int
|
Maximum tokens for model response generation. |
prompt_history_file |
Path
|
File path for interactive chat history. |
prompt_template_file |
Path
|
File containing prompt template for training/inference. |
prompt_template |
str
|
Direct string template for prompts. |
use_wandb |
bool
|
Enable/disable Weights & Biases logging. |
Configuration Priority
- Environment variables (CHATHPC_ prefix)
- .env file
- Direct initialization
- JSON config file
- File secrets
Example
config = AppConfig(base_model_path="/path/to/model")
config = AppConfig.from_json("config.json")
Note
- All paths are handled as Path objects
- UTF-8 encoding used for all file operations
- Either prompt_template_file or prompt_template must be set
Source code in src/chathpc/app/app.py
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 | |
check_for_prompt_template(values)
classmethod
¶
Validate prompt template configuration.
This validator ensures that exactly one of prompt_template_file or prompt_template is set in the configuration. Having both or neither is invalid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
dict
|
Dictionary of configuration values to validate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
The validated configuration values. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither or both prompt template options are set. |
Example
Valid configurations: - prompt_template_file set, prompt_template None - prompt_template set, prompt_template_file None
Invalid configurations: - Both prompt_template and prompt_template_file set - Neither prompt_template nor prompt_template_file set
Source code in src/chathpc/app/app.py
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 | |
from_json(json_or_file, extra_params=None)
classmethod
¶
Create an AppConfig instance from JSON configuration sources.
This class method creates an AppConfig instance by combining settings from a primary JSON source and optional additional parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_or_file
|
Union[str, Path, dict]
|
Primary configuration source - either a path to a JSON file or a dictionary with configuration values. |
required |
extra_params
|
Union[str, Path, dict]
|
Additional configuration source to override or supplement primary settings. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
AppConfig |
AppConfig
|
A new AppConfig instance initialized with combined settings. |
Example
# From JSON file
config = AppConfig.from_json("config.json")
# With extra parameters
config = AppConfig.from_json("config.json", {"max_response_tokens": 800})
# From dictionary
config = AppConfig.from_json({"data_file": "data.json"})
Note
When both sources are provided, settings from extra_params override corresponding values from the primary source.
Source code in src/chathpc/app/app.py
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 | |