跳转到主要内容

LLM的Comet日志记录器

项目描述

CometLLM 已被 opik 替换 - 请参阅 https://github.com/comet-ml/opik

cometLLM

PyPI version GitHub cometLLM Documentation Downloads

CometLLM 是一个用于记录和可视化您的LLM提示和链的工具。使用CometLLM来识别有效的提示策略,简化您的故障排除,并确保可重复的工作流程!

CometLLM Preview

⚡️ 快速开始

使用pip安装 comet_llm Python库

pip install comet_llm

如果您还没有,请创建您的免费Comet账户 并从账户设置页面获取您的API密钥。

现在您可以开始记录您的第一个提示和响应

import comet_llm

comet_llm.log_prompt(
    prompt="What is your name?",
    output=" My name is Alex.",
    api_key="<YOUR_COMET_API_KEY>",
)

🎯 功能

  • 记录您的提示和响应,包括提示模板、变量、时间戳、持续时间以及您需要的任何元数据。
  • 在UI中可视化您的提示和响应。
  • 记录您所需的粒度的链执行。
  • 在UI中可视化您的链执行。
  • 自动跟踪使用OpenAI聊天模型时的提示。
  • 跟踪和分析用户反馈。
  • 在UI中比较提示并链式执行。

👀 示例

要将单个LLM调用作为单个提示记录,请使用comet_llm.log_prompt。如果您需要更精细的记录,可以使用comet_llm.start_chain记录一个可能包括多个LLM调用、上下文检索或数据预处理和后处理的执行链。

记录完整提示和响应

import comet_llm

comet_llm.log_prompt(
    prompt="Answer the question and if the question can't be answered, say \"I don't know\"\n\n---\n\nQuestion: What is your name?\nAnswer:",
    prompt_template="Answer the question and if the question can't be answered, say \"I don't know\"\n\n---\n\nQuestion: {{question}}?\nAnswer:",
    prompt_template_variables={"question": "What is your name?"},
    metadata= {
        "usage.prompt_tokens": 7,
        "usage.completion_tokens": 5,
        "usage.total_tokens": 12,
    },
    output=" My name is Alex.",
    duration=16.598,
)

阅读完整文档以获取有关记录提示的更多详细信息.

记录LLM链

from comet_llm import Span, end_chain, start_chain
import datetime
from time import sleep


def retrieve_context(user_question):
    if "open" in user_question:
        return "Opening hours: 08:00 to 17:00 all days"


def llm_answering(user_question, current_time, context):
    prompt_template = """You are a helpful chatbot. You have access to the following context:
    {context}
    The current time is: {current_time}
    Analyze the following user question and decide if you can answer it, if the question can't be answered, say \"I don't know\":
    {user_question}
    """

    prompt = prompt_template.format(
        user_question=user_question, current_time=current_time, context=context
    )

    with Span(
        category="llm-call",
        inputs={"prompt_template": prompt_template, "prompt": prompt},
    ) as span:
        # Call your LLM model here
        sleep(0.1)
        result = "Yes we are currently open"
        usage = {"prompt_tokens": 52, "completion_tokens": 12, "total_tokens": 64}

        span.set_outputs(outputs={"result": result}, metadata={"usage": usage})

    return result


def main(user_question, current_time):
    start_chain(inputs={"user_question": user_question, "current_time": current_time})

    with Span(
        category="context-retrieval",
        name="Retrieve Context",
        inputs={"user_question": user_question},
    ) as span:
        context = retrieve_context(user_question)

        span.set_outputs(outputs={"context": context})

    with Span(
        category="llm-reasoning",
        inputs={
            "user_question": user_question,
            "current_time": current_time,
            "context": context,
        },
    ) as span:
        result = llm_answering(user_question, current_time, context)

        span.set_outputs(outputs={"result": result})

    end_chain(outputs={"result": result})


main("Are you open?", str(datetime.datetime.now().time()))

阅读完整文档以获取有关记录链的更多详细信息.

⚙️ 配置

您可以配置您的Comet凭据以及您要记录数据的位置

名称 Python参数名称 环境变量名称
Comet API密钥 api_key COMET_API_KEY
Comet工作区名称 workspace COMET_WORKSPACE
Comet项目名称 project COMET_PROJECT_NAME

📝 许可证

版权所有(c)Comet 2023年至今。cometLLM是免费且开源的软件,许可协议为MIT许可证

CometLLM 已被 opik 替换 - 请参阅 https://github.com/comet-ml/opik

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

comet_llm-2.2.7.tar.gz (37.0 kB 查看哈希值)

上传时间

构建分发

comet_llm-2.2.7-py3-none-any.whl (73.2 kB 查看哈希值)

上传时间 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面