
如何快速實現(xiàn)REST API集成以優(yōu)化業(yè)務(wù)流程
?
LangChain 的提示詞模板(Prompt Template)是用于構(gòu)建和管理提示詞的工具,能夠提高與大型語言模型交互的效率。首先需要安裝
langchain_coze
pip install langchain_coze
基本的字符串提示模板,用于生成簡單的文本提示。它允許開發(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)
用于聊天模型的提示模板,支持多角色消息的組合和管理。適用于需要模擬對話的場景。
# 從 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)
包含少量示例的提示模板,幫助模型更好地理解任務(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)
?
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)
模型名稱 | 簡要介紹 |
---|---|
AzureChatOpenAI | 微軟Azure平臺上的OpenAI服務(wù) |
BedrockChat | AWS Bedrock平臺的聊天模型 |
ChatAnthropic | Anthropic公司的Claude大模型 |
ChatAnyscale | 提供可擴(kuò)展的AI模型服務(wù)平臺 |
ChatBaichuan | 百川智能的大語言模型 |
ChatCohere | Cohere提供的自然語言處理模型 |
ChatCoze | Coze平臺的聊天模型 |
ChatOctoAI | OctoAI提供的AI模型服務(wù) |
ChatDatabricks | Databricks平臺的聊天模型 |
ChatDeepInfra | DeepInfra提供的AI模型服務(wù) |
ChatEdenAI | EdenAI平臺的聊天模型 |
ChatEverlyAI | EverlyAI提供的AI服務(wù) |
ChatFireworks | Fireworks AI平臺 |
ChatFriendli | Friendli提供的AI服務(wù) |
ChatGooglePalm | Google PaLM大語言模型 |
ChatHuggingFace | Hugging Face平臺的模型 |
ChatHunyuan | 騰訊混元大模型 |
ChatJavelinAIGateway | Javelin AI Gateway服務(wù) |
ChatKinetica | Kinetica提供的AI模型 |
ChatKonko | Konko AI平臺 |
ChatLiteLLM | LiteLLM輕量級模型服務(wù) |
ChatLiteLLMRouter | LiteLLM路由服務(wù) |
ChatMLX | Apple MLX框架的模型 |
ChatMLflowAIGateway | MLflow AI網(wǎng)關(guān) |
ChatMaritalk | Maritalk聊天模型 |
ChatMlflow | MLflow平臺的模型 |
ChatNebula | Nebula AI平臺 |
ChatOCIGenAI | Oracle Cloud Infrastructure生成式AI |
ChatOllama | Ollama本地運行的開源模型 |
ChatOpenAI | OpenAI的GPT模型 |
ChatPerplexity | Perplexity AI搜索模型 |
ChatPremAI | PremAI平臺的模型 |
ChatSparkLLM | 訊飛星火大模型 |
ChatSnowflakeCortex | Snowflake Cortex AI服務(wù) |
ChatTongyi | 阿里通義千問大模型 |
ChatVertexAI | Google Vertex AI平臺 |
ChatYandexGPT | Yandex的GPT模型 |
ChatYuan2 | 元語義Yuan 2.0模型 |
ChatZhipuAI | 智譜清言大模型 |
ChatLlamaCpp | Llama C++實現(xiàn)的模型 |
ErnieBotChat | 百度文心一言大模型 |
FakeListChatModel | 模擬聊天模型 |
GPTRouter | GPT路由服務(wù) |
GigaChat | Sber的GigaChat模型 |
HumanInputChatModel | 人工輸入模型 |
JinaChat | Jina AI的聊天模型 |
LlamaEdgeChatService | Llama Edge聊天服務(wù) |
MiniMaxChat | MiniMax大模型 |
MoonshotChat | 月之暗面(Moonshot)大模型 |
PaiEasChatEndpoint | 阿里云PAI平臺 |
PromptLayerChatOpenAI | PromptLayer增強(qiáng)的OpenAI模型 |
QianfanChatEndpoint | 百度千帆大模型平臺 |
SolarChat | Upstage的Solar大模型 |
VolcEngineMaasChat | 火山引擎大模型服務(wù) |
ChatYi | 零一萬物Yi大模型 |
本文章轉(zhuǎn)載微信公眾號@索隆程序員