langfuse-core
Advanced tools
Comparing version 3.3.6 to 3.4.0
@@ -184,2 +184,99 @@ 'use strict'; | ||
mustache.escape = function (text) { | ||
return text; | ||
}; | ||
class BasePromptClient { | ||
constructor(prompt) { | ||
this.name = prompt.name; | ||
this.version = prompt.version; | ||
this.config = prompt.config; | ||
} | ||
_transformToLangchainVariables(content) { | ||
return content.replace(/\{\{(.*?)\}\}/g, "{$1}"); | ||
} | ||
} | ||
class TextPromptClient extends BasePromptClient { | ||
constructor(prompt) { | ||
super(prompt); | ||
this.promptResponse = prompt; | ||
this.prompt = prompt.prompt; | ||
} | ||
compile(variables) { | ||
return mustache.render(this.promptResponse.prompt, variables ?? {}); | ||
} | ||
getLangchainPrompt() { | ||
/** | ||
* Converts Langfuse prompt into string compatible with Langchain PromptTemplate. | ||
* | ||
* It specifically adapts the mustache-style double curly braces {{variable}} used in Langfuse | ||
* to the single curly brace {variable} format expected by Langchain. | ||
* | ||
* @returns {string} The string that can be plugged into Langchain's PromptTemplate. | ||
*/ | ||
return this._transformToLangchainVariables(this.prompt); | ||
} | ||
} | ||
class ChatPromptClient extends BasePromptClient { | ||
constructor(prompt) { | ||
super(prompt); | ||
this.promptResponse = prompt; | ||
this.prompt = prompt.prompt; | ||
} | ||
compile(variables) { | ||
return this.prompt.map(chatMessage => ({ | ||
...chatMessage, | ||
content: mustache.render(chatMessage.content, variables ?? {}) | ||
})); | ||
} | ||
getLangchainPrompt() { | ||
/** | ||
* Converts Langfuse prompt into string compatible with Langchain PromptTemplate. | ||
* | ||
* It specifically adapts the mustache-style double curly braces {{variable}} used in Langfuse | ||
* to the single curly brace {variable} format expected by Langchain. | ||
* Example usage: | ||
* | ||
* ``` | ||
* import { ChatPromptTemplate } from "@langchain/core/prompts"; | ||
* | ||
* const langchainChatPrompt = ChatPromptTemplate.fromMessages( | ||
* langfuseChatPrompt.getLangchainPrompt().map((m) => [m.role, m.content]) | ||
* ); | ||
* | ||
* const formattedPrompt = await langchainPrompt.format(values); | ||
* | ||
* ``` | ||
* @returns {ChatMessage[]} Chat messages with variables that can be plugged into Langchain's ChatPromptTemplate. | ||
*/ | ||
return this.prompt.map(chatMessage => ({ | ||
...chatMessage, | ||
content: this._transformToLangchainVariables(chatMessage.content) | ||
})); | ||
} | ||
} | ||
const DEFAULT_PROMPT_CACHE_TTL_SECONDS = 60; | ||
class LangfusePromptCacheItem { | ||
constructor(value, ttlSeconds) { | ||
this.value = value; | ||
this._expiry = Date.now() + ttlSeconds * 1000; | ||
} | ||
get isExpired() { | ||
return Date.now() > this._expiry; | ||
} | ||
} | ||
class LangfusePromptCache { | ||
constructor() { | ||
this._cache = new Map(); | ||
this._defaultTtlSeconds = DEFAULT_PROMPT_CACHE_TTL_SECONDS; | ||
} | ||
getIncludingExpired(key) { | ||
return this._cache.get(key) ?? null; | ||
} | ||
set(key, value, ttlSeconds) { | ||
const effectiveTtlSeconds = ttlSeconds ?? this._defaultTtlSeconds; | ||
this._cache.set(key, new LangfusePromptCacheItem(value, effectiveTtlSeconds)); | ||
} | ||
} | ||
class LangfuseMemoryStorage { | ||
@@ -197,3 +294,2 @@ constructor() { | ||
const DEFAULT_PROMPT_CACHE_TTL_SECONDS = 60; | ||
class LangfuseFetchHttpError extends Error { | ||
@@ -746,4 +842,10 @@ constructor(response, body) { | ||
async createPrompt(body) { | ||
const prompt = await this.createPromptStateless(body); | ||
return new LangfusePromptClient(prompt); | ||
const promptResponse = await this.createPromptStateless({ | ||
...body, | ||
type: body.type ?? "text" | ||
}); | ||
if (promptResponse.type === "chat") { | ||
return new ChatPromptClient(promptResponse); | ||
} | ||
return new TextPromptClient(promptResponse); | ||
} | ||
@@ -776,3 +878,9 @@ async getPrompt(name, version, options) { | ||
} | ||
const prompt = new LangfusePromptClient(data); | ||
let prompt; | ||
if (data.type === "chat") { | ||
prompt = new ChatPromptClient(data); | ||
} else { | ||
prompt = new TextPromptClient(data); | ||
} | ||
// const prompt = data.type === "chat" ? new ChatPromptClient(data) : new TextPromptClient(data); | ||
this._promptCache.set(this._getPromptCacheKey(name, version), prompt, cacheTtlSeconds); | ||
@@ -915,49 +1023,4 @@ return prompt; | ||
} | ||
class LangfusePromptClient { | ||
constructor(prompt) { | ||
this.promptResponse = prompt; | ||
this.name = prompt.name; | ||
this.version = prompt.version; | ||
this.prompt = prompt.prompt; | ||
this.config = prompt.config; | ||
} | ||
compile(variables) { | ||
return mustache.render(this.promptResponse.prompt, variables ?? {}); | ||
} | ||
getLangchainPrompt() { | ||
/** | ||
* Converts Langfuse prompt into string compatible with Langchain PromptTemplate. | ||
* | ||
* It specifically adapts the mustache-style double curly braces {{variable}} used in Langfuse | ||
* to the single curly brace {variable} format expected by Langchain. | ||
* | ||
* @returns {string} The string that can be plugged into Langchain's PromptTemplate. | ||
*/ | ||
return this.prompt.replace(/\{\{(.*?)\}\}/g, "{$1}"); | ||
} | ||
} | ||
class LangfusePromptCacheItem { | ||
constructor(value, ttlSeconds) { | ||
this.value = value; | ||
this._expiry = Date.now() + ttlSeconds * 1000; | ||
} | ||
get isExpired() { | ||
return Date.now() > this._expiry; | ||
} | ||
} | ||
class LangfusePromptCache { | ||
constructor() { | ||
this._cache = new Map(); | ||
this._defaultTtlSeconds = DEFAULT_PROMPT_CACHE_TTL_SECONDS; | ||
} | ||
getIncludingExpired(key) { | ||
return this._cache.get(key) ?? null; | ||
} | ||
set(key, value, ttlSeconds) { | ||
const effectiveTtlSeconds = ttlSeconds ?? this._defaultTtlSeconds; | ||
this._cache.set(key, new LangfusePromptCacheItem(value, effectiveTtlSeconds)); | ||
} | ||
} | ||
exports.DEFAULT_PROMPT_CACHE_TTL_SECONDS = DEFAULT_PROMPT_CACHE_TTL_SECONDS; | ||
exports.ChatPromptClient = ChatPromptClient; | ||
exports.LangfuseCore = LangfuseCore; | ||
@@ -968,7 +1031,7 @@ exports.LangfuseEventClient = LangfuseEventClient; | ||
exports.LangfuseObjectClient = LangfuseObjectClient; | ||
exports.LangfusePromptClient = LangfusePromptClient; | ||
exports.LangfuseSpanClient = LangfuseSpanClient; | ||
exports.LangfuseTraceClient = LangfuseTraceClient; | ||
exports.LangfuseWebStateless = LangfuseWebStateless; | ||
exports.TextPromptClient = TextPromptClient; | ||
exports.utils = utils; | ||
//# sourceMappingURL=index.cjs.js.map |
{ | ||
"name": "langfuse-core", | ||
"version": "3.3.6", | ||
"version": "3.4.0", | ||
"engines": { | ||
@@ -40,3 +40,3 @@ "node": ">=18" | ||
}, | ||
"gitHead": "bf5cbeed67b4f0fe99ca1fa1da3f05d8fb5737be" | ||
"gitHead": "fe611378af0ac2e8b6389c05588b16acf9b224e4" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
364509
3958