Partialing Prompt Templates¶
Cassandra-powered prompt templates support partialing. Building on the previous examples:
In [1]:
Copied!
from langchain.prompts import createCassandraPromptTemplate
from langchain.prompts import createCassandraPromptTemplate
In [2]:
Copied!
from cqlsession import getCQLSession, getCQLKeyspace
astraSession = getCQLSession()
astraKeyspace = getCQLKeyspace()
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:
"""
cassPrompt = createCassandraPromptTemplate(
session=astraSession,
keyspace=astraKeyspace,
template=ctemplate0,
input_variables=['city', 'name', 'user_question'],
field_mapper={
'user_age': ('people', 'age'),
'city_nickname': ('nickname_by_city', 'nickname'),
},
)
from cqlsession import getCQLSession, getCQLKeyspace
astraSession = getCQLSession()
astraKeyspace = getCQLKeyspace()
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:
"""
cassPrompt = createCassandraPromptTemplate(
session=astraSession,
keyspace=astraKeyspace,
template=ctemplate0,
input_variables=['city', 'name', 'user_question'],
field_mapper={
'user_age': ('people', 'age'),
'city_nickname': ('nickname_by_city', 'nickname'),
},
)
/home/stefano/.virtualenvs/langchain-cassio-3.10/lib/python3.10/site-packages/cassandra/io/asyncorereactor.py:34: DeprecationWarning: The asyncore module is deprecated and will be removed in Python 3.12. The recommended replacement is asyncio import asyncore /home/stefano/.virtualenvs/langchain-cassio-3.10/lib/python3.10/site-packages/cassandra/datastax/cloud/__init__.py:173: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated ssl_context = SSLContext(PROTOCOL_TLS) /home/stefano/.virtualenvs/langchain-cassio-3.10/lib/python3.10/site-packages/cassandra/io/asyncorereactor.py:347: DeprecationWarning: ssl.match_hostname() is deprecated self._connect_socket()
Creating a Partial Prompt¶
Let us "partial" the prompt above, specifying the DB-lookup inputs and leaving only the user question unspecified:
In [3]:
Copied!
cassPartialPrompt = cassPrompt.partial(city='lisbon', name='Pedro')
cassPartialPrompt = cassPrompt.partial(city='lisbon', name='Pedro')
When rendering the template, the full lookup takes place at once:
In [4]:
Copied!
print(cassPartialPrompt.format(user_question='Em verdade, o que quiseres?'))
print(cassPartialPrompt.format(user_question='Em verdade, o que quiseres?'))
Please answer a question from a user. Keep in mind that the user's age is 1 and they live in a city with nickname ACidade. USER'S QUESTION: Em verdade, o que quiseres? YOUR ANSWER:
Partialing and Chat Prompt Templates¶
Unfortunately, this is not supported at the LangChain level. Let's create a two-item chat sequence,
In [5]:
Copied!
systemTemplate = """
You are a chat assistant, helping a user of age {user_age} from a city
they refer to as {city_nickname}.
"""
cassSystemPrompt = createCassandraPromptTemplate(
session=astraSession,
keyspace=astraKeyspace,
template=systemTemplate,
input_variables=['city', 'name'],
field_mapper={
'user_age': ('people', 'age'),
'city_nickname': ('nickname_by_city', 'nickname'),
},
)
from langchain.prompts import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
systemMessagePrompt = SystemMessagePromptTemplate(prompt=cassSystemPrompt)
humanTemplate = "{text}"
humanMessagePrompt = HumanMessagePromptTemplate.from_template(humanTemplate)
cassChatPrompt = ChatPromptTemplate.from_messages(
[systemMessagePrompt, humanMessagePrompt]
)
systemTemplate = """
You are a chat assistant, helping a user of age {user_age} from a city
they refer to as {city_nickname}.
"""
cassSystemPrompt = createCassandraPromptTemplate(
session=astraSession,
keyspace=astraKeyspace,
template=systemTemplate,
input_variables=['city', 'name'],
field_mapper={
'user_age': ('people', 'age'),
'city_nickname': ('nickname_by_city', 'nickname'),
},
)
from langchain.prompts import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
systemMessagePrompt = SystemMessagePromptTemplate(prompt=cassSystemPrompt)
humanTemplate = "{text}"
humanMessagePrompt = HumanMessagePromptTemplate.from_template(humanTemplate)
cassChatPrompt = ChatPromptTemplate.from_messages(
[systemMessagePrompt, humanMessagePrompt]
)
and try to "partial" it by specifying the DB-lookup parameters:
In [6]:
Copied!
try:
cassChatPartialPrompt = cassChatPrompt.partial(
city='turin',
name='beppe'
)
except NotImplementedError:
print('"NotImplementedError" raised by partialing ChatPromptTemplate')
try:
cassChatPartialPrompt = cassChatPrompt.partial(
city='turin',
name='beppe'
)
except NotImplementedError:
print('"NotImplementedError" raised by partialing ChatPromptTemplate')
"NotImplementedError" raised by partialing ChatPromptTemplate