AlexHung29629 commited on
Commit
a058d44
·
verified ·
1 Parent(s): b1780e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -12
app.py CHANGED
@@ -18,28 +18,115 @@ MAX_MAX_NEW_TOKENS = 2048
18
  DEFAULT_MAX_NEW_TOKENS = 1024
19
  MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
20
 
21
- CHAT_TEMPLATE="""{%- set default_system_message = "A user will ask you to solve a task. You should first draft your thinking process (inner monologue) until you have derived the final answer. Afterwards, write a self-contained summary of your thoughts (i.e. your summary should be succinct but contain all the critical steps you needed to reach the conclusion). You should use Markdown and Latex to format your response. Write both your thoughts and summary in the same language as the task posed by the user.\n\nYour thinking process must follow the template below:\n<think>\nYour thoughts or/and draft, like working through an exercise on scratch paper.\n</think>\n\nHere, provide a concise summary that reflects your reasoning and presents a clear final answer to the user.\n\nProblem:" %}
22
-
23
  {{- bos_token }}
24
-
25
  {%- if messages[0]['role'] == 'system' %}
26
- {%- set system_message = messages[0]['content'] %}
27
- {%- set loop_messages = messages[1:] %}
 
 
 
 
 
28
  {%- else %}
29
  {%- set system_message = default_system_message %}
30
  {%- set loop_messages = messages %}
31
  {%- endif %}
 
 
 
 
 
 
 
 
 
 
32
  {{- '[SYSTEM_PROMPT]' + system_message + '[/SYSTEM_PROMPT]' }}
33
-
34
  {%- for message in loop_messages %}
35
- {%- if message['role'] == 'user' %}
36
- {{- '[INST]' + message['content'] + '[/INST]' }}
37
- {%- elif message['role'] == 'system' %}
38
- {{- '[SYSTEM_PROMPT]' + message['content'] + '[/SYSTEM_PROMPT]' }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  {%- elif message['role'] == 'assistant' %}
40
- {{- message['content'] + eos_token }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  {%- else %}
42
- {{- raise_exception('Only user, system and assistant roles are supported!') }}
43
  {%- endif %}
44
  {%- endfor %}"""
45
 
 
18
  DEFAULT_MAX_NEW_TOKENS = 1024
19
  MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
20
 
21
+ CHAT_TEMPLATE="""{%- set today = strftime_now("%Y-%m-%d") %}
22
+ {%- set default_system_message = "Your knowledge base was last updated on 2023-10-01.\nThe current date is {today}.\n\nWhen you\'re not sure about some information or when the user\'s request requires up-to-date or specific data, you must use the available tools to fetch the information. Do not hesitate to use tools whenever they can provide a more accurate or complete response. If no relevant tools are available, then clearly state that you don\'t have the information and avoid making up anything.\nIf the user\'s question is not clear, ambiguous, or does not provide enough context for you to accurately answer the question, you do not try to answer it right away and you rather ask the user to clarify their request (e.g. \"What are some good restaurants around me?\" => \"Where are you?\" or \"When is the next flight to Tokyo\" => \"Where do you travel from?\").\nYou are always very attentive to dates, in particular you try to resolve dates and when asked about information at specific dates, you discard information that is at another date.\nYou follow these instructions in all languages, and always respond to the user in the language they use or request.\nNext sections describe the capabilities that you have.\n\n\n# MULTI-MODAL INSTRUCTIONS\n\nYou have the ability to read images, but you cannot generate images. You also cannot transcribe audio files or videos.\nYou cannot read nor transcribe audio files or videos." %}
23
  {{- bos_token }}
 
24
  {%- if messages[0]['role'] == 'system' %}
25
+ {%- if messages[0]['content'] is string %}
26
+ {%- set system_message = messages[0]['content'] %}
27
+ {%- set loop_messages = messages[1:] %}
28
+ {%- else %}
29
+ {%- set system_message = messages[0]['content'][0]['text'] %}
30
+ {%- set loop_messages = messages[1:] %}
31
+ {%- endif %}
32
  {%- else %}
33
  {%- set system_message = default_system_message %}
34
  {%- set loop_messages = messages %}
35
  {%- endif %}
36
+ {%- if not tools is defined %}
37
+ {%- set tools = none %}
38
+ {%- elif tools is not none %}
39
+ {%- set parallel_tool_prompt = "# TOOL CALLING INSTRUCTIONS\n\nYou may have access to tools that you can use to fetch information or perform actions. You must use these tools in the following situations:\n\n1. When the request requires up-to-date information.\n2. When the request requires specific data that you do not have in your knowledge base.\n3. When the request involves actions that you cannot perform without tools.\n\nAlways prioritize using tools to provide the most accurate and helpful response. If tools are not available, inform the user that you cannot perform the requested action at the moment." %}
40
+ {%- if system_message is defined %}
41
+ {%- set system_message = system_message + "\n\n" + parallel_tool_prompt %}
42
+ {%- else %}
43
+ {%- set system_message = parallel_tool_prompt %}
44
+ {%- endif %}
45
+ {%- endif %}
46
  {{- '[SYSTEM_PROMPT]' + system_message + '[/SYSTEM_PROMPT]' }}
47
+ {%- set user_messages = loop_messages | selectattr("role", "equalto", "user") | list %}
48
  {%- for message in loop_messages %}
49
+ {%- if message["role"] == "user" %}
50
+ {%- if tools is not none and (message == user_messages[-1]) %}
51
+ {{- "[AVAILABLE_TOOLS] [" }}
52
+ {%- for tool in tools %}
53
+ {%- set tool = tool.function %}
54
+ {{- '{"type": "function", "function": {' }}
55
+ {%- for key, val in tool.items() if key != "return" %}
56
+ {%- if val is string %}
57
+ {{- '"' + key + '": "' + val + '"' }}
58
+ {%- else %}
59
+ {{- '"' + key + '": ' + val|tojson }}
60
+ {%- endif %}
61
+ {%- if not loop.last %}
62
+ {{- ", " }}
63
+ {%- endif %}
64
+ {%- endfor %}
65
+ {{- "}}" }}
66
+ {%- if not loop.last %}
67
+ {{- ", " }}
68
+ {%- else %}
69
+ {{- "]" }}
70
+ {%- endif %}
71
+ {%- endfor %}
72
+ {{- "[/AVAILABLE_TOOLS]" }}
73
+ {%- endif %}
74
+ {%- if message['content'] is string %}
75
+ {{- '[INST]' + message['content'] + '[/INST]<think>\n' }}
76
+ {%- else %}
77
+ {{- '[INST]' }}
78
+ {%- for block in message['content'] %}
79
+ {%- if block['type'] == 'text' %}
80
+ {{- block['text'] }}
81
+ {%- elif block['type'] == 'image' or block['type'] == 'image_url' %}
82
+ {{- '[IMG]' }}
83
+ {%- else %}
84
+ {{- raise_exception('Only text and image blocks are supported in message content!') }}
85
+ {%- endif %}
86
+ {%- endfor %}
87
+ {{- '[/INST]<think>\n' }}
88
+ {%- endif %}
89
+ {%- if enable_thinking is defined %}
90
+ {%- if enable_thinking is false %}
91
+ {{- 'Answer directly</think>\n' }}
92
+ {%- endif %}
93
+ {%- endif %}
94
+ {%- elif message["role"] == "tool_calls" or message.tool_calls is defined %}
95
+ {%- if message.tool_calls is defined %}
96
+ {%- set tool_calls = message.tool_calls %}
97
+ {%- else %}
98
+ {%- set tool_calls = message.content %}
99
+ {%- endif %}
100
+
101
+ {%- for tool_call in tool_calls %}
102
+ {{- "[TOOL_CALLS]" }}
103
+ {{- tool_call.function.name }}
104
+ {%- if not tool_call.id is defined or tool_call.id|length < 9 %}
105
+ {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (1)" + tool_call.id) }}
106
+ {%- endif %}
107
+ {{- '[CALL_ID]' + tool_call.id[-9:] }}
108
+ {{- '[ARGS]' + tool_call.function.arguments|tojson }}
109
+ {%- endfor %}
110
+ {{- eos_token }}
111
  {%- elif message['role'] == 'assistant' %}
112
+ {%- if message['content'] is string %}
113
+ {{- message['content'] + eos_token }}
114
+ {%- else %}
115
+ {{- message['content'][0]['text'] + eos_token }}
116
+ {%- endif %}
117
+ {%- elif message["role"] == "tool_results" or message["role"] == "tool" %}
118
+ {%- if message.content is defined and message.content.content is defined %}
119
+ {%- set content = message.content.content %}
120
+ {%- else %}
121
+ {%- set content = message.content %}
122
+ {%- endif %}
123
+ {{- '[TOOL_RESULTS] {"content": ' + content|string + ", " }}
124
+ {%- if not message.tool_call_id is defined or message.tool_call_id|length < 9 %}
125
+ {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9! (2)" + message.tool_call_id) }}
126
+ {%- endif %}
127
+ {{- '"call_id": "' + message.tool_call_id[-9:] + '"}[/TOOL_RESULTS]' }}
128
  {%- else %}
129
+ {{- raise_exception("Only user and assistant roles are supported, with the exception of an initial optional system message!") }}
130
  {%- endif %}
131
  {%- endfor %}"""
132