跳到主要内容

🌙 Lunary - GenAI 可观测性

Lunary 是一个开源平台,提供可观测性提示管理分析功能,帮助团队管理和改进 LLM 聊天机器人。

您可以随时通过电子邮件联系我们,或直接安排演示

与 LiteLLM Python SDK 一起使用

前提条件

pip install litellm lunary

快速开始

首先,在 Lunary 控制面板上获取您的 Lunary 公钥。

只使用 2 行代码,即可使用 Lunary 即时记录您在所有提供商上的响应

litellm.success_callback = ["lunary"]
litellm.failure_callback = ["lunary"]

完整代码

from litellm import completion

os.environ["LUNARY_PUBLIC_KEY"] = "your-lunary-public-key" # from https://app.lunary.ai/)
os.environ["OPENAI_API_KEY"] = ""

litellm.success_callback = ["lunary"]
litellm.failure_callback = ["lunary"]

response = completion(
model="gpt-4o",
messages=[{"role": "user", "content": "Hi there 👋"}],
user="ishaan_litellm"
)

与 LangChain ChatLiteLLM 一起使用

import os
from langchain.chat_models import ChatLiteLLM
from langchain.schema import HumanMessage
import litellm

os.environ["LUNARY_PUBLIC_KEY"] = "" # from https://app.lunary.ai/settings
os.environ['OPENAI_API_KEY']="sk-..."

litellm.success_callback = ["lunary"]
litellm.failure_callback = ["lunary"]

chat = ChatLiteLLM(
model="gpt-4o"
messages = [
HumanMessage(
content="what model are you"
)
]
chat(messages)

与提示模板一起使用

您可以使用 Lunary 管理提示模板,并通过 LiteLLM 将它们应用于您的所有 LLM 提供商。

from litellm import completion
from lunary

template = lunary.render_template("template-slug", {
"name": "John", # Inject variables
})

litellm.success_callback = ["lunary"]

result = completion(**template)

与自定义链一起使用

您可以将 LLM 调用封装在自定义链中,以便将它们可视化为跟踪。

import litellm
from litellm import completion
import lunary

litellm.success_callback = ["lunary"]
litellm.failure_callback = ["lunary"]

@lunary.chain("My custom chain name")
def my_chain(chain_input):
chain_run_id = lunary.run_manager.current_run_id
response = completion(
model="gpt-4o",
messages=[{"role": "user", "content": "Say 1"}],
metadata={"parent_run_id": chain_run_id},
)

response = completion(
model="gpt-4o",
messages=[{"role": "user", "content": "Say 2"}],
metadata={"parent_run_id": chain_run_id},
)
chain_output = response.choices[0].message
return chain_output

my_chain("Chain input")

与 LiteLLM 代理服务器一起使用

步骤1:安装依赖项并设置环境变量

安装依赖项

pip install litellm lunary

https://app.lunary.ai/settings 获取您的 Lunary 公钥

export LUNARY_PUBLIC_KEY="<your-public-key>"

步骤2:创建 config.yaml 并设置 lunary 回调

model_list:
- model_name: "*"
litellm_params:
model: "*"
litellm_settings:
success_callback: ["lunary"]
failure_callback: ["lunary"]

步骤3:启动 LiteLLM 代理

litellm --config config.yaml

步骤4:发起请求

curl -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "You are a helpful math tutor. Guide the user through the solution step by step."
},
{
"role": "user",
"content": "how can I solve 8x + 7 = -23"
}
]
}'

您可以在此页面找到有关向 LiteLLM 代理发起请求的不同方法的更多详细信息

支持与创始人交流