分類目錄:《大模型從入門到應用》總目錄
LangChain系列文章:
- 基礎知識
- 快速入門
- 安裝與環(huán)境配置
- 鏈(Chains)、代理(Agent:)和記憶(Memory)
- 快速開發(fā)聊天模型
- 模型(Models)
- 基礎知識
- 大型語言模型(LLMs)
- 基礎知識
- LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(Human Input LLM)
- 緩存LLM的調用結果
- 加載與保存LLM類、流式傳輸LLM與Chat Model響應和跟蹤tokens使用情況
- 聊天模型(Chat Models)
- 基礎知識
- 使用少量示例和響應流式傳輸
- 文本嵌入模型
- Aleph Alpha、Amazon Bedrock、Azure OpenAI、Cohere等
- Embaas、Fake Embeddings、Google Vertex AI PaLM等
- 提示(Prompts)
- 基礎知識
- 提示模板
- 基礎知識
- 連接到特征存儲
- 創(chuàng)建自定義提示模板和含有Few-Shot示例的提示模板
- 部分填充的提示模板和提示合成
- 序列化提示信息
- 示例選擇器(Example Selectors)
- 輸出解析器(Output Parsers)
- 記憶(Memory)
- 基礎知識
- 記憶的類型
- 會話緩存記憶、會話緩存窗口記憶和實體記憶
- 對話知識圖譜記憶、對話摘要記憶和會話摘要緩沖記憶
- 對話令牌緩沖存儲器和基于向量存儲的記憶
- 將記憶添加到LangChain組件中
- 自定義對話記憶與自定義記憶類
- 聊天消息記錄
- 記憶的存儲與應用
- 索引(Indexes)
- 基礎知識
- 文檔加載器(Document Loaders)
- 文本分割器(Text Splitters)
- 向量存儲器(Vectorstores)
- 檢索器(Retrievers)
- 鏈(Chains)
- 基礎知識
- 通用功能
- 自定義Chain和Chain的異步API
- LLMChain和RouterChain
- SequentialChain和TransformationChain
- 鏈的保存(序列化)與加載(反序列化)
- 鏈與索引
- 文檔分析和基于文檔的聊天
- 問答的基礎知識
- 圖問答(Graph QA)和帶來源的問答(Q&A with Sources)
- 檢索式問答
- 文本摘要(Summarization)、HyDE和向量數(shù)據(jù)庫的文本生成
- 代理(Agents)
- 基礎知識
- 代理類型
- 自定義代理(Custom Agent)
- 自定義MRKL代理
- 帶有ChatModel的LLM聊天自定義代理和自定義多操作代理(Custom MultiAction Agent)
- 工具
- 基礎知識
- 自定義工具(Custom Tools)
- 多輸入工具和工具輸入模式
- 人工確認工具驗證和Tools作為OpenAI函數(shù)
- 工具包(Toolkit)
- 代理執(zhí)行器(Agent Executor)
- 結合使用Agent和VectorStore
- 使用Agents的異步API和創(chuàng)建ChatGPT克隆
- 處理解析錯誤、訪問中間步驟和限制最大迭代次數(shù)
- 為代理程序設置超時時間和限制最大迭代次數(shù)和為代理程序和其工具添加共享內存
- 計劃與執(zhí)行
- 回調函數(shù)(Callbacks)
使用少量示例
本部分的內容介紹了如何在聊天模型(Chat Models)中使用少量示例。關于如何最好地進行少量示例提示尚未形成明確的共識。因此,我們尚未固定任何關于此的抽象概念,而是使用現(xiàn)有的抽象概念。
交替的人工智能/人類消息
進行少量示例提示的第一種方式是使用交替的人工智能/人類消息。以下是一個示例:
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate, LLMChain
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
AIMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.schema import (
AIMessage,
HumanMessage,
SystemMessage
)
chat = ChatOpenAI(temperature=0)
template="You are a helpful assistant that translates english to pirate."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
example_human = HumanMessagePromptTemplate.from_template("Hi")
example_ai = AIMessagePromptTemplate.from_template("Argh me mateys")
human_template="{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, example_human, example_ai, human_message_prompt])
chain = LLMChain(llm=chat, prompt=chat_prompt)
# 從格式化的消息中獲取聊天完成結果
chain.run("I love programming.")
輸出:
"I be lovin' programmin', me hearty!"
系統(tǒng)消息
OpenAI提供了一個可選的name
參數(shù),我們也建議與系統(tǒng)消息一起使用以進行少量示例提示。以下是如何使用此功能的示例:
template="You are a helpful assistant that translates english to pirate."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
example_human = SystemMessagePromptTemplate.from_template("Hi", additional_kwargs={"name": "example_user"})
example_ai = SystemMessagePromptTemplate.from_template("Argh me mateys", additional_kwargs={"name": "example_assistant"})
human_template="{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, example_human, example_ai, human_message_prompt])
chain = LLMChain(llm=chat, prompt=chat_prompt)
# 從格式化的消息中獲取聊天完成結果
chain.run("I love programming.")
輸出:
"I be lovin' programmin', me hearty!"
響應流式傳輸
本部分介紹了如何在聊天模型中使用流式傳輸:
from langchain.chat_models import ChatOpenAI
from langchain.schema import (
HumanMessage,
)
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
chat = ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0)
resp = chat([HumanMessage(content="Write me a song about sparkling water.")])
輸出:文章來源:http://www.zghlxwxcb.cn/news/detail-605908.html
Verse 1:
Bubbles rising to the top
A refreshing drink that never stops
Clear and crisp, it's pure delight
A taste that's sure to excite
Chorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibe
Verse 2:
No sugar, no calories, just pure bliss
A drink that's hard to resist
It's the perfect way to quench my thirst
A drink that always comes first
Chorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibe
Bridge:
From the mountains to the sea
Sparkling water, you're the key
To a healthy life, a happy soul
A drink that makes me feel whole
Chorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibe
Outro:
Sparkling water, you're the one
A drink that's always so much fun
I'll never let you go, my friend
Sparkling
參考文獻:
[1] LangChain ????? 中文網,跟著LangChain一起學LLM/GPT開發(fā):https://www.langchain.com.cn/
[2] LangChain中文網 - LangChain 是一個用于開發(fā)由語言模型驅動的應用程序的框架:http://www.cnlangchain.com/文章來源地址http://www.zghlxwxcb.cn/news/detail-605908.html
到了這里,關于自然語言處理從入門到應用——LangChain:模型(Models)-[聊天模型(Chat Models):使用少量示例和響應流式傳輸]的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!