Skip to main content

LLM 相关工具集

Project description

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("完成!")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langfarm-0.1.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langfarm-0.1.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file langfarm-0.1.2.tar.gz.

File metadata

  • Download URL: langfarm-0.1.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/23.2.0

File hashes

Hashes for langfarm-0.1.2.tar.gz
Algorithm Hash digest
SHA256 25c2503d6ad2350318d9b59568393ca12f9bb6ca33e2a98e6995553f3b637009
MD5 de6ae2706ef7a5432e6287cf33173932
BLAKE2b-256 ce2dc3e66674f4b078a9bd780774f0350175f9e33116b7d5d737d55cce19261f

See more details on using hashes here.

File details

Details for the file langfarm-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: langfarm-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/23.2.0

File hashes

Hashes for langfarm-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5232942259ab073a6fc2abe319d5ce67bff13b1af9e2740b846187c0117489e1
MD5 b7fc0ea4dd511d34ae44441c5b531b17
BLAKE2b-256 0544d1e7524ba4936a07aa60f6e58ee4ad6012aa07674297aef4c3d3795f5a78

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page