Featured image of post Work With Langfuse.md

Work With Langfuse.md

使用 langfuse 来监控 LLMs 应用

我们在开发 LLMs 应用时,会考虑 LLMs 调用过程中的性能问题,以及监视过程中的输出。

这个时候 langsmith 以及 langfuse 用处就很大了。

但是,有时候我们本地有计算资源,不想使用云端的资源进行 LLM 调用资源监控,因此就不会考虑 langsmith。

此时,我们可以使用 langfuse 来做这个事情。

部署

部署 langfuse 非常简单,只需要做:

1
2
3
git clone https://github.com/langfuse/langfuse.git
cd langfuse
docker compose up -d

这样一来就部署成功了。

替换

如果之前使用 openai 的 sdk,我们可以这样来继续使用。

项目中安装 langfuse

1
pip install langfuse

配置 API key,你需要在部署好的 langfuse 中使用。

1
2
3
LANGFUSE_SECRET_KEY=<secret key>
LANGFUSE_PUBLIC_KEY=<public key>
LANGFUSE_HOST="http://localhost:3001"

我这里设置 langfuse 的端口是 3001;你应该根据你自己的配置来做。

替换原本的 openai 即可。

1
2
3
# remove: import openai

from langfuse.openai import openai

除此之外,langfuse 也支持 langchainllamaindex,这里就不再赘述了。

思考

coze 也在做大模型 agent 框架,但是思路不太一样。coze 正在做全部的内容,包括工作流以及 LLMs,比较封闭。

但是 langfuse 相对开放,允许使用 langchain,使用其他的模型。

作为开发者,小厂商,相比之下,我更喜欢 langfuse 的模式。因为我可以有更多的选择。但是,如果项目周期比较紧张,coze 又勉强能用,我会选择 coze。

问题

  1. 当我替换掉 openai 的 sdk 时,发生了异常。
1
Unexpected error occurred. Please check your request and contact support: https://langfuse.com/support.
  1. 当我测试 test_langfuse.py 的时候还是有问题。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os

from langfuse.decorators import observe
from langfuse.openai import openai


@observe()
def story():
  return (
    openai.chat.completions.create(
      model="moonshot-v1-auto", # kimi api 测试
      max_tokens=100,
      messages=[
        {"role": "system", "content": "You are a great storyteller."},
        {"role": "user", "content": "Once upon a time in a galaxy far, far away..."},
      ],
    )
    .choices[0]
    .message.content
  )


@observe()
def main():
  return story()


def test_langfuse():
  assert os.getenv("OPENAI_BASE_URL") is not None
  assert os.getenv("OPENAI_API_KEY") is not None
  main()

对于此问题,我开启了一个 discussion

此外,如果想要查看原始代码,可以从 https://github.com/svtter/pdf-reader 中获取。

comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计