보장된 가용성
보장된 가용성 (Guaranteed Availability)
이것을 구현하는 것은 애플리케이션에 복잡성을 더하므로 보통은 필요하지 않아요. Langfuse 프롬프트 관리는 여러 캐싱 레이어 덕분에 가용성이 매우 높고, 성능을 면밀히 모니터링하고 있어요(status page). 하지만 100% 가용성이 필요하다면 다음 옵션을 사용할 수 있어요.
출처: 문서
본문
Langfuse API는 업타임이 높고 프롬프트는 SDK에서 로컬로 캐시되어 네트워크 문제가 애플리케이션에 영향을 주지 않아요.
하지만 get_prompt() / getPrompt()은 다음 경우 예외를 던져요:
- 사용 가능한 로컬(신선하거나 오래된) 캐시 프롬프트가 없음 — 첫 접근하는 새 애플리케이션 인스턴스,
- 그리고 네트워크 요청이 실패함 — 네트워킹 또는 Langfuse API 문제(재시도 후)
100% 가용성을 보장하는 두 가지 옵션이 있어요:
- 애플리케이션 시작 시 프롬프트를 사전 페치하고, 프롬프트를 사용할 수 없으면 애플리케이션을 종료한다.
- 이 경우 사용할 폴백 프롬프트를 제공한다.
옵션 1: 프롬프트 사전 페치
애플리케이션 시작 시 프롬프트를 사전 페치하고, 프롬프트를 사용할 수 없으면 애플리케이션을 종료해요.
Python (Flask)JS/TS (Express)
from flask import Flask, jsonify
from langfuse import Langfuse
# Initialize the Flask app and Langfuse client
app = Flask(__name__)
langfuse = Langfuse()
def fetch_prompts_on_startup():
try:
# Fetch and cache the production version of the prompt
langfuse.get_prompt("movie-critic")
except Exception as e:
print(f"Failed to fetch prompt on startup: {e}")
sys.exit(1) # Exit the application if the prompt is not available
# Call the function during application startup
fetch_prompts_on_startup()
@app.route('/get-movie-prompt/<movie>', methods=['GET'])
def get_movie_prompt(movie):
prompt = langfuse.get_prompt("movie-critic")
compiled_prompt = prompt.compile(criticlevel="expert", movie=movie)
return jsonify({"prompt": compiled_prompt})
if __name__ == '__main__':
app.run(debug=True)
import express from "express";
import { LangfuseClient } from "@langfuse/client";
// Initialize the Express app and Langfuse client
const app = express();
const langfuse = new LangfuseClient();
async function fetchPromptsOnStartup() {
try {
// Fetch and cache the production version of the prompt
await langfuse.prompt.get("movie-critic");
} catch (error) {
console.error("Failed to fetch prompt on startup:", error);
process.exit(1); // Exit the application if the prompt is not available
}
}
// Call the function during application startup
fetchPromptsOnStartup();
app.get("/get-movie-prompt/:movie", async (req, res) => {
const movie = req.params.movie;
const prompt = await langfuse.prompt.get("movie-critic");
const compiledPrompt = prompt.compile({ criticlevel: "expert", movie });
res.json({ prompt: compiledPrompt });
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
옵션 2: 폴백
이 경우 사용할 폴백 프롬프트를 제공해요:
Python SDKJS/TS SDK
from langfuse import Langfuse
langfuse = Langfuse()
# Get `text` prompt with fallback
prompt = langfuse.get_prompt(
"movie-critic",
fallback="Do you like {{movie}}?"
)
# Get `chat` prompt with fallback
chat_prompt = langfuse.get_prompt(
"movie-critic-chat",
type="chat",
fallback=[{"role": "system", "content": "You are an expert on {{movie}}"}]
)
# True if the prompt is a fallback
prompt.is_fallback
import { LangfuseClient } from "@langfuse/client";
const langfuse = new LangfuseClient();
// Get `text` prompt with fallback
const prompt = await langfuse.prompt.get("movie-critic", {
fallback: "Do you like {{movie}}?",
});
// Get `chat` prompt with fallback
const chatPrompt = await langfuse.prompt.get("movie-critic-chat", {
type: "chat",
fallback: [{ role: "system", content: "You are an expert on {{movie}}" }],
});
// True if the prompt is a fallback
prompt.isFallback;