Prompts 即提示詞,在 Language Model 中,,Prompts是指用戶的一些列指令和輸入,。
Prompts 用于指導(dǎo)Model的響應(yīng),幫助 Language Model 理解上下文,并生成相關(guān)和連貫的輸出(如回答問題,、拓寫句子和總結(jié)問題),。 Prompts 是決定 Language Model 輸出內(nèi)容的唯一輸入。
語言模型的提示是用戶提供的一組指令或輸入,,用于指導(dǎo)模型的響應(yīng),,幫助它理解上下文并生成相關(guān)且連貫的基于語言的輸出,例如回答問題,、完成句子或進(jìn)行對話. LangChain provides several classes and functions to help construct and work with prompts.
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from langchain.prompts import PromptTemplate
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI()
#### PromptTemplate ####
prompt_template = PromptTemplate.from_template(
"Tell me a {adjective} joke about {content}"
)
prompt = prompt_template.format(adjective="funny", content="chickens")
print(prompt)
print(llm.invoke(prompt))
輸出:
代碼語言:python
代碼運(yùn)行次數(shù):0
復(fù)制
(LLM) ? prompt_template python3 00.py
Tell me a funny joke about chickens
content='Why did the chicken go to the seance?\n\nTo talk to the other side!'
(LLM) ? prompt_template
Prompts Templates 也提供了 ChatPromptTemplate 的支持
代碼語言:python
代碼運(yùn)行次數(shù):0
復(fù)制
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI()
#### 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 am doing well, thanks!"),
("human", "{content}"),
]
)
messages = chat_template.format_messages(name="windeal", content="What is your name")
print(messages)
print(llm(messages))
輸出:
代碼語言:python
代碼運(yùn)行次數(shù):0
復(fù)制
(LLM) ? prompt_template python3 00.py
[SystemMessage(content='You are a helpful ai bot, your name is windeal'), HumanMessage(content='Hello, how are you doing?'), AIMessage(content='I am doing well, thanks!'), HumanMessage(content='What is your name')]
content='My name is Windeal. How can I assist you today?'
(LLM) ? prompt_template
LangChain 提供了 Example Selects 這一組件,用于在有大量示例時,,從中選擇需要包含在 Prompt 中的示例,。
Example Selects 的基類接口如下:
代碼語言:python
代碼運(yùn)行次數(shù):0
復(fù)制
class BaseExampleSelector(ABC):
"""Interface for selecting examples to include in prompts."""
@abstractmethod
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
"""Select which examples to use based on the inputs."""