Skip to content

Commit

Permalink
Fixed the system prompt for JSON mode, enabling the use of Pydantic n…
Browse files Browse the repository at this point in the history
…ested models.
  • Loading branch information
aastroza committed Dec 4, 2023
1 parent 7a9f57b commit 0cdad68
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions instructor/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ def handle_response_model(
elif mode == Mode.JSON or mode == Mode.MD_JSON:
if mode == Mode.JSON:
new_kwargs["response_format"] = {"type": "json_object"}
# check that the first message is a system message
# if it is not, add a system message to the beginning
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 response_model.model_json_schema()['$defs']:
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']}\n
references: \n{response_model.model_json_schema()['$defs']}\n
"""
else:
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']}
"""
else:
message = f"""
As a genius expert, your task is to understand the content and provide
Expand All @@ -110,6 +115,8 @@ def handle_response_model(
},
)
new_kwargs["stop"] = "```"
# check that the first message is a system message
# if it is not, add a system message to the beginning
if new_kwargs["messages"][0]["role"] != "system":
new_kwargs["messages"].insert(
0,
Expand Down

0 comments on commit 0cdad68

Please sign in to comment.