本文来介绍另一款生产级AI应用维护平台:LangFuse,它是开源的,是LangSmith 的平替,并且它可集成 LangChain,同时也可直接对接 OpenAI API。
官方网站:https://langfuse.com/
项目地址:https://github.com/langfuse
0. 环境准备
(1)先注册,登录,官网地址在上面
(2)创建Project

(3)生成私钥和公钥
(4)本地安装 langfuse
!pip install langfuse openai
Collecting langfuse
Downloading langfuse-2.27.1-py3-none-any.whl (157 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m157.5/157.5 kB[0m [31m3.3 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting openai
Downloading openai-1.23.6-py3-none-any.whl (311 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m311.6/311.6 kB[0m [31m8.6 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting backoff<3.0.0,>=2.2.1 (from langfuse)
Downloading backoff-2.2.1-py3-none-any.whl (15 kB)
Collecting httpx<1.0,>=0.15.4 (from langfuse)
Downloading httpx-0.27.0-py3-none-any.whl (75 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m75.6/75.6 kB[0m [31m8.8 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: idna<4.0,>=3.7 in /usr/local/lib/python3.10/dist-packages (from langfuse) (3.7)
Collecting packaging<24.0,>=23.2 (from langfuse)
Downloading packaging-23.2-py3-none-any.whl (53 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m53.0/53.0 kB[0m [31m5.8 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: pydantic<3.0,>=1.10.7 in /usr/local/lib/python3.10/dist-packages (from langfuse) (2.7.0)
Requirement already satisfied: wrapt<2.0,>=1.14 in /usr/local/lib/python3.10/dist-packages (from langfuse) (1.14.1)
Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai) (3.7.1)
Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai) (1.7.0)
Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai) (1.3.1)
Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai) (4.66.2)
Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from openai) (4.11.0)
Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai) (1.2.1)
Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1.0,>=0.15.4->langfuse) (2024.2.2)
Collecting httpcore==1.* (from httpx<1.0,>=0.15.4->langfuse)
Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m77.9/77.9 kB[0m [31m6.7 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1.0,>=0.15.4->langfuse)
Downloading h11-0.14.0-py3-none-any.whl (58 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m58.3/58.3 kB[0m [31m6.6 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3.0,>=1.10.7->langfuse) (0.6.0)
Requirement already satisfied: pydantic-core==2.18.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3.0,>=1.10.7->langfuse) (2.18.1)
Installing collected packages: packaging, h11, backoff, httpcore, httpx, openai, langfuse
Attempting uninstall: packaging
Found existing installation: packaging 24.0
Uninstalling packaging-24.0:
Successfully uninstalled packaging-24.0
Successfully installed backoff-2.2.1 h11-0.14.0 httpcore-1.0.5 httpx-0.27.0 langfuse-2.27.1 openai-1.23.6 packaging-23.2
# 设置OpenAI KEY环境变量
import os
import getpass
os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')
OpenAI API Key:··········
1. 开始使用
LangFuse有两种集成方式:
OpenAI API集成
LangChain集成
在运行之前,先将你的公钥和私钥放到环境变量中。例如.env文件中加入:
# 把如下内容加入到.env文件中,也可以使用如下方式添加环境变量
LANGFUSE_SECRET_KEY = "sk-lf-xxxxx"
LANGFUSE_PUBLIC_KEY = "pk-lf-xxxxx"
# 添加私钥
os.environ['LANGFUSE_SECRET_KEY'] = getpass.getpass('LANGFUSE_SECRET_KEY:')
LANGFUSE_SECRET_KEY:··········
# 添加公钥
os.environ['LANGFUSE_PUBLIC_KEY'] = getpass.getpass('LANGFUSE_PUBLIC_KEY')
LANGFUSE_PUBLIC_KEY··········
这样才能使你的程序与你在LangFuse官网上建立的跟踪项目链接起来。
1.1 OpenAI API集成方式
集成步骤:
(1)引入langfuse中的openai:from langfuse.openai import openai,用这个才能集成langfuse
(2)使用Langfuse实例的trace函数,传入一些个人和项目信息
(3)openai接口调用,多了一个trace_id参数
from datetime import datetime
from langfuse.openai import openai ## 1. 引入langfuse中的openai
from langfuse import Langfuse
import os
## 2. 使用Langfuse实例的trace函数,传入一些个人和项目信息
trace = Langfuse().trace(
name = "hello-world",
user_id = "Arron",
release = "v0.0.1"
)
completion = openai.chat.completions.create(
name="hello-world",
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "对我说'Hello, World!'"}
],
temperature=0,
trace_id=trace.id, ## 3. openai接口调用,多了一个trace_id参数
)
print(completion.choices[0].message.content)
## 输出:Hello, World!
Hello, World!
运行之后,看下LangFuse平台的调用情况

1.2 LangChain集成方式
通过 LangChain 的回调集成。
集成步骤:
(1)从langfuse中引入CallbackHandler:from langfuse.callback import CallbackHandler
(2)在CallbackHandler中设置个人和项目信息
(3)正常创建LangChain应用和流程
(4)invoke时,将CallbackHandler填入config参数中:config={“callbacks”:[handler]}
!pip install langchain langchain_openai
Collecting langchain
Downloading langchain-0.1.16-py3-none-any.whl (817 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m817.7/817.7 kB[0m [31m6.4 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting langchain_openai
Downloading langchain_openai-0.1.3-py3-none-any.whl (33 kB)
Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (6.0.1)
Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.0.29)
Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (3.9.5)
Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (4.0.3)
Collecting dataclasses-json<0.7,>=0.5.7 (from langchain)
Downloading dataclasses_json-0.6.4-py3-none-any.whl (28 kB)
Collecting jsonpatch<2.0,>=1.33 (from langchain)
Downloading jsonpatch-1.33-py2.py3-none-any.whl (12 kB)
Collecting langchain-community<0.1,>=0.0.32 (from langchain)
Downloading langchain_community-0.0.34-py3-none-any.whl (1.9 MB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m1.9/1.9 MB[0m [31m11.5 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting langchain-core<0.2.0,>=0.1.42 (from langchain)
Downloading langchain_core-0.1.46-py3-none-any.whl (299 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m299.3/299.3 kB[0m [31m12.2 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting langchain-text-splitters<0.1,>=0.0.1 (from langchain)
Downloading langchain_text_splitters-0.0.1-py3-none-any.whl (21 kB)
Collecting langsmith<0.2.0,>=0.1.17 (from langchain)
Downloading langsmith-0.1.51-py3-none-any.whl (115 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m116.0/116.0 kB[0m [31m12.8 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: numpy<2,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.25.2)
Requirement already satisfied: pydantic<3,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.7.0)
Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.31.0)
Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (8.2.3)
Requirement already satisfied: openai<2.0.0,>=1.10.0 in /usr/local/lib/python3.10/dist-packages (from langchain_openai) (1.23.6)
Collecting tiktoken<1,>=0.5.2 (from langchain_openai)
Downloading tiktoken-0.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m1.8/1.8 MB[0m [31m18.3 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)
Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.2.0)
Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)
Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.5)
Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)
Collecting marshmallow<4.0.0,>=3.18.0 (from dataclasses-json<0.7,>=0.5.7->langchain)
Downloading marshmallow-3.21.1-py3-none-any.whl (49 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m49.4/49.4 kB[0m [31m5.8 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting typing-inspect<1,>=0.4.0 (from dataclasses-json<0.7,>=0.5.7->langchain)
Downloading typing_inspect-0.9.0-py3-none-any.whl (8.8 kB)
Collecting jsonpointer>=1.9 (from jsonpatch<2.0,>=1.33->langchain)
Downloading jsonpointer-2.4-py2.py3-none-any.whl (7.8 kB)
Requirement already satisfied: packaging<24.0,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2.0,>=0.1.42->langchain) (23.2)
Collecting orjson<4.0.0,>=3.9.14 (from langsmith<0.2.0,>=0.1.17->langchain)
Downloading orjson-3.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (141 kB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m141.1/141.1 kB[0m [31m15.9 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.10.0->langchain_openai) (3.7.1)
Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai<2.0.0,>=1.10.0->langchain_openai) (1.7.0)
Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.10.0->langchain_openai) (0.27.0)
Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.10.0->langchain_openai) (1.3.1)
Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.10.0->langchain_openai) (4.66.2)
Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.10.0->langchain_openai) (4.11.0)
Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (0.6.0)
Requirement already satisfied: pydantic-core==2.18.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (2.18.1)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2024.2.2)
Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.10/dist-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.3)
Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from tiktoken<1,>=0.5.2->langchain_openai) (2023.12.25)
Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.10.0->langchain_openai) (1.2.1)
Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.10.0->langchain_openai) (1.0.5)
Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.10.0->langchain_openai) (0.14.0)
Collecting mypy-extensions>=0.3.0 (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain)
Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)
Installing collected packages: orjson, mypy-extensions, marshmallow, jsonpointer, typing-inspect, tiktoken, jsonpatch, langsmith, dataclasses-json, langchain-core, langchain-text-splitters, langchain_openai, langchain-community, langchain
Successfully installed dataclasses-json-0.6.4 jsonpatch-1.33 jsonpointer-2.4 langchain-0.1.16 langchain-community-0.0.34 langchain-core-0.1.46 langchain-text-splitters-0.0.1 langchain_openai-0.1.3 langsmith-0.1.51 marshmallow-3.21.1 mypy-extensions-1.0.0 orjson-3.10.1 tiktoken-0.6.0 typing-inspect-0.9.0
from langfuse.callback import CallbackHandler
handler = CallbackHandler(
trace_name="SayHello",
user_id="Arron",
)
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema import HumanMessage
from langchain.prompts.chat import HumanMessagePromptTemplate
from langchain.prompts import ChatPromptTemplate
model = ChatOpenAI(model="gpt-3.5-turbo-0613")
prompt_template = """
我的名字叫【{name}】,我的个人介绍是【{description}】。
请根据我的名字和介绍,帮我想一段有吸引力的自我介绍的句子,以此来吸引读者关注和点赞我的账号。
"""
prompt = ChatPromptTemplate.from_messages([
HumanMessagePromptTemplate.from_template(prompt_template)
])
# 定义输出解析器
parser = StrOutputParser()
chain = (
{"name":RunnablePassthrough(), "description":RunnablePassthrough() } ## 这里是给prompt的输入,两个参数
| prompt
| model
| parser
)
## invoke的第一个参数,传入json格式的参数,key与prompt中的参数名一致
response = chain.invoke({'name': 'Arron', 'description': '热爱AI,持续学习,持续干货输出'}, config={"callbacks":[handler]})
print(response)
/usr/local/lib/python3.10/dist-packages/langchain_core/_api/beta_decorator.py:87: LangChainBetaWarning: The function `loads` is in beta. It is actively being worked on, so the API may change.
warn_beta(
大家好,我是Arron,一个热爱AI的学习者,不仅如此,我还持续输出干货。无论是深入探讨AI技术的最新进展,还是分享实用的学习方法,我都努力为大家提供有价值的内容。如果你也对AI充满热情,想要不断学习和成长,那么请关注我的账号,我们一起探索AI的无限可能!
运行之后看下LangFuse平台,应该能看到你的项目和调用了。

2. 详细信息查看 - Traces页面
使用Traces页面,可以看到你每次程序运行的详细过程,包括每一步的输入、输出、耗时、token数等。
(1)进入Traces,可以看到你所有的运行目录。

(2)点击上方的任一ID,可以进入到本次运行的详细跟踪页面。该页面包含了本次运行的过程:详细执行步骤(右侧)。点击任一步骤,可以在左侧看到本步骤的输入和输出,以及耗时和token数(图中的145->133表示输入145token,输出133token)。

有了这个页面,你可以轻易的跟踪输入输出是否有问题,哪一步有问题,从而更好的调优你的程序。这个比自己打日志的信息更详细和更好用多了。

7506

被折叠的 条评论
为什么被折叠?



