亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

langchain Prompt大語言模型使用技巧詳解

 更新時(shí)間:2023年07月12日 10:48:23   作者:flydean  
這篇文章主要為大家介紹了langchain Prompt大語言模型使用技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

簡介

prompts是大語言模型的輸入,他是基于大語言模型應(yīng)用的利器。沒有差的大語言模型,只有差的prompts。

寫好prompts才能發(fā)揮大語言模型300%的功力。

理論上,要寫好prompts其實(shí)不是那么容易的,但是langchain把這個(gè)理論變成了現(xiàn)實(shí),一起來看看吧。

好的prompt

有時(shí)候,不是我們使用的語言模型不夠好,而是因?yàn)槲覀儗懙膒rompt不夠優(yōu)秀。

以下是一些寫好大語言模型的prompts的幾條原則:

  • 具體和詳細(xì):prompts應(yīng)該具有明確的問題或任務(wù),同時(shí)包含足夠的細(xì)節(jié)和背景信息,以便大語言模型能夠理解和回答。
  • 可理解和可回答:prompts應(yīng)該明確清晰,讓大語言模型能夠理解并且回答。避免使用過于抽象、模糊或帶有攻擊性的語言。
  • 有情境和背景:prompts應(yīng)該包含足夠的情境和背景信息,讓大語言模型能夠理解問題的重要性和意義,并在回答中提供有意義的信息。
  • 有目標(biāo)和方向:prompts應(yīng)該明確問題或任務(wù)的目標(biāo)和方向,以便大語言模型能夠?yàn)樾枰男畔⑻峁┣逦陀杏玫拇鸢浮?/li>
  • 可擴(kuò)展和可定制:prompts應(yīng)該設(shè)計(jì)成易于擴(kuò)展和定制,以適應(yīng)不同的應(yīng)用場景和用戶需求。

因?yàn)楹芏鄷r(shí)候,在類似的場景中,我們的prompts的大體結(jié)構(gòu)是一樣的,只有具體的細(xì)節(jié)描述有所不同,這時(shí)候,就需要用到prompt template.

什么是prompt template

prompt template就是一個(gè)prompt的模板,通過prompt template,我們可以快速的生成多個(gè)prompt。

基本上prompt template已經(jīng)幫我們描述好了場景,要做的事情。我們只需要填入具體的內(nèi)容即可。

下面是一個(gè)prompt template的簡單例子:

from langchain import PromptTemplate
template = """/
假如你是一個(gè)金融公司的理財(cái)經(jīng)理,請你分析一下{stock}這只股票。
"""
prompt = PromptTemplate.from_template(template)
prompt.format(stock="騰訊控股")
假如你是一個(gè)金融公司的理財(cái)經(jīng)理,請你分析一下騰訊控股這只股票。

這樣,對于用戶來說,只需要輸入需要問詢的股票名稱即可。其他的一長串文字就不需要了,大大節(jié)省了prompt構(gòu)建的時(shí)間。

當(dāng)然,這只是一個(gè)非常簡單的例子,你還可以在prompt template中設(shè)置回答的格式,提供具體的例子等等,從而得到更好的回復(fù)。

在langchain中創(chuàng)建prompt template

簡單點(diǎn)說prompt template就是一個(gè)格式化輸入的東西。在langchain中,對應(yīng)的工具類叫做PromptTemplate。

上面的簡單例子中,我們已經(jīng)大體看到了如何使用PromptTemplate。

在上例中,我們調(diào)用了PromptTemplate.from_template方法,傳入了一個(gè)template的字符串。

在template的字符串中,我們用括號定義了一個(gè)變量。最后調(diào)用prompt.format方法,指定變量的名稱和值,完成prompt的最終創(chuàng)建。

另外,prompt template中還可以指定多個(gè)變量:

template = "請告訴我一個(gè)關(guān)于{personA}的{thingsB}"
prompt_template = PromptTemplate.from_template(template)
prompt_template.format(personA="小張", thingsB="故事")

只需要在format中指定變量名稱即可。

除了是用PromptTemplate.from_template方法之外,我們還可以直接使用PromptTemplate的構(gòu)造函數(shù)來創(chuàng)建prompt。

PromptTemplate的構(gòu)造函數(shù)可以接受兩個(gè)參數(shù):input_variables和template。

input_variables是template中的變量名字,它是一個(gè)數(shù)組。

template就是模板的具體內(nèi)容,是個(gè)字符串。

比如,我們可以構(gòu)造無變量的模板:

no_input_prompt = PromptTemplate(input_variables=[], template="這是一個(gè)無參數(shù)模板。")
no_input_prompt.format()

我們還可以構(gòu)造帶參數(shù)模板:

one_input_prompt = PromptTemplate(input_variables=["stock"], template="假如你是一個(gè)金融公司的理財(cái)經(jīng)理,請你分析一下{stock}這只股票。")
one_input_prompt.format(stock="騰訊控股")

還有多個(gè)參數(shù)的模板:

multiple_input_prompt = PromptTemplate(
    input_variables=["personA", "thingsB"], 
    template="請告訴我一個(gè)關(guān)于{personA}的{thingsB}"
)
multiple_input_prompt.format(personA="小張", thingsB="故事")

Chat特有的prompt template

之前在介紹langchain的時(shí)候有跟大家提到過,chat雖然是基于LLM的,但是和基本的LLM還有有區(qū)別的。

最主要的區(qū)別在于,chat消息是不同角色的。比如在openai中,chat消息就可以被分為AI, human或者system這幾種角色。

這樣做雖然復(fù)雜了一點(diǎn),但是可以更好的對消息進(jìn)行分類處理。

我們看下langchain中關(guān)于chat的PromptTemplate有哪些:

from langchain.prompts import (
    ChatPromptTemplate,
    PromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)

和普通的prompt template一樣,我們可以調(diào)用MessagePromptTemplate的from_template來創(chuàng)建對應(yīng)的prompt:

template="現(xiàn)在你的角色是{role},請按該角色進(jìn)行后續(xù)的對話."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template="{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

當(dāng)然你可以通過構(gòu)造函數(shù)來創(chuàng)建prompt:

prompt=PromptTemplate(
    template="現(xiàn)在你的角色是{role},請按該角色進(jìn)行后續(xù)的對話.",
    input_variables=["role"],
)

有了一個(gè)或者多個(gè)MessagePromptTemplates之后,就可以使用這些MessagePromptTemplates來構(gòu)建ChatPromptTemplate了:

chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
chat_prompt.format_prompt(role="醫(yī)生", text="幫我看看我的顏值還行嗎?").to_messages()

總結(jié)

好了, 基本的langchain中的prompt template已經(jīng)介紹完畢。大家去試試看吧。

以上就是langchain Prompt大語言模型使用技巧詳解的詳細(xì)內(nèi)容,更多關(guān)于langchain Prompt大語言模型的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論