입력, 출력, 메타데이터앱을 통해 흐르는 데이터를 추적하는 것은 시스템 성능을 이해하는 데 중요합니다. 그러나 시간에 따른 앱 버전 관리도 코드나 애플리케이션 매개변수의 수정이 출력을 어떻게 변경하는지 이해하는 데 중요합니다. Weave의 Model 클래스는 이러한 변경 사항을 Weave에서 추적하는 방법입니다. 이 튜토리얼에서 배울 내용:
  • Weave Model를 사용하여 애플리케이션과 매개변수를 추적하고 버전 관리하는 방법.
  • 이미 로깅된 Weave Model를 내보내고, 수정하고, 재사용하는 방법.

weave.Model

weave.Model 클래스는 현재 Python에서만 지원됩니다.
Weave Model를 사용하면 모델 벤더 ID, 프롬프트, 온도 등과 같은 매개변수가 저장되고 변경 시 버전 관리됩니다. Weave에서 Model를 생성하려면 다음이 필요합니다:
  • weave.Model
  • 모든 클래스 필드에 대한 타입 정의
  • 타입이 지정된 invoke 함수와 @weave.op() 데코레이터
클래스 필드나 모델을 정의하는 코드를 변경하면 이러한 변경 사항이 기록되고 버전이 업데이트됩니다. 이를 통해 앱의 다양한 버전에서 생성된 결과를 비교할 수 있습니다. 아래 예시에서는 모델 이름, 온도 및 시스템 프롬프트가 추적되고 버전 관리됩니다:
import json
from openai import OpenAI

import weave

@weave.op()
def extract_dinos(wmodel: weave.Model, sentence: str) -> dict:
    response = wmodel.client.chat.completions.create(
        model=wmodel.model_name,
        temperature=wmodel.temperature,
        messages=[
            {
                "role": "system",
                "content": wmodel.system_prompt
            },
            {
                "role": "user",
                "content": sentence
            }
            ],
            response_format={ "type": "json_object" }
        )
    return response.choices[0].message.content

# Sub-class with a weave.Model
# highlight-next-line
class ExtractDinos(weave.Model):
    client: OpenAI = None
    model_name: str
    temperature: float
    system_prompt: str

    # Ensure your function is called `invoke` or `predict`
    # highlight-next-line
    @weave.op()
    # highlight-next-line
    def invoke(self, sentence: str) -> dict:
        dino_data  = extract_dinos(self, sentence)
        return json.loads(dino_data)
이제 invoke로 모델을 인스턴스화하고 호출할 수 있습니다:
weave.init('jurassic-park')
client = OpenAI()

system_prompt = """Extract any dinosaur `name`, their `common_name`, \
names and whether its `diet` is a herbivore or carnivore, in JSON format."""

# highlight-next-line
dinos = ExtractDinos(
    client=client,
    model_name='gpt-4o',
    temperature=0.4,
    system_prompt=system_prompt
)

sentence = """I watched as a Tyrannosaurus rex (T. rex) chased after a Triceratops (Trike), \
both carnivore and herbivore locked in an ancient dance. Meanwhile, a gentle giant \
Brachiosaurus (Brachi) calmly munched on treetops, blissfully unaware of the chaos below."""

# highlight-next-line
result = dinos.invoke(sentence)
print(result)
.invoke를 호출한 후 Weave에서 트레이스를 볼 수 있습니다 이제 모델 매개변수와 코드를 추적합니다 weave.op()로 장식된 모델 함수에 대해. 모델도 버전 관리되는 것을 볼 수 있으며, 이 경우 “v21”이고, 모델을 클릭하면 해당 버전의 모델을 사용한 모든 호출을 볼 수 있습니다 Re-using a weave model weave.Model 사용에 관한 참고 사항:
  • Weave predict 대신 invoke를 함수 이름으로 사용할 수 있습니다 Model선호하는 경우.
  • 다른 클래스 메서드를 weave로 추적하려면 weave.op()
  • 밑줄로 시작하는 매개변수는 weave에서 무시되며 로깅되지 않습니다

로깅된 weave.Model

Weave는 호출된 Models를 저장하고 버전 관리하기 때문에 이러한 모델을 내보내고 재사용할 수 있습니다. Model 참조 가져오기 Weave UI에서 특정 버전의 Model 참조를 얻을 수 있습니다 Model 사용하기 Model 객체의 URI가 있으면 내보내고 재사용할 수 있습니다. 내보낸 모델은 이미 초기화되어 사용할 준비가 되어 있습니다:
# the exported weave model is already initialised and ready to be called
# highlight-next-line
new_dinos = weave.ref("weave:///morgan/jurassic-park/object/ExtractDinos:ey4udBU2MU23heQFJenkVxLBX4bmDsFk7vsGcOWPjY4").get()

# set the client to the openai client again
new_dinos.client = client

new_sentence = """I also saw an Ankylosaurus grazing on giant ferns"""
new_result = new_dinos.invoke(new_sentence)
print(new_result)
여기서 동일한 Model 버전(v21)이 새 입력과 함께 사용된 것을 볼 수 있습니다: Re-using a weave model

다음 단계는?