├── students_dataset.csv ├── typeddict_demo.py ├── json_schema.json ├── pydantic_demo.py ├── with_structured_output_typeddict.py ├── with_structured_output_pydantic.py ├── with_structured_output_llama.py └── with_structured_output_json.py /students_dataset.csv: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /typeddict_demo.py: -------------------------------------------------------------------------------- 1 | from typing import TypedDict 2 | 3 | class Person(TypedDict): 4 | 5 | name: str 6 | age: int 7 | 8 | new_person: Person = {'name':'nitish', 'age':'35'} 9 | 10 | print(new_person) -------------------------------------------------------------------------------- /json_schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "title": "student", 3 | "description": "schema about students", 4 | "type": "object", 5 | "properties":{ 6 | "name":"string", 7 | "age":"integer" 8 | }, 9 | "required":["name"] 10 | } -------------------------------------------------------------------------------- /pydantic_demo.py: -------------------------------------------------------------------------------- 1 | from pydantic import BaseModel, EmailStr, Field 2 | from typing import Optional 3 | 4 | class Student(BaseModel): 5 | 6 | name: str = 'nitish' 7 | age: Optional[int] = None 8 | email: EmailStr 9 | cgpa: float = Field(gt=0, lt=10, default=5, description='A decimal value representing the cgpa of the student') 10 | 11 | 12 | new_student = {'age':'32', 'email':'abc@gmail.com'} 13 | 14 | student = Student(**new_student) 15 | 16 | student_dict = dict(student) 17 | 18 | print(student_dict['age']) 19 | 20 | student_json = student.model_dump_json() -------------------------------------------------------------------------------- /with_structured_output_typeddict.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from dotenv import load_dotenv 3 | from typing import TypedDict, Annotated, Optional, Literal 4 | 5 | load_dotenv() 6 | 7 | model = ChatOpenAI() 8 | 9 | # schema 10 | class Review(TypedDict): 11 | 12 | key_themes: Annotated[list[str], "Write down all the key themes discussed in the review in a list"] 13 | summary: Annotated[str, "A brief summary of the review"] 14 | sentiment: Annotated[Literal["pos", "neg"], "Return sentiment of the review either negative, positive or neutral"] 15 | pros: Annotated[Optional[list[str]], "Write down all the pros inside a list"] 16 | cons: Annotated[Optional[list[str]], "Write down all the cons inside a list"] 17 | name: Annotated[Optional[str], "Write the name of the reviewer"] 18 | 19 | 20 | structured_model = model.with_structured_output(Review) 21 | 22 | result = structured_model.invoke("""I recently upgraded to the Samsung Galaxy S24 Ultra, and I must say, it’s an absolute powerhouse! The Snapdragon 8 Gen 3 processor makes everything lightning fast—whether I’m gaming, multitasking, or editing photos. The 5000mAh battery easily lasts a full day even with heavy use, and the 45W fast charging is a lifesaver. 23 | 24 | The S-Pen integration is a great touch for note-taking and quick sketches, though I don't use it often. What really blew me away is the 200MP camera—the night mode is stunning, capturing crisp, vibrant images even in low light. Zooming up to 100x actually works well for distant objects, but anything beyond 30x loses quality. 25 | 26 | However, the weight and size make it a bit uncomfortable for one-handed use. Also, Samsung’s One UI still comes with bloatware—why do I need five different Samsung apps for things Google already provides? The $1,300 price tag is also a hard pill to swallow. 27 | 28 | Pros: 29 | Insanely powerful processor (great for gaming and productivity) 30 | Stunning 200MP camera with incredible zoom capabilities 31 | Long battery life with fast charging 32 | S-Pen support is unique and useful 33 | 34 | Review by Nitish Singh 35 | """) 36 | 37 | print(result['name']) -------------------------------------------------------------------------------- /with_structured_output_pydantic.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from dotenv import load_dotenv 3 | from typing import TypedDict, Annotated, Optional, Literal 4 | from pydantic import BaseModel, Field 5 | 6 | load_dotenv() 7 | 8 | model = ChatOpenAI() 9 | 10 | # schema 11 | class Review(BaseModel): 12 | 13 | key_themes: list[str] = Field(description="Write down all the key themes discussed in the review in a list") 14 | summary: str = Field(description="A brief summary of the review") 15 | sentiment: Literal["pos", "neg"] = Field(description="Return sentiment of the review either negative, positive or neutral") 16 | pros: Optional[list[str]] = Field(default=None, description="Write down all the pros inside a list") 17 | cons: Optional[list[str]] = Field(default=None, description="Write down all the cons inside a list") 18 | name: Optional[str] = Field(default=None, description="Write the name of the reviewer") 19 | 20 | 21 | structured_model = model.with_structured_output(Review) 22 | 23 | result = structured_model.invoke("""I recently upgraded to the Samsung Galaxy S24 Ultra, and I must say, it’s an absolute powerhouse! The Snapdragon 8 Gen 3 processor makes everything lightning fast—whether I’m gaming, multitasking, or editing photos. The 5000mAh battery easily lasts a full day even with heavy use, and the 45W fast charging is a lifesaver. 24 | 25 | The S-Pen integration is a great touch for note-taking and quick sketches, though I don't use it often. What really blew me away is the 200MP camera—the night mode is stunning, capturing crisp, vibrant images even in low light. Zooming up to 100x actually works well for distant objects, but anything beyond 30x loses quality. 26 | 27 | However, the weight and size make it a bit uncomfortable for one-handed use. Also, Samsung’s One UI still comes with bloatware—why do I need five different Samsung apps for things Google already provides? The $1,300 price tag is also a hard pill to swallow. 28 | 29 | Pros: 30 | Insanely powerful processor (great for gaming and productivity) 31 | Stunning 200MP camera with incredible zoom capabilities 32 | Long battery life with fast charging 33 | S-Pen support is unique and useful 34 | 35 | Review by Nitish Singh 36 | """) 37 | 38 | print(result) -------------------------------------------------------------------------------- /with_structured_output_llama.py: -------------------------------------------------------------------------------- 1 | from dotenv import load_dotenv 2 | from typing import Optional, Literal 3 | from pydantic import BaseModel, Field 4 | from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint 5 | 6 | load_dotenv() 7 | 8 | llm = HuggingFaceEndpoint( 9 | repo_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0", 10 | task="text-generation" 11 | ) 12 | 13 | model = ChatHuggingFace(llm=llm) 14 | 15 | # schema 16 | class Review(BaseModel): 17 | 18 | key_themes: list[str] = Field(description="Write down all the key themes discussed in the review in a list") 19 | summary: str = Field(description="A brief summary of the review") 20 | sentiment: Literal["pos", "neg"] = Field(description="Return sentiment of the review either negative, positive or neutral") 21 | pros: Optional[list[str]] = Field(default=None, description="Write down all the pros inside a list") 22 | cons: Optional[list[str]] = Field(default=None, description="Write down all the cons inside a list") 23 | name: Optional[str] = Field(default=None, description="Write the name of the reviewer") 24 | 25 | 26 | structured_model = model.with_structured_output(Review) 27 | 28 | result = structured_model.invoke("""I recently upgraded to the Samsung Galaxy S24 Ultra, and I must say, it’s an absolute powerhouse! The Snapdragon 8 Gen 3 processor makes everything lightning fast—whether I’m gaming, multitasking, or editing photos. The 5000mAh battery easily lasts a full day even with heavy use, and the 45W fast charging is a lifesaver. 29 | 30 | The S-Pen integration is a great touch for note-taking and quick sketches, though I don't use it often. What really blew me away is the 200MP camera—the night mode is stunning, capturing crisp, vibrant images even in low light. Zooming up to 100x actually works well for distant objects, but anything beyond 30x loses quality. 31 | 32 | However, the weight and size make it a bit uncomfortable for one-handed use. Also, Samsung’s One UI still comes with bloatware—why do I need five different Samsung apps for things Google already provides? The $1,300 price tag is also a hard pill to swallow. 33 | 34 | Pros: 35 | Insanely powerful processor (great for gaming and productivity) 36 | Stunning 200MP camera with incredible zoom capabilities 37 | Long battery life with fast charging 38 | S-Pen support is unique and useful 39 | 40 | Review by Nitish Singh 41 | """) 42 | 43 | print(result) -------------------------------------------------------------------------------- /with_structured_output_json.py: -------------------------------------------------------------------------------- 1 | from langchain_openai import ChatOpenAI 2 | from dotenv import load_dotenv 3 | from typing import TypedDict, Annotated, Optional, Literal 4 | from pydantic import BaseModel, Field 5 | 6 | load_dotenv() 7 | 8 | model = ChatOpenAI() 9 | 10 | # schema 11 | json_schema = { 12 | "title": "Review", 13 | "type": "object", 14 | "properties": { 15 | "key_themes": { 16 | "type": "array", 17 | "items": { 18 | "type": "string" 19 | }, 20 | "description": "Write down all the key themes discussed in the review in a list" 21 | }, 22 | "summary": { 23 | "type": "string", 24 | "description": "A brief summary of the review" 25 | }, 26 | "sentiment": { 27 | "type": "string", 28 | "enum": ["pos", "neg"], 29 | "description": "Return sentiment of the review either negative, positive or neutral" 30 | }, 31 | "pros": { 32 | "type": ["array", "null"], 33 | "items": { 34 | "type": "string" 35 | }, 36 | "description": "Write down all the pros inside a list" 37 | }, 38 | "cons": { 39 | "type": ["array", "null"], 40 | "items": { 41 | "type": "string" 42 | }, 43 | "description": "Write down all the cons inside a list" 44 | }, 45 | "name": { 46 | "type": ["string", "null"], 47 | "description": "Write the name of the reviewer" 48 | } 49 | }, 50 | "required": ["key_themes", "summary", "sentiment"] 51 | } 52 | 53 | 54 | structured_model = model.with_structured_output(json_schema) 55 | 56 | result = structured_model.invoke("""I recently upgraded to the Samsung Galaxy S24 Ultra, and I must say, it’s an absolute powerhouse! The Snapdragon 8 Gen 3 processor makes everything lightning fast—whether I’m gaming, multitasking, or editing photos. The 5000mAh battery easily lasts a full day even with heavy use, and the 45W fast charging is a lifesaver. 57 | 58 | The S-Pen integration is a great touch for note-taking and quick sketches, though I don't use it often. What really blew me away is the 200MP camera—the night mode is stunning, capturing crisp, vibrant images even in low light. Zooming up to 100x actually works well for distant objects, but anything beyond 30x loses quality. 59 | 60 | However, the weight and size make it a bit uncomfortable for one-handed use. Also, Samsung’s One UI still comes with bloatware—why do I need five different Samsung apps for things Google already provides? The $1,300 price tag is also a hard pill to swallow. 61 | 62 | Pros: 63 | Insanely powerful processor (great for gaming and productivity) 64 | Stunning 200MP camera with incredible zoom capabilities 65 | Long battery life with fast charging 66 | S-Pen support is unique and useful 67 | 68 | Review by Nitish Singh 69 | """) 70 | 71 | print(result) --------------------------------------------------------------------------------