|
20 | 20 | "id": "1", |
21 | 21 | "metadata": {}, |
22 | 22 | "outputs": [ |
| 23 | + { |
| 24 | + "name": "stdout", |
| 25 | + "output_type": "stream", |
| 26 | + "text": [ |
| 27 | + "Found default environment files: ['/home/vscode/.pyrit/.env', '/home/vscode/.pyrit/.env.local']\n", |
| 28 | + "Loaded environment file: /home/vscode/.pyrit/.env\n", |
| 29 | + "Loaded environment file: /home/vscode/.pyrit/.env.local\n" |
| 30 | + ] |
| 31 | + }, |
23 | 32 | { |
24 | 33 | "name": "stdout", |
25 | 34 | "output_type": "stream", |
|
88 | 97 | "cell_type": "markdown", |
89 | 98 | "id": "2", |
90 | 99 | "metadata": {}, |
| 100 | + "source": [ |
| 101 | + "## JSON Output\n", |
| 102 | + "\n", |
| 103 | + "You can also get the output in JSON format for further processing or storage. In this example, we define a simple JSON schema that describes a person with `name` and `age` properties.\n", |
| 104 | + "\n", |
| 105 | + "For more information about structured outputs with OpenAI, see [the OpenAI documentation](https://platform.openai.com/docs/guides/structured-outputs)." |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": null, |
| 111 | + "id": "3", |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [ |
| 114 | + { |
| 115 | + "name": "stdout", |
| 116 | + "output_type": "stream", |
| 117 | + "text": [ |
| 118 | + "Found default environment files: ['/home/vscode/.pyrit/.env', '/home/vscode/.pyrit/.env.local']\n", |
| 119 | + "Loaded environment file: /home/vscode/.pyrit/.env\n", |
| 120 | + "Loaded environment file: /home/vscode/.pyrit/.env.local\n" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "name": "stdout", |
| 125 | + "output_type": "stream", |
| 126 | + "text": [ |
| 127 | + "{\n", |
| 128 | + " \"name\": \"Bob\",\n", |
| 129 | + " \"age\": 32\n", |
| 130 | + "}\n" |
| 131 | + ] |
| 132 | + } |
| 133 | + ], |
| 134 | + "source": [ |
| 135 | + "import json\n", |
| 136 | + "\n", |
| 137 | + "import jsonschema\n", |
| 138 | + "\n", |
| 139 | + "from pyrit.models import Message, MessagePiece\n", |
| 140 | + "from pyrit.prompt_target import OpenAIChatTarget\n", |
| 141 | + "from pyrit.setup import IN_MEMORY, initialize_pyrit_async\n", |
| 142 | + "\n", |
| 143 | + "await initialize_pyrit_async(memory_db_type=IN_MEMORY) # type: ignore\n", |
| 144 | + "\n", |
| 145 | + "# Define a simple JSON schema for a person\n", |
| 146 | + "person_schema = {\n", |
| 147 | + " \"type\": \"object\",\n", |
| 148 | + " \"properties\": {\n", |
| 149 | + " \"name\": {\"type\": \"string\"},\n", |
| 150 | + " \"age\": {\"type\": \"integer\", \"minimum\": 0, \"maximum\": 150},\n", |
| 151 | + " },\n", |
| 152 | + " \"required\": [\"name\", \"age\"],\n", |
| 153 | + " \"additionalProperties\": False,\n", |
| 154 | + "}\n", |
| 155 | + "\n", |
| 156 | + "prompt = \"Create a JSON object describing a person named Bob who is 32 years old.\"\n", |
| 157 | + "# Create the message piece and message\n", |
| 158 | + "message_piece = MessagePiece(\n", |
| 159 | + " role=\"user\",\n", |
| 160 | + " original_value=prompt,\n", |
| 161 | + " original_value_data_type=\"text\",\n", |
| 162 | + " prompt_metadata={\n", |
| 163 | + " \"response_format\": \"json\",\n", |
| 164 | + " \"json_schema\": json.dumps(person_schema),\n", |
| 165 | + " },\n", |
| 166 | + ")\n", |
| 167 | + "message = Message(message_pieces=[message_piece])\n", |
| 168 | + "\n", |
| 169 | + "# Create the OpenAI Chat target\n", |
| 170 | + "target = OpenAIChatTarget()\n", |
| 171 | + "\n", |
| 172 | + "# Send the prompt, requesting JSON output\n", |
| 173 | + "response = await target.send_prompt_async(message=message) # type: ignore\n", |
| 174 | + "\n", |
| 175 | + "# Validate and print the response\n", |
| 176 | + "response_json = json.loads(response[0].message_pieces[0].converted_value)\n", |
| 177 | + "print(json.dumps(response_json, indent=2))\n", |
| 178 | + "jsonschema.validate(instance=response_json, schema=person_schema)" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "markdown", |
| 183 | + "id": "4", |
| 184 | + "metadata": {}, |
91 | 185 | "source": [ |
92 | 186 | "## OpenAI Configuration\n", |
93 | 187 | "\n", |
|
125 | 219 | "name": "python", |
126 | 220 | "nbconvert_exporter": "python", |
127 | 221 | "pygments_lexer": "ipython3", |
128 | | - "version": "3.11.13" |
| 222 | + "version": "3.11.14" |
129 | 223 | } |
130 | 224 | }, |
131 | 225 | "nbformat": 4, |
|
0 commit comments