dspy.History

dspy.History

dspy.History는 대화 기록(conversation history)을 나타내는 클래스입니다. BaseModel을 상속하며, 각 메시지는 해당 시그니처의 키(key)를 가진 딕셔너리로 표현됩니다.

출처: 문서

본문

dspy.History는 대화 기록을 나타내는 클래스입니다. 대화 기록은 메시지의 리스트이며, 각 메시지 엔티티는 연결된 시그니처의 키를 가져야 합니다. 예를 들어 다음 시그니처가 있다고 해 봅시다.

class MySignature(dspy.Signature):
    question: str = dspy.InputField()
    history: dspy.History = dspy.InputField()
    answer: str = dspy.OutputField()

그러면 history는 "question"과 "answer" 키를 가진 딕셔너리들의 리스트가 되어야 합니다.

Examples

import dspy

dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))

class MySignature(dspy.Signature):
    question: str = dspy.InputField()
    history: dspy.History = dspy.InputField()
    answer: str = dspy.OutputField()

history = dspy.History(
    messages=[
        {"question": "What is the capital of France?", "answer": "Paris"},
        {"question": "What is the capital of Germany?", "answer": "Berlin"},
    ]
)

predict = dspy.Predict(MySignature)
outputs = predict(question="What is the capital of France?", history=history)

대화 기록을 캡처하는 예시입니다.

import dspy

dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))

class MySignature(dspy.Signature):
    question: str = dspy.InputField()
    history: dspy.History = dspy.InputField()
    answer: str = dspy.OutputField()

predict = dspy.Predict(MySignature)
outputs = predict(question="What is the capital of France?")
history = dspy.History(messages=[{"question": "What is the capital of France?", **outputs}])
outputs_with_history = predict(question="Are you sure?", history=history)

Attributes

messages: list[dict[str, Any]] (instance-attribute)

model_config = pydantic.ConfigDict(frozen=True, str_strip_whitespace=True, validate_assignment=True, extra='forbid') (class-attribute, instance-attribute)

더 알아보기 (Learn more)