프롬프트의 변수

프롬프트의 변수 (Variables in Prompts)

변수는 프롬프트에서 동적 문자열을 위한 플레이스홀더예요. 프롬프트 정의 자체를 바꾸지 않고도 런타임에 커스터마이즈할 수 있는 유연한 프롬프트 템플릿을 만들 수 있게 해줘요.

출처: 문서

본문

모든 프롬프트는 {{variable}} 구문으로 변수를 지원해요. Langfuse에서 프롬프트를 가져와 컴파일하면 프롬프트 템플릿에 삽입할 이 변수들의 값을 제공해요.

시작하기

변수가 있는 프롬프트 만들기

Langfuse UIPython SDKJS/TS SDK

Langfuse UI에서 프롬프트를 만들 때 프롬프트 텍스트의 어디에든 이중 중괄호 {{variable_name}}를 사용해 변수를 정의하면 돼요.

변수는 텍스트 프롬프트채팅 프롬프트 모두에서 동작해요. 어떤 메시지 콘텐츠에서도 사용할 수 있어요.

from langfuse import get_client

langfuse = get_client()

# Text prompt with variables
langfuse.create_prompt(
    name="movie-critic",
    type="text",
    prompt="As a {{criticLevel}} movie critic, do you like {{movie}}?",
    labels=["production"],
)

# Chat prompt with variables
langfuse.create_prompt(
    name="movie-critic-chat",
    type="chat",
    prompt=[
        {
            "role": "system",
            "content": "You are a {{criticLevel}} movie critic."
        },
        {
            "role": "user",
            "content": "What do you think about {{movie}}?"
        }
    ],
    labels=["production"],
)
import { LangfuseClient } from "@langfuse/client";

const langfuse = new LangfuseClient();

// Text prompt with variables
await langfuse.prompt.create({
  name: "movie-critic",
  type: "text",
  prompt: "As a {{criticLevel}} movie critic, do you like {{movie}}?",
  labels: ["production"],
});

// Chat prompt with variables
await langfuse.prompt.create({
  name: "movie-critic-chat",
  type: "chat",
  prompt: [
    {
      role: "system",
      content: "You are a {{criticLevel}} movie critic.",
    },
    {
      role: "user",
      content: "What do you think about {{movie}}?",
    },
  ],
  labels: ["production"],
});

런타임에 변수 컴파일하기

애플리케이션에서 .compile() 메서드를 사용해 변수를 실제 값으로 대체해요. 변수를 키워드 인자(Python) 또는 객체(JavaScript/TypeScript)로 전달하세요.

Python SDKJS/TS SDKLangChain (Python)LangChain (JS/TS)

from langfuse import get_client

langfuse = get_client()

# Get the prompt
prompt = langfuse.get_prompt("movie-critic")

# Compile with variable values
compiled_prompt = prompt.compile(
    criticLevel="expert",
    movie="Dune 2"
)

# -> compiled_prompt = "As an expert movie critic, do you like Dune 2?"

# Use with your LLM
response = openai.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": compiled_prompt}]
)
import { LangfuseClient } from "@langfuse/client";

const langfuse = new LangfuseClient();

// Get the prompt
const prompt = await langfuse.prompt.get("movie-critic", {
  type: "text",
});

// Compile with variable values
const compiledPrompt = prompt.compile({
  criticLevel: "expert",
  movie: "Dune 2",
});

// -> compiledPrompt = "As an expert movie critic, do you like Dune 2?"

// Use with your LLM
const response = await openai.chat.completions.create({
  model: "gpt-4",
  messages: [{ role: "user", content: compiledPrompt }],
});
from langfuse import get_client
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate

langfuse = get_client()

# For text prompts
langfuse_prompt = langfuse.get_prompt("movie-critic")
langchain_prompt = PromptTemplate.from_template(langfuse_prompt.get_langchain_prompt())

# Compile with variables
compiled = langchain_prompt.format(criticLevel="expert", movie="Dune 2")
# -> "As an expert movie critic, do you like Dune 2?"

# For chat prompts
langfuse_chat_prompt = langfuse.get_prompt("movie-critic-chat")
langchain_chat_prompt = ChatPromptTemplate.from_messages(
    langfuse_chat_prompt.get_langchain_prompt()
)

# Compile with variables
compiled_messages = langchain_chat_prompt.format_messages(
    criticLevel="expert",
    movie="Dune 2"
)
import { LangfuseClient } from "@langfuse/client";
import { PromptTemplate, ChatPromptTemplate } from "@langchain/core/prompts";

const langfuse = new LangfuseClient();

// For text prompts
const langfusePrompt = await langfuse.prompt.get("movie-critic", {
  type: "text",
});
const langchainPrompt = PromptTemplate.fromTemplate(
  langfusePrompt.getLangchainPrompt()
);

// Compile with variables
const compiled = await langchainPrompt.format({
  criticLevel: "expert",
  movie: "Dune 2",
});
// -> "As an expert movie critic, do you like Dune 2?"

// For chat prompts
const langfuseChatPrompt = await langfuse.prompt.get("movie-critic-chat", {
  type: "chat",
});
const langchainChatPrompt = ChatPromptTemplate.fromMessages(
  langfuseChatPrompt.getLangchainPrompt()
);

// Compile with variables
const compiledMessages = await langchainChatPrompt.formatMessages({
  criticLevel: "expert",
  movie: "Dune 2",
});

정확히 원하는 것이 아니었나요? 다음 유사 기능을 고려해 보세요:

또는 관련 FAQ 페이지:

더 알아보기 (Learn more)