Introduction to LLMs: An Overview of Prompts

Data Mastery Series — Episode 23: The Chat with Document and Langchain Series (Part 4)

Donato_TH
Donato Story
7 min readMay 11, 2024

--

Connect with me and follow our journey: Linkedin, Facebook

Welcome back to our Data Mastery Series, where we continue to explore innovative applications of Large Language Models (LLMs) within the Chat with Document and Langchain framework. In this fourth installment, we delve into the art of crafting precise prompts that enhance the interaction between humans and AI, demonstrating how to tailor queries for specific responses. This approach not only refines the quality of AI-generated content but also streamlines user experience by focusing on direct and meaningful outputs.

If you’re catching up, make sure to explore our previous episodes for foundational insights:

1. Understanding the Basic Prompt

Starting with the basics, we learn to direct our LLM using a straightforward prompt. Here’s a simple example of asking for Thai food recommendations:

### Input code ###
prompt = """
Recommend top 3 Thai food that tourists should try.
"""

print(llm(prompt))
### Output ###
1. Pad Thai - This popular stir-fried noodle dish is a staple in Thai cuisine and is a must-try for any tourist. Made with rice noodles, shrimp or chicken, tofu, eggs, and various vegetables, Pad Thai is bursting with flavor from the sweet and tangy tamarind-based sauce.
2. Tom Yum Soup - This hot and sour soup is a popular Thai dish that is both refreshing and satisfying. Made with fragrant herbs, spices, and a variety of seafood or chicken, Tom Yum Soup is the perfect balance of spicy, sour, and savory flavors.
3. Green Curry - Thai curries are famous for their rich and complex flavors, and the green curry is no exception. Made with a paste of green chilies, herbs, and spices, this curry is simmered with meat or vegetables and coconut milk to create a creamy and aromatic dish. It is usually served with steamed rice and is a must-try for any tourist looking to experience authentic Thai cuisine.

This is the most direct way to get specific recommendations from our LLM, demonstrating the model’s capacity to handle straightforward queries effectively.

2. Template-Based Prompting

To add a layer of versatility, we use templates to craft prompts dynamically:

### Input code ###
prompt_template = "Recommend top 3 {item} that tourists should try."
item = "Thai food"
final_prompt = prompt_template.format(item=item)

print (f"Final Prompt: {final_prompt}")
print ("-----------")
print (f"LLM Output: {llm(final_prompt)}")
### Output ###
Final Prompt: Recommend top 3 Thai food that tourists should try.
-----------
LLM Output:
1. Pad Thai - This dish is a popular stir-fried noodle dish made with rice noodles, eggs, tofu, shrimp, and a tangy sauce made from tamarind, fish sauce, and sugar. It is usually topped with crushed peanuts, bean sprouts, and lime wedges.
2. Tom Yum Goong - This is a spicy and sour soup that is a staple in Thai cuisine. It is made with shrimp, lemongrass, kaffir lime leaves, galangal, and chili peppers. The combination of flavors creates a delicious and unique taste that is a must-try for tourists.
3. Green Curry - This is a classic Thai curry dish made with coconut milk, green curry paste, chicken or beef, and a variety of vegetables such as bamboo shoots, eggplant, and bell peppers. The creamy and spicy flavors of this dish make it a favorite among tourists.

This approach allows for reusability and flexibility, enabling the user to change the query item without rewriting the entire prompt.

3. Tailored Prompts for Specific Outputs

Adding background information or specific instructions can refine the AI’s responses:

### Input code ###
prompt_background = "Answer in concise version, bullet style, only 3 bullets, and in Thai language. "
prompt_template = "Recommend top 3 {item} that tourists should try."
item = "Thai food"
final_prompt = prompt_background + prompt_template.format(item=item)

print (f"Final Prompt: {final_prompt}")
print ("-----------")
print (f"LLM Output: {llm(final_prompt)}")
### Output ###
Final Prompt: Answer in concise version, bullet style, only 3 bullets, and in Thai language. Recommend top 3 Thai food that tourists should try.
-----------
LLM Output:
- ข้าวผัด - อาหารไทยแท้ที่มีรสชาติเข้มข้นและมีความหลากหลายในวัตถุดิบ
- ต้มยำกุ้ง - อาหารไทยที่มีความเผ็ดร้อนและหอมหวานอร่อย
- ส้มตำ - อาหารไทยที่มีรสชาติเปรี้ยวหวานและมีส่วนผสมสดใหม่

This method ensures that the responses are not only relevant but also formatted and contextualized according to the user’s needs.

4. Leveraging LangChain’s PromptTemplate

For those utilizing the LangChain library, PromptTemplate offers a structured way to manage prompts:

### Input code ###
from langchain.prompts import PromptTemplate

prompt = PromptTemplate(
input_variables=["item"],
template="Recommend top 3 {item} that tourists should try.",
)

final_prompt = prompt.format(item="Thai food")

print (f"Final Prompt: {final_prompt}")
print ("-----------")
print (f"LLM Output: {llm(final_prompt)}")
### Output ###
Final Prompt: Recommend top 3 Thai food that tourists should try.
-----------
LLM Output:
1. Pad Thai: This is a popular stir-fried rice noodle dish that is a staple in Thai cuisine. It is made with rice noodles, shrimp or chicken, bean sprouts, tofu, and various seasonings and spices. It is typically served with lime wedges and crushed peanuts on the side.
2. Tom Yum Goong: This is a hot and sour soup that is a must-try for any tourist in Thailand. It is made with lemongrass, kaffir lime leaves, galangal, chili peppers, shrimp, and mushrooms. The combination of spicy, sour, and fragrant flavors makes it a favorite among locals and tourists alike.
3. Green Curry: This is a delicious and aromatic curry dish that is made with coconut milk, green curry paste, meat (usually chicken or beef), and a variety of vegetables such as bamboo shoots, eggplant, and bell peppers. It is usually served with steamed rice and is known for its creamy and spicy flavor.

This segment of the library helps in maintaining clean and modular code.

5. Building Sequential Chains with LangChain

When dealing with complex user interactions, sequential chains become invaluable. These chains allow us to handle multi-step queries where each response feeds into the next. Here’s how we set it up for a culinary exploration:

### Input code ###
template = """Your job is to come up with a classic dish from the area that the users suggests.
% USER LOCATION
{user_location}

YOUR RESPONSE:
"""
prompt_template = PromptTemplate(input_variables=["user_location"], template=template)

location_chain = LLMChain(llm=llm, prompt=prompt_template)

template = """Given a meal, give a short and simple recipe on how to make that dish at home, bullet point topic (Recipe, Ingredients and Instructions) and sub topic style.
% MEAL
{user_meal}

YOUR RESPONSE:
"""
prompt_template = PromptTemplate(input_variables=["user_meal"], template=template)

meal_chain = LLMChain(llm=llm, prompt=prompt_template)

overall_chain = SimpleSequentialChain(chains=[location_chain, meal_chain], verbose=True)

review = overall_chain.run("Thai")
### Output ###
One classic dish from Thailand is Pad Thai. This dish is a stir-fried rice noodle dish with a combination of sweet, sour, and savory flavors. It typically includes ingredients such as rice noodles, shrimp, tofu, eggs, bean sprouts, green onions, and a sauce made from tamarind paste, fish sauce, garlic, and palm sugar. It is often served with crushed peanuts and lime wedges on the side. Pad Thai is a staple dish in Thai cuisine and can be found in many restaurants and street food stalls across the country.

Recipe:
- Pad Thai
Ingredients:
- 8 oz rice noodles
- 5 cloves of garlic
- 4 green onions
- 1/4 cup of tamarind paste
- 1/4 cup of fish sauce
- 2 tbsp palm sugar
- 1/4 cup of vegetable oil
- 1 medium sized shrimp
- 1/2 cup of tofu
- 2 eggs
- 1 cup of bean sprouts
- 1/4 cup of crushed peanuts
- 1 lime
Instructions:
1. Soak rice noodles in warm water for 20 minutes, then drain and set aside.
2. Mix tamarind paste, fish sauce, and palm sugar in a small bowl to make the sauce.
3. Heat vegetable oil in a wok or large pan over high heat.
4. Add minced garlic and stir until fragrant.
5. Add chopped green onions and stir for another minute.
6. Add shrimp and tofu to the pan and cook until shrimp turns pink.
7. Push the ingredients to one side of the pan, crack eggs into the other side and scramble.
8. Add drained noodles and pour the sauce over the noodles, stirring constantly.
9. Add bean sprouts

This sequence illustrates how you can engage an LLM to first identify a regional dish based on a location, then provide a simple recipe for it. This is an excellent way of showing how LLMs can handle interconnected tasks that require context from previous interactions.

As we explore the art of prompt crafting with LangChain, we recognize the depth and diversity of techniques available for tailoring AI interactions. This episode has introduced five methods, yet many more await discovery and mastery. Experiment with different strategies to optimize your use of LLMs. Next, we will delve into basic use cases of LLMs, providing practical insights into their application in real-world scenarios. Join us to enhance your understanding and efficacy with these powerful tools.

Data Science

33 stories

Dashboard

3 stories

Donato_Journey

5 stories

Course_Review

3 stories

Your engagement and insights are invaluable. Feel free to share your thoughts, questions, or insights below, or connect with me on

Medium: medium.com/donato-story
Facebook:
web.facebook.com/DonatoStory
Linkedin:
linkedin.com/in/nattapong-thanngam

--

--

Donato_TH
Donato Story

Data Science Team Lead at Data Cafe, Project Manager (PMP #3563199), Black Belt-Lean Six Sigma certificate