记录自定义 LLM 追踪
如果您没有以正确的格式记录 LLM 追踪,也不会有任何问题,数据仍然会被记录。但是,这些数据将不会以特定于 LLM 的方式进行处理或呈现。
从 OpenAI 模型记录追踪的最佳方法是使用 Python 和 TypeScript 的 langsmith
SDK 中提供的包装器。但是,您也可以按照以下指南记录来自自定义模型的追踪。
LangSmith 为 LLM 追踪提供特殊的渲染和处理,包括令牌计数(假设令牌计数无法从模型提供商处获得)和基于令牌的成本计算。为了充分利用此功能,您必须以特定格式记录您的 LLM 追踪。
聊天式模型
对于聊天式模型,输入必须是 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 });
上面的代码将记录以下追踪
流式输出
对于流式处理,您可以将输出“reduce”为与非流式版本相同的格式。目前仅在 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 来计数令牌,根据提供的 ls_model_name
最佳猜测模型的 tokenizer。许多模型已经在响应中包含令牌计数作为一部分。您可以通过在响应中提供 usage_metadata
字段将这些令牌计数发送到 LangSmith。如果令牌信息传递给 LangSmith,系统将代替使用 TikToken 而使用此信息。
您可以向函数的响应添加 usage_metadata
键,其中包含一个字典,其中包含键 input_tokens
、output_tokens
和 total_tokens
。如果使用 LangChain 或 OpenAI 包装器,这些字段将自动正确填充。
如果 ls_model_name
在 extra.metadata
中不存在,则可能会使用 extra.invocation_metadata
中的其他字段来估计令牌计数。以下字段按优先级顺序使用
metadata.ls_model_name
invocation_params.model
invocation_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" });
上面的代码将记录以下追踪