Dynamic AI Prompts with PromptTemplates, Ruby, Langchain.rb and OpenAI
Tags: ruby, langchain, openai
Published: 2024-01-15 10:21:24

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

{% embed https://youtu.be/xQRpFtDctkE?si=yfXkq4C5xdRNWOEy %}

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.
```ruby
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
```ruby
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.
```ruby
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.
```ruby
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.

###Step 6
Prompt the user for the profession.
```ruby
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.
```ruby
puts chat.message(prompt.format(profession: user_input))
```

###Full Code
```ruby
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))
```