| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | from llama_models.llama3.api.datatypes import * # noqa: F403 |
| 8 | from llama_stack.apis.inference import * # noqa: F403 |
| 9 | from llama_models.llama3.prompt_templates import ( |
| 10 | BuiltinToolGenerator, |
| 11 | FunctionTagCustomToolGenerator, |
| 12 | JsonCustomToolGenerator, |
| 13 | SystemDefaultGenerator, |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | def prepare_messages(request: ChatCompletionRequest) -> List[Message]: |
| 18 | |
| 19 | assert request.tool_choice == ToolChoice.auto, "Only `ToolChoice.auto` supported" |
| 20 | |
| 21 | existing_messages = request.messages |
| 22 | existing_system_message = None |
| 23 | if existing_messages[0].role == Role.system.value: |
| 24 | existing_system_message = existing_messages.pop(0) |
| 25 | |
| 26 | assert ( |
| 27 | existing_messages[0].role != Role.system.value |
| 28 | ), "Should only have 1 system message" |
| 29 | |
| 30 | messages = [] |
| 31 | |
| 32 | default_gen = SystemDefaultGenerator() |
| 33 | default_template = default_gen.gen() |
| 34 | |
| 35 | sys_content = "" |
| 36 | |
| 37 | tool_template = None |
| 38 | if request.tools: |
| 39 | tool_gen = BuiltinToolGenerator() |
| 40 | tool_template = tool_gen.gen(request.tools) |
| 41 | |
| 42 | sys_content += tool_template.render() |
| 43 | sys_content += "\n" |
| 44 | |
| 45 | sys_content += default_template.render() |
| 46 | |
| 47 | if existing_system_message: |
| 48 | # TODO: this fn is needed in many places |
| 49 | def _process(c): |
| 50 | if isinstance(c, str): |
| 51 | return c |
| 52 | else: |
| 53 | return "<media>" |
| 54 | |
| 55 | sys_content += "\n" |
| 56 | |
| 57 | if isinstance(existing_system_message.content, str): |
| 58 | sys_content += _process(existing_system_message.content) |
| 59 | elif isinstance(existing_system_message.content, list): |
| 60 | sys_content += "\n".join( |
| 61 | [_process(c) for c in existing_system_message.content] |
| 62 | ) |
| 63 | |
| 64 | messages.append(SystemMessage(content=sys_content)) |
| 65 | |
| 66 | has_custom_tools = any(isinstance(dfn.tool_name, str) for dfn in request.tools) |
| 67 | if has_custom_tools: |
| 68 | if request.tool_prompt_format == ToolPromptFormat.json: |
| 69 | tool_gen = JsonCustomToolGenerator() |
| 70 | elif request.tool_prompt_format == ToolPromptFormat.function_tag: |
| 71 | tool_gen = FunctionTagCustomToolGenerator() |
| 72 | else: |
| 73 | raise ValueError( |
| 74 | f"Non supported ToolPromptFormat {request.tool_prompt_format}" |
| 75 | ) |
| 76 | |
| 77 | custom_tools = [t for t in request.tools if isinstance(t.tool_name, str)] |
| 78 | custom_template = tool_gen.gen(custom_tools) |
| 79 | messages.append(UserMessage(content=custom_template.render())) |
| 80 | |
| 81 | # Add back existing messages from the request |
| 82 | messages += existing_messages |
| 83 | |
| 84 | return messages |