提示模版

?

LangChain 的提示詞模板(Prompt Template)是用于構(gòu)建和管理提示詞的工具,能夠提高與大型語言模型交互的效率。首先需要安裝langchain_coze

pip install langchain_coze


PromptTemplate

基本的字符串提示模板,用于生成簡單的文本提示。它允許開發(fā)者定義一個基本的文本結(jié)構(gòu),并用輸入變量進(jìn)行格式化。

# 從 langchain_core.prompts 模塊導(dǎo)入 PromptTemplate 類
from langchain_core.prompts import PromptTemplate

# 創(chuàng)建一個基本的提示模板
# PromptTemplate 是一個類,用于定義和格式化提示模板
# input_variables 是一個列表,包含模板中的變量名
# template 是一個字符串,定義了提示的格式,其中包含占位符 {name} 和 {age}
template = PromptTemplate(
input_variables=["name", "age"], # 定義模板中的變量名
template="My name is {name} and I am {age} years old." # 定義提示的格式
)

# 格式化提示
# 使用 format 方法將變量值填入模板中
# name 變量的值是 "Alice",age 變量的值是 30
prompt = template.format(name="Alice", age=30)

# 打印格式化后的提示
# 輸出: My name is Alice and I am 30 years old.
print(prompt)


ChatPromptTemplate

用于聊天模型的提示模板,支持多角色消息的組合和管理。適用于需要模擬對話的場景。

# 從 langchain_core.prompts 模塊導(dǎo)入 ChatPromptTemplate 類
from langchain_core.prompts import ChatPromptTemplate

# 創(chuàng)建聊天提示模板
# ChatPromptTemplate 是一個類,用于定義和格式化聊天提示模板
# messages 是一個列表,包含多個消息模板
# 每個消息模板都是一個元組,包含消息的角色和內(nèi)容
template = ChatPromptTemplate([
("system", "You are a helpful AI bot. Your name is {name}."), # 系統(tǒng)消息,包含占位符 {name}
("human", "Hello, how are you doing?"), # 用戶消息
("ai", "I'm doing well, thanks!"), # AI 消息
("human", "{user_input}"), # 用戶消息,包含占位符 {user_input}
])

# 調(diào)用模板并傳入?yún)?shù)
# 使用 invoke 方法將變量值填入模板中
# name 變量的值是 "Bob",user_input 變量的值是 "What is your name?"
prompt_value = template.invoke(
{
"name": "Bob",
"user_input": "What is your name?"
}
)

# 輸出結(jié)果
# 打印格式化后的消息
print(prompt_value)

FewShotPromptTemplate

包含少量示例的提示模板,幫助模型更好地理解任務(wù)。適用于需要示例來引導(dǎo)模型的場景。

# 從 langchain_core.prompts 模塊導(dǎo)入 FewShotPromptTemplate 和 PromptTemplate 類
from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate

# 定義示例提示
# PromptTemplate 是一個類,用于定義和格式化提示模板
# template 參數(shù)是一個字符串,定義了提示的格式,其中包含占位符 {animal} 和 {characteristic}
# input_variables 參數(shù)是一個列表,包含模板中的變量名
example_prompt = PromptTemplate(
template="The {animal} is known for its {characteristic}.",
input_variables=["animal", "characteristic"]
)

# 創(chuàng)建 FewShotPromptTemplate
# FewShotPromptTemplate 是一個類,用于定義和格式化少樣本提示模板
# example_prompt 參數(shù)是一個 PromptTemplate 對象,定義了示例提示的格式
# examples 參數(shù)是一個列表,包含多個示例,每個示例是一個字典,包含變量名和值
# prefix 參數(shù)是一個字符串,定義了提示的前綴
# suffix 參數(shù)是一個字符串,定義了提示的后綴
# example_separator 參數(shù)是一個字符串,定義了示例之間的分隔符
few_shot_template = FewShotPromptTemplate(
example_prompt=example_prompt,
examples=[
{"animal": "dog", "characteristic": "loyalty"},
{"animal": "cat", "characteristic": "independence"},
{"animal": "elephant", "characteristic": "intelligence"},
],
prefix="Here are some facts about animals:",
suffix="Based on the examples above, what can you tell me about a rabbit?",
example_separator="\n\n"
)

# 使用格式化方法生成提示
# 使用 format 方法將變量值填入模板中
# animal 變量的值是 "rabbit",characteristic 變量的值是 "playfulness"
formatted_prompt = few_shot_template.format(animal="rabbit", characteristic="playfulness")

# 打印格式化后的提示
print(formatted_prompt)

調(diào)用模型

?

Langchain支持的國內(nèi)模型包括月之暗面科技的moonshot系列、百川智能的baichuan系列、阿里云的通義千問、智譜AI的glm-4以及訊飛星火3.0等。首先要下載langchain_community。

pip install langchain_community


智譜

目前glm-4-flash是免費的,可以使用glm-4-flash進(jìn)行學(xué)習(xí)。

# 導(dǎo)入必要的模塊和類
from langchain_community.chat_models import ChatZhipuAI
from langchain_core.prompts import PromptTemplate
import os

# 獲取智譜AI的API密鑰
# 從環(huán)境變量中獲取智譜AI的API密鑰
api_key = os.environ.get("ZHIPUAI_API_KEY")
# print(api_key) # 可以打印API密鑰進(jìn)行調(diào)試,但在生產(chǎn)環(huán)境中不建議打印敏感信息

# 創(chuàng)建ChatZhipuAI模型
# 使用智譜AI的GLM模型,并傳入API密鑰、模型名稱、溫度和最大令牌數(shù)
model = ChatZhipuAI(api_key=api_key, model="glm-4-flash", temperature=0.7, max_tokens=1024)

# 創(chuàng)建PromptTemplate模板
# 定義提示模板,包含占位符 {text}
template = "請用英語翻譯以下句子:{text}"
# 創(chuàng)建PromptTemplate對象,傳入模板和輸入變量
prompt_template = PromptTemplate(template=template, input_variables=["text"])

# 格式化提示模板
# 使用format方法將變量值填入模板中
my = prompt_template.format(text="你好,世界!")

# 調(diào)用模型生成響應(yīng)
# 使用invoke方法將格式化后的提示傳遞給模型,并獲取響應(yīng)
response = model.invoke(my)

# 打印模型的響應(yīng)內(nèi)容
print(response.content)

然后ChatZhipu中添加streaming=True參數(shù)啟用流式傳輸功能,然后輸出寫一下代碼

# 流式傳輸響應(yīng)
for response in model.stream(my):
print(response.content)

其他模型AI大模型列表

模型名稱簡要介紹
AzureChatOpenAI微軟Azure平臺上的OpenAI服務(wù)
BedrockChatAWS Bedrock平臺的聊天模型
ChatAnthropicAnthropic公司的Claude大模型
ChatAnyscale提供可擴(kuò)展的AI模型服務(wù)平臺
ChatBaichuan百川智能的大語言模型
ChatCohereCohere提供的自然語言處理模型
ChatCozeCoze平臺的聊天模型
ChatOctoAIOctoAI提供的AI模型服務(wù)
ChatDatabricksDatabricks平臺的聊天模型
ChatDeepInfraDeepInfra提供的AI模型服務(wù)
ChatEdenAIEdenAI平臺的聊天模型
ChatEverlyAIEverlyAI提供的AI服務(wù)
ChatFireworksFireworks AI平臺
ChatFriendliFriendli提供的AI服務(wù)
ChatGooglePalmGoogle PaLM大語言模型
ChatHuggingFaceHugging Face平臺的模型
ChatHunyuan騰訊混元大模型
ChatJavelinAIGatewayJavelin AI Gateway服務(wù)
ChatKineticaKinetica提供的AI模型
ChatKonkoKonko AI平臺
ChatLiteLLMLiteLLM輕量級模型服務(wù)
ChatLiteLLMRouterLiteLLM路由服務(wù)
ChatMLXApple MLX框架的模型
ChatMLflowAIGatewayMLflow AI網(wǎng)關(guān)
ChatMaritalkMaritalk聊天模型
ChatMlflowMLflow平臺的模型
ChatNebulaNebula AI平臺
ChatOCIGenAIOracle Cloud Infrastructure生成式AI
ChatOllamaOllama本地運行的開源模型
ChatOpenAIOpenAI的GPT模型
ChatPerplexityPerplexity AI搜索模型
ChatPremAIPremAI平臺的模型
ChatSparkLLM訊飛星火大模型
ChatSnowflakeCortexSnowflake Cortex AI服務(wù)
ChatTongyi阿里通義千問大模型
ChatVertexAIGoogle Vertex AI平臺
ChatYandexGPTYandex的GPT模型
ChatYuan2元語義Yuan 2.0模型
ChatZhipuAI智譜清言大模型
ChatLlamaCppLlama C++實現(xiàn)的模型
ErnieBotChat百度文心一言大模型
FakeListChatModel模擬聊天模型
GPTRouterGPT路由服務(wù)
GigaChatSber的GigaChat模型
HumanInputChatModel人工輸入模型
JinaChatJina AI的聊天模型
LlamaEdgeChatServiceLlama Edge聊天服務(wù)
MiniMaxChatMiniMax大模型
MoonshotChat月之暗面(Moonshot)大模型
PaiEasChatEndpoint阿里云PAI平臺
PromptLayerChatOpenAIPromptLayer增強(qiáng)的OpenAI模型
QianfanChatEndpoint百度千帆大模型平臺
SolarChatUpstage的Solar大模型
VolcEngineMaasChat火山引擎大模型服務(wù)
ChatYi零一萬物Yi大模型

本文章轉(zhuǎn)載微信公眾號@索隆程序員

上一篇:

大模型實戰(zhàn)(二):全民AI?關(guān)于如何在本地私有化部署基于大模型的 AI 應(yīng)用開發(fā)平臺Dify,并集成Xinference大模型

下一篇:

萬字解讀AI Agent架構(gòu)體系,API和RPA將成為重點
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊

多API并行試用

數(shù)據(jù)驅(qū)動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費