Notebooks
L
Langfuse
Prompt Management Openai Functions

Prompt Management Openai Functions

observabilityllmsgenaicookbookprompt-managementhacktoberfestlarge-language-modelsnextraLangfuselangfuse-docs

description: Cookbook on how to use Langfuse Prompt Management to version control prompts collaboratively when using OpenAI functions. category: Prompt Management

Example: Langfuse Prompt Management for OpenAI functions (Python)

Langfuse Prompt Management helps to version control and manage prompts collaboratively in one place. This example demostrates how to use the flexible config object on Langfuse prompts to store function calling options and model parameters.

Setup

[ ]
[13]
[14]
True

Add prompt to Langfuse Prompt Management

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 json_schema variable
  • Config including model_name, temperature, and json_schema
  • labels to include production to immediately use the prompt as the default
[15]

Prompt in Langfuse UI

Langfuse Prompt Management

Example application

Get current prompt version from Langfuse

[16]

We can now use the prompt to compile our system message

[17]
'Extract the key information from this text and return it in JSON format. Use the following schema: TEST SCHEMA'

And it includes the config object

prompt.config
{'model': 'gpt-3.5-turbo-1106',
 'json_schema': {'genre': 'string (genre of story)',
  'keywords': 'array of strings',
  'key_content': 'string (1 sentence)',
  'critic_score': 'number (between 0 bad and 10 exceptional)',
  'main_character': 'string (name of protagonist)',
  'critic_review_comment': 'string (write similar to a new york times critic)'},
 'temperature': 0}

Create example function

In this example we use the native Langfuse OpenAI integration by importing from langfuse.openai. This enables tracing in Langfuse and is not required for using Langfuse prompts management.

[19]

Use Langfuse prompt to construct the summarize_story example function.

Note: You can link the generation in Langfuse Tracing to the prompt version by passing the langfuse_prompt parameter to the create method. Have a look at our prompt management docs to learn how to link prompt and generation with other integrations and SDKs.

[20]

Execute it

[21]
[ ]
{'genre': 'Fantasy',
 'keywords': ['lonely cat',
  'invisible',
  'benevolent spirit',
  'unsung hero',
  'mysterious deed',
  'true happiness'],
 'key_content': 'In a bustling city, a lonely cat named Whisper discovers an abandoned hat with the power to make her invisible, leading her to become a benevolent spirit and unsung hero to the less fortunate.',
 'critic_score': 9,
 'main_character': 'Whisper',
 'critic_review_comment': "Whisper's journey from loneliness to self-discovery through acts of kindness is a heartwarming and enchanting tale that captivates the reader with its magical elements and profound message about true happiness."}

View trace in Langfuse

As we used the native Langfuse integration with the OpenAI SDK, we can view the trace in Langfuse.

Trace of OpenAI functions in Langfuse

Iterate on prompt in Langfuse

We can now iterate on the prompt in Langfuse UI including model parameters and function calling options without changing the code or redeploying the application.