pw.xpacks.llm.prompts
class BasePromptTemplate(**data)
[source]Base class for the prompt templates.
Subclasses should implement as_udf
.
class FunctionPromptTemplate(**data)
[source]Utility class to create prompt templates from callables or UDF.
as_udf
may take kwargs to partially pre-fill the prompt template.
as_udf(**kwargs)
sourceReturn the function_template
callable as UDF
.
Given keyword arguments will be passed to the callable during runtime.
class StringPromptTemplate(**data)
[source]Utility class to create prompt templates that can be applied to tables.
import pandas as pd
import pathway as pw
prompt_template = "Answer the following question. Context: {context}. Question: {query}"
t = pw.debug.table_from_pandas(pd.DataFrame([{"context": "Here are some facts...",
"query": "How much do penguins weigh in average?"}]))
template = StringPromptTemplate(template=prompt_template)
template_udf = template.as_udf()
t = t.select(prompt=template_udf(context=pw.this.context, query=pw.this.query))
as_udf(**kwargs)
sourceReturn the template
string as a UDF
that calls the
format
function.
Given keyword arguments will be passed to the callable during runtime.
format(**kwargs)
sourceFormat the template string with keyword arguments.
class RAGPromptTemplate(**data)
[source]A class to validate and handle prompt templates for RAG.
Checks the given template during construction and raises a ValidationError if the template doesn’t have the expected placeholders.
as_udf(**kwargs)
sourceReturn the template
string as a UDF
that calls the
format
function.
Given keyword arguments will be passed to the callable during runtime.
format(**kwargs)
sourceFormat the template string with keyword arguments.
classmethod is_valid_rag_template(template)
sourceValidate the prompt template by checking for required placeholders.
The template must contain the {context}
and {query}
placeholders.
class RAGFunctionPromptTemplate(**data)
[source]A class to validate and handle callable prompt templates for RAG.
Checks the callable template during construction and raises a ValidationError if the template doesn’t have the expected placeholders.
as_udf(**kwargs)
sourceReturn the function_template
callable as UDF
.
Given keyword arguments will be passed to the callable during runtime.
classmethod is_valid_rag_template(template)
sourceValidate the function prompt template by running with empty placeholders.
prompt_short_qa(context, query, additional_rules='')
sourceGenerate a RAG prompt with given context.
Specifically for getting short and concise answers. Given a question, and list of context documents, generates prompt to be sent to the LLM. Suggests specific formatting for yes/no questions and dates.
- Parameters
- context (
str
) – Information sources or the documents to be passed to the LLM as context. - query (
str
) – Question or prompt to be answered. - additional_rules (
str
) – Optional parameter for rest of the string args that may include additional instructions or information.
- context (
- Returns
Prompt containing question and the relevant docs.
prompt_qa(context, query, information_not_found_response='No information found.', additional_rules='')
sourceGenerate RAG prompt with given context.
Given a question and list of context documents, generates prompt to be sent to the LLM.
- Parameters
- context (
str
) – Information sources or the documents to be passed to the LLM as context. - query (
str
) – Question or prompt to be answered. - information_not_found_response – Response LLM should generate in case answer cannot be inferred from the given documents.
- additional_rules (
str
) – Optional parameter for rest of the string args that may include additional instructions or information.
- context (
- Returns
Prompt containing question and relevant docs.
import pandas as pd
import pathway as pw
from pathway.xpacks.llm import prompts
t = pw.debug.table_from_pandas(pd.DataFrame([{"context": "Here are some facts...",
"query": "How much do penguins weigh in average?"}]))
r = t.select(prompt=prompts.prompt_qa(pw.this.context, pw.this.query))
prompt_summarize(text_list)
sourceGenerate a summarization prompt with the list of texts.
- Parameters
text_list (list
[str
]) – List of text documents. - Returns
Summarized text.
prompt_query_rewrite_hyde(query)
sourceGenerate prompt for query rewriting using the HyDE technique.
- Parameters
query (str
) – Original search query or user prompt. - Returns
Transformed query.
prompt_query_rewrite(query, *additional_args)
sourceGenerate prompt for query rewriting.
Prompt function to generate and augment index search queries using important names, entities and information from the given input. Generates three transformed queries concatenated with comma to improve the search performance.
- Parameters
- query (
str
) – Original search query or user prompt. - additional_args (
str
) – Additional information that may help LLM in generating the query.
- query (
- Returns
Transformed query.