채팅 프롬프트의 메시지 플레이스홀더
채팅 프롬프트의 메시지 플레이스홀더 (Message Placeholders in Chat Prompts)
메시지 플레이스홀더(Message Placeholders)를 사용하면 채팅 프롬프트 내의 특정 위치에 채팅 메시지 목록([{role: "...", content: "..."}])을 삽입할 수 있어요.
출처: 문서
본문
프롬프트에 여러 플레이스홀더를 정의하고 런타임에 다른 값으로 해석(resolve)할 수 있어요. 메시지 플레이스홀더는 Playground와 Prompt Experiments에서도 지원돼요.
애플리케이션에서 플레이스홀더를 사용하려면 최소 langfuse >= 3.1.0(python) 또는 langfuse >= 3.38.0(js)이 필요해요.
플레이스홀더가 있는 프롬프트 만들기
UIPython SDKJS/TS SDK
- Add message placeholder 버튼을 사용해 어떤 프롬프트에도 플레이스홀더를 만들 수 있어요.
- 애플리케이션에서 참조할 플레이스홀더의 이름을 선택하세요.
from langfuse import get_client
langfuse = get_client()
langfuse.create_prompt(
name="movie-critic-chat",
type="chat",
prompt=[
{ "role": "system", "content": "You are an {{criticlevel}} movie critic" },
{ "type": "placeholder", "name": "chat_history" },
{ "role": "user", "content": "What should I watch next?" },
],
labels=["production"], # directly promote to production
)
import { LangfuseClient } from "@langfuse/client";
const langfuse = new LangfuseClient();
await langfuse.prompt.create({
name: "movie-critic-chat",
type: "chat",
prompt: [
{ role: "system", content: "You are an {{criticlevel}} movie critic" },
{ type: "placeholder", name: "chat_history" },
{ role: "user", content: "What should I watch next?" },
],
labels: ["production"], // directly promote to production
});
런타임에 플레이스홀더 해석하기
SDK에서 ChatPromptClient의 .compile(variables, placeholders) 메서드를 사용해 플레이스홀더에 채워질 값을 설정해요.
채워진 메시지는 role과 content 속성을 가진 ChatMessage 형식이어야 하지만, compile은 메시지 형식을 검증하지 않으므로 커스텀 형식도 허용돼요.
Python SDKJS/TS SDKLangChain (Python)LangChain (JS/TS)
from langfuse import get_client
langfuse = get_client()
# Use prompt with placeholders in your application
prompt = langfuse.get_prompt("movie-critic-chat")
# Compile the variable and resolve the placeholder with a list of messages.
compiled_prompt = prompt.compile(criticlevel="expert", chat_history=[
{"role": "user", "content": "I love Ron Fricke movies like Baraka"},
{"role": "user", "content": "Also, the Korean movie Memories of a Murderer"}
])
# -> compiled_prompt = [
# { "role": "system", "content": "You are an expert movie critic" },
# { "role": "user", "content": "I love Ron Fricke movies like Baraka" },
# { "role": "user", "content": "Also, the Korean movie Memories of a Murderer" },
# { "role": "user", "content": "What should I watch next?" },
# ]
import { LangfuseClient } from "@langfuse/client";
const langfuse = new LangfuseClient();
const prompt = await langfuse.prompt.get("movie-critic-chat", {
type: "chat",
});
// Compile the variable and resolve the placeholder with a list of messages.
const compiledPrompt = prompt.compile(
// variables
{ criticlevel: "expert" },
// placeholders
{
chat_history: [
{ role: "user", content: "I love Ron Fricke movies like Baraka" },
{
role: "user",
content: "Also, the Korean movie Memories of a Murderer",
},
],
}
);
// -> compiledPrompt = [
// { role: "system", content: "You are an expert movie critic" },
// { role: "user", content: "I love Ron Fricke movies like Baraka" },
// { role: "user", content: "Also, the Korean movie Memories of a Murderer" },
// { role: "user", content: "What should I watch next?" },
// ]
from langfuse import get_client
from langchain_core.prompts import ChatPromptTemplate
langfuse = get_client()
langfuse_prompt = langfuse.get_prompt("movie-critic-chat")
# Using langchain, you can obtain a MessagesPlaceholder object for unresolved placeholders
langchain_prompt = ChatPromptTemplate.from_messages(langfuse_prompt.get_langchain_prompt())
# -> langchain_prompt = [
# SystemMessage(content="You are an expert movie critic"),
# MessagesPlaceholder(name="chat_history"),
# HumanMessage(content="What should I watch next?"),
# ]
import { LangfuseClient } from "@langfuse/client";
import { ChatPromptTemplate } from "@langchain/core/prompts";
const langfuse = new LangfuseClient();
// Get current `production` version
const langfusePrompt = await langfuse.prompt.get("movie-critic-chat", {
type: "chat",
});
// Using langchain, you can obtain a ChatPromptTemplate with MessagesPlaceholder objects for unresolved placeholders
const langchainPrompt = ChatPromptTemplate.fromMessages(
langfusePrompt.getLangchainPrompt()
);
// -> langchainPrompt = [
// SystemMessage(content="You are an expert movie critic"),
// MessagesPlaceholder(name="chat_history"),
// HumanMessage(content="What should I watch next?"),
// ]
정확히 원하는 것이 아니었나요? 다음 유사 기능을 고려해 보세요:
- Variables — 프롬프트에 동적 텍스트 삽입용
- Prompt references — 하위 프롬프트 재사용용