Skip to content

Commit

Permalink
Optimize chat prompts (Remove JSON Output:) (#82)
Browse files Browse the repository at this point in the history
* Remove "JSON Output:" from prompt

* llm_providers: Explicitly append "JSON Output:" to non-chat completion prompts
  • Loading branch information
irgolic authored Mar 30, 2023
1 parent 86412f4 commit 42fc197
Show file tree
Hide file tree
Showing 10 changed files with 4 additions and 25 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ Given below is XML that describes the information to extract from this document
</output>

ONLY return a valid JSON object (no other text is necessary). The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise.

JSON Output:
```

Call the `Guard` object with the LLM API call as the first argument and add any additional arguments to the LLM API call as the remaining arguments.
Expand Down
2 changes: 0 additions & 2 deletions docs/rail/prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,4 @@ Given the following document, answer the following questions. If the answer does

```
ONLY return a valid JSON object (no other text is necessary). The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter `None`.
JSON Output:
```
10 changes: 2 additions & 8 deletions guardrails/constants.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ Given below is XML that describes the information to extract from this document

<json_suffix_prompt_v2>
ONLY return a valid JSON object (no other text is necessary). The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise. If you are unsure anywhere, enter "None".

JSON Output:
</json_suffix_prompt_v2>


<json_suffix_prompt_v2_wo_none>
ONLY return a valid JSON object (no other text is necessary). The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise.

JSON Output:
</json_suffix_prompt_v2_wo_none>


Expand All @@ -46,8 +42,7 @@ Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<![CDATA[<string name='foo' format='two-words lower-case' />`]]> => `{{{{'foo': 'example one'}}}}`
- `<![CDATA[<list name='bar'><string format='upper-case' /></list>]]>` => `{{{{"bar": ['STRING ONE', 'STRING TWO', etc.]}}}}`
- `<![CDATA[<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>]]>` => `{{{{'baz': {{{{'foo': 'Some String', 'index': 1}}}}}}}}`

JSON Object:</complete_json_suffix>
</complete_json_suffix>

<complete_json_suffix_v2>
Given below is XML that describes the information to extract from this document and the tags to extract it into.
Expand All @@ -60,8 +55,7 @@ Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<![CDATA[<string name='foo' format='two-words lower-case' />`]]> => `{{{{'foo': 'example one'}}}}`
- `<![CDATA[<list name='bar'><string format='upper-case' /></list>]]>` => `{{{{"bar": ['STRING ONE', 'STRING TWO', etc.]}}}}`
- `<![CDATA[<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>]]>` => `{{{{'baz': {{{{'foo': 'Some String', 'index': 1}}}}}}}}`

JSON Object:</complete_json_suffix_v2>
</complete_json_suffix_v2>


</constants>
2 changes: 1 addition & 1 deletion guardrails/llm_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def openai_wrapper(text: str, *args, **kwargs):
api_key = os.environ.get("OPENAI_API_KEY")
openai_response = openai.Completion.create(
api_key=api_key,
prompt=text,
prompt=text + "\n\nJson Output:\n\n",
*args,
**kwargs,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,3 @@ Given below is XML that describes the information to extract from this document


ONLY return a valid JSON object (no other text is necessary). The JSON MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and specific types. Be correct and concise.

JSON Output:
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,3 @@ Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<string name='foo' format='two-words lower-case' />` => `{{'foo': 'example one'}}`
- `<list name='bar'><string format='upper-case' /></list>` => `{{"bar": ['STRING ONE', 'STRING TWO', etc.]}}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{{'baz': {{'foo': 'Some String', 'index': 1}}}}`

JSON Object:
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,3 @@ Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}`
- `<list name='bar'><string format='upper-case' /></list>` => `{"bar": ['STRING ONE', 'STRING TWO', etc.]}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{'baz': {'foo': 'Some String', 'index': 1}}`

JSON Object:
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,3 @@ Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<string name='foo' format='two-words lower-case' />` => `{{'foo': 'example one'}}`
- `<list name='bar'><string format='upper-case' /></list>` => `{{"bar": ['STRING ONE', 'STRING TWO', etc.]}}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{{'baz': {{'foo': 'Some String', 'index': 1}}}}`

JSON Object:
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,3 @@ Here are examples of simple (XML, JSON) pairs that show the expected behavior:
- `<string name='foo' format='two-words lower-case' />` => `{{'foo': 'example one'}}`
- `<list name='bar'><string format='upper-case' /></list>` => `{{"bar": ['STRING ONE', 'STRING TWO', etc.]}}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{{'baz': {{'foo': 'Some String', 'index': 1}}}}`

JSON Object:
3 changes: 1 addition & 2 deletions tests/unit_tests/utils/test_reask_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ def test_get_reask_prompt(example_rail, reasks, reask_json):
- `<string name='foo' format='two-words lower-case' />` => `{{'foo': 'example one'}}`
- `<list name='bar'><string format='upper-case' /></list>` => `{{"bar": ['STRING ONE', 'STRING TWO', etc.]}}`
- `<object name='baz'><string name="foo" format="capitalize two-words" /><integer name="index" format="1-indexed" /></object>` => `{{'baz': {{'foo': 'Some String', 'index': 1}}}}`
JSON Object:""" # noqa: E501
""" # noqa: E501

result_prompt, _ = reask_utils.get_reask_prompt(
ET.fromstring(example_rail), reasks, reask_json
Expand Down

0 comments on commit 42fc197

Please sign in to comment.