home
자바
home

4. 답변의 형식을 컨트롤 하는 방법

from langchain_core.prompts import ChatPromptTemplate chat_prompt_template = ChatPromptTemplate.from_messages([ ("system", "You are a helpful assistant."), ("human", "What is the capital of {country}?"), ]) chat_prompt = chat_prompt_template.invoke({"country": "France"}) print(chat_prompt)
Python
복사
ChatPromptTemplate 을 사용하는 것이 더 랭체인스러운 방법
후 강의에 나오는 LCEL(프롬프트를 연결하는 것) 이라는 것도 ChatPromptTemplate은 연동이 됨
from pydantic import BaseModel, Field class CapitalAnswer(BaseModel): capital: str = Field(description="The capital city of the country.") population: int = Field(description="The population of the capital city.") language: str = Field(description="The primary language spoken in the capital city.") currency: str = Field(description="The currency used in the capital city.") structured_llm = llm.with_structured_output(CapitalAnswer)
Python
복사
JsonOutputParser 를 사용하는 것보다 pydantic 을 사용하는게 낫다.
JsonOutputParser 는 json 구조로 던져지고
pydantic은 내가 구조화한 구조대로 값이 들어온다. model_dump()를 사용해서 json 형식으로 변경도 가능