이것은 대화형 노트북입니다. 로컬에서 실행하거나 아래 링크를 사용할 수 있습니다:

커스텀 비용 모델 설정하기

Weave는 사용된 토큰 수와 사용된 모델을 기반으로 비용을 계산합니다. Weave는 이 사용량과 모델을 출력에서 가져와 호출과 연결합니다. 자체 토큰 사용량을 계산하고 이를 weave에 저장하는 간단한 커스텀 모델을 설정해 보겠습니다.

환경 설정

필요한 모든 패키지를 설치하고 가져옵니다. 환경에WANDB_API_KEY를 설정하여wandb.login()로 쉽게 로그인할 수 있습니다(이는 비밀로 colab에 제공되어야 함). 로그를 기록할 W&B 프로젝트를name_of_wandb_project에 설정합니다. NOTE: name_of_wandb_project는 또한{team_name}/{project_name} 형식으로 트레이스를 기록할 팀을 지정할 수 있습니다. 그런 다음weave.init()
%pip install wandb weave datetime --quiet
python
import os

import wandb
from google.colab import userdata

import weave

os.environ["WANDB_API_KEY"] = userdata.get("WANDB_API_KEY")
name_of_wandb_project = "custom-cost-model"

wandb.login()
python
weave_client = weave.init(name_of_wandb_project)

weave로 모델 설정하기

from weave import Model

class YourModel(Model):
    attribute1: str
    attribute2: int

    def simple_token_count(self, text: str) -> int:
        return len(text) // 3

    # This is a custom op that we are defining
    # It takes in a string, and outputs a dict with the usage counts, model name, and the output
    @weave.op()
    def custom_model_generate(self, input_data: str) -> dict:
        # Model logic goes here
        # Here is where you would have a custom generate function
        prediction = self.attribute1 + " " + input_data

        # Usage counts
        prompt_tokens = self.simple_token_count(input_data)
        completion_tokens = self.simple_token_count(prediction)

        # We return a dictionary with the usage counts, model name, and the output
        # Weave will automatically associate this with the trace
        # This object {usage, model, output} matches the output of a OpenAI Call
        return {
            "usage": {
                "input_tokens": prompt_tokens,
                "output_tokens": completion_tokens,
                "total_tokens": prompt_tokens + completion_tokens,
            },
            "model": "your_model_name",
            "output": prediction,
        }

    # In our predict function we call our custom generate function, and return the output.
    @weave.op()
    def predict(self, input_data: str) -> dict:
        # Here is where you would do any post processing of the data
        outputs = self.custom_model_generate(input_data)
        return outputs["output"]

커스텀 비용 추가하기

여기서 커스텀 비용을 추가하고, 이제 커스텀 비용이 있고 호출에 사용량이 있으므로include_cost로 호출을 가져올 수 있으며 호출에는summary.weave.costs 아래에 비용이 있습니다.
model = YourModel(attribute1="Hello", attribute2=1)
model.predict("world")

# We then add a custom cost to our project
weave_client.add_cost(
    llm_id="your_model_name", prompt_token_cost=0.1, completion_token_cost=0.2
)

# We can then query for the calls, and with include_costs=True
# we receive the costs back attached to the calls
calls = weave_client.get_calls(filter={"trace_roots_only": True}, include_costs=True)

list(calls)