MCPcopy Index your code
hub / github.com/abetlen/llama-cpp-python / chatml_function_calling

Function chatml_function_calling

llama_cpp/llama_chat_format.py:4131–4574  ·  view source on GitHub ↗
(
    llama: llama.Llama,
    messages: List[llama_types.ChatCompletionRequestMessage],
    functions: Optional[List[llama_types.ChatCompletionFunction]] = None,
    function_call: Optional[llama_types.ChatCompletionRequestFunctionCall] = None,
    tools: Optional[List[llama_types.ChatCompletionTool]] = None,
    tool_choice: Optional[llama_types.ChatCompletionToolChoiceOption] = None,
    temperature: float = 0.2,
    top_p: float = 0.95,
    top_k: int = 40,
    min_p: float = 0.05,
    typical_p: float = 1.0,
    stream: bool = False,
    stop: Optional[Union[str, List[str]]] = [],
    response_format: Optional[llama_types.ChatCompletionRequestResponseFormat] = None,
    max_tokens: Optional[int] = None,
    presence_penalty: float = 0.0,
    frequency_penalty: float = 0.0,
    repeat_penalty: float = 1.1,
    tfs_z: float = 1.0,
    mirostat_mode: int = 0,
    mirostat_tau: float = 5.0,
    mirostat_eta: float = 0.1,
    model: Optional[str] = None,
    logits_processor: Optional[llama.LogitsProcessorList] = None,
    grammar: Optional[llama.LlamaGrammar] = None,
    logprobs: Optional[bool] = None,
    top_logprobs: Optional[int] = None,
    **kwargs,  # type: ignore
)

Source from the content-addressed store, hash-verified

4129
4130@register_chat_completion_handler("chatml-function-calling")
4131def chatml_function_calling(
4132 llama: llama.Llama,
4133 messages: List[llama_types.ChatCompletionRequestMessage],
4134 functions: Optional[List[llama_types.ChatCompletionFunction]] = None,
4135 function_call: Optional[llama_types.ChatCompletionRequestFunctionCall] = None,
4136 tools: Optional[List[llama_types.ChatCompletionTool]] = None,
4137 tool_choice: Optional[llama_types.ChatCompletionToolChoiceOption] = None,
4138 temperature: float = 0.2,
4139 top_p: float = 0.95,
4140 top_k: int = 40,
4141 min_p: float = 0.05,
4142 typical_p: float = 1.0,
4143 stream: bool = False,
4144 stop: Optional[Union[str, List[str]]] = [],
4145 response_format: Optional[llama_types.ChatCompletionRequestResponseFormat] = None,
4146 max_tokens: Optional[int] = None,
4147 presence_penalty: float = 0.0,
4148 frequency_penalty: float = 0.0,
4149 repeat_penalty: float = 1.1,
4150 tfs_z: float = 1.0,
4151 mirostat_mode: int = 0,
4152 mirostat_tau: float = 5.0,
4153 mirostat_eta: float = 0.1,
4154 model: Optional[str] = None,
4155 logits_processor: Optional[llama.LogitsProcessorList] = None,
4156 grammar: Optional[llama.LlamaGrammar] = None,
4157 logprobs: Optional[bool] = None,
4158 top_logprobs: Optional[int] = None,
4159 **kwargs, # type: ignore
4160) -> Union[
4161 llama_types.CreateChatCompletionResponse,
4162 Iterator[llama_types.CreateChatCompletionStreamResponse],
4163]:
4164 function_calling_template = (
4165 "{% for message in messages %}"
4166 "<|im_start|>{{ message.role }}\n"
4167 # System message
4168 "{% if message.role == 'system' %}"
4169 "{{ message.content }}"
4170 "{% if tool_calls %}"
4171 "\n\nYou have access to the following functions:\n"
4172 "{% for tool in tools %}"
4173 "\nfunctions.{{ tool.function.name }}:\n"
4174 "{{ tool.function.parameters | tojson }}"
4175 "\n{% endfor %}"
4176 "\n\nYou can respond to users messages with either a single message or one or more function calls."
4177 "\n\nTo respond with a message begin the message with 'message:', use the following format:"
4178 "\n\nmessage:"
4179 "\n<message>"
4180 "\n\nTo respond with one or more function calls begin the message with 'functions.<function_name>:', use the following format:"
4181 "\n\nfunctions.<function_name>:"
4182 '\n{ "arg1": "value1", "arg2": "value2" }'
4183 "\nfunctions.<function_name>:"
4184 '\n{ "arg1": "value1", "arg2": "value2" }'
4185 "{% endif %}"
4186 "<|im_end|>\n"
4187 "{% endif %}"
4188 # User message

Callers

nothing calls this directly

Calls 8

from_stringMethod · 0.80
create_completionMethod · 0.80
from_json_schemaMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…