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

Function chat_completion_handler

llama_cpp/llama_chat_format.py:613–755  ·  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]]] = [],
        seed: Optional[int] = None,
        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,
        logit_bias: Optional[Dict[str, float]] = None,
        logprobs: Optional[bool] = None,
        top_logprobs: Optional[int] = None,
        **kwargs,  # type: ignore
    )

Source from the content-addressed store, hash-verified

611 chat_formatter: ChatFormatter,
612) -> LlamaChatCompletionHandler:
613 def chat_completion_handler(
614 *,
615 llama: llama.Llama,
616 messages: List[llama_types.ChatCompletionRequestMessage],
617 functions: Optional[List[llama_types.ChatCompletionFunction]] = None,
618 function_call: Optional[llama_types.ChatCompletionRequestFunctionCall] = None,
619 tools: Optional[List[llama_types.ChatCompletionTool]] = None,
620 tool_choice: Optional[llama_types.ChatCompletionToolChoiceOption] = None,
621 temperature: float = 0.2,
622 top_p: float = 0.95,
623 top_k: int = 40,
624 min_p: float = 0.05,
625 typical_p: float = 1.0,
626 stream: bool = False,
627 stop: Optional[Union[str, List[str]]] = [],
628 seed: Optional[int] = None,
629 response_format: Optional[
630 llama_types.ChatCompletionRequestResponseFormat
631 ] = None,
632 max_tokens: Optional[int] = None,
633 presence_penalty: float = 0.0,
634 frequency_penalty: float = 0.0,
635 repeat_penalty: float = 1.1,
636 tfs_z: float = 1.0,
637 mirostat_mode: int = 0,
638 mirostat_tau: float = 5.0,
639 mirostat_eta: float = 0.1,
640 model: Optional[str] = None,
641 logits_processor: Optional[llama.LogitsProcessorList] = None,
642 grammar: Optional[llama.LlamaGrammar] = None,
643 logit_bias: Optional[Dict[str, float]] = None,
644 logprobs: Optional[bool] = None,
645 top_logprobs: Optional[int] = None,
646 **kwargs, # type: ignore
647 ) -> Union[
648 llama_types.CreateChatCompletionResponse,
649 Iterator[llama_types.CreateChatCompletionStreamResponse],
650 ]:
651 result = chat_formatter(
652 messages=messages,
653 functions=functions,
654 function_call=function_call,
655 tools=tools,
656 tool_choice=tool_choice,
657 **kwargs,
658 )
659 prompt = llama.tokenize(
660 result.prompt.encode("utf-8"),
661 add_bos=not result.added_special,
662 special=True,
663 )
664 if result.stop is not None:
665 stop = [] if stop is None else [stop] if isinstance(stop, str) else stop
666 rstop = result.stop if isinstance(result.stop, list) else [result.stop]
667 stop = stop + rstop
668
669 stopping_criteria = None
670 if result.stopping_criteria is not None:

Callers

nothing calls this directly

Calls 8

from_json_schemaMethod · 0.80
from_stringMethod · 0.80
create_completionMethod · 0.80
tokenizeMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…