Prompt Templates, intro¶
CassIO powers a sophisticated set of bindings to seamlessly inject data from Cassandra tables into your LangChain prompt templates.
Basic usage¶
from langchain.prompts import createCassandraPromptTemplate
This cell simply obtains a Session
object, i.e. an active connection to your database. Replace with custom code if you are not using Astra DB.
from cqlsession import getCQLSession, getCQLKeyspace
cqlMode = 'astra_db' # 'astra_db'/'local'
session = getCQLSession(mode=cqlMode)
keyspace = getCQLKeyspace(mode=cqlMode)
ctemplate0 = """Please answer a question from a user.
Keep in mind that the user's age is {user_age} and they live in a city with
nickname {city_nickname}.
USER'S QUESTION: {user_question}
YOUR ANSWER:
"""
Natural binding with the DB¶
In the (string) template above, some variables are to be filled with a DB lookup.
The following instructions specifies the details of the binding: for instance,
the variable user_age
is to be found on table people
, specifically in column age
:
cassPrompt = createCassandraPromptTemplate(
session=session,
keyspace=keyspace,
template=ctemplate0,
input_variables=['city', 'name', 'user_question'],
field_mapper={
'user_age': ('people', 'age'),
'city_nickname': ('nickname_by_city', 'nickname'),
},
)
Note that in the command above we specify the primary key columns as input_variables
, and not the variable names found in the prompt string above.
When formatting the Prompt Template, we will have to specify the primary key values for the DB lookup -- the rest is done by the prompt template.
In this case there are two lookups from as many tables: the prompt template takes care of everything, provided you pass all the primary key columns required across tables.
print(cassPrompt.format(city='turin', name='beppe',
user_question='Is functional programming fun?'))
Please answer a question from a user. Keep in mind that the user's age is 2 and they live in a city with nickname CereaNeh. USER'S QUESTION: Is functional programming fun? YOUR ANSWER: