Skip to main content

Langfarm

langfarm 是 LLM 应用程序开发的工具集,增加 LLM 应用开发的便利。

Tongyi 集成 Langfuse

准备:本地安装部署 Langfuse

请参考:Langfuse 快速开始

使用 Langchain 的 Callback

安装依赖

pip install langchain-community
pip install langfarm
# 加载 .env 配置
pip install python-dotenv

使用示例

重点是 from langfarm.hooks.langfuse.callback import CallbackHandler 代替 from langfuse.callback import CallbackHandler

import time

from dotenv import load_dotenv
from langchain_community.llms import Tongyi
from langfarm.hooks.langfuse.callback import CallbackHandler

# 加载 .env 配置
load_dotenv()

llm = Tongyi(model="qwen-plus")
langfuse_handler = CallbackHandler()

query = '请用50个字描写春天的景色。'
result = llm.invoke(query, config={"callbacks": [langfuse_handler]})

print(result)
print("等待 5 秒,等待 langfuse 异步上报。")
time.sleep(5)
print("完成!")

然后打开 langfuse 界面查看,http://localhost:3000/

dashscope 使用 observe 也可以取得 token 用量

安装依赖

pip install dashscope
pip install langfarm
# 加载 .env 配置
pip install python-dotenv

使用示例

重点是 from langfarm.hooks.dashscope import Generation 代码 from dashscope import Generation

import os
import time

from dotenv import load_dotenv
from langfuse.decorators import observe, langfuse_context
from langfarm.hooks.dashscope import Generation

load_dotenv()


@observe(as_type="generation")
def tongyi_generation(model_name: str, query: str) -> str:
    response = Generation.call(
        api_key=os.getenv('DASHSCOPE_API_KEY'),
        model=model_name,
        prompt=query,
        result_format="message"
    )

    if response.status_code == 200:
        if response.output.text is not None:
            return response.output.text
        else:
            # result_format="message"
            return response.output.choices[0].message.content
    else:
        tip = "请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code"
        raise Exception(
            f"HTTP返回码:{response.status_code};错误码:{response.code};错误信息:{response.message}。{tip}")


@observe()
def dashscope_hook_call(query: str) -> str:
    output = tongyi_generation("qwen-plus", query)
    langfuse_context.update_current_trace(input=query, output=output)
    return output


if __name__ == '__main__':
    input_query = "请用50个字描写秋天的景色。"
    result = dashscope_hook_call(input_query)
    print(result)
    print("等待 2 秒,等待 langfuse 异步上报。")
    time.sleep(2)
    print("完成!")

Metadata

Release files for langfarm 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langfarm 0.1.1
File Size Uploaded Provenance Actions
langfarm-0.1.1.tar.gz 4.8 kB Without provenance Details for langfarm-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langfarm 0.1.1
File Interpreter ABI Platform Actions
langfarm-0.1.1-py3-none-any.whl Python 3 none any Details for langfarm-0.1.1-py3-none-any.whl

Total release size: 11.0 kB

Release files / langfarm-0.1.1.tar.gz

Download URL langfarm-0.1.1.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
78efb72df82c0617196ba31e5e04f188de535e91d3e400e5c22f446a146420e0
BLAKE2b-256 checksum
How to use checksums
7c1c8e1b955f63d96ebff25ab31dd849553ae0c78bf2cc181d2a945d3268186e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.13.0 Darwin/23.2.0

Release files / langfarm-0.1.1-py3-none-any.whl

Download URL langfarm-0.1.1-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
50c4da86d0d1a9de4dd79c3372d2481c8304a03ed5a42f5ea6aa1a0b73ab7c5b
BLAKE2b-256 checksum
How to use checksums
13d50c3b5bf1680f17bb7f90e89a76f67c37031fa92755e95b6d91034a9bd1f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.13.0 Darwin/23.2.0

Security for langfarm 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page