Feast Prompt Templates¶
This example shows how to bind a LangChain prompt template to an existing Feast feature store and have the prompt rendered with information from the (online) store.
Note: to run this code, you need to go first complete the "Feast/Cassandra setup" instructions given earlier.
Initialization of the feature store¶
In [1]:
Copied!
from feast import FeatureStore
feast_repo_path = "feast_store/user_features/feature_repo/"
store = FeatureStore(repo_path=feast_repo_path)
from feast import FeatureStore
feast_repo_path = "feast_store/user_features/feature_repo/"
store = FeatureStore(repo_path=feast_repo_path)
Creation of the prompt template¶
In [2]:
Copied!
from langchain.prompts import createFeastPromptTemplate
from langchain.prompts import createFeastPromptTemplate
In [3]:
Copied!
ecommercePrompt = """You are a helpful assistant for
customers of an e-commerce website.
You will give them a suggestion on what to do,
based on their profile and pattern of usage so far.
The customer information is summarized by the following values:
> Customer age: {age}
> Number of purchases so far: {purchases}
> Site visits (per month): {visit_frequency}
> Active cart now? (1 = yes): {active_cart}
USER QUESTION: {user_question}
YOUR SUGGESTION: """
ecommercePrompt = """You are a helpful assistant for
customers of an e-commerce website.
You will give them a suggestion on what to do,
based on their profile and pattern of usage so far.
The customer information is summarized by the following values:
> Customer age: {age}
> Number of purchases so far: {purchases}
> Site visits (per month): {visit_frequency}
> Active cart now? (1 = yes): {active_cart}
USER QUESTION: {user_question}
YOUR SUGGESTION: """
In [4]:
Copied!
ecommerceTemplate = createFeastPromptTemplate(
store=store,
template=ecommercePrompt,
input_variables=["user_name", "user_question"],
field_mapper={
'age': ('user_data', 'age'),
'purchases': ('user_data', 'purchases'),
'visit_frequency': ('user_data', 'visit_frequency'),
'active_cart': ('active_cart', 'active_cart'),
},
)
ecommerceTemplate = createFeastPromptTemplate(
store=store,
template=ecommercePrompt,
input_variables=["user_name", "user_question"],
field_mapper={
'age': ('user_data', 'age'),
'purchases': ('user_data', 'purchases'),
'visit_frequency': ('user_data', 'visit_frequency'),
'active_cart': ('active_cart', 'active_cart'),
},
)
Formatting the template¶
Full rendering¶
In [5]:
Copied!
print(ecommerceTemplate.format(
user_name='marilyn', user_question='How do I remove an item from the cart?'
))
print(ecommerceTemplate.format(
user_name='marilyn', user_question='How do I remove an item from the cart?'
))
You are a helpful assistant for customers of an e-commerce website. You will give them a suggestion on what to do, based on their profile and pattern of usage so far. The customer information is summarized by the following values: > Customer age: 55 > Number of purchases so far: 12 > Site visits (per month): 0.17000000178813934 > Active cart now? (1 = yes): 0 USER QUESTION: How do I remove an item from the cart? YOUR SUGGESTION:
Partialing the template¶
In [6]:
Copied!
eCommPartial = ecommerceTemplate.partial(user_name='rodolfo')
print(eCommPartial.format(user_question='When is the next special offer?'))
eCommPartial = ecommerceTemplate.partial(user_name='rodolfo')
print(eCommPartial.format(user_question='When is the next special offer?'))
You are a helpful assistant for customers of an e-commerce website. You will give them a suggestion on what to do, based on their profile and pattern of usage so far. The customer information is summarized by the following values: > Customer age: 21 > Number of purchases so far: 323 > Site visits (per month): 8.59000015258789 > Active cart now? (1 = yes): 1 USER QUESTION: When is the next special offer? YOUR SUGGESTION: