Introduction to Prompting

In this notebook, we’ll explore the basics of prompting and rendering prompts. Follow along with the code and instructions to get a hands-on experience. Feel free to modify and experiment with the code as you go!

pip install langtree --quiet
Note: you may need to restart the kernel to use updated packages.

(The above is required for compiling the docs)

Import the required modules

For this example we will be doing an example of the built in prompting features.

langtree.core: This is where the core functionality lives!

langtree.prompting: This is where the prompting utils live.

from langtree.core import Prompt
from langtree.prompting import UserMessage, SystemMessage

Define the prompts

Here we are defining two prompts. These aren’t assigned to roles at first. To assign these to message roles, we can use the message types as shown below.

We pass strings to instantiate a prompt object.

system_prompt = Prompt("You are a helpful assistant")
user_prompt = Prompt("{{banana}}.{{dev}} is cool")

Now we can render them by just calling the prompt.

First, let’s render the system prompt.

sys_content = system_prompt()
sys_msg = SystemMessage(content=sys_content)

print(sys_msg)
{'role': 'system', 'content': 'You are a helpful assistant'}

Now lets render the user prompt. This is slightly different, since the user prompt has substitutions

A substitution is defined in the string we pass with {{keyword}}. keyword is automatically now a keyword arg we can pass to the prompt to render a string. Lets see how rendering with substitutions looks:

usr_content = user_prompt(banana="open", dev="ai")
usr_msg = UserMessage(content=usr_content)

print(usr_msg)
{'role': 'user', 'content': 'open.ai is cool'}

More Prompting

Now that we know how prompting works, we can do arithmetic on prompts!

This is great for few shot prompting since you can iteratively add and configure your prompts.

system_prompt = Prompt("You are a helpful assistant")
user_prompt = Prompt("{{banana}}.{{dev}} is cool. ")
prompt = user_prompt + system_prompt

Here we are defining the same prompts as before, but then we can add them together.

When we add prompts together, what happens is the underlying templates are added together, this then results in one big template. This means that the keyword args from both are now available on one prompt object.

content = prompt(banana="openai", dev="com")
combined_msg = UserMessage(content=content)

print(combined_msg)
{'role': 'user', 'content': 'openai.com is cool. You are a helpful assistant'}

WARNING: THERE CAN ONLY BE ONE OF EACH PROMPT KEYWORD, make sure you name your prompt vars accordingly.

Recap

In this notebook, we covered how to make prompts, messages, and how to add them together!