참조 :
https://python.langchain.com/docs/modules/model_io/prompts/quick_start
https://python.langchain.com/docs/modules/model_io/prompts/composition
Prompt Template
predefined recipes for generating prompts for language models.
- LLM, chatModel 은 prompt를 input으로 전달됨
- Prompt는 template을 제공
- Prompt template으로 생성된 prompt를 string 또는 Message와 합쳐서 사용할 수 있음
PromptTemplate
from langchain.prompts import PromptTemplate
prompt_template = PromptTemplate.from_template(
"Tell me a {adjective} joke about {content}."
)
prompt_template.format(adjective="funny", content="chickens")
output) Tell me a funny joke about chickens.
- 기본적으로 pyhthon의 format 문을 이용해서 적용
ChatPromptTemplate
2-tuple representation of (type, content)
from langchain_core.prompts import ChatPromptTemplate
chat_template = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful AI bot. Your name is {name}."),
("human", "Hello, how are you doing?"),
("ai", "I'm doing well, thanks!"),
("human", "{user_input}"),
]
)
messages = chat_template.format_messages(name="Bob", user_input="What is your name?")
print(messages)
output) [SystemMessage(content='You are a helpful AI bot. Your name is Bob.'), HumanMessage(content='Hello, how are you doing?'), AIMessage(content="I'm doing well, thanks!"), HumanMessage(content='What is your name?')]
an instance of MessagePromptTemplate or BaseMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain.prompts import HumanMessagePromptTemplate
from langchain_core.messages import SystemMessage
from langchain_openai import ChatOpenAI
chat_template = ChatPromptTemplate.from_messages(
[
SystemMessage(
content=(
"You are a helpful assistant that re-writes the user's text to "
"sound more upbeat."
)
),
HumanMessagePromptTemplate.from_template("{text}"),
]
)
messages = chat_template.format_messages(text="I don't like eating tasty things")
print(messages)
[SystemMessage(content="You are a helpful assistant that re-writes the user's text to sound more upbeat."), HumanMessage(content="I don't like eating tasty things")]
Composition
LangChain provides a user friendly interface for composing different parts of prompts together.
String prompt composition
You can work with either prompts directly or strings (the first element in the list needs to be a prompt)
그냥 붙여서 사용 (첫번째 element는 prompt 여야 함)
from langchain.chains import LLMChain
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
import os
#################################################################
# API Key
os.environ["OPENAI_API_KEY"] = "********************************"
model = ChatOpenAI()
prompt = (
PromptTemplate.from_template("Tell me a joke about {topic}")
+ ", make it funny"
+ "\n\nand in {language}"
)
chain = LLMChain(llm=model, prompt=prompt)
print(chain.run(topic="sports", language="spanish"))
output)
/DATA/venv/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.
warn_deprecated(
¿Por qué los tenistas nunca pueden jugar al escondite bien?
Porque siempre están buscando la red.
>> chain.run() 은 deprecated 되었다고해서 chain.invoke()를 사용하도록 다음과 같이 수정하여 실행
result = chain.invoke({ "topic": "sports", "language": "spanish" })
print(result)
output)
{'topic': 'sports', 'language': 'spanish', 'text': '¿Por qué el balón fue al psicólogo?\n\nPorque tenía muchos problemas para meterse en la cabeza de los jugadores.'}
Chat prompt composition
A chat prompt is made up a of a list of messages
import os
#################################################################
# API Key
os.environ["OPENAI_API_KEY"] = "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"
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain.chains import LLMChain
from langchain_openai import ChatOpenAI
prompt = SystemMessage(content="You are a nice pirate")
new_prompt = (
prompt + HumanMessage(content="hi") + AIMessage(content="what?") + "{input}"
)
new_prompt.format_messages(input="i said hi")
model = ChatOpenAI()
chain = LLMChain(llm=model, prompt=new_prompt)
result = chain.invoke("i said hi")
print(result)
output)
{'input': 'i said hi', 'text': 'Oh, sorry about that! Hi there! How can I help you today?'}
'ML&DL and LLM' 카테고리의 다른 글
LangChain - 1.2.5 MessagePromptTemplate (0) | 2024.03.28 |
---|---|
LangChain - 1.2.4 Few-shot prompt template (0) | 2024.03.28 |
LangChain - 1.1 Model I/O Concept (0) | 2024.03.27 |
LangChain (0) | 2024.03.27 |
[Langchain] Prompt 튜닝 (1) | 2024.03.05 |