메인 콘텐츠로 건너뛰기
Weave는 Cerebras Cloud SDK를 통해 이루어진 LLM 호출을 자동으로 추적하고 로깅합니다.”

트레이스

LLM 호출 추적은 디버깅과 성능 모니터링에 매우 중요합니다. Weave는 Cerebras Cloud SDK의 트레이스를 자동으로 수집하여 이를 지원합니다. 다음은 Weave를 Cerebras와 함께 사용하는 예입니다:
import os
import weave
from cerebras.cloud.sdk import Cerebras

# weave 프로젝트 초기화
weave.init("cerebras_speedster")

# 평소처럼 Cerebras SDK 사용
api_key = os.environ["CEREBRAS_API_KEY"]
model = "llama3.1-8b"  # Cerebras 모델

client = Cerebras(api_key=api_key)

response = client.chat.completions.create(
    model=model,
    messages=[{"role": "user", "content": "What's the fastest land animal?"}],
)

print(response.choices[0].message.content)
이제 Weave가 Cerebras SDK를 통해 이루어지는 모든 LLM 호출을 추적하고 로깅합니다. Weave 웹 인터페이스에서 token 사용량, 응답 시간 등의 세부 정보를 포함한 트레이스를 볼 수 있습니다. cerebras_calls.png

사용자 정의 ops로 래핑하기

Weave ops는 실험의 재현성과 추적 가능성을 높이는 강력한 방법입니다. 코드를 자동으로 버전 관리하고 입력과 출력을 캡처해 줍니다. 다음은 Cerebras SDK와 함께 Weave ops를 활용하는 예입니다:
import os
import weave
from cerebras.cloud.sdk import Cerebras

# Weave 프로젝트 초기화
weave.init("cerebras_speedster")

client = Cerebras(api_key=os.environ["CEREBRAS_API_KEY"])

# Weave가 이 함수의 입력, 출력 및 코드를 추적합니다
@weave.op
def animal_speedster(animal: str, model: str) -> str:
    "동물이 얼마나 빨리 달릴 수 있는지 알아보기"
    
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": f"How fast can a {animal} run?"}],
    )
    return response.choices[0].message.content

animal_speedster("cheetah", "llama3.1-8b")
animal_speedster("ostrich", "llama3.1-8b")
animal_speedster("human", "llama3.1-8b")

더 쉽게 실험할 수 있도록 Model 만들기

Weave의 Model 클래스는 앱의 여러 버전을 체계적으로 정리하고 비교하는 데 도움이 됩니다. 이는 특히 Cerebras 모델을 실험할 때 유용합니다. 다음은 예시입니다:
import os
import weave
from cerebras.cloud.sdk import Cerebras

# weave 프로젝트 초기화
weave.init("cerebras_speedster")

client = Cerebras(api_key=os.environ["CEREBRAS_API_KEY"])

class AnimalSpeedModel(weave.Model):
    model: str
    temperature: float

    @weave.op
    def predict(self, animal: str) -> str:
        "동물의 최고 속도를 예측합니다"        

        response = client.chat.completions.create(
            model=self.model,
            messages=[{"role": "user", "content": f"What's the top speed of a {animal}?"}],
            temperature=self.temperature
        )
        return response.choices[0].message.content

speed_model = AnimalSpeedModel(
    model="llama3.1-8b",
    temperature=0.7
)
result = speed_model.predict(animal="cheetah")
print(result)
이 설정을 사용하면 Cerebras 기반 추론을 추적하면서 다양한 모델과 파라미터로 손쉽게 실험할 수 있습니다! cerebras_model.png