Agents
Agents
llama_cpp_agent.llm_agent
StreamingResponse
dataclass
Represents a streaming response with text and an indicator for the last response.
Source code in llama_cpp_agent/llm_agent.py
__init__(text, is_last_response)
Initializes a new StreamingResponse object.
Parameters:
-
text(str) –The text content of the streaming response.
-
is_last_response(bool) –Indicates whether this is the last response in the stream.
Source code in llama_cpp_agent/llm_agent.py
LlamaCppAgent
A base agent that can be used for chat, structured output and function calling.
Source code in llama_cpp_agent/llm_agent.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 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 | |
__init__(provider, name='llamacpp_agent', system_prompt='You are a helpful assistant.', predefined_messages_formatter_type=MessagesFormatterType.CHATML, custom_messages_formatter=None, chat_history=None, add_tools_and_structures_documentation_to_system_prompt=True, debug_output=False)
Initializes a new LlamaCppAgent object.
Parameters:
-
provider(LlmProvider) –The underlying llm provider (LlamaCppServerProvider, LlamaCppPythonProvider, TGIServerProvider or VLLMServerProvider).
-
name(str, default:'llamacpp_agent') –The name of the agent.
-
system_prompt(str, default:'You are a helpful assistant.') –The system prompt used in chat interactions.
-
predefined_messages_formatter_type(MessagesFormatterType, default:CHATML) –The type of predefined messages formatter.
-
custom_messages_formatter(MessagesFormatter, default:None) –Custom message's formatter.
-
chat_history(ChatHistory, default:None) –This will handle the chat history.
-
add_tools_and_structures_documentation_to_system_prompt(bool, default:True) –Will suffix system prompt dynamically with documentation for function calling or structured output.
-
debug_output(bool, default:False) –Indicates whether debug output should be enabled.
Source code in llama_cpp_agent/llm_agent.py
add_message(message, role)
Adds a message to the chat history.
Parameters:
-
message(str) –The content of the message.
-
role(Literal['system'] | Literal['user'] | Literal['assistant'] | Literal['tool']) –The role of the message sender.
Source code in llama_cpp_agent/llm_agent.py
get_text_response(prompt=None, structured_output_settings=None, llm_sampling_settings=None, streaming_callback=None, returns_streaming_generator=False, print_output=False)
Get a text response from the LLM provider.
Parameters:
-
prompt(str | list[int], default:None) –The prompt for the LLM.
-
structured_output_settings(LlmStructuredOutputSettings, default:None) –Settings for structured output.
-
llm_sampling_settings(LlmSamplingSettings, default:None) –Sampling settings for the LLM.
-
streaming_callback(Callable[[StreamingResponse], None], default:None) –Callback for streaming responses.
-
returns_streaming_generator(bool, default:False) –Whether to return a generator streaming the results.
-
print_output(bool, default:False) –Whether to print the output.
Returns:
-
Union[str, List[dict], BaseModel, Generator[Any, Any, str | BaseModel | list[BaseModel]]]–Union[str, List[dict], BaseModel, Generator[Any, Any, str | BaseModel | list[BaseModel]]: The generated response. A string message, a list of function calls, an object from structured output or a generator for the response
Source code in llama_cpp_agent/llm_agent.py
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 | |
get_chat_response(message=None, role=Roles.user, prompt_suffix=None, chat_history=None, system_prompt=None, system_prompt_modules=None, add_message_to_chat_history=True, add_response_to_chat_history=True, structured_output_settings=None, llm_sampling_settings=None, streaming_callback=None, returns_streaming_generator=False, print_output=False)
Get a chat response based on the input message and context.
Parameters:
-
message(str, default:None) –The input message.
-
role(Literal['system', 'user', 'assistant', 'tool'], default:user) –The role of the message sender.
-
prompt_suffix(str, default:None) –Suffix to append after the prompt.
-
chat_history(ChatHistory, default:None) –Overwrite internal ChatHistory of the agent.
-
system_prompt(str, default:None) –Overwrites the system prompt set on the agent initialization.
-
system_prompt_modules(SystemPromptModules, default:None) –Additional sections added to the system prompt.
-
add_message_to_chat_history(bool, default:True) –Whether to add the input message to the chat history.
-
add_response_to_chat_history(bool, default:True) –Whether to add the generated response to the chat history.
-
structured_output_settings(LlmStructuredOutputSettings, default:None) –Settings for structured output.
-
llm_sampling_settings(LlmSamplingSettings, default:None) –Sampling settings for the LLM.
-
streaming_callback(Callable[[StreamingResponse], None], default:None) –Callback for streaming responses.
-
returns_streaming_generator(bool, default:False) –Whether to return a generator streaming the results.
-
print_output(bool, default:False) –Whether to print the generated response.
Returns:
-
Union[str, List[dict], BaseModel, Generator[Any, Any, str | BaseModel | list[BaseModel]]]–Union[str, List[dict], BaseModel, Generator[Any, Any, str | BaseModel | list[BaseModel]]: The generated chat response. A string message, a list of function calls, an object from structured output or a generator for the response
Source code in llama_cpp_agent/llm_agent.py
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 | |
Structured Output Settings
llama_cpp_agent.llm_output_settings.settings
LlmStructuredOutputType
Bases: Enum
Enum for defining different types of structured outputs that can be generated by a Language Model.
Source code in llama_cpp_agent/llm_output_settings/settings.py
LlmStructuredOutputSettings
Bases: BaseModel
Settings for structured output of large language models for using tools like function calling and creating instances of pydantic models.
Attributes:
-
output_type(LlmStructuredOutputType) –Defines the type of structured output.
-
function_tools(Optional[List[LlamaCppFunctionTool]]) –Tools to enable function calling.
-
pydantic_models(Optional[List[type[BaseModel]]]) –List of pydantic models for structured data output.
-
add_thoughts_and_reasoning_field(Optional[bool]) –Add thoughts and reasoning field to function calling. Defaults to False.
-
thoughts_and_reasoning_field_name(Optional[str]) –Field name for the thoughts and reasoning field. Defaults to "thoughts_and_reasoning".
-
function_calling_name_field_name(Optional[str]) –Name of the JSON field for the name of the used function. Defaults to "function".
-
function_calling_content(Optional[str]) –Name of the JSON field for the arguments of the used function. Defaults to "arguments".
-
output_model_name_field_name(Optional[str]) –Name of the JSON field for the name of the used pydantic model. Defaults to "model".
-
output_model_attributes_field_name(Optional[str]) –Name of the JSON field for the fields of the pydantic model. Defaults to "fields".
Methods:
-
from_llama_cpp_function_tools–Create settings from a list of LlamaCppFunctionTools with a specific output type.
-
from_pydantic_models–Create settings from a list of Pydantic models with a specific output type.
-
from_open_ai_tools–Create settings from OpenAI tools for structured outputs.
-
from_functions–Create settings from a list of callable functions with a specific output type.
-
from_llama_index_tools–Create settings from a list of llama-index tools with a specific output type.
-
to_openai_tools–Return a list of OpenAI tools.
-
add_llama_cpp_function_tool–Add a LlamaCppFunctionTool to the settings.
-
add_pydantic_model–Add a Pydantic model to the settings, ensuring it matches the specified output type.
-
add_open_ai_tool–Add an OpenAI tool to the settings, ensuring it matches the specified output type.
-
add_function_tool–Add a callable function to the settings, ensuring it matches the specified output type.
-
add_llama_index_tool–Add a llama-index tool, like QueryEngineTool, to the settings, ensuring it matches the specified output type.
-
get_llm_documentation–Generate documentation for the models and tools configured within the settings, based on the output type.
-
get_gbnf_grammar–Generate a GBNF grammar for tools configured within the settings, based on the output type.
-
get_json_schema–Generate a JSON schema for the tools configured within the settings, based on the output type.
Source code in llama_cpp_agent/llm_output_settings/settings.py
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 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 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 | |
from_llama_cpp_function_tools(llama_cpp_function_tools, allow_parallel_function_calling=False, add_thoughts_and_reasoning_field=False, add_heartbeat_field=False)
staticmethod
Create settings from a list of LlamaCppFunctionTools with a specific output type.
Parameters:
-
llama_cpp_function_tools(List[LlamaCppFunctionTool]) –List of function tools.
-
allow_parallel_function_calling(bool, default:False) –Whether to enable parallel function calling. Defaults to False.
-
add_thoughts_and_reasoning_field(bool, default:False) –Whether to add thoughts and reasoning field to function calling. Defaults to False.:
-
add_heartbeat_field(bool, default:False) –Whether to add heartbeat field to function calling. Defaults to False.:
Returns: LlmStructuredOutputSettings: Configured settings object.
Source code in llama_cpp_agent/llm_output_settings/settings.py
from_pydantic_models(models, output_type, add_thoughts_and_reasoning_field=False)
staticmethod
Create settings from a list of Pydantic models with a specific output type.
Parameters:
-
models(List[BaseModel]) –List of Pydantic models.
-
output_type(LlmStructuredOutputType) –Desired output type.
Returns:
-
LlmStructuredOutputSettings–Configured settings object.
Raises:
-
NotImplementedError–If no structured output is specified for the output type.
Source code in llama_cpp_agent/llm_output_settings/settings.py
from_open_ai_tools(tools, allow_parallel_function_calling=False)
staticmethod
Create settings from OpenAI tools for structured outputs.
Parameters:
-
tools(List[Tuple[Dict[str, Any], Callable]]) –List of OpenAI tools defined by a schema and associated function.
-
allow_parallel_function_calling(bool, default:False) –Whether to enable parallel function calling. Defaults to False.
Returns:
-
LlmStructuredOutputSettings–Configured settings object.
Source code in llama_cpp_agent/llm_output_settings/settings.py
from_functions(tools, allow_parallel_function_calling=False, add_thoughts_and_reasoning_field=False, add_heartbeat_field=False)
staticmethod
Create settings from a list of llama-index tools with a specific output type.
Parameters:
-
tools(list) –List of llama-index tools.
-
allow_parallel_function_calling(bool, default:False) –Whether to enable parallel function calling. Defaults to False.
-
add_thoughts_and_reasoning_field(bool, default:False) –Whether to add a thoughts and reasoning field to output.
-
add_heartbeat_field(bool, default:False) –Whether to add a heartbeat field to output.
Returns: LlmStructuredOutputSettings: Configured settings object.
Raises:
-
NotImplementedError–If the specified output type is not supported for tools.
Source code in llama_cpp_agent/llm_output_settings/settings.py
from_llama_index_tools(tools, allow_parallel_function_calling=False, add_thoughts_and_reasoning_field=False, add_heartbeat_field=False)
staticmethod
Create settings from a list of llama-index tools with a specific output type. Has to be either LlmOutputType.function_call or LlmOutputType.parallel_function_call.
Parameters:
-
tools(list) –List of llama-index tools.
-
allow_parallel_function_calling(bool, default:False) –Whether to enable parallel function calling. Defaults to False.
-
add_thoughts_and_reasoning_field(bool, default:False) –Whether to add a thoughts and reasoning field to output.
-
add_heartbeat_field(bool, default:False) –Whether to add a heartbeat field to output.
Returns: LlmStructuredOutputSettings: Configured settings object.
Raises:
-
NotImplementedError–If the specified output type is not supported for tools.
Source code in llama_cpp_agent/llm_output_settings/settings.py
to_openai_tools()
Return a list of OpenAI tools. Returns: List[Dict[str, Any]]: List of OpenAI tools.
Raises:
-
NotImplementedError–If the specified output type is not supported for tools.
Source code in llama_cpp_agent/llm_output_settings/settings.py
add_llama_cpp_function_tool(tool)
Add a LlamaCppFunctionTool to the settings.
Parameters:
-
tool(LlamaCppFunctionTool) –The function tool to add.
Source code in llama_cpp_agent/llm_output_settings/settings.py
add_pydantic_model(model, name=None)
Add a Pydantic model to the settings, ensuring it matches the specified output type.
Parameters:
-
model(BaseModel) –The Pydantic model to add.
Raises:
-
NotImplementedError–If no structured output is specified.
Source code in llama_cpp_agent/llm_output_settings/settings.py
add_open_ai_tool(open_ai_schema_and_function, name=None)
Add an OpenAI tool to the settings, ensuring it matches the specified output type.
Parameters:
-
open_ai_schema_and_function(Tuple[Dict[str, Any], Callable]) –The OpenAI schema and associated function to add.
Raises:
-
NotImplementedError–If the output type does not support adding tools.
Source code in llama_cpp_agent/llm_output_settings/settings.py
add_function_tool(function, name=None)
Add a callable function to the settings, ensuring it matches the specified output type.
Parameters:
-
function(Callable) –The function to add.
Raises:
-
NotImplementedError–If the output type does not support adding tools.
Source code in llama_cpp_agent/llm_output_settings/settings.py
add_llama_index_tool(tool, name=None)
Add a llama-index tool, like QueryEngineTool, to the settings, ensuring it matches the specified output type.
Parameters:
-
tool–The llama-index tool to add.
Raises:
-
NotImplementedError–If the output type does not support adding tools.
Source code in llama_cpp_agent/llm_output_settings/settings.py
get_llm_documentation(provider)
Generate documentation for the models and tools configured within the settings, based on the output type.
Returns:
-
str–Generated documentation for the configured models or tools.
Raises:
-
NotImplementedError–If no structured output is specified.
Source code in llama_cpp_agent/llm_output_settings/settings.py
get_gbnf_grammar()
Generate a GBNF grammar for tools configured within the settings, based on the output type.
Returns:
-
str–Generated GBNF grammar for the configured models or tools.
Raises:
-
NotImplementedError–If no structured output is specified.
Source code in llama_cpp_agent/llm_output_settings/settings.py
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 | |
get_json_schema()
Generate a JSON schema for the tools configured within the settings, based on the output type.
Returns:
-
Dict–Generated JSON schema for the configured models or tools.
Raises:
-
NotImplementedError–If no structured output is specified.
Source code in llama_cpp_agent/llm_output_settings/settings.py
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 | |
add_function_name_to_heartbeat_list(function_name)
Add a function name to the heartbeat list. This way a heartbeat field get added to the function calling output.
Source code in llama_cpp_agent/llm_output_settings/settings.py
add_all_current_functions_to_heartbeat_list(excluded=None)
Add a function name to the heartbeat list. This way a heartbeat field get added to the function calling output.
Source code in llama_cpp_agent/llm_output_settings/settings.py
handle_function_call(function_call_response)
Handle a function call response and return the output.
Parameters:
-
function_call_response(dict) –The function call response.
Returns:
-
str–The output of the function call or an error message.
Source code in llama_cpp_agent/llm_output_settings/settings.py
intern_function_call(function_call)
Internal method to handle a function call and return the output.
Parameters:
-
function_call(dict) –The function call dictionary.
Returns: str: The output of the function call or an error message.
Source code in llama_cpp_agent/llm_output_settings/settings.py
intern_parallel_function_call(function_calls)
Internal method to handle a function call and return the output.
Parameters:
-
function_calls List[dict]–The function call dictionary.
Returns:
-
str–The output of the function call or an error message.
Source code in llama_cpp_agent/llm_output_settings/settings.py
Function Calling Agent
llama_cpp_agent.function_calling_agent
activate_message_mode
Bases: BaseModel
Activates message mode.
Source code in llama_cpp_agent/function_calling_agent.py
send_message
Bases: BaseModel
Sends a message to the user.
Source code in llama_cpp_agent/function_calling_agent.py
write_text_file
Bases: BaseModel
Writes content to a file.
Source code in llama_cpp_agent/function_calling_agent.py
write_file(content)
Write content to a file.
Parameters:
-
content(str) –The content to write to the file.
read_text_file
Bases: BaseModel
Reads the content of a file.
Source code in llama_cpp_agent/function_calling_agent.py
read_file()
Reads the content of a file.
FunctionCallingAgent
An agent that uses function calling to interact with its environment and the user.
Parameters:
-
llama_llm(Llama | LlamaLLMSettings | LlamaCppEndpointSettings | OpenAIEndpointSettings) –An instance of Llama, LlamaLLMSettings, LlamaCppEndpointSettings or LlamaCppServerLLMSettings as LLM.
-
llama_generation_settings(LlamaLLMGenerationSettings | LlamaCppGenerationSettings | OpenAIGenerationSettings) –Generation settings for Llama.
-
messages_formatter_type(MessagesFormatterType, default:CHATML) –Type of messages formatter.
-
custom_messages_formatter(MessagesFormatter, default:None) –Optional Custom messages formatter.
-
streaming_callback(Callable[[StreamingResponse], None], default:None) –Callback function for streaming responses.
-
k_last_messages_from_chat_history(int, default:0) –Number of last messages to consider from chat history.
-
system_prompt(str, default:None) –System prompt for interaction.
-
llama_cpp_function_tools(List[LlamaCppFunctionTool], default:None) –List of LlamaCppFunctionTool instances.
-
allow_parallel_function_calling(bool, default:False) –Allow parallel function calling (Default=False)
-
add_send_message_to_user_function(bool, default:True) –Flag to add send_message_to_user function.
-
send_message_to_user_callback(Callable[[str], None], default:None) –Callback for sending a message to the user.
-
debug_output(bool, default:False) –Enable debug output.
Attributes:
-
send_message_to_user_callback(Callable[[str], None]) –Callback for sending a message to the user.
-
llama_cpp_tools(List[LlamaCppFunctionTool]) –List of LlamaCppFunctionTool instances.
-
tool_registry(LlamaCppFunctionToolRegistry) –Function tool registry.
-
llama_generation_settings(LlamaLLMGenerationSettings) –Generation settings for Llama.
-
system_prompt(str) –System prompt for interaction.
-
llama_cpp_agent(LlamaCppAgent) –LlamaCppAgent instance for interaction.
-
k_last_messages_from_chat_history(int) –Number of last messages to consider from chat history.
-
streaming_callback(Callable[[StreamingResponse], None]) –Callback function for streaming responses.
Methods:
-
save–str): Save the agent's state to a file.
-
load_from_file–str, llama_llm, python_functions, pydantic_functions, send_message_to_user_callback, streaming_callback) -> FunctionCallingAgent: Load the agent's state from a file.
-
load_from_dict–dict) -> FunctionCallingAgent: Load the agent's state from a dictionary.
-
as_dict–Convert the agent's state to a dictionary.
-
generate_response–str): Generate a response based on the input message.
-
send_message_to_user–str): Send a message to the user.
Source code in llama_cpp_agent/function_calling_agent.py
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 | |
__init__(llama_llm, messages_formatter_type=MessagesFormatterType.CHATML, custom_messages_formatter=None, streaming_callback=None, k_last_messages_from_chat_history=0, system_prompt=None, llama_cpp_function_tools=None, basic_file_tools=False, allow_parallel_function_calling=False, add_send_message_to_user_function=True, send_message_to_user_callback=None, debug_output=False)
Initialize the FunctionCallingAgent.
Parameters:
-
llama_llm(LlmProvider) –The LLM Provider.
-
messages_formatter_type(MessagesFormatterType, default:CHATML) –Type of messages formatter.
-
custom_messages_formatter(MessagesFormatter, default:None) –Optional Custom messages formatter.
-
streaming_callback(Callable[[StreamingResponse], None], default:None) –Callback function for streaming responses.
-
k_last_messages_from_chat_history(int, default:0) –Number of last messages to consider from chat history.
-
system_prompt(str, default:None) –System prompt for interaction.
-
llama_cpp_function_tools(List[LlamaCppFunctionTool], default:None) –List of LlamaCppFunctionTool instances.
-
allow_parallel_function_calling(bool, default:False) –Allow parallel function calling (Default=False)
-
add_send_message_to_user_function(bool, default:True) –Flag to add send_message_to_user function.
-
send_message_to_user_callback(Callable[[str], None], default:None) –Callback for sending a message to the user.
-
debug_output(bool, default:False) –Enable debug output.
Source code in llama_cpp_agent/function_calling_agent.py
load_from_dict(agent_dict)
staticmethod
Load the agent's state from a dictionary.
Parameters:
-
agent_dict(dict) –The dictionary containing the agent's state.
Returns:
-
FunctionCallingAgent(FunctionCallingAgent) –The loaded FunctionCallingAgent instance.
Source code in llama_cpp_agent/function_calling_agent.py
as_dict()
Convert the agent's state to a dictionary.
Returns:
-
dict(dict) –The dictionary representation of the agent's state.
send_message_to_user(message)
Send a message to the user.
Parameters:
-
message(str) –The message send to the user.
Source code in llama_cpp_agent/function_calling_agent.py
Structured Output Agent
llama_cpp_agent.structured_output_agent
StructuredOutputAgent
An agent that creates structured output based on pydantic models from unstructured text.
Parameters:
-
llama_llm(Union[Llama, LlamaLLMSettings, LlamaCppEndpointSettings, OpenAIEndpointSettings]) –An instance of Llama, LlamaLLMSettings, LlamaCppServerLLMSettings, OpenAIEndpointSettings as LLM.
-
llama_generation_settings(Union[LlamaLLMGenerationSettings, LlamaCppGenerationSettings, OpenAIGenerationSettings]) –Generation settings for Llama or LlamaCppServer.
-
messages_formatter_type(MessagesFormatterType, default:CHATML) –Type of messages formatter.
-
custom_messages_formatter(MessagesFormatter, default:None) –Custom messages formatter.
-
streaming_callback(Callable[[StreamingResponse], None], default:None) –Callback function for streaming responses.
-
debug_output(bool, default:False) –Enable debug output.
Attributes:
-
llama_generation_settings(Union[LlamaLLMGenerationSettings, LlamaCppServerGenerationSettings]) –Generation settings for Llama or LlamaCppServer.
-
grammar_cache(dict) –Cache for generated grammars.
-
system_prompt_template(PromptTemplate) –Template for the system prompt.
-
creation_prompt_template(PromptTemplate) –Template for the creation prompt.
-
llama_cpp_agent(LlamaCppAgent) –LlamaCppAgent instance for interaction.
-
streaming_callback(Callable[[StreamingResponse], None]) –Callback function for streaming responses.
Methods:
-
save–str): Save the agent's state to a file.
-
load_from_file–str, llama_llm, streaming_callback) -> StructuredOutputAgent: Load the agent's state from a file.
-
load_from_dict–dict) -> StructuredOutputAgent: Load the agent's state from a dictionary.
-
as_dict–Convert the agent's state to a dictionary.
-
create_object–Type[BaseModel], data: str = "") -> object: Create an object of the given model from the given data.
Source code in llama_cpp_agent/structured_output_agent.py
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 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 | |
__init__(llama_llm, messages_formatter_type=MessagesFormatterType.CHATML, custom_messages_formatter=None, streaming_callback=None, debug_output=False)
Initialize the StructuredOutputAgent.
Parameters:
-
llama_llm(Union[Llama, LlamaLLMSettings, LlamaCppEndpointSettings, OpenAIEndpointSettings]) –An instance of Llama, LlamaLLMSettings, or LlamaCppServerLLMSettings as LLM.
-
llama_generation_settings(Union[LlamaLLMGenerationSettings, LlamaCppGenerationSettings, OpenAIGenerationSettings]) –Generation settings for Llama or LlamaCppServer or OpenAIEndpoint.
-
messages_formatter_type(MessagesFormatterType, default:CHATML) –Type of messages formatter.
-
custom_messages_formatter(MessagesFormatter, default:None) –Custom messages formatter.
-
streaming_callback(Callable[[StreamingResponse], None], default:None) –Callback function for streaming responses.
-
debug_output(bool, default:False) –Enable debug output.
Source code in llama_cpp_agent/structured_output_agent.py
save(file_path)
Save the agent's state to a file.
Parameters:
-
file_path(str) –The path to the file.
Source code in llama_cpp_agent/structured_output_agent.py
as_dict()
Convert the agent's state to a dictionary.
Returns:
-
dict(dict) –The dictionary representation of the agent's state.
create_object(model, data='', llm_sampling_settings=None, returns_streaming_generator=False)
Creates an object of the given model from the given data.
Parameters:
-
model(Type[BaseModel]) –The model to create the object from.
-
data(str, default:'') –The data to create the object from.
Returns:
-
object(object) –The created object.
Source code in llama_cpp_agent/structured_output_agent.py
Misc
Messages Formatter
llama_cpp_agent.messages_formatter
MessagesFormatterType
Bases: Enum
Enum representing different types of predefined messages formatters.
Source code in llama_cpp_agent/messages_formatter.py
deepseek_r1_distill_qwen_chat_prompt_markers = {Roles.system: PromptMarkers('<|begin▁of▁sentence|>', ''), Roles.user: PromptMarkers('<|User|>', ''), Roles.assistant: PromptMarkers('<|Assistant|>', ''), Roles.tool: PromptMarkers('', '')}
module-attribute
Instruction:
{prompt}
Response:
get_predefined_messages_formatter(formatter_type)
Gets a predefined messages formatter based on the formatter type.
Parameters:
-
formatter_type(MessagesFormatterType) –The type of messages formatter.
Returns:
-
MessagesFormatter(MessagesFormatter) –The predefined messages formatter.
Source code in llama_cpp_agent/messages_formatter.py
Prompt template
llama_cpp_agent.llm_prompt_template
PromptTemplateField
dataclass
Data class representing a field in a prompt template.
Attributes:
-
name(str) –The name of the template field.
-
value(str) –The value associated with the template field.
Source code in llama_cpp_agent/llm_prompt_template.py
PromptTemplateFields
Class representing a collection of PromptTemplateField objects.
Methods:
-
add_field–str, value: str): Add a new field to the collection.
-
remove_field–str): Remove a field by name from the collection.
-
edit_field–str, new_value: str): Edit the value of an existing field.
-
find_field–str) -> PromptTemplateField: Find and return a field by name.
-
list_fields–Get a list of all fields in the collection.
-
get_fields_dict–Get a dictionary representation of the fields.
-
set_fields_from_dict–Dict[str, str]): Set the fields using a dictionary.
Attributes:
-
fields(List[PromptTemplateField]) –List of PromptTemplateField objects.
Source code in llama_cpp_agent/llm_prompt_template.py
add_field(name, value)
remove_field(name)
edit_field(name, new_value)
Edit the value of an existing field.
find_field(name)
list_fields()
get_fields_dict()
set_fields_from_dict(field_dict)
PromptTemplate
Class representing a prompt template.
Methods:
-
generate_prompt–Union[dict, PromptTemplateFields], remove_empty_template_field=True) -> str:
Class Methods
from_string(template_string: str) -> PromptTemplate: Create a PromptTemplate from a string. from_file(template_file: str) -> PromptTemplate: Create a PromptTemplate from a file.
Attributes:
-
template(str) –The template string containing placeholders.
Source code in llama_cpp_agent/llm_prompt_template.py
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 | |
__init__(template_file=None, template_string=None)
Initialize a PromptTemplate instance.
Parameters:
-
template_file(str, default:None) –The path to a file containing the template.
-
template_string(str, default:None) –The template string.
Source code in llama_cpp_agent/llm_prompt_template.py
from_string(template_string)
classmethod
Create a PromptTemplate instance from a string.
Parameters:
-
template_string(str) –The template string.
Returns:
-
PromptTemplate–Created PromptTemplate instance.
Source code in llama_cpp_agent/llm_prompt_template.py
from_file(template_file)
classmethod
Create a PromptTemplate instance from a file.
Parameters:
-
template_file(str) –The path to a file containing the template.
Returns:
-
PromptTemplate–Created PromptTemplate instance.
Source code in llama_cpp_agent/llm_prompt_template.py
generate_prompt(template_fields, remove_empty_template_field=True)
Generate a prompt by replacing placeholders in the template with values.
Parameters:
-
template_fields(Union[dict, PromptTemplateFields]) –The template fields.
-
remove_empty_template_field(bool, default:True) –If True, removes lines with empty placeholders.
Returns:
-
str(str) –The generated prompt.