get_format_instructions()
를 가지고 있고, Runnable이며 Chain에 주입이 가능. 또한 PromptTemplate를 통해 직접 instructions를 주입 하는 것도 가능from pydantic import BaseModel
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import Field
class Person(BaseModel):
name: str = Field(description="person's name")
hometown: str = Field(description="person's hometown")
birthday: str = Field(description="person's birthday")
# prompt 생성
chat_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"""
You are an AI that provides information about historical figures.
{format_instructions}
""",
),
("human", "Tell me about {name}"),
]
)
# chat model 생성
chat = ChatOpenAI()
# output parser 생성
output_parser = PydanticOutputParser(pydantic_object=Person)
# chain 형성
runnable = chat_prompt | chat | output_parser
# chain 실행
res = runnable.invoke(
{
"name": "소녀시대 윤아",
"format_instructions": output_parser.get_format_instructions(),
}
)
print(res)
---
"""
# output
name='윤아' hometown='서울, 대한민국' birthday='May 30, 1990'
"""
개체(Entity)
속성(Attribute)
관계(Relationship)
개체(Entity)
속성(Attribute)
관계(Relationship)