Support JSON_OBJECT mode from Anyscale (#275)

This commit is contained in:
Jason Liu
2023-12-13 00:05:54 -05:00
committed by GitHub
parent cb5385c313
commit b2a38ffe10
15 changed files with 115 additions and 98 deletions
+18 -16
View File
@@ -91,26 +91,28 @@ def handle_response_model(
"type": "function",
"function": {"name": response_model.openai_schema["name"]},
}
elif mode == Mode.JSON or mode == Mode.MD_JSON:
elif mode in {Mode.JSON, Mode.MD_JSON, Mode.JSON_SCHEMA}:
# If its a JSON Mode we need to massage the prompt a bit
# in order to get the response we want in a json format
message = f"""
As a genius expert, your task is to understand the content and provide
the parsed objects in json that match the following json_schema:\n
{response_model.model_json_schema()['properties']}
"""
# Check for nested models
if "$defs" in response_model.model_json_schema():
message += f"\nHere are some more definitions to adhere too:\n{response_model.model_json_schema()['$defs']}"
if mode == Mode.JSON:
new_kwargs["response_format"] = {"type": "json_object"}
message = f"""Make sure that your response to any message matches the json_schema below,
do not deviate at all: \n{response_model.model_json_schema()['properties']}
"""
# Check for nested models
if "$defs" in response_model.model_json_schema():
message += f"\nHere are some more definitions to adhere too:\n{response_model.model_json_schema()['$defs']}"
else:
message = f"""
As a genius expert, your task is to understand the content and provide
the parsed objects in json that match the following json_schema (do not deviate at all and its okay if you cant be exact):\n
{response_model.model_json_schema()['properties']}
"""
# Check for nested models
if "$defs" in response_model.model_json_schema():
message += f"\nHere are some more definitions to adhere too:\n{response_model.model_json_schema()['$defs']}"
elif mode == Mode.JSON_SCHEMA:
new_kwargs["response_format"] = {
"type": "json_object",
"schema": response_model.model_json_schema(),
}
elif mode == Mode.MD_JSON:
new_kwargs["messages"].append(
{
"role": "assistant",