@@ -74,86 +74,76 @@ def make_fct(f):
7474@lmql .query
7575async def fct_call (fcts ):
7676 '''lmql
77- incontext
78- action_fcts = {str(f.__name__): make_fct(f) for f in fcts}
79- "[CALL]"
80- truncated = CALL
81- if not CALL.endswith("|") and not CALL.endswith(DELIMITER_END):
77+ action_fcts = {str(f.__name__): make_fct(f) for f in fcts}
78+ "[CALL]" where STOPS_AT(CALL, "|") and STOPS_AT(CALL, DELIMITER_END)
79+
80+ truncated = CALL
81+ if not CALL.endswith("|") and not CALL.endswith(DELIMITER_END):
82+ return CALL
83+ else:
84+ if CALL.endswith(DELIMITER_END):
85+ CALL = CALL[:-len(DELIMITER_END)]
86+ else:
87+ CALL = CALL[:-len("|")]
88+
89+ if "(" not in CALL:
8290 return CALL
83- else:
84- if CALL.endswith(DELIMITER_END):
85- CALL = CALL[:-len(DELIMITER_END)]
86- else:
87- CALL = CALL[:-len("|")]
88-
89- if "(" not in CALL:
90- return CALL
91-
92- action, args = CALL.split("(", 1)
93- action = action.strip()
94- if action not in action_fcts.keys():
95- print("unknown action", [action], list(action_fcts.keys()))
96- " Unknown action: {action} {DELIMITER_END}"
97- result = ""
91+
92+ action, args = CALL.split("(", 1)
93+ action = action.strip()
94+ if action not in action_fcts.keys():
95+ print("unknown action", [action], list(action_fcts.keys()))
96+ " Unknown action: {action} {DELIMITER_END}"
97+ result = ""
98+ return "(error)"
99+ else:
100+ try:
101+ result = await action_fcts[action].call("(" + args.strip())
102+ return DELIMITER + str(CALL) + "| " + str(result)
103+ except Exception:
104+ result = "Error."
98105 return "(error)"
99- else:
100- try:
101- result = await action_fcts[action].call("(" + args.strip())
102- return DELIMITER + str(CALL) + "| " + str(result)
103- except Exception:
104- result = "Error."
105- return "(error)"
106- where
107- STOPS_AT(CALL, "|") and STOPS_AT(CALL, DELIMITER_END)
108106 '''
109107
110108
111109@lmql .query
112110async def inline_segment (fcts ):
113111 '''lmql
114- incontext
115- "[SEGMENT]"
116- if not SEGMENT.endswith(DELIMITER):
117- return SEGMENT
118- else:
119- "[CALL]"
120- result = CALL.split("|", 1)[1]
121- return SEGMENT[:-len(DELIMITER)] + CALL + DELIMITER_END
122- where
123- STOPS_AT(SEGMENT, DELIMITER) and fct_call(CALL, fcts)
112+ "[SEGMENT]" where STOPS_AT(SEGMENT, DELIMITER)
113+ if not SEGMENT.endswith(DELIMITER):
114+ return SEGMENT
115+ else:
116+ "[CALL]" where fct_call(CALL, fcts) and len(TOKENS(CALL)) > 0
117+ result = CALL.split("|", 1)[1]
118+ return SEGMENT[:-len(DELIMITER)] + CALL + DELIMITER_END
124119 '''
125120
126121@lmql .query
127- async def inline_use (fcts ):
122+ async def inline_use (fcts , instruct = True ):
128123 '''lmql
129- incontext
130- action_fcts = {str(f.__name__): make_fct(f) for f in fcts}
131- first_tool_name = list(action_fcts.keys())[0] if len(action_fcts) > 0 else "tool"
124+ action_fcts = {str(f.__name__): make_fct(f) for f in fcts}
125+ first_tool_name = list(action_fcts.keys())[0] if len(action_fcts) > 0 else "tool"
132126
133- # add instruction prompt if no few-shot prompt was already used
134- if not INLINE_USE_PROMPT in context.prompt:
135- """
136- \n \n {:system} Instructions: In your reasoning, you can use the following tools:"""
137-
138- for fct in action_fcts.values():
139- "\n - {fct.name}: {fct.description} Usage: {DELIMITER}{fct.example} | {fct.example_result}{DELIMITER_END}"
140- ' Example Use: ... this means they had <<calc("5-2") | 3 >> 3 apples left...\n '
141- " You can also use the tools multiple times in one reasoning step.\n \n "
142- "Reasoning with Tools: {:assistant}"
143- else:
144- "\n \n Inline Tool Use:\n \n "
127+ # add instruction prompt if no few-shot prompt was already used
128+ if instruct and not INLINE_USE_PROMPT in context.prompt:
129+ """
130+ \n \n Instructions: In your reasoning, you can use the following tools:"""
145131
146- # decode segment-by-segment, handling action calls along the way
147- truncated = ""
148- while True:
149- "[SEGMENT]"
150- if not SEGMENT.endswith(DELIMITER_END):
151- " " # seems to be needed for now
152- return truncated + SEGMENT
153- truncated += SEGMENT
154- return truncated
155- where
156- inline_segment(SEGMENT, fcts)
132+ for fct in action_fcts.values():
133+ "\n - {fct.name}: {fct.description} Usage: {DELIMITER}{fct.example} | {fct.example_result}{DELIMITER_END}"
134+ ' Example Use: ... this means they had <<calc("5-2") | 3 >> 3 apples left...\n '
135+ " You can also use the tools multiple times in one reasoning step.\n \n "
136+ "Reasoning with Tools:\n \n "
137+
138+ # decode segment-by-segment, handling action calls along the way
139+ truncated = ""
140+ while True:
141+ "[SEGMENT]" where inline_segment(SEGMENT, fcts)
142+ if not SEGMENT.endswith(DELIMITER_END):
143+ " " # seems to be needed for now
144+ return truncated + SEGMENT
145+ truncated += SEGMENT
146+ return truncated
157147 '''
158148inline_use .demonstrations = INLINE_USE_PROMPT
159149
0 commit comments