Notebooks
L
Langfuse
Js Prompt Management Langchain

Js Prompt Management Langchain

observabilityllmsgenaicookbookprompt-managementhacktoberfestlarge-language-modelsnextraLangfuselangfuse-docs

Example: Langfuse Prompt Management with Langchain (JS)

Python JS/TS

Langfuse Prompt Management helps to version control and manage prompts collaboratively in one place.

This example demonstrates how to use Langfuse Prompt Management together with Langchain JS.

Set Up Environment

Get your Langfuse API keys by signing up for Langfuse Cloud or self-hosting Langfuse. You’ll also need your OpenAI API key.

Note: This cookbook uses Deno.js for execution, which requires different syntax for importing packages and setting environment variables. For Node.js applications, the setup process is similar but uses standard npm packages and process.env.

[ ]

With the environment variables set, we can now initialize the langfuseSpanProcessor which is passed to the main OpenTelemetry SDK that orchestrates tracing.

[ ]

The LangfuseClient provides additional functionality beyond OpenTelemetry tracing, such as scoring, prompt management, and data retrieval. It automatically uses the same environment variables we set earlier.

[3]

Example 1: Text Prompt

Add new prompt

We add the prompt used in this example via the SDK. Alternatively, you can also edit and version the prompt in the Langfuse UI.

  • Name that identifies the prompt in Langfuse Prompt Management
  • Prompt with topic variable
  • Config including modelName, temperature
  • labels to include production to immediately use prompt as the default

For the sake of this notebook, we will add the prompt in Langfuse and use it right away. Usually, you'd update the prompt from time to time in Langfuse and your application fetches the current production version.

[ ]

Prompt in Langfuse

Prompt in Langfuse

Run example

Get current prompt version from Langfuse

[19]

The prompt includes the prompt string

[ ]

and the config object

[ ]

Transform prompt into Langchain PromptTemplate

Use the utility method .getLangchainPrompt() to transform the Langfuse prompt into a string that can be used in Langchain.

Context: Langfuse declares input variables in prompt templates using double brackets ({{input variable}}). Langchain uses single brackets for declaring input variables in PromptTemplates ({input variable}). The utility method .getLangchainPrompt() replaces the double brackets with single brackets.

Also, pass the Langfuse prompt as metadata to the PromptTemplate to automatically link generations that use the prompt.

[24]

Setup Langfuse Tracing for Langchain JS

We'll use the native Langfuse Tracing for Langchain JS when executing this chain. This is fully optional and can be used independently from Prompt Management.

[25]

Create chain

We use the modelName and temperature stored in prompt.config.

[32]

Invoke chain

[33]

View trace in Langfuse

As we passed the langfuse callback handler, we can explore the execution trace in Langfuse.

Trace in Langfuse

Public trace in the Langfuse UI

Example 1: OpenAI functions and JsonOutputFunctionsParser

Add prompt to Langfuse

[ ]

Prompt in Langfuse

Prompt in Langfuse

Fetch prompt

[35]

Transform into schema

[39]

Build chain

[41]

Invoke chain

[43]

View trace in Langfuse

Trace in Langfuse

Public trace in the Langfuse UI