# LangSmith Set up tracking. https://smith.langchain.com
# !pip install -qU langchain-teddynote
from langchain_teddynote import logging
# Enter a project name.
logging.langsmith("CH02-Prompt")
from langchain_openai import ChatOpenAI
from langchain_teddynote.messages import stream_response
# Object creation
llm = ChatOpenAI(
temperature=0, # creativity
model_name="gpt-4-turbo", # model name
)
# Inquiry content
question = "What is the capital of South Korea?"
# 질의
answer = llm.stream(question)
stream_response(answer)
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Example Selector
If there are many examples, you may need to choose an example to include at the prompt. Example Selector is the class responsible for this task.
from langchain_core.prompts.few_shot import FewShotPromptTemplate
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
examples = [
{
"question": "Who lived longer, Steve Jobs or Einstein?",
"answer": """Do you need additional questions for this question? Yes.
Additional question: At what age did Steve Jobs?
Intermediate answer: Steve Jobs died at the age of 56.
Additional question: At what age did Einstein die?
Intermediate answer: Einstein died at the age of 76.
The final answer is: Einstein
""",
},
{
"question": "When was Naver's founder born?",
"answer": ""Do you need additional questions for this question? Yes
Additional question: Who is the founder of Naver?
Intermediate answer: Naver was founded by Lee Hae-jin.
Additional question: When was Lee Hae-jin born?
Intermediate answer: Lee Hae-jin was born on June 22, 1967.
Final answer: June 22, 1967
""",
},
{
"question": "Who was the king who ruled in the year that Yulgok Yi I's mother was born?",
"answer": """Does this question require further questions? Yes.
Additional question: At what age did Steve Jobs die?
Intermediate answer: Yulgok Yi I's mother is Shin Saimdang.
Additional question: When was Shin Saimdang born?
Intermediate answer: Shin Saimdang was born in 1504.
Additional question: Who ruled Joseon in 1504?
Intermediate answer: The king who ruled Joseon in 1504 was Yeonsangun.
Final answer: Yeonsangun
""",
},
{
question": "Are the directors of Oldboy and Parasite from the same country?",
"answer": """Is there a follow-up question for this question? Yes.
Additional question: Who is the director of Oldboy?
Intermediate answer: The director of Oldboy is Park Chan-wook.
Additional question: What country is Park Chan-wook from?
Intermediate answer: Park Chan-wook is from South Korea.
Additional question: Who is the director of Parasite?
Intermediate answer: The director of Parasite is Bong Joon-ho.
Additional question: What country is Bong Joon-ho from?
Intermediate answer: Bong Joon-ho is from South Korea.
Final answer: Yes
""",
},
]
Question:
Who lived longer, Steve Jobs and Einstein?
Answer:
Need additional questions for this question: yes.
Additional question: How old did Steve Jobs die?
Interim answer: Steve Jobs died at the age of 56.
Additional question: How old did Einstein die?
Interim answer: Einstein died at the age of 76.
The final answer is: Einstein
prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
suffix="Question:\n{question}\nAnswer:",
input_variables=["question"],
)
question = "Google이 In the year of its founding
Bill Gates How old is ?"
final_prompt = prompt.format(question=question)
print(final_prompt)
Question:
Who lived longer, Steve Jobs and Einstein?
Answer:
Need additional questions for this question: yes.
Additional question: How old did Steve Jobs die?
Interim answer: Steve Jobs died at the age of 56.
Additional question: How old did Einstein die?
Interim answer: Einstein died at the age of 76.
The final answer is: Einstein
Question:
When was Naver's founder born?
Answer:
Need additional questions for this question: yes.
Additional questions: Who is Naver's founder?
Interim answer: Naver was founded by a stakeholder.
Additional questions: When was the stakeholder born?
Interim answer: The stakeholder was born on June 22, 1967.
The final answer is: June 22, 1967
Question:
Who was the reigning king of the year that U.K.'s mother was born?
Answer:
Need additional questions for this question: yes.
Additional questions: Who is Yulgui's mother?
Middle answer: Yulgui's mother is a gentleman's office.
Additional questions: When was the Gentleman Party born?
Interim answer: The Shrine Party was born in 1504.
Additional question: Who was the king who ruled Joseon in 1504?
Middle answer: The king who ruled Joseon in 1504 is the operator.
The final answer is: Operator
Question:
Are the directors of Old Boy and Parasites from the same country?
Answer:
Need additional questions for this question: yes.
Additional questions: Who is Old Boy's director?
Interim answer: Old Boy's director is Park Chan-wook.
Additional questions: Which country is Park Chan-wook from?
Interim answer: Park Chan-wook is from Korea.
Additional question: Who is the parasite director?
Interim answer: The parasite director is Bong Joon-ho.
Additional question: Which country is Bong Joon-ho from?
Interim answer: Bong Joon-ho is from Korea.
The final answer is: yes
Question:
How old is Bill Gates in the year Google was founded?
Answer:
# Output the results
answer = llm.stream(final_prompt)
stream_response(answer)
Need additional questions for this question: yes.
Additional questions: When was Google founded?
Interim answer: Google was founded in 1998.
Additional questions: When was Bill Gates born?
Interim answer: Bill Gates was born on October 28, 1955.
The final answer is: Bill Gates was 43 in 1998.
prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
suffix="Question:\n{question}\nAnswer:",
input_variables=["question"],
)
# chain generation
chain = prompt | llm | StrOutputParser()
# Output the results
answer = chain.stream(
{"question": "Google In this year of founding
Bill Gates How old is?"}
)
stream_response(answer)
Need additional questions for this question: yes.
Additional questions: When was Google founded?
Interim answer: Google was founded in 1998.
Additional questions: When was Bill Gates born?
Interim answer: Bill Gates was born on October 28, 1955.
The final answer is: Bill Gates is 43 years old in 1998.
from langchain_core.example_selectors import (
MaxMarginalRelevanceExampleSelector,
SemanticSimilarityExampleSelector,
)
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma
# Vector DB create(repository name, embedding class)
chroma = Chroma("example_selector", OpenAIEmbeddings())
example_selector = SemanticSimilarityExampleSelector.from_examples(
# Here is a list of examples to choose from.
examples,
# Here is an embedding class that generates embeddings used to measure semantic similarity.
OpenAIEmbeddings(),
# Here we have a VectorStore class which is used to store embeddings and perform similarity searches.
Chroma,
# This is the number of examples to generate.
k=1,
)
# Select the example that most closely resembles the input.
selected_examples = example_selector.select_examples({"question": question})
question = "Google In this year of founding Bill Gates How old is?"
print(f"The most similar example to the input:\n{question}\n")
for example in selected_examples:
print(f'question:\n{example["question"]}')
print(f'answer:\n{example["answer"]}')
The most similar example for input:
How old is Bill Gates in the year Google was founded?
question:
When was Naver's founder born?
answer:
Need additional questions for this question: yes.
Additional questions: Who is Naver's founder?
Interim answer: Naver was founded by a stakeholder.
Additional questions: When was the stakeholder born?
Interim answer: The stakeholder was born on June 22, 1967.
The final answer is: June 22, 1967
prompt = FewShotPromptTemplate(
example_selector=example_selector,
example_prompt=example_prompt,
suffix="Question:\n{question}\nAnswer:",
input_variables=["question"],
)
question = "Google In this year of founding Bill Gates How old is?"
example_selector_prompt = prompt.format(question=question)
print(example_selector_prompt)
Question:
When was Naver's founder born?
Answer:
Need additional questions for this question: yes.
Additional questions: Who is Naver's founder?
Interim answer: Naver was founded by a stakeholder.
Additional questions: When was the stakeholder born?
Interim answer: The stakeholder was born on June 22, 1967.
The final answer is: June 22, 1967
Question:
How old is Bill Gates in the year Google was founded?
Answer:
# Output the results
answer = chain.stream(
{"question": "Google In this year of founding
Bill Gates How old is?"}
)
stream_response(answer)
Need additional questions for this question: yes.
Additional questions: When was Google founded?
Interim answer: Google was founded in 1998.
Additional questions: When was Bill Gates born?
Interim answer: Bill Gates was born on October 28, 1955.
Final calculation: 43 minus 1955 years from 1998.
The final answer is: 43 years old
examples = [
{
"instruction": "You are an expert in minutes writing. Please write minutes based on the information given.",
"input": "On December 25, 2023, XYZ Company's marketing strategy meeting started at 3 p.m. The meeting was attended by Kim Soo-jin, the marketing team leader, Park Ji-min, the digital marketing manager, and Lee Jun-ho, the social media manager.
Minutes: XYZ Company Marketing Strategy Meeting
Date: December 25, 2023
Location: XYZ Company Conference Room
Attendees: Kim Soo-jin (Marketing Team Leader), Park Ji-min (Digital Marketing Manager), Lee Jun-ho (Social Media Manager)
1. Opening
- The meeting begins with an opening speech by Team Leader Kim Soo-jin.
- The purpose of the meeting is to establish a marketing strategy for the first half of 2024 and discuss new social media campaign ideas.
2. Market Trends Overview (Kim Soo-jin)
- Team Leader Kim Soo-jin presents analysis of recent market trends.
- Shares insights on changes in consumer behavior and competitor strategies.
3. Digital Marketing Strategy (Park Ji-min)
- Park Ji-min presents on digital marketing strategy.
- Focuses on online advertising and SEO optimization methods.
4. Social Media Campaign (Lee Jun-ho)
- Lee Jun-ho suggests an idea for a new social media campaign.
- Outline your plans for influencer marketing and content strategy.
5. General discussion
- Sharing and discussing ideas among team members.
- Discussing budget and resource allocation for each strategy.
6. finish
- Confirm the date and time of the next meeting.
- Park Ji-min is in charge of organizing and distributing meeting minutes.
""",
},
{
"instruction": "You are a summary expert. Summarize the content based on the information given below.
",
"input": "This document is a 20-page report on the 'Strategy for Sustainable Urban Development'.
. The report comprehensively addresses the importance of sustainable urban development, the current challenges of urbanization, and various strategies to make urban development sustainable.
The report also highlights successful examples of sustainable urban development in several countries and summarizes the lessons learned from these examples..",
"answer": """
Document Summary: Strategy Report for Sustainable Urban Development
- Importance: Highlights why sustainable urban development is essential and the resulting social, economic and environmental benefits.
- Current Issues: Analyze the major problems arising from the current urbanization process, such as environmental pollution, resource depletion, and increasing inequality.
- Strategy: Presents a range of strategies to achieve sustainable urban development, including green building, improved public transport, increased energy efficiency and enhanced community engagemen.
- Case studies: Presenting successful examples of sustainable development in cities around the world, including Copenhagen, Denmark and Yokohama, Japan, to illustrate feasible strategies.
- Lessons learned: Summarize key lessons learned from these cases. Highlighted lessons include the importance of a multi-faceted approach, collaboration with communities, and the need for long-term planning
You are a sentence correction expert. Please correct the following sentence.This report provides an in-depth analysis of how sustainable urban development can be achieved in a realistic and effective manner.
""",
},
{
"instruction": "You are a sentence correction expert. Please correct the following sentence",
"input": "Our company is planning to introduce a new marketing strategy. This will make communication with customers more effective.",
"answer": "The company expects that by introducing a new marketing strategy, it will be able to improve communication with customers more effectively.",
},
]
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate
from langchain_core.example_selectors import (
SemanticSimilarityExampleSelector,
)
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma
chroma = Chroma("fewshot_chat", OpenAIEmbeddings())
example_prompt = ChatPromptTemplate.from_messages(
[
("human", "{instruction}:\n{input}"),
("ai", "{answer}"),
]
)
example_selector = SemanticSimilarityExampleSelector.from_examples(
# Here is a list of examples to choose from.
examples,
# Here is an embedding class that generates embeddings that are used to measure semantic similarity..
OpenAIEmbeddings(),
# Here we have a VectorStore class which is used to store embeddings and perform similarity searches..
chroma,
# This is the number of examples to generate.
k=1,
)
few_shot_prompt = FewShotChatMessagePromptTemplate(
example_selector=example_selector,
example_prompt=example_prompt,
)
question = {
"instruction": "회의록을 작성해 주세요",
"input": "2023년 12월 26일, ABC 기술 회사의 제품 개발 팀은 새로운 모바일 애플리케이션 프로젝트에 대한 주간 진행 상황 회의를 가졌다. 이 회의에는 프로젝트 매니저인 최현수, 주요 개발자인 황지연, UI/UX 디자이너인 김태영이 참석했다. 회의의 주요 목적은 프로젝트의 현재 진행 상황을 검토하고, 다가오는 마일스톤에 대한 계획을 수립하는 것이었다. 각 팀원은 자신의 작업 영역에 대한 업데이트를 제공했고, 팀은 다음 주까지의 목표를 설정했다.",
}
example_selector.select_examples(question)
[{'answer':'\n Minutes: XYZ Company Marketing Strategy Meeting\n Il: December 25, 2023 \n Place: XYZ Company Meeting Room \n Attendee: Kim Su-jin (Marketing Team Leader), Park Ji-min (Digital Marketing Manager), Lee Jun-ho (Social Media Manager)\n\n1. Opening\n -The meeting begins with the opening of Team Kim Soo-jin.\n -The purpose of the meeting is to establish a marketing strategy in the first half of 2024 and discuss new social media campaign ideas.\n\n2. Market Trends Overview (Kim Soo-jin) \n -Kim Soo-jin Kim presents an analysis of recent market trends. \n -Share insights on consumer behavior change and competitor strategies.\n\n3. Digital Marketing Strategy (Bak Ji-min)\n - Park Ji-min announces about digital marketing strategy.\n -Focus on online advertising and SEO optimization.\n\n4. Social Media Campaign (Junho Lee) \n - Lee Jun-ho proposes ideas for new social media campaigns.\n -Explains plans for inflation marketing and content strategy.\n\n5. Comprehensive discussion\n -Share and discuss ideas between team members.\n - Discuss budget and resource allocation for each strategy.\n\n6. Finish\n -Date and time confirmed for next meeting.\n -Scheduling and distribution of minutes was held by Park Ji-min.\n','input':'On December 25, 2023, the XYZ company's marketing strategy meeting began at 3pm. Marketing team leader Kim Su-jin, digital marketer Park Ji-min, and social media manager Lee Jun-ho attended the meeting. The main purpose of the meeting was to develop a marketing strategy for the first half of 2024 and discuss ideas for new social media campaigns. Team leader Kim Su-jin provided a brief overview of recent market trends, and each team member presented a strategic idea in his field.','instruction':'You are an expert in writing minutes. Please fill out the minutes based on the information given'}]
final_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant.",
),
few_shot_prompt,
("human", "{instruction}\n{input}"),
]
)
# chain generation
chain = final_prompt | llm
# Execution and output of results
answer = chain.stream(question)
stream_response(answer)
Minutes: ABC Technology Company Product Development Team Weekly Progress Meeting
Date: December 26, 2023
Venue: ABC Technology Company Meeting Room
Attendees: Hyunsoo Choi (Project Manager), Hwang Ji-yeon (Main Developer), Tae-young Kim (UI/UX Designer)
One. Opening
-The Hyunsoon Choi project manager opens the meeting and explains the purpose of the meeting.
-Purpose: Review the project's current progress and plan for upcoming milestones.
2. Progress report
-Hang Ji-yeon developers report on development progress.
-Explain the status of features currently under development and the problems that have been solved recently.
-Share the features you set for completion goals by next week.
-Designer Kim Tae-young reports on UI/UX design progress.
-Released improvements based on recently completed design elements and user feedback.
-Share design work plans for the next step.
3. Discuss problems and solutions
-Share key issues facing team members and discuss solutions.
-Hyunsoon Choi sets the priority for each problem and directs resource allocation for resolution.
4. Set goals for next week
-Hyun Hyun-soo sets out the main goals for next week, clarifying the roles and expected results of each team member.
-Check out the specific goals that Hwang Ji-yeon and Kim Tae-young have to achieve in their respective fields.
5. finish
-Date and time confirmed for the next meeting.
-The meeting minutes are organized and distributed by Hyun-soo Choi.
The meeting ended after all agendas were sufficiently discussed and each team member's next week's goals were clearly set.
question = {
"instruction": "please write the minutes",
}
example_selector.select_examples(question)
[{'answer':'The company is expected to improve its communication with customers more effectively by introducing a new marketing strategy.','input':'Our company is trying to introduce a new marketing strategy. This will make communication with customers more effective.','instruction':'You are a sentence correction expert. Correct the next sentence'}]
# Results when using the default example selector without customization
example_selector.select_examples({"instruction": "Please summarize the following sentence.
"})
[{'answer':'The company is expected to improve its communication with customers more effectively by introducing a new marketing strategy.','input':'Our company is trying to introduce a new marketing strategy. This will make communication with customers more effective.','instruction':'You are a sentence correction expert. Correct the next sentence'}]
from langchain_teddynote.prompts import CustomExampleSelector
# Creating a custom example selector
custom_selector = CustomExampleSelector(examples, OpenAIEmbeddings())
# Results when using a custom example selector
custom_selector.select_examples({"instruction": "Please correct the following sentence.
"})
[{'instruction':'You are a sentence correction expert. Correct the following sentence','input':'Our company is about to introduce a new marketing strategy. This will make communication with customers more effective.','answer':'The company is expected to improve its communication with customers more effectively by introducing a new marketing strategy.'}]
example_prompt = ChatPromptTemplate.from_messages(
[
("human", "{instruction}:\n{input}"),
("ai", "{answer}"),
]
)
custom_fewshot_prompt = FewShotChatMessagePromptTemplate(
example_selector=custom_selector, # Using a custom example selector
example_prompt=example_prompt, # Using example prompts
)
custom_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant.",
),
custom_fewshot_prompt,
("human", "{instruction}\n{input}"),
]
)
# chain creates.
chain = custom_prompt | llm
question = {
"instruction": "회의록을 작성해 주세요",
"input": "On December 26, 2023, the product development team at ABC Technology Company held a weekly progress meeting on a new mobile application project. The meeting was attended by project manager Hyunsoo Choi, key developer Jiyeon Hwang, and UI/UX designer Taeyoung Kim. The main purpose of the meeting was to review the current progress of the project and establish a plan for upcoming milestones. Each team member provided an update on their work area, and the team set goals for the next week.",
}
# Execution and output of results
stream_response(chain.stream(question))
Minutes: ABC Technology Company Product Development Team Weekly Progress Meeting
Date: December 26, 2023
Venue: ABC Technology Company Meeting Room
Attendees: Hyunsoo Choi (Project Manager), Hwang Ji-yeon (Main Developer), Tae-young Kim (UI/UX Designer)
One. Opening
-The Hyunsoon Choi project manager opens the meeting and explains the purpose of the meeting.
-Purpose: Review the project's current progress and plan for upcoming milestones.
2. Report project progress
-Hang Ji-yeon developer reports on current development progress.
-Describe the main functional development situation and the technical problems that have occurred.
-Discuss solutions and necessary resources.
-Designer Kim Tae-young reports on UI/UX design progress.
-Provide an overview of recently completed design elements and user feedback.
-Announce the next level of design planning.
3. Upcoming milestone planning
-Hyunsoon Choi presents the main goals and milestones until next week.
-The development team focused on fixing and optimizing bugs for key features.
-The design team will work on design modifications to improve user experience.
4. Troubleshooting and resource coordination
-Discuss the problems facing team members and the additional resources needed.
-Hyunsoon Choi promises to consult with the top management to secure additional resources.
5. Next meeting schedule
-The next week's progress meeting will be held next Monday at the same time.
6. finish
-Hyunsoon Choi closes the meeting and reaffirms clear goals and expectations for each team member.
-The minutes will be organized by Hwang Ji-yeon and shared inside the team.
question = {
"instruction": "Please summarize the document",
"input": "This document is a detailed 30-page report on the 'Global Economic Outlook 2023'. The report covers the current state of the world economy, economic growth rates of major countries, global trade trends, and economic forecasts for the coming year. The report also analyzes the impact of various economic, political, and environmental factors on the world economy.",
}
# Execution and output of results
stream_response(chain.stream(question))
Document summary: 2023 Global Economic Outlook Report
-Current status: Provides an analysis of the current situation of the global economy, and describes the progress and major challenges of economic recovery.
-Economic growth rate: Review the economic growth rate of major countries and provide detailed information about the economic situation of each country.
-Global trade trends: Analyze recent global trade patterns and changes, and highlight the impact of trade policies and international relations on trade.
-Economic forecasting: presents an economic outlook for the 2023 year, assessing growth potential and potential risk factors.
-Impact analysis: In-depth analysis of the impact of economic, political and environmental factors on the global economy.
This report covers the key trends and forecasts of the global economy in 2023, and assesses the complex impact of various factors on the economy.
question = {
"instruction": "Please correct the sentence",
"input": "The company expects sales to increase this year. The new strategy is working well..",
}
# Execution and output of results
stream_response(chain.stream(question))
The company expects sales to increase this year. This is because the new strategy is working fine.