Improved JSON prompting for GPT-4 and recover incomplete JSON responses from Code Llama

This commit is contained in:
Nicholas Albion
2023-09-27 10:53:44 +10:00
parent cf97a1be5e
commit 6dd5a032fa
8 changed files with 108 additions and 67 deletions

View File

@@ -70,8 +70,7 @@ def parse_agent_response(response, function_calls: FunctionCallSet | None):
"""
if function_calls:
text = re.sub(r'^.*```json\s*', '', response['text'], flags=re.DOTALL)
text = text.strip('` \n')
text = response['text']
values = list(json.loads(text).values())
if len(values) == 1:
return values[0]
@@ -140,7 +139,7 @@ class JsonPrompter:
return "\n".join(
self.function_descriptions(functions, function_to_call)
+ [
"The response MUST be a JSON object matching this schema:",
"Here is the schema for the expected JSON object:",
"```json",
self.function_parameters(functions, function_to_call),
"```",
@@ -194,7 +193,7 @@ class JsonPrompter:
system = (
"Help choose the appropriate function to call to answer the user's question."
if function_to_call is None
else f"Define the arguments for {function_to_call} to answer the user's question."
else f"Please provide a JSON object that defines the arguments for the `{function_to_call}` function to answer the user's question."
) + "\nThe response must contain ONLY the JSON object, with NO additional text or explanation."
data = (
@@ -202,11 +201,6 @@ class JsonPrompter:
if function_to_call
else self.functions_summary(functions)
)
response_start = (
f"Here are the arguments for the `{function_to_call}` function: ```json\n"
if function_to_call
else "Here's the function the user should call: "
)
if self.is_instruct:
return f"[INST] <<SYS>>\n{system}\n\n{data}\n<</SYS>>\n\n{prompt} [/INST]"