目录
前言
一、langfuse是什么?
二、使用零星记录
1.评估打分
2.score的问题与解决
总结
前言
langfuse使用过程的一些坑点,做一些记录,便于日后回顾查找,也为同样在学习的小伙伴们异同一些可能的帮助。
期望在学习使用一段时间之后,可以系统性的写一些更有价值的内容出来。
一些说明:这里不是主要记录如何使用langfuse,主要是针对过程出现的问题记录,这样产生的作用与价值更大。
一、langfuse是什么?
LangFuse 是一个开源的大型语言模型(LLM)工程平台,主要用于帮助团队更快地构建生产级的 LLM 应用程序。它提供了追踪、评估、提示管理和指标等功能,以帮助调试和改进你的 LLM 应用程序。LangFuse 支持多种集成方式,例如 OpenAI API 集成和 LangChain 集成,并且可以在线进行数据标注和收集,也支持从本地导入数据集。
二、使用零星记录
1.评估打分
socre即打分,主要用在了评估环节。
在实现一个AI应用的时候,这个应用的主体就是一个Prompt提示词,接下来是提供数据集进行评估,通过打分来看这个提示词的效果如何。
1)创建数据集需要在langfuse的Web界面操作
2)添加数据集数据
这里也可以是jsonl数据,无法是自己读取文件循环加载写入即可
def init_dataset():# example items, could also be json instead of stringslocal_items = [{"input": {"country": "Italy"}, "expected_output": "Rome"},{"input": {"country": "Spain"}, "expected_output": "Madrid"},{"input": {"country": "Brazil"}, "expected_output": "Brasília"},{"input": {"country": "Japan"}, "expected_output": "Tokyo"},{"input": {"country": "India"}, "expected_output": "New Delhi"},{"input": {"country": "Canada"}, "expected_output": "Ottawa"},{"input": {"country": "South Korea"}, "expected_output": "Seoul"},{"input": {"country": "Argentina"}, "expected_output": "Buenos Aires"},{"input": {"country": "South Africa"}, "expected_output": "Pretoria"},{"input": {"country": "Egypt"}, "expected_output": "Cairo"},]# Upload to Langfusefor item in local_items:langfuse.create_dataset_item(dataset_name=ds_name,# any python object or valueinput=item["input"],# any python object or value, optionalexpected_output=item["expected_output"],)
3)定义评估函数
这里定义实际输出=预期输出,主要用于分类判断如输出Y、N,或固定的选项
def simple_evaluation(output, expected_output):return output == expected_output
4)运行测试
如下代码简单说明下:run_my_langchain_llm_app是通过langchain方式调用大模型的,run_langchain_experiment是遍历数据集数据项调用大模型并进行评估打分。
def run_my_langchain_llm_app(input, system_message, callback_handler):# needs to include {country}messages = [SystemMessage(content=system_message),HumanMessage(content=input),]model = ChatOpenAI(model="qwen-turbo", callbacks=[callback_handler])completion = model.invoke(messages)return completion.contentdef run_langchain_experiment(experiment_name, system_message):dataset = langfuse.get_dataset(ds_name)for item in dataset.items:handler = item.get_langchain_handler(run_name=experiment_name)completion = run_my_langchain_llm_app(item.input["country"], system_message, handler)handler.root_span.score(name="exact_match",value=simple_evaluation(completion, item.expected_output),)run_langchain_experiment("langchain_famous_city","The user will input countries, respond with the most famous city in this country",
)
2.score的问题与解决
如上代码都是我从官网拿过来做测试的,但是发现如下代码报错。提示handler的root_span为None,不知是不是langfuse官方有更新
handler.root_span.score(name="exact_match",value=simple_evaluation(completion, item.expected_output),
)
然后我继续看相关文档,发现score还有一种写法是用在直接调用openai的情况,改了下在langchain下也可以使用
langfuse.score(name="exact_match",value=simple_evaluation(completion, item.expected_output),trace_id=handler.get_trace_id(),
)
本质是通过trace_id属性将score与测试run关联了起来
总结
本文主要针对langfuse使用过程进行零星记录,希望可以帮助一些小伙伴。