langfuse使用零星记录

目录

前言

一、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 strings
    local_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 Langfuse
    for item in local_items:
        langfuse.create_dataset_item(
            dataset_name=ds_name,
            # any python object or value
            input=item["input"],
            # any python object or value, optional
            expected_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.content


def 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使用过程进行零星记录,希望可以帮助一些小伙伴。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值