Introduction to LLMs: A Component Overview
Data Mastery Series — Episode 22: The Chat with Document and Langchain Series (Part 3)
Introduction to LLMs: A Component Overview
Data Mastery Series — Episode 22: The Chat with Document and Langchain Series (Part 3)

Connect with me and follow our journey: Linkedin, Facebook
We continue to explore the intricate functionalities of Large Language Models (LLMs) in this episode. Our focus today includes the components and capabilities of LLMs developed by OpenAI. We’ll look into system messaging within ChatOpenAI, differences between LLM models like GPT-3.5 Turbo Instruct and GPT-4, techniques for visualizing embeddings, and the basics of crafting prompts and setting parameters.
If you’re catching up, make sure to explore our previous episodes for foundational insights:
- Part 1: Chat with Document: Basics and Demonstrations
- Part 2: Chat with Document: A Closer Look at Splitting, Embeddings, and RAG
Understanding ChatOpenAI
ChatOpenAI plays a pivotal role in AI-human interaction by structuring conversations into three key types of messages:
- SystemMessage: Guides the AI’s behavior with specific commands.
- HumanMessage: Inputs from users, such as questions or statements.
- AIMessage: The AI’s responses, tailored based on human inputs and system guidance.
Input code
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage, AIMessage
chat = ChatOpenAI(openai_api_key='your_openai_api_key')
System sets the context for AI's responses
system_prompt = "You are a helpful AI that suggests dinner in one short sentence."
human_query = "What shall we have for dinner today?"
response1 = chat([
SystemMessage(content=system_prompt),
HumanMessage(content=human_query)
])
print("AI:", response1.content)
Output
AI: How about trying a new recipe for homemade pizza tonight?
This example shows how the SystemMessage sets up the context, and the HumanMessage directly asks the user’s question. The AI’s response is both relevant and simple.
To illustrate memory in conversations:
Input code
Continuing the conversation, the user reacts to the AI's suggestion
follow_up_query = "Nah, I'm a bit over it."
AI remembers previous interaction and suggests an alternative
response2 = chat([
SystemMessage(content=system_prompt),
HumanMessage(content=human_query),
AIMessage(content=response1.content), # AI remembers its previous suggestion
HumanMessage(content=follow_up_query)
])
print("Me:", human_query)
print("AI:", response1.content)
print("Me:", follow_up_query)
print("AI:", response2.content)
Output
Me: What shall we have for dinner today?
AI: How about trying a new recipe for homemade pizza tonight?
Me: Nah, I'm a bit over it
AI: How about a comforting bowl of creamy pasta carbonara for dinner?
This interaction demonstrates the AI’s ability to remember past conversations and adapt its suggestions.
Understanding LLM Models
Different models can yield varied results and may require specific coding styles to be utilized effectively. To demonstrate these differences, I posed the question in Thai, “It’s so hot out. Any ideas for places to beat the heat?” to compare how GPT-3.5 Turbo Instruct and GPT-4 respond.
Code for GPT-3.5 Turbo Instruct:
Input code
question = "ร้อน ไปเที่ยวไหนดีครับ" # Translates to: "It's so hot out. Any ideas for places to beat the heat?"
Initialize the OpenAI language model with a specific model name and API key
llm = OpenAI(model_name="gpt-3.5-turbo-instruct", openai_api_key='your_openai_api_key')
Query the language model with a question
response = llm(question)
Print the response
print("Response from GPT-3.5 Turbo Instruct:")
print(response)
Output
Response from GPT-3.5 Turbo Instruct:
- ชายหาดหัวหิน - ด้วยความสวยงามของทะเลและการรับประทานอาหารทะเล และการพักผ่อนในรีสอร์ทต่าง ๆ ที่มีบรรยากาศเงียบสงบ ชายหาดหัวหินเป็นหนึ่งในที่เที่ยวที่น่าสนใจมากที่สุดในประเทศไทย
- เกาะช้าง - เกาะช้างมีทะเลสวยงามและแหล่งดำน้ำที่น่าสำรวจ นอกจากนี้ยังมีกิจกรรมที่สนุกสนานอย่างตกป
'''
Translates to:
Response from GPT-3.5 Turbo Instruct:
- ชายหาดหัวหิน - Hua Hin Beach, known for its beautiful sea, seafood dining, and tranquil resort atmosphere, is one of the most appealing destinations in Thailand.
- เกาะช้าง - Ko Chang features beautiful beaches and diving spots along with fun activities like fis
'''
Code for GPT-4:
Input code
question = "ร้อน ไปเที่ยวไหนดีครับ" #Equal to --> It's so hot out. Any ideas for places to beat the heat?
Call to GPT-4 using the Chat API
response_gpt4 = openai.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are my best friend"},
{"role": "user", "content": question}
]
)
Print the response from GPT-4
print("Response from GPT-4:")
print(response_gpt4.choices[0].message.content)
Output
Response from GPT-4:
ถ้าคุณชอบแอกทิวิตี้ธรรมชาติ ผมขอแนะนำให้ไปเที่ยวที่ 'อุทยานแห่งชาติขุนช่างเคี่ยน' ที่จังหวัดเชียงรายครับ ที่นี่ประกอบด้วยภูเขาสูง เขื่อนและน้ำตกสวยๆ หรือถ้าคุณชอบทะเล ก็ยังสามารถไปเที่ยวที่ 'เกาะสมุย' ซึ่งมีทั้งหาดทรายขาวสะอาด ไลฟ์สไตล์ที่เรียกน้ำมาครับ
'''
Translates to:
Response from GPT-4:
If you enjoy natural activities, I recommend visiting 'Khun Chae National Park' in Chiang Rai Province. It features high mountains, dams, and beautiful waterfalls. Or, if you prefer the beach, 'Koh Samui' offers clean white sandy beaches and a vibrant lifestyle.
'''
While both models suggested relevant destinations in Thailand for cooling off, their responses differed significantly in quality:
- GPT-3.5 Turbo Instruct: Provided useful responses that, however, ended abruptly. This indicates potential limitations in handling longer or more complex queries.
- GPT-4: Delivered a complete, fluid, and naturally phrased response in Thai, demonstrating advancements in language processing and context management
For more insights, check ou OpenAI’s LLM models and Google’s foundation models.
Understanding More About Embedding (Visualization)
Embeddings transform text into numerical vectors, which are fundamental in enabling various AI applications. In this demonstration, I will use OpenAI’s Embedding capabilities to analyze 10 words: [“Thai”, “Thailand”, “USA”, “America”, “Samsung”, “iPhone”, “Apple”, “Orange”, “Cat”, “Dog”]. By applying the dimensionality reduction technique t-SNE, we reduce these embeddings to two dimensions for visualization.

Figure: Visualization of Text Embeddings (Image by Author)
The visualization reveals:
- ‘USA’ and ‘America’ cluster closely together, similar to ‘Thai’ and ‘Thailand’, and ‘Cat’ and ‘Dog’.
- ‘Apple’ and ‘Orange’ are near each other, but ‘Apple’ is more closely associated with ‘iPhone’ and ‘Samsung’ than with ‘Orange’.
This concept of embedding is crucial for understanding similarity in Retrieval-Augmented Generation (RAG).
Understanding LLM Parameters
Tuning hyperparameters is also essential for effective LLM usage:
- Temperature: Controls the randomness of the response generation. Lower values lead to more predictable responses.
- Top_p: Sets the threshold for selecting the most likely next words, focusing the generation on a narrower set of choices.
For this demonstration, we asked the question, “What are the top three popular tourist foods in Thailand?” while varying the temperature settings at [0.10, 0.75, 1.4], conducting two trials at each setting.
Output
Results for temperature = 0.1:
round: 1
- Pad Thai: This stir-fried noodle dish is a staple in Thai cuisine and is popular among tourists for its combination of sweet, sour, and spicy flavors.
- Tom Yum Goong: This hot and sour soup is made with shrimp, lemongrass, lime, and chili, and is a must-try for those looking for a spicy and flavorful dish.
- Mango Sticky Rice: This popular dessert consists of sticky rice cooked in coconut milk and served with fresh mango slices. It is a refreshing and sweet treat that is loved by tourists and locals alike.
round: 2
- Pad Thai: This stir-fried noodle dish is a staple in Thai cuisine and is popular among tourists for its combination of sweet, sour, and spicy flavors.
- Tom Yum Goong: This hot and sour soup is made with shrimp, lemongrass, and other herbs and spices, making it a popular choice for tourists looking for a flavorful and spicy dish.
- Mango Sticky Rice: This dessert is made with sticky rice, fresh mango, and coconut milk, and is a must-try for tourists looking for a sweet and refreshing treat in Thailand.
##############################
Results for temperature = 0.75:
round: 1
- Pad Thai: This iconic stir-fried noodle dish is a must-try for any visitor to Thailand. It is made with rice noodles, chicken or shrimp, scrambled eggs, tofu, and a flavorful sauce made from tamarind, fish sauce, and palm sugar. It is often topped with crushed peanuts and served with lime wedges and fresh bean sprouts.
- Tom Yum Goong: This hot and sour soup is a staple in Thai cuisine and is a popular dish among tourists. It is made with a spicy broth flavored with lemongrass, kaffir lime leaves, and chili peppers, and is typically loaded with shrimp, mushrooms, and other herbs and spices.
- Mango Sticky Rice: This popular dessert is made with sticky rice cooked in coconut milk and served with fresh slices of ripe mango. It is a refreshing and sweet treat that is perfect for hot days in Thailand. It is also commonly topped with coconut cream and sesame seeds for added flavor and texture.
round: 2
- Pad Thai: This stir-fried noodle dish is a staple in Thai cuisine and is popular among both locals and tourists. The dish typically consists of rice noodles, vegetables, egg, tofu, and protein (chicken, shrimp, or pork) all cooked in a flavorful sauce made from tamarind, fish sauce, sugar, and chili.
- Tom Yum Goong: This hot and sour soup is another popular dish in Thailand. It is made with a fragrant broth, lemongrass, lime leaves, galangal, chilies, and shrimp. The combination of spicy, sour, and savory flavors makes it a favorite among tourists.
- Mango Sticky Rice: This sweet and creamy dessert is a must-try for tourists in Thailand. It consists of sticky rice cooked in coconut milk and served with fresh slices of ripe mango. It is often topped with a sprinkle of sesame seeds for added texture.
##############################
Results for temperature = 1.4:
round: 1
- Pad Thai - a popular stir-fried rice noodle dish with added meat, tofu, and eggs, seasoned with tamarind, fish sauce, and chili.
- Tom Yum Goong - a spicy and sour soup with shrimp (goong), galangal, lemongrass, kaffir lime leaves, and chili commonly served at street food stalls and restaurants in Thailand.
- Green Curry - a spicy and creamy curry dish made with a combination of green chili peppers, Thai herbs, coconut milk, and various proteins such as chicken, beef, or fish.
round: 2
- Pad Thai - This staple dish is one of the national dishes of Thailand and can be found in almost every corner of the country. It consists of stir-fried rice noodles, tofu, eggs, and often either chicken, shrimp, or other proteins. It is typically served with crushed peanuts, lime, and chili flakes for added flavor.
- Tom Yum Goong - This iconic Thai soup dish is a popular favorite among tourists. It is made with lemongrass, galangal, kaffir lime leaves, mushrooms, chili peppers, and shrimp, all cooked in a rich and spicy broth. It is a popular choice for those seeking a burst of flavor and heat in their food.
- Mango Sticky Rice - This popular dessert is a must-try while in Thailand. It is made of sweet sticky rice, coconut milk, and slices of ripe mango. The combination of the sweet and creamy rice with the juicy and tangy mango is a perfect balance of flavors and textures, making it a favorite among tourists.
As we analyze the results, we observe that a lower temperature setting like 0.1 results in very similar outputs across both rounds. As the temperature increases, the variation between rounds also increases, showcasing more diverse responses. This pattern mirrors the behavior with top_p settings: a lower top_p leads to consistent answers across trials, while a higher top_p increases diversity in the responses.
Output
Results for top_P = 0.1:
round: 1
- Pad Thai: This stir-fried noodle dish is a staple in Thai cuisine and is popular among tourists for its combination of sweet, sour, and spicy flavors.
- Tom Yum Goong: This hot and sour soup is made with shrimp, lemongrass, lime, and chili, and is a must-try for those looking for a spicy and flavorful dish.
- Mango Sticky Rice: This dessert is made with sticky rice, fresh mango, and coconut milk, and is a favorite among tourists for its sweet and refreshing taste.
round: 2
- Pad Thai: This stir-fried noodle dish is a staple in Thai cuisine and is popular among tourists for its combination of sweet, sour, and spicy flavors.
- Tom Yum Goong: This hot and sour soup is made with shrimp, lemongrass, lime, and chili, and is a must-try for those looking for a spicy and flavorful dish.
- Mango Sticky Rice: This dessert is made with sticky rice, fresh mango, and coconut milk, and is a favorite among tourists for its sweet and refreshing taste.
##############################
Results for top_P = 0.5:
round: 1
- Pad Thai: This dish is a stir-fried noodle dish made with rice noodles, eggs, tofu, shrimp, and a variety of vegetables. It is often served with a side of crushed peanuts and a wedge of lime.
- Tom Yum Goong: This is a spicy and sour soup made with shrimp, lemongrass, lime leaves, and chili peppers. It is often served with rice and is a popular choice among tourists for its bold flavors.
- Mango Sticky Rice: This dessert is made with sticky rice, fresh mango slices, and coconut milk. It is a popular street food in Thailand and is often served as a refreshing treat on a hot day.
round: 2
- Pad Thai: This dish is a stir-fried noodle dish made with rice noodles, eggs, tofu, shrimp, and a variety of vegetables. It is often served with a side of lime, crushed peanuts, and chili flakes for added flavor.
- Tom Yum Goong: This is a spicy and sour soup made with shrimp, lemongrass, galangal, lime leaves, and chili peppers. It is a popular dish in Thailand and is often served as a starter or main course.
- Mango Sticky Rice: This is a popular dessert in Thailand made with sticky rice, coconut milk, and fresh mango slices. It is often served as a sweet and refreshing treat after a meal.
##############################
Results for top_P = 1.0:
round: 1
- Pad Thai - a stir-fried noodle dish with protein (usually chicken or shrimp), vegetables, and a sweet and savory sauce.
- Tom Yum Goong - a spicy and sour soup made with shrimp, lemongrass, and other herbs and spices.
- Mango Sticky Rice - a popular dessert made with sweet sticky rice, fresh mango, and coconut milk.
round: 2
- Pad Thai: This stir-fried noodle dish is a staple in Thailand and a favorite among tourists. It is made with rice noodles, vegetables, protein (such as shrimp or tofu), and a flavorful sauce made with tamarind, fish sauce, and other spices.
- Tom Yum: This spicy and sour soup is a must-try for any visitor to Thailand. It is made with a broth of lemongrass, lime leaves, galangal, and chili peppers, and can be served with shrimp, chicken, or mushrooms.
- Mango Sticky Rice: This popular Thai dessert is made with sticky rice, fresh mango slices, and a drizzle of coconut milk. It is a refreshing and sweet treat that can be found at many street food stalls and restaurants throughout the country.
I hope this discussion has enhanced your understanding of the composition and capabilities of Large Language Models (LLMs). In our next episode, we will shift our focus to practical applications, providing a beginner’s guide to use cases such as text summarization, question and answer systems, and various types of queries.
Data Science Explore the world of data science with Donato_Story
Dashboard Discover the power of data visualization with Donato_Story
Donato_Journey Join me on my journey (Thai version)
Course_Review Discover the training courses with Donato_Story (Thai version)
Thank you for your engagement and curiosity throughout this series. I look forward to continuing this journey with you and exploring more facets of artificial intelligence. Stay tuned, and please, continue to follow this series for more insightful and actionable content.
Medium: medium.com/donato-story
Facebook: web.facebook.com/DonatoStory
Linkedin: linkedin.com/in/nattapong-thanngam
Originally published on Medium
Related
Chat with Document: A Closer Look at Splitting, Embeddings, and RAG
Data Mastery Series — Episode 21: The Chat with Document and Langchain Series (Part 2)
Corrective RAG
Data Mastery Series — Episode 53: RAG ที่ “คิด” ก่อน “ตอบ” และ “แก้ไข” เมื่อผิดพลาด
Generative AI Summarization: Multimodal Approaches
Data Mastery Series — Episode 25: The Chat with Document and Langchain Series (Part 6)
Hierarchical Multi-Agent Systems
Data Mastery Series — Episode 59: การสร้างระบบ AI ทีมงานด้วย Supervisor Agent กับทีมย่อย