Langfuse:开源LLM应用调试神器,手把手带你部署并实现langfuse跟踪监控本地LLM系统

一,What is Langfuse? 什么是 Langfuse?

Langfuse 是一个开源 LLM 工程平台,可帮助团队协作调试,分析和调试 LLM 应用程序。所有平台功能都是本地集成的,以加快开发工作流程。Langfuse 开放。它适用于任何模型,任何框架,允许复杂的嵌套,并具有开放的 API 来构建下游用例。LLM 应用程序使用越来越复杂的抽象,如链,工具代理和高级提示。Langfuse 中的嵌套跟踪有助于了解发生了什么并确定问题的根本原因。

为什么选择 Langfuse?

  • Open source: Fully open source with public API for custom integrations
    开源:完全开源,使用公共 API 进行自定义集成
  • Production optimized: Designed with minimal performance overhead
    生产优化:以最小的性能开销设计
  • Best-in-class SDKs: Native SDKs for Python and JavaScript
    一流的 SDK:适用于 Python 和 JavaScript 的原生 SDK
  • Framework support: Integrated with popular frameworks like OpenAI SDK, LangChain, and LlamaIndex
    框架支持:与 OpenAI SDK,LangChain 和 LlamaIndex 等流行框架集成
  • Multi-modal: Support for tracing text, images and other modalities
    多模式:支持跟踪文本、图像和其他模式
  • Full platform: Suite of tools for the complete LLM application development lifecycle
    完整的平台:用于完整 LLM 应用程序开发生命周期的工具套件

请添加图片描述

二,部署并集成langfuse

1.通过docker本地部署langfuse服务

# 获取最新的 Langfuse 仓库副本
git clone https://github.com/langfuse/langfuse.git
cd langfuse

# 运行 Langfuse 的 docker compose
docker compose up

2.新建项目以获取api-key

请添加图片描述

3.在llm应用文件下创建.env文件,将刚才创建的key配置到文件中。

LANGFUSE_SECRET_KEY="sk-lf-..."
LANGFUSE_PUBLIC_KEY="pk-lf-.."
LANGFUSE_HOST="http://localhost:3000"
#openai 配置
OPENAI_API_KEY="EMPTY"  # 当前使用的值
OPENAI_API_BASE="http://localhost:80/v1"

# 服务配置
PORT=8888  # 服务端口,可根据需要修改
HOST="0.0.0.0"  # 监听地址

4.安装所需依赖:


pip install python-dotenv

pip install langfuse

5.加载环境变量

from dotenv import load_dotenv
import os

# 加载.env文件中的环境变量

load_dotenv()

# 然后可以通过os.environ获取环境变量
openai_api_base = os.getenv("OPENAI_API_BASE", "http://localhost:8080/v1")
api_key = os.environ.get("OPENAI_API_KEY")

6.通过@observe() 装饰器跟踪本地LLM 应用程序,LangfuseOpenAI 集成来自动捕获所有模型参数。完整程序代码如下:

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import re
import os
from dotenv import load_dotenv
from langfuse.decorators import observe
from langfuse.openai import openai  # 使用Langfuse的OpenAI客户端

# 加载.env文件中的环境变量

load_dotenv()

# 从环境变量中获取配置

openai_api_key = os.getenv("OPENAI_API_KEY", "EMPTY")
openai_api_base = os.getenv("OPENAI_API_BASE", "http://localhost:8080/v1")
model = "/home"

app = FastAPI()

class TranslationRequest(BaseModel):
    src_lang: str
    tgt_lang: str
    text: str

@app.post("/translate")
@observe()  # 添加Langfuse装饰器来监控翻译函数
async def translate(request: TranslationRequest):
    text = re.sub(r'[\s+]', '', request.text)
    if not request.src_lang or not request.tgt_lang or not text:
        raise HTTPException(status_code=400, detail="缺少参数")
    
    prompt = f"请将以下内容从{request.src_lang}翻译为{request.tgt_lang},仅输出翻译结果:\n{text}"
    

    # 使用Langfuse的OpenAI客户端

    client = openai.OpenAI(
        api_key=openai_api_key,
        base_url=openai_api_base
    )
    
    completion = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}],
        stream=False
    )
    
    result = completion.choices[0].message.content.strip()
    return {"result": result}

# 添加启动代码,使用环境变量中的端口和主机配置

if __name__ == "__main__":
    import uvicorn
    port = int(os.getenv("PORT", 8888))
    host = os.getenv("HOST", "0.0.0.0")
    uvicorn.run(app, host=host, port=port)

7.测试跟踪效果,看到langfuse后台已经成功跟踪到了llm应用输入输出数据情况

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值