무제
[Langchain] Template 본문
1. PromptTemplate : 단일 프롬프트 단일 문자열을 처리할 때 적
template = PromptTemplate.from_template("What is a good name for a company that makes {product}?")
2. ChatPromptTemplate : 챗봇 기반 모델의 대화흐름을 반영할 때 적합
template = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a list generating machine. Everything you are asked will be answered with a comma separated list of max {max_items} in lowercase in Korean.Do NOT reply with anything else.",
),
("human", "{question}"),
]
)
3. FewShotPromptTemplate : 소수의 예시를 통해 모델에 특정 답변 형태를 유도할 수 있음
from langchain.chat_models import ChatOpenAI
from langchain.prompts.few_shot import FewShotPromptTemplate, FewShotChatMessagePromptTemplate
from langchain.callbacks import StreamingStdOutCallbackHandler
from langchain.prompts.prompt import PromptTemplate
from langchain.prompts import ChatPromptTemplate
import os
chat = ChatOpenAI(
model = "gpt-4o-mini",
temperature=0.1,
streaming=True,
callbacks=[
StreamingStdOutCallbackHandler(),
],
)
examples = [
{
"question": "What do you know about France?",
"answer": """
Here is what I know:
Capital: Paris
Language: French
Food: Wine and Cheese
Currency: Euro
""",
},
{
"question": "What do you know about Italy?",
"answer": """
I know this:
Capital: Rome
Language: Italian
Food: Pizza and Pasta
Currency: Euro
""",
},
{
"question": "What do you know about Greece?",
"answer": """
I know this:
Capital: Athens
Language: Greek
Food: Souvlaki and Feta Cheese
Currency: Euro
""",
},
]
example_prompt = PromptTemplate.from_template("Human: {question}\nAI:{answer}")
prompt = FewShotPromptTemplate(
example_prompt=example_prompt,
examples=examples,
suffix="Human: What do you know about {country}?",
input_variables=["country"],
)
chain = prompt | chat
chain.invoke({"country": "Turkey"})
# ChatPromptTemplate 사용 예시
"""
example_prompt = ChatPromptTemplate.from_messages(
[
("human", "What do you know about {question}?"),
("ai", "{answer}"),
]
)
example_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=examples,
)
final_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a geography expert, you give short answers."),
example_prompt,
("human", "What do you know about {question}?"),
]
)
chain = final_prompt | chat
chain.invoke({"question": "Thailand"})
"""
'Project > LLM' 카테고리의 다른 글
| [Langchain] Qdrant Vector DB (2) | 2024.11.12 |
|---|---|
| [Langchain] Splitter / Vector DB (0) | 2024.11.10 |
| [Langchain] Memory (0) | 2024.10.25 |
| [Langchain] Cache (0) | 2024.10.23 |
| LLM 프로젝트 1일차 키워드 정리 (0) | 2024.10.14 |
Comments