Building Apps for Startups with Ademar Tutor

Building Apps for Startups with Ademar Tutor

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Dynamic AI Prompts with PromptTemplates, Ruby, Langchain.rb and OpenAI
Copy link
Facebook
Email
Notes
More

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.

Ademar Tutor's avatar
Ademar Tutor
Jan 14, 2024
4

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Dynamic AI Prompts with PromptTemplates, Ruby, Langchain.rb and OpenAI
Copy link
Facebook
Email
Notes
More
Share

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.

Thanks for reading Building AI Apps for Startups with Ademar Tutor! Subscribe for free to receive new posts and support my work.

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.

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

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.

Step 4

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

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.

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.

Full Code


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
hey@ademartutor.com

Let's work together!

Nasser's avatar
Learn with Omar's avatar
Ademar Tutor's avatar
4 Likes
4

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Dynamic AI Prompts with PromptTemplates, Ruby, Langchain.rb and OpenAI
Copy link
Facebook
Email
Notes
More
Share

Discussion about this post

User's avatar
Understanding Big-O Notation with Ruby
If the whole Big-O notation thing baffled you in college, guess what? You're in great company! 😅 Many of us had our share of head-scratching moments.
Jan 8, 2024 • 
Ademar Tutor
1

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Understanding Big-O Notation with Ruby
Copy link
Facebook
Email
Notes
More
Observability with OpenTelemetry, Jaeger and Rails
Discussion and setup of OpenTelemetry and Jaeger with Ruby on Rails
Feb 19, 2024 • 
Ademar Tutor
2

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Observability with OpenTelemetry, Jaeger and Rails
Copy link
Facebook
Email
Notes
More
Hello AI world with Ruby!
Building a small terminal-based chatbot app with OpenAI.
Jan 12, 2024 • 
Ademar Tutor
2

Share this post

Building Apps for Startups with Ademar Tutor
Building Apps for Startups with Ademar Tutor
Hello AI world with Ruby!
Copy link
Facebook
Email
Notes
More
2

Ready for more?

© 2025 Ademar Tutor
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More

Create your profile

User's avatar

Only paid subscribers can comment on this post

Already a paid subscriber? Sign in

Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.