컴포넌트 구성(Component config)
컴포넌트 구성(Component config)
AutoGen의 컴포넌트는 선언적으로(declaratively) 구성될 수 있어요. 이건 AutoGen Studio 같은 구성 기반 환경을 지원하기 위한 것이지만, 다른 많은 시나리오에서도 유용해요.
이 시스템을 "컴포넌트 구성(component configuration)" 이라고 불러요. AutoGen에서 컴포넌트는 간단히, config 객체로부터 생성될 수 있고, 자기 자신도 config 객체로 덤프(dump)될 수 있는 무엇이에요. 이렇게 하면 코드에서 컴포넌트를 정의한 뒤 그로부터 config 객체를 얻을 수 있죠.
이 시스템은 범용적이라서, AutoGen 밖에서 정의된 컴포넌트(예: 확장)도 같은 방식으로 구성할 수 있어요.
상태(state)와는 어떻게 다른가?
이 점을 분명히 하는 게 아주 중요해요. 객체를 직렬화한다는 건, 그 객체 자체를 이루는 모든 데이터를 포함한다는 뜻이에요. 메시지 히스토리 같은 것도 포함해서요. 직렬화된 상태에서 역직렬화하면 완전히 똑같은 객체를 되돌려받아야 합니다. 하지만 컴포넌트 구성은 그렇지 않아요.
컴포넌트 구성은 객체의 청사진(blueprint) 으로 생각하면 돼요. 같은 구성 객체를 여러 번 찍어내서(스탬프) 같은 구성의 인스턴스를 여러 개 만들 수 있죠.
사용법
파이썬에 컴포넌트가 있고 그 config를 얻고 싶다면, 그 컴포넌트에 dump_component()을 호출하면 돼요. 그 결과 객체를 다시 load_component()에 넘기면 컴포넌트를 되돌려받을 수 있어요.
config에서 컴포넌트 로드하기
config 객체에서 컴포넌트를 로드하려면 load_component() 메서드를 쓰세요. 이 메서드는 config 객체를 받아 컴포넌트 객체를 반환해요. 이 메서드는 원하는 인터페이스에 대해 호출하는 게 가장 좋아요. 예를 들어 모델 클라이언트를 로드할 때:
from autogen_core.models import ChatCompletionClient
config = {
"provider": "openai_chat_completion_client",
"config": {"model": "gpt-4o"},
}
client = ChatCompletionClient.load_component(config)
컴포넌트 클래스 만들기
어떤 클래스에 컴포넌트 기능을 더하려면:
- 클래스 상속 목록에
Component()호출을 추가하세요. _to_config()와_from_config()메서드를 구현하세요.
예를 들어:
from autogen_core import Component, ComponentBase
from pydantic import BaseModel
class Config(BaseModel):
value: str
class MyComponent(ComponentBase[Config], Component[Config]):
component_type = "custom"
component_config_schema = Config
def __init__(self, value: str):
self.value = value
def _to_config(self) -> Config:
return Config(value=self.value)
@classmethod
def _from_config(cls, config: Config) -> "MyComponent":
return cls(value=config.value)
비밀값(Secrets)
config 객체의 필드가 비밀 값이라면, SecretStr로 표시해야 해요. 그러면 그 값이 config 객체로 덤프되지 않게 보장되죠.
예를 들어:
from pydantic import BaseModel, SecretStr
class ClientConfig(BaseModel):
endpoint: str
api_key: SecretStr