시작하기무료로 시작하기

구조화된 출력 요청하기

Lexical graph를 만들 때 텍스트 스플리터를 사용해 등장인물 이름과 대사를 추출했지만, 이 방법은 종종 분할 경계 주변의 문맥이 손실되는 문제가 있어요. 예를 들어, 텍스트에 포함된 무대 지시문은 해당 시점에 그 인물이 누구에게 말하는지 파악하는 데 활용할 수 있어요.

이제 새로운 Line 클래스 안에서 Character 클래스를 사용해, 대사 자체와 화자, 그리고 그 대사가 향하는 대상 인물들의 목록을 함께 표현할 거예요. 또한 이러한 대사들을 한 번에 여러 개 추출할 수 있도록 래퍼 클래스인 LineOutput도 만들어요.

추출 지침을 담은 prompt 변수와 llm이라는 이름의 LLM 인스턴스는 미리 정의되어 있어요.

이 연습은 강의의 일부입니다

LangChain과 Neo4j로 배우는 Graph RAG

강의 보기

연습 안내

  • LineOutput 클래스를 수정해 Line 객체의 목록을 요청하세요.
  • LineOutput 클래스를 사용해 구조화된 출력을 생성하는 LLM을 정의하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

class Line(BaseModel):
    text: str = Field(..., description="The text of the line")
    spoken_by: Character = Field(..., description="The character who speaks the line")
    spoken_to: Optional[list[Character]] = Field(None, description="The character who the line is spoken to")

class LineOutput(BaseModel):
    # Request more than one line from the text
    lines: ____[Line] = Field(..., description="The lines from the text")

# Return an instance of LineOutput
structured_llm = llm.____(____)

for line in structured_llm.invoke(prompt.format_messages(text=text)):
	print(line)
코드 편집 및 실행