openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/lib/test_pydantic.py
163lines · modecode
| 1 | from __future__ import annotations |
| 2 | |
| 3 | from inline_snapshot import snapshot |
| 4 | |
| 5 | import openai |
| 6 | from openai._compat import PYDANTIC_V2 |
| 7 | |
| 8 | from .schema_types.query import Query |
| 9 | |
| 10 | |
| 11 | def test_most_types() -> None: |
| 12 | if PYDANTIC_V2: |
| 13 | assert openai.pydantic_function_tool(Query)["function"] == snapshot( |
| 14 | { |
| 15 | "name": "Query", |
| 16 | "strict": True, |
| 17 | "parameters": { |
| 18 | "$defs": { |
| 19 | "Column": { |
| 20 | "enum": [ |
| 21 | "id", |
| 22 | "status", |
| 23 | "expected_delivery_date", |
| 24 | "delivered_at", |
| 25 | "shipped_at", |
| 26 | "ordered_at", |
| 27 | "canceled_at", |
| 28 | ], |
| 29 | "title": "Column", |
| 30 | "type": "string", |
| 31 | }, |
| 32 | "Condition": { |
| 33 | "properties": { |
| 34 | "column": {"title": "Column", "type": "string"}, |
| 35 | "operator": {"$ref": "#/$defs/Operator"}, |
| 36 | "value": { |
| 37 | "anyOf": [ |
| 38 | {"type": "string"}, |
| 39 | {"type": "integer"}, |
| 40 | {"$ref": "#/$defs/DynamicValue"}, |
| 41 | ], |
| 42 | "title": "Value", |
| 43 | }, |
| 44 | }, |
| 45 | "required": ["column", "operator", "value"], |
| 46 | "title": "Condition", |
| 47 | "type": "object", |
| 48 | "additionalProperties": False, |
| 49 | }, |
| 50 | "DynamicValue": { |
| 51 | "properties": {"column_name": {"title": "Column Name", "type": "string"}}, |
| 52 | "required": ["column_name"], |
| 53 | "title": "DynamicValue", |
| 54 | "type": "object", |
| 55 | "additionalProperties": False, |
| 56 | }, |
| 57 | "Operator": {"enum": ["=", ">", "<", "<=", ">=", "!="], "title": "Operator", "type": "string"}, |
| 58 | "OrderBy": {"enum": ["asc", "desc"], "title": "OrderBy", "type": "string"}, |
| 59 | "Table": {"enum": ["orders", "customers", "products"], "title": "Table", "type": "string"}, |
| 60 | }, |
| 61 | "properties": { |
| 62 | "table_name": {"$ref": "#/$defs/Table"}, |
| 63 | "columns": { |
| 64 | "items": {"$ref": "#/$defs/Column"}, |
| 65 | "title": "Columns", |
| 66 | "type": "array", |
| 67 | }, |
| 68 | "conditions": { |
| 69 | "items": {"$ref": "#/$defs/Condition"}, |
| 70 | "title": "Conditions", |
| 71 | "type": "array", |
| 72 | }, |
| 73 | "order_by": {"$ref": "#/$defs/OrderBy"}, |
| 74 | }, |
| 75 | "required": ["table_name", "columns", "conditions", "order_by"], |
| 76 | "title": "Query", |
| 77 | "type": "object", |
| 78 | "additionalProperties": False, |
| 79 | }, |
| 80 | } |
| 81 | ) |
| 82 | else: |
| 83 | assert openai.pydantic_function_tool(Query)["function"] == snapshot( |
| 84 | { |
| 85 | "name": "Query", |
| 86 | "strict": True, |
| 87 | "parameters": { |
| 88 | "title": "Query", |
| 89 | "type": "object", |
| 90 | "properties": { |
| 91 | "table_name": {"$ref": "#/definitions/Table"}, |
| 92 | "columns": {"type": "array", "items": {"$ref": "#/definitions/Column"}}, |
| 93 | "conditions": { |
| 94 | "title": "Conditions", |
| 95 | "type": "array", |
| 96 | "items": {"$ref": "#/definitions/Condition"}, |
| 97 | }, |
| 98 | "order_by": {"$ref": "#/definitions/OrderBy"}, |
| 99 | }, |
| 100 | "required": ["table_name", "columns", "conditions", "order_by"], |
| 101 | "definitions": { |
| 102 | "Table": { |
| 103 | "title": "Table", |
| 104 | "description": "An enumeration.", |
| 105 | "enum": ["orders", "customers", "products"], |
| 106 | "type": "string", |
| 107 | }, |
| 108 | "Column": { |
| 109 | "title": "Column", |
| 110 | "description": "An enumeration.", |
| 111 | "enum": [ |
| 112 | "id", |
| 113 | "status", |
| 114 | "expected_delivery_date", |
| 115 | "delivered_at", |
| 116 | "shipped_at", |
| 117 | "ordered_at", |
| 118 | "canceled_at", |
| 119 | ], |
| 120 | "type": "string", |
| 121 | }, |
| 122 | "Operator": { |
| 123 | "title": "Operator", |
| 124 | "description": "An enumeration.", |
| 125 | "enum": ["=", ">", "<", "<=", ">=", "!="], |
| 126 | "type": "string", |
| 127 | }, |
| 128 | "DynamicValue": { |
| 129 | "title": "DynamicValue", |
| 130 | "type": "object", |
| 131 | "properties": {"column_name": {"title": "Column Name", "type": "string"}}, |
| 132 | "required": ["column_name"], |
| 133 | "additionalProperties": False, |
| 134 | }, |
| 135 | "Condition": { |
| 136 | "title": "Condition", |
| 137 | "type": "object", |
| 138 | "properties": { |
| 139 | "column": {"title": "Column", "type": "string"}, |
| 140 | "operator": {"$ref": "#/definitions/Operator"}, |
| 141 | "value": { |
| 142 | "title": "Value", |
| 143 | "anyOf": [ |
| 144 | {"type": "string"}, |
| 145 | {"type": "integer"}, |
| 146 | {"$ref": "#/definitions/DynamicValue"}, |
| 147 | ], |
| 148 | }, |
| 149 | }, |
| 150 | "required": ["column", "operator", "value"], |
| 151 | "additionalProperties": False, |
| 152 | }, |
| 153 | "OrderBy": { |
| 154 | "title": "OrderBy", |
| 155 | "description": "An enumeration.", |
| 156 | "enum": ["asc", "desc"], |
| 157 | "type": "string", |
| 158 | }, |
| 159 | }, |
| 160 | "additionalProperties": False, |
| 161 | }, |
| 162 | } |
| 163 | ) |
| 164 | |