메인 콘텐츠로 건너뛰기
Open In Colab Weave 는 weave.init() 이 호출된 후 Cohere Python library 를 통해 이루어지는 LLM 호출을 자동으로 추적하고 로그를 남깁니다.

Traces

개발 단계와 프로덕션 단계 모두에서 LLM 애플리케이션의 Traces 를 중앙 데이터베이스에 저장하는 것이 중요합니다. 이러한 Traces 는 디버깅에 사용될 뿐만 아니라, 애플리케이션을 개선하는 데 도움이 되는 Datasets 로 활용됩니다. Weave 는 cohere-python 의 Traces 를 자동으로 캡처합니다. 평소와 같이 라이브러리를 사용하되, weave.init() 을 호출하여 시작하세요:
import cohere
import os
import weave

# 평소와 같이 Cohere 라이브러리를 사용합니다
co = cohere.Client(api_key=os.environ["COHERE_API_KEY"])

weave.init("cohere_project")

response = co.chat(
    message="How is the weather in Boston?",
    # 질문에 답하기 전에 웹 검색을 수행합니다. 고유한 커스텀 커넥터를 사용할 수도 있습니다.
    connectors=[{"id": "web-search"}],
)
print(response.text)
weave.init() 을 호출할 때 W&B Teams 를 지정하지 않으면 기본 Entities 가 사용됩니다. 기본 Entities 를 확인하거나 업데이트하려면 W&B Models 문서의 User Settings 를 참조하세요. Cohere 모델의 강력한 기능 중 하나는 connectors 를 사용하는 것입니다. 이를 통해 엔드포인트 측에서 다른 API로 요청을 보낼 수 있습니다. 그러면 응답에는 커넥터에서 반환된 문서로 링크되는 인용 요소와 함께 생성된 텍스트가 포함됩니다. cohere_trace.png
LLM 호출을 추적할 수 있도록 Cohere 의 Client.chat, AsyncClient.chat, Client.chat_stream, AsyncClient.chat_stream 메소드를 자동으로 패치합니다.

사용자 고유의 ops로 래핑하기

Weave ops 는 실험 시 코드를 자동으로 버전 관리하여 결과를 재현 가능하게 만들고, 입력과 출력을 캡처합니다. Cohere 의 채팅 메소드를 호출하는 함수를 @weave.op() 데코레이터로 생성하기만 하면, Weave 가 입력과 출력을 자동으로 추적합니다. 다음은 예시입니다:
import cohere
import os
import weave

co = cohere.Client(api_key=os.environ["COHERE_API_KEY"])

weave.init("cohere_project")

@weave.op()
def weather(location: str, model: str) -> str:
    response = co.chat(
        model=model,
        message=f"How is the weather in {location}?",
        # 질문에 답하기 전에 웹 검색을 수행합니다. 고유한 커스텀 커넥터를 사용할 수도 있습니다.
        connectors=[{"id": "web-search"}],
    )
    return response.text

print(weather("Boston", "command"))
cohere_ops.png

더 쉬운 실험을 위해 Model 만들기

다양한 요소가 복합적으로 작용할 때 실험을 체계화하는 것은 어렵습니다. Model 클래스를 사용하면 시스템 프롬프트나 사용 중인 모델과 같은 앱의 실험적 세부 사항을 캡처하고 정리할 수 있습니다. 이는 앱의 서로 다른 반복(iterations)을 구성하고 비교하는 데 도움이 됩니다. 코드의 버전 관리 및 입력/출력 캡처 외에도, Model 은 애플리케이션의 동작을 제어하는 구조화된 파라미터를 캡처하여 어떤 파라미터가 가장 효과적이었는지 쉽게 찾을 수 있게 해줍니다. 또한 Weave Models 를 serveEvaluation 과 함께 사용할 수도 있습니다. 아래 예시에서는 modeltemperature 를 사용하여 실험할 수 있습니다. 이 중 하나를 변경할 때마다 WeatherModel 의 새로운 버전 이 생성됩니다.
import weave
import cohere
import os

weave.init('weather-cohere')

class WeatherModel(weave.Model):
    model: str
    temperature: float
  
    @weave.op()
    def predict(self, location: str) -> str:
        co = cohere.Client(api_key=os.environ["COHERE_API_KEY"])
        response = co.chat(
            message=f"How is the weather in {location}?",
            model=self.model,
            temperature=self.temperature,
            connectors=[{"id": "web-search"}]
        )
        return response.text

weather_model = WeatherModel(
    model="command",
    temperature=0.7
)
result = weather_model.predict("Boston")
print(result)
cohere_model.png