메인 콘텐츠로 건너뛰기

사용자 정의 비용 추가

add_cost 메서드를 사용하여 사용자 정의 비용을 추가할 수 있습니다. 필수 필드는 llm_id, prompt_token_cost, completion_token_cost 세 가지입니다. llm_id는 LLM의 이름입니다(예: gpt-4o). prompt_token_costcompletion_token_cost는 LLM의 토큰당 비용입니다(LLM 가격이 100만 토큰 기준으로 제공되는 경우, 해당 값으로 변환해서 지정해야 합니다). 또한 effective_date를 datetime으로 설정하여 특정 날짜부터 비용이 적용되도록 할 수 있습니다. 기본값은 현재 날짜입니다.
import weave
from datetime import datetime

client = weave.init("my_custom_cost_model")

client.add_cost(
    llm_id="your_model_name",
    prompt_token_cost=0.01,
    completion_token_cost=0.02
)

client.add_cost(
    llm_id="your_model_name",
    prompt_token_cost=10,
    completion_token_cost=20,
    # 예를 들어, 특정 날짜 이후에 모델의 가격을 인상하려는 경우
    effective_date=datetime(2025, 4, 22),
)

비용 쿼리하기

query_costs 메서드를 사용해서 비용을 쿼리할 수 있습니다. 비용을 쿼리하는 방법은 여러 가지가 있으며, 단일 비용 ID를 전달하거나 LLM 모델 이름 목록을 전달할 수 있습니다.
import weave

client = weave.init("my_custom_cost_model")

costs = client.query_costs(llm_ids=["your_model_name"])

cost = client.query_costs(costs[0].id)

사용자 정의 비용 삭제

purge_costs 메서드를 사용해 사용자 정의 비용을 삭제할 수 있습니다. 비용 ID 목록을 전달하면 해당 ID를 가진 비용이 삭제됩니다.
import weave

client = weave.init("my_custom_cost_model")

costs = client.query_costs(llm_ids=["your_model_name"])
client.purge_costs([cost.id for cost in costs])

프로젝트 비용 계산

약간의 설정만 하면 calls_query를 사용하고 include_costs=True를 추가해 프로젝트의 비용을 계산할 수 있습니다.
import weave

weave.init("project_costs")
@weave.op()
def get_costs_for_project(project_name: str):
    total_cost = 0
    requests = 0

    client = weave.init(project_name)
    # 프로젝트의 모든 호출을 가져옵니다
    calls = list(
        client.get_calls(filter={"trace_roots_only": True}, include_costs=True)
    )

    for call in calls:
        # 호출에 비용 정보가 있다면 총 비용에 더합니다
        if call.summary["weave"] is not None and call.summary["weave"].get("costs", None) is not None:
            for k, cost in call.summary["weave"]["costs"].items():
                requests += cost["requests"]
                total_cost += cost["prompt_tokens_total_cost"]
                total_cost += cost["completion_tokens_total_cost"]

    # 총 비용, 요청 수, 호출 수를 반환합니다
    return {
        "total_cost": total_cost,
        "requests": requests,
        "calls": len(calls),
    }

# 함수를 @weave.op()으로 데코레이트했기 때문에,
# 총합이 과거 비용 총액 계산을 위해 weave에 저장됩니다
get_costs_for_project("my_custom_cost_model")

커스텀 비용이 적용된 커스텀 모델 설정하기

커스텀 모델에 비용을 설정하는 방법은 커스텀 모델로 비용 설정하기 쿡북을 참고하세요.

Colab에서 실행하기