Dynamic prompt generation #2808
-
|
Hi, in our project we generate user prompts like this, in which I would call a "dynamic way", cause the length of the prompt depends on the length of the data passed in the arguments Is does not seem to me that with Langfuse is possible to manage prompt that are more complex right now, or is it possible? is it possible to define a prompt function? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hey @LLazzar! I'm here to help you with any questions or issues you may have. Feel free to ask me anything! Yes, Langfuse can handle dynamic prompt generation and allows you to define prompt functions. You can use the Langfuse SDK to create and manage prompts dynamically. Here is an example of how you can achieve this:
langfuse.create_prompt(
name="dynamic-prompt",
prompt="You are an extremely helpful assistant. Please assume that the person asking you questions needs your support and is totally honest with you.",
isActive=True # immediately promote to production
)
import json
from typing import List
def generate_user_prompt(option_queries_list: List[OptionQueries], documents_with_pages: List[DocumentWithPages]) -> str:
clean_page_content(documents_with_pages)
clean_option_queries(option_queries_list)
prompt_dict = {
"question": option_queries_list[0].language_query_dict["en"].questionText.strip(),
"possible_answers": {
f"Answer {index}": {
"answer_text": option_queries.language_query_dict["en"].answerText,
"answer_explanation": option_queries.language_query_dict["en"].answerExplanation
}
for index, option_queries in enumerate(option_queries_list, start=1)
},
"documents": {
f"Document {index}": {
f"Page {page.page_number}": page.page_content
for page in document_with_page.pages
}
for index, document_with_page in enumerate(documents_with_pages, start=1)
}
}
return "```json\n" + json.dumps(prompt_dict, indent=4) + "\n```"
# Fetch the prompt template from Langfuse
prompt = langfuse.get_prompt("dynamic-prompt")
prompt_template = prompt.prompt
# Generate the dynamic prompt
dynamic_prompt = generate_user_prompt(option_queries_list, documents_with_pages)
assembled_messages = [
{
"role": "system",
"content": prompt_template,
},
{
"role": "user",
"content": dynamic_prompt,
},
]
response = openai.create_chat_completion(
model="gpt-3.5-turbo",
messages=assembled_messages,
)Langfuse also provides utility methods to transform prompts into formats compatible with other frameworks like Langchain. For example, you can use the |
Beta Was this translation helpful? Give feedback.
-
|
Hi @LLazzar,
I have seen some people manage Jinja templates in langfuse prompt management, which can then be interpreted at runtime. Is this an option for you? |
Beta Was this translation helpful? Give feedback.
Hi @LLazzar,
I have seen some people manage Jinja templates in langfuse prompt management, which can then be interpreted at runtime. Is this an option for you?