fireworks-ai-llama-3-firefunction-v2.jinja 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. {%- set loop_messages = messages -%}
  2. {%- set message_roles = ['system', 'user', 'assistant', 'tool'] -%}
  3. {%- set system_prompt_suffix -%}
  4. {%- filter trim -%}
  5. In addition to plain text responses, you can chose to call one or more of the provided functions.
  6. Use the following rule to decide when to call a function:
  7. * if the response can be generated from your internal knowledge (e.g., as in the case of queries like "What is the capital of Poland?"), do so
  8. * if you need external information that can be obtained by calling one or more of the provided functions, generate a function calls
  9. If you decide to call functions:
  10. * prefix function calls with functools marker (no closing marker required)
  11. * all function calls should be generated in a single JSON list formatted as functools[{"name": [function name], "arguments": [function arguments as JSON]}, ...]
  12. * follow the provided JSON schema. Do not hallucinate arguments or values. Do to blindly copy values from the provided samples
  13. * respect the argument type formatting. E.g., if the type if number and format is float, write value 7 as 7.0
  14. * make sure you pick the right functions that match the user intent
  15. Available functions as JSON spec:
  16. {%- endfilter -%}
  17. {%- endset -%}
  18. {%- set system_prompt_suffix = system_prompt_suffix + "\n" + functions -%}
  19. {%- set system_prompt_suffix = system_prompt_suffix + '\nToday is ' + datetime + '.' -%}
  20. {%- set ns = namespace(role='', content='') -%}
  21. {#- Basic consistency checks -#}
  22. {%- if not loop_messages -%}
  23. {{ raise_exception('Expected non-empty messages') }}
  24. {%- endif -%}
  25. {%- for message in loop_messages -%}
  26. {%- set ns.role = message['role'] | lower -%}
  27. {%- if ns.role not in message_roles -%}
  28. {%- set message_roles_string = message_roles | join(', ') -%}
  29. {{ raise_exception('Invalid role ' + message['role'] + '. Only ' + message_roles_string + ' are supported.') }}
  30. {%- endif -%}
  31. {%- set msg_content = message['content'] | default('', true) | trim -%}
  32. {%- if loop.index0 == 0 -%}
  33. {%- if ns.role == 'system' -%}
  34. {%- set system_prompt = '<|start_header_id|>' + 'system' + '<|end_header_id|>\n\n' + message['content'] | trim + '\n' + system_prompt_suffix + '<|eot_id|>' -%}
  35. {%- else -%}
  36. {%- set system_prompt = '<|start_header_id|>' + 'system' + '<|end_header_id|>\n\nYou are a helpful assistant with access to functions.\n' + system_prompt_suffix + '<|eot_id|>' -%}
  37. {%- endif -%}
  38. {%- set ns.content = bos_token + system_prompt -%}
  39. {{- ns.content -}}
  40. {%- endif -%}
  41. {%- if loop.index0 > 0 or ns.role != 'system' -%}
  42. {%- set ns.content = '<|start_header_id|>' + ns.role + '<|end_header_id|>\n\n' + msg_content -%}
  43. {%- if 'tool_calls' in message and message['tool_calls'] -%}
  44. {%- set tool = namespace(calls=[]) -%}
  45. {%- for call in message['tool_calls'] -%}
  46. {%- set tool.calls = tool.calls + ['{"name": "' + call['function']['name'] + '", "arguments": ' + call['function']['arguments'] + '}'] -%}
  47. {%- endfor -%}
  48. {%- set ns.content = ns.content + ' functools[' + tool.calls | join(', ') + ']' -%}
  49. {%- endif -%}
  50. {%- set ns.content = ns.content + '<|eot_id|>' -%}
  51. {{- ns.content -}}
  52. {%- endif -%}
  53. {%- endfor -%}
  54. {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}