记录自定义LLM跟踪
如果您未以正确的格式记录 LLM 追踪,系统不会崩溃,数据仍会被记录。然而,这些数据将无法以针对 LLM 的特定方式进行处理或渲染。
追踪 OpenAI 模型日志的最佳方式是使用 包装器,该包装器可在 langsmith SDK(适用于 Python 和 TypeScript)中找到。不过,您也可以按照以下指南记录自定义模型的日志。
LangSmith 为 LLM 追踪提供特殊的渲染和处理功能,包括令牌计数(假设模型提供商未提供令牌计数)和基于令牌的成本计算。 为了充分利用此功能,您必须以特定格式记录您的 LLM 追踪。
Chat-style models
对于聊天风格模型,输入必须是 OpenAI 兼容格式的对话消息列表,表示为 Python 字典或 TypeScript 对象。每条消息必须包含键 role 和 content。
输出接受以下任何格式:
- 一个包含键
choices的字典/对象,其值是一个字典/对象列表。每个字典/对象必须包含键message,该键映射到一个消息对象,该消息对象包含键role和content。 - 一个包含键
message的字典/对象,其值是一个消息对象,该消息对象包含键role和content。 - 一个包含两个元素的元组/数组,其中第一个元素是角色,第二个元素是内容。
- 一个包含键
role和content的字典/对象。
你的函数输入应命名为 messages。
您也可以提供以下 metadata 个字段,以帮助 LangSmith 识别模型并计算成本。如果使用 LangChain 或 OpenAI 包装器,这些字段将自动正确填充。要了解如何使用 metadata 个字段,请参阅 此指南。
ls_provider: 模型的提供者,例如 \"openai\"、\"anthropic\" 等。ls_model_name: 模型的名称,例如 \"gpt-4o-mini\"、\"claude-3-opus-20240307\" 等。
- Python
- TypeScript
from langsmith import traceable
inputs = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "I'd like to book a table for two."},
]
output = {
"choices": [
{
"message": {
"role": "assistant",
"content": "Sure, what time would you like to book the table for?"
}
}
]
}
# Can also use one of:
# output = {
# "message": {
# "role": "assistant",
# "content": "Sure, what time would you like to book the table for?"
# }
# }
#
# output = {
# "role": "assistant",
# "content": "Sure, what time would you like to book the table for?"
# }
#
# output = ["assistant", "Sure, what time would you like to book the table for?"]
@traceable(
run_type="llm",
metadata={"ls_provider": "my_provider", "ls_model_name": "my_model"}
)
def chat_model(messages: list):
return output
chat_model(inputs)
import { traceable } from "langsmith/traceable";
const messages = [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "I'd like to book a table for two." }
];
const output = {
choices: [
{
message: {
role: "assistant",
content: "Sure, what time would you like to book the table for?"
}
}
]
};
// Can also use one of:
// const output = {
// message: {
// role: "assistant",
// content: "Sure, what time would you like to book the table for?"
// }
// };
//
// const output = {
// role: "assistant",
// content: "Sure, what time would you like to book the table for?"
// };
//
// const output = ["assistant", "Sure, what time would you like to book the table for?"];
const chatModel = traceable(
async ({ messages }: { messages: { role: string; content: string }[] }) => {
return output;
},
{ run_type: "llm", name: "chat_model", metadata: { ls_provider: "my_provider", ls_model_name: "my_model" } }
);
await chatModel({ messages });
上述代码将输出以下跟踪信息:

流式输出
对于流式输出,您可以将输出“缩减”为与非流式版本相同的格式。目前仅支持 Python。
def _reduce_chunks(chunks: list):
all_text = "".join([chunk["choices"][0]["message"]["content"] for chunk in chunks])
return {"choices": [{"message": {"content": all_text, "role": "assistant"}}]}
@traceable(
run_type="llm",
reduce_fn=_reduce_chunks,
metadata={"ls_provider": "my_provider", "ls_model_name": "my_model"}
)
def my_streaming_chat_model(messages: list):
for chunk in ["Hello, " + messages[1]["content"]]:
yield {
"choices": [
{
"message": {
"content": chunk,
"role": "assistant",
}
}
]
}
list(
my_streaming_chat_model(
[
{"role": "system", "content": "You are a helpful assistant. Please greet the user."},
{"role": "user", "content": "polly the parrot"},
],
)
)
手动提供令牌计数
要了解如何基于令牌使用信息设置基于令牌的计费跟踪,请参阅此指南。
默认情况下,LangSmith 使用 TikToken 来统计 token 数量,该功能基于提供的 ls_model_name 对模型的 tokenizer 进行最佳猜测。 许多模型已在响应中包含 token 数量。您可以通过在响应中提供 usage_metadata 字段,将这些 token 数量发送给 LangSmith。 如果向 LangSmith 传递了 token 信息,系统将使用该信息 而非 TikToken 进行统计。
您可以向函数的响应添加一个usage_metadata键,该键包含一个字典,其中包含input_tokens、output_tokens和total_tokens作为键。 如果使用 LangChain 或OpenAI 包装器,这些字段将自动正确填充。
如果 ls_model_name 中不存在,可能会使用 extra.invocation_metadata 中的其他字段来估算 token 数量。以下字段按优先级顺序使用:
metadata.ls_model_nameinvocation_params.modelinvocation_params.model_name
- Python
- TypeScript
from langsmith import traceable
inputs = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "I'd like to book a table for two."},
]
output = {
"choices": [
{
"message": {
"role": "assistant",
"content": "Sure, what time would you like to book the table for?"
}
}
],
"usage_metadata": {
"input_tokens": 27,
"output_tokens": 13,
"total_tokens": 40,
},
}
@traceable(
run_type="llm",
metadata={"ls_provider": "my_provider", "ls_model_name": "my_model"}
)
def chat_model(messages: list):
return output
chat_model(inputs)
import { traceable } from "langsmith/traceable";
const messages = [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "I'd like to book a table for two." },
];
const output = {
choices: [
{
message: {
role: "assistant",
content: "Sure, what time would you like to book the table for?",
},
},
],
usage_metadata: {
input_tokens: 27,
output_tokens: 13,
total_tokens: 40,
},
};
const chatModel = traceable(
async ({
messages,
}: {
messages: { role: string; content: string }[];
model: string;
}) => {
return output;
},
{ run_type: "llm", name: "chat_model", metadata: { ls_provider: "my_provider", ls_model_name: "my_model" } }
);
await chatModel({ messages });
指导式模型
对于指令风格模型(字符串输入,字符串输出),您的输入必须包含一个键为 prompt 且值为字符串的字段。其他输入也是允许的。输出必须返回一个对象,序列化后需包含键为 choices 的列表,该列表中每个字典/对象都必须包含一个键为 text 且值为字符串的字段。 关于 metadata 和 usage_metadata 的规则与聊天风格模型相同。
- Python
- TypeScript
@traceable(
run_type="llm",
metadata={"ls_provider": "my_provider", "ls_model_name": "my_model"}
)
def hello_llm(prompt: str):
return {
"choices": [
{"text": "Hello, " + prompt}
],
"usage_metadata": {
"input_tokens": 4,
"output_tokens": 5,
"total_tokens": 9,
},
}
hello_llm("polly the parrot\n")
import { traceable } from "langsmith/traceable";
const helloLLM = traceable(
({ prompt }: { prompt: string }) => {
return {
choices: [
{ text: "Hello, " + prompt }
],
usage_metadata: {
input_tokens: 4,
output_tokens: 5,
total_tokens: 9,
},
};
},
{ run_type: "llm", name: "hello_llm", metadata: { ls_provider: "my_provider", ls_model_name: "my_model" } }
);
await helloLLM({ prompt: "polly the parrot\n" });
上述代码将输出以下跟踪信息:
