DEV Community

Cover image for Dynamic AI Prompts with PromptTemplates, Ruby, Langchain.rb and OpenAI
Ademar Tutor
Ademar Tutor

Posted on • Originally published at blog.ademartutor.com

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.

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

This tutorial talks about how to build a Ruby Chatbot with Langchain, PromptTemplate and OpenAI.Read full blog here:https://blog.ademartutor.com/p/dynamic-ai...

favicon youtube.com

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"

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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" }
)
Enter fullscreen mode Exit fullscreen mode

Step 4

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

chat = Langchain::Conversation.new(llm: openai)
Enter fullscreen mode Exit fullscreen mode

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.

Step 6

Prompt the user for the profession.

puts "Please enter the profession:"
user_input = gets.chomp
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)