r"""Modifies the content of the message to include the instruction of using the response format. The message will not be modified in the following cases: - response_format is None - message content is not a string - message role is assistant Args: response_format (O
(
message: OpenAIMessage,
response_format: Optional[Type[BaseModel]],
)
| 20 | |
| 21 | |
| 22 | def try_modify_message_with_format( |
| 23 | message: OpenAIMessage, |
| 24 | response_format: Optional[Type[BaseModel]], |
| 25 | ) -> None: |
| 26 | r"""Modifies the content of the message to include the instruction of using |
| 27 | the response format. |
| 28 | |
| 29 | The message will not be modified in the following cases: |
| 30 | - response_format is None |
| 31 | - message content is not a string |
| 32 | - message role is assistant |
| 33 | |
| 34 | Args: |
| 35 | response_format (Optional[Type[BaseModel]]): The Pydantic model class. |
| 36 | message (OpenAIMessage): The message to be modified. |
| 37 | """ |
| 38 | if response_format is None: |
| 39 | return |
| 40 | |
| 41 | if not isinstance(message["content"], str): |
| 42 | return |
| 43 | |
| 44 | if message["role"] == "assistant": |
| 45 | return |
| 46 | |
| 47 | json_schema = response_format.model_json_schema() |
| 48 | updated_prompt = textwrap.dedent( |
| 49 | f"""\ |
| 50 | {message["content"]} |
| 51 | |
| 52 | Please generate a JSON response adhering to the following JSON schema: |
| 53 | {json_schema} |
| 54 | Make sure the JSON response is valid and matches the EXACT structure defined in the schema. Your result should ONLY be a valid json object, WITHOUT ANY OTHER TEXT OR COMMENTS. |
| 55 | """ # noqa: E501 |
| 56 | ) |
| 57 | message["content"] = updated_prompt |
no test coverage detected