Dynamic AI Prompts with PromptTemplates, Ruby, Langchain.rb and OpenAI

Dynamic AI Prompts with PromptTemplates, Ruby, Langchain.rb and OpenAI

This tutorial talks about how to build a Ruby Chatbot with Langchain, PromptTemplate and OpenAI.

Previously, we've built a basic terminal chatbot with Ruby. Now, we are going to focus on PrompTemplates.

What are PromptTemplates?

PromptTemplates in Langchain.rb are game-changers. They allow you to create flexible, dynamic prompts with placeholders, which can be filled in with user input or other data at runtime.

This adaptability is critical in prompt engineering, where the goal is to generate prompts that elicit the most accurate and relevant responses from an AI model.

What is Prompt Engineering?

Prompt engineering is the art of crafting prompts that guide AI models to provide the desired output. The way you phrase a prompt can significantly impact the model's response.

PromptTemplates offer a way to experiment with different phrasings and structures without hard-coding every possible prompt.

Building an example chatbot

Imagine building a chatbot that explains to preschools what specific profession and skills they need to succeed in that profession.

Step 1
Require the necessary libraries: OpenAI and Langchain.

require "openai"
require "langchain"

Step 2
Set the logger level of Langchain to info, which means it will log informational messages. It is not a required step, but it's always good to know what's happening in the background with Langchain.rb

Langchain.logger.level =  :info

Step 3

Initialize a new Langchain::LLM::OpenAI object. This object is used to interact with OpenAI's API.
You need to provide your API key and other options.
llm_options is an empty hash here, but it can be used to pass additional options.
default_options sets the default language model to GPT-4 for chat completions.

openai = Langchain::LLM::OpenAI.new(
  api_key: ENV["OPENAI_API_KEY"],
  llm_options: {},
  default_options: { chat_completion_model_name: "gpt-4" }
)

Step 4
Create a new conversation object. This object will manage the conversation flow using the OpenAI model.

chat = Langchain::Conversation.new(llm: openai)

Step 5
Create a new PromptTemplate object. This template has a placeholder {profession} that can be filled in. input_variables defines the placeholders expected in the template.

prompt = Langchain::Prompt::PromptTemplate.new(
  template: "Explain to a pre-schooler what an {profession} is and what they need to in order to be successful in the field.",
  input_variables: [ "profession" ]
)

Step 6
Prompt the user for the profession.

puts "Please enter the profession:"
user_input = gets.chomp

Step 7
Format the prompt template with the user's input and send the message to the OpenAI model. The response from the model is then printed out.

puts chat.message(prompt.format(profession: user_input))
Full Code
require "openai"
require "langchain"

Langchain.logger.level = :info

openai = Langchain::LLM::OpenAI.new(
  api_key: ENV["OPENAI_API_KEY"],
  llm_options: {},
  default_options: { chat_completion_model_name: "gpt-4" }
)

chat = Langchain::Conversation.new(llm: openai)

prompt = Langchain::Prompt::PromptTemplate.new(
  template: "Explain to a pre-schooler what an {profession} is and what they need to in order to be successful in the field.",
  input_variables: [ "profession" ]
)

puts "Please enter the profession:"
user_input = gets.chomp

puts chat.message(prompt.format(profession: user_input))

Originally posted on:
blog.ademartutor.com/p/dynamic-ai-prompts-w..


Hello there!

Do you have a startup idea or an exciting project you’re passionate about? I’d love to bring your vision to life!

I’m a software developer with 13 years of experience in building apps for startups, I specialize in Rails + Hotwire/React.

Whether you’re looking to innovate, grow your business, or bring a creative idea to the forefront, I’m here to provide tailored solutions that meet your unique needs.

Let’s collaborate to make something amazing!

Sincerely,
Ademar Tutor