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

Function functionary_chat_handler

llama_cpp/llama_chat_format.py:1461–1819  ·  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,
    **kwargs,  # type: ignore
)

Source from the content-addressed store, hash-verified

1459
1460@register_chat_completion_handler("functionary")
1461def functionary_chat_handler(
1462 llama: llama.Llama,
1463 messages: List[llama_types.ChatCompletionRequestMessage],
1464 functions: Optional[List[llama_types.ChatCompletionFunction]] = None,
1465 function_call: Optional[llama_types.ChatCompletionRequestFunctionCall] = None,
1466 tools: Optional[List[llama_types.ChatCompletionTool]] = None,
1467 tool_choice: Optional[llama_types.ChatCompletionToolChoiceOption] = None,
1468 temperature: float = 0.2,
1469 top_p: float = 0.95,
1470 top_k: int = 40,
1471 min_p: float = 0.05,
1472 typical_p: float = 1.0,
1473 stream: bool = False,
1474 stop: Optional[Union[str, List[str]]] = [],
1475 response_format: Optional[llama_types.ChatCompletionRequestResponseFormat] = None,
1476 max_tokens: Optional[int] = None,
1477 presence_penalty: float = 0.0,
1478 frequency_penalty: float = 0.0,
1479 repeat_penalty: float = 1.1,
1480 tfs_z: float = 1.0,
1481 mirostat_mode: int = 0,
1482 mirostat_tau: float = 5.0,
1483 mirostat_eta: float = 0.1,
1484 model: Optional[str] = None,
1485 logits_processor: Optional[llama.LogitsProcessorList] = None,
1486 grammar: Optional[llama.LlamaGrammar] = None,
1487 **kwargs, # type: ignore
1488) -> Union[llama_types.ChatCompletion, Iterator[llama_types.ChatCompletionChunk]]:
1489 SYSTEM_MESSAGE = """A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. The assistant calls functions with appropriate input when necessary"""
1490
1491 def generate_type_definition(
1492 param: Dict[str, llama_types.JsonType], indent_level: int, shared_defs
1493 ) -> str:
1494 indent = " " * indent_level
1495 if "$ref" in param:
1496 # Reference to a shared definition
1497 ref_name = param["$ref"].split("/")[
1498 -1
1499 ] # Extract the type name from the reference
1500 return ref_name
1501 elif param.get("type") == "array":
1502 items = param.get("items", {})
1503 item_type = generate_type_definition(items, indent_level + 1, shared_defs)
1504 return f"Array<{item_type}>"
1505 elif param.get("type") == "object":
1506 properties = param.get("properties", {})
1507 nested_schema = "{\n"
1508 for nested_param_name, nested_param in properties.items():
1509 nested_param_type = generate_type_definition(
1510 nested_param, indent_level + 1, shared_defs
1511 )
1512 nested_schema += (
1513 f"{indent} {nested_param_name}: {nested_param_type},\n"
1514 )
1515 nested_schema += indent + "}"
1516 return nested_schema
1517 elif "enum" in param:
1518 # Enum type

Callers

nothing calls this directly

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…