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

相关推荐

  1. langfuse使用零星记录

    2024-04-29 01:52:04       32 阅读
  2. vue 零散记录之全局安装包

    2024-04-29 01:52:04       56 阅读
  3. 学习中的零碎记录

    2024-04-29 01:52:04       60 阅读
  4. 软考之零碎片段记录(五)

    2024-04-29 01:52:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-29 01:52:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-29 01:52:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-29 01:52:04       82 阅读
  4. Python语言-面向对象

    2024-04-29 01:52:04       91 阅读

热门阅读

  1. UI图中的opacity效果和代码效果不一样

    2024-04-29 01:52:04       26 阅读
  2. 基于EBAZ4205矿板的图像处理:01简介

    2024-04-29 01:52:04       30 阅读
  3. 生成式人工智能AIGC技术的发展现状和未来趋势

    2024-04-29 01:52:04       36 阅读
  4. Face XY project

    2024-04-29 01:52:04       24 阅读
  5. Ruoyi-Vue前端部署-nginx部署多个vue前端项目

    2024-04-29 01:52:04       31 阅读
  6. pytorch运行物体检测模型 SSD

    2024-04-29 01:52:04       27 阅读
  7. php 姓名加星号

    2024-04-29 01:52:04       30 阅读
  8. c++刷题------ 最长无重复子数组

    2024-04-29 01:52:04       35 阅读
  9. Windows电脑的显存容量查看

    2024-04-29 01:52:04       24 阅读
  10. 设计模式:迪米特法则(Law of Demeter,LoD)介绍

    2024-04-29 01:52:04       30 阅读
  11. Python zerorpc如何使用

    2024-04-29 01:52:04       29 阅读
  12. Linux详解:进程终止、错误码

    2024-04-29 01:52:04       34 阅读