mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
bump tutorials
This commit is contained in:
@@ -45,9 +45,92 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 18,
|
||||
"id": "d4bb6258-b03a-4621-8a73-29056a20ec0f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from typing_extensions import Annotated\n",
|
||||
"from pydantic import BaseModel, AfterValidator, WithJsonSchema\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def name_must_contain_space(v: str) -> str:\n",
|
||||
" if \" \" not in v:\n",
|
||||
" raise ValueError(\"Name must contain a space.\")\n",
|
||||
" return v\n",
|
||||
"\n",
|
||||
"def uppercase_name(v: str) -> str:\n",
|
||||
" return v.upper()\n",
|
||||
"\n",
|
||||
"FullName = Annotated[\n",
|
||||
" str, \n",
|
||||
" AfterValidator(name_must_contain_space), \n",
|
||||
" AfterValidator(uppercase_name),\n",
|
||||
" WithJsonSchema(\n",
|
||||
" {\n",
|
||||
" \"type\": \"string\",\n",
|
||||
" \"description\": \"The user's full name\",\n",
|
||||
" }\n",
|
||||
" )]\n",
|
||||
"\n",
|
||||
"class UserDetail(BaseModel):\n",
|
||||
" age: int\n",
|
||||
" name: FullName"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"id": "23f8cadd",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"UserDetail(age=30, name='JASON LIU')"
|
||||
]
|
||||
},
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"UserDetail(age=30, name=\"Jason Liu\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"id": "e4f53ecf",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'properties': {'age': {'title': 'Age', 'type': 'integer'},\n",
|
||||
" 'name': {'description': \"The user's full name\",\n",
|
||||
" 'title': 'Name',\n",
|
||||
" 'type': 'string'}},\n",
|
||||
" 'required': ['age', 'name'],\n",
|
||||
" 'title': 'UserDetail',\n",
|
||||
" 'type': 'object'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"UserDetail.model_json_schema()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"id": "2284a7e8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -61,21 +144,6 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from typing_extensions import Annotated\n",
|
||||
"from pydantic import BaseModel, AfterValidator\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def name_must_contain_space(v: str) -> str:\n",
|
||||
" if \" \" not in v:\n",
|
||||
" raise ValueError(\"Name must contain a space.\")\n",
|
||||
" return v.lower()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class UserDetail(BaseModel):\n",
|
||||
" age: int\n",
|
||||
" name: Annotated[str, AfterValidator(name_must_contain_space)]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"try:\n",
|
||||
" person = UserDetail.model_validate({\"age\": 24, \"name\": \"Jason\"})\n",
|
||||
"except Exception as e:\n",
|
||||
@@ -94,7 +162,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 22,
|
||||
"id": "3242856f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -102,10 +170,13 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1 validation error for UserDetail\n",
|
||||
"2 validation errors for UserDetail\n",
|
||||
"age\n",
|
||||
" Input should be greater than 0 [type=greater_than, input_value=-10, input_type=int]\n",
|
||||
" For further information visit https://errors.pydantic.dev/2.5/v/greater_than\n"
|
||||
" For further information visit https://errors.pydantic.dev/2.5/v/greater_than\n",
|
||||
"name\n",
|
||||
" Value error, Name must contain a space. [type=value_error, input_value='Jason', input_type=str]\n",
|
||||
" For further information visit https://errors.pydantic.dev/2.5/v/value_error\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -113,10 +184,11 @@
|
||||
"from pydantic import Field\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class UserDetail(BaseModel):\n",
|
||||
" age: int = Field(..., gt=0)\n",
|
||||
" name: str\n",
|
||||
"Age = Annotated[int, Field(gt=0)]\n",
|
||||
"\n",
|
||||
"class UserDetail(BaseModel):\n",
|
||||
" age: Age\n",
|
||||
" name: FullName\n",
|
||||
"\n",
|
||||
"try:\n",
|
||||
" person = UserDetail(age=-10, name=\"Jason\")\n",
|
||||
@@ -124,34 +196,6 @@
|
||||
" print(e)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "0035a329",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1 validation error for AssistantMessage\n",
|
||||
"message\n",
|
||||
" String should have at least 10 characters [type=string_too_short, input_value='Hey', input_type=str]\n",
|
||||
" For further information visit https://errors.pydantic.dev/2.5/v/string_too_short\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"class AssistantMessage(BaseModel):\n",
|
||||
" message: str = Field(..., min_length=10)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"try:\n",
|
||||
" message = AssistantMessage(message=\"Hey\")\n",
|
||||
"except Exception as e:\n",
|
||||
" print(e)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4f689121",
|
||||
|
||||
Reference in New Issue
Block a user