鎮(zhèn)海官方網(wǎng)站建設(shè)網(wǎng)絡(luò)銷售怎么做才能有業(yè)務(wù)
分類目錄:《自然語言處理從入門到應(yīng)用》總目錄
使用少量示例
本部分的內(nèi)容介紹了如何在聊天模型(Chat Models)中使用少量示例。關(guān)于如何最好地進(jìn)行少量示例提示尚未形成明確的共識(shí)。因此,我們尚未固定任何關(guān)于此的抽象概念,而是使用現(xiàn)有的抽象概念。
交替的人工智能/人類消息
進(jìn)行少量示例提示的第一種方式是使用交替的人工智能/人類消息。以下是一個(gè)示例:
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)# 從格式化的消息中獲取聊天完成結(jié)果
chain.run("I love programming.")
輸出:
"I be lovin' programmin', me hearty!"
系統(tǒng)消息
OpenAI提供了一個(gè)可選的name
參數(shù),我們也建議與系統(tǒng)消息一起使用以進(jìn)行少量示例提示。以下是如何使用此功能的示例:
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)# 從格式化的消息中獲取聊天完成結(jié)果
chain.run("I love programming.")
輸出:
"I be lovin' programmin', me hearty!"
響應(yīng)流式傳輸
本部分介紹了如何在聊天模型中使用流式傳輸:
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.")])
輸出:
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 exciteChorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibeVerse 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 firstChorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibeBridge:
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 wholeChorus:
Sparkling water, oh so fine
A drink that's always on my mind
With every sip, I feel alive
Sparkling water, you're my vibeOutro:
Sparkling water, you're the one
A drink that's always so much fun
I'll never let you go, my friend
Sparkling
參考文獻(xiàn):
[1] LangChain 🦜?🔗 中文網(wǎng),跟著LangChain一起學(xué)LLM/GPT開發(fā):https://www.langchain.com.cn/
[2] LangChain中文網(wǎng) - LangChain 是一個(gè)用于開發(fā)由語言模型驅(qū)動(dòng)的應(yīng)用程序的框架:http://www.cnlangchain.com/