最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

簡介

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

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

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

好的prompt

有時候,不是我們使用的語言模型不夠好,而是因為我們寫的prompt不夠優(yōu)秀。

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

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

因為很多時候,在類似的場景中,我們的prompts的大體結(jié)構(gòu)是一樣的,只有具體的細(xì)節(jié)描述有所不同,這時候,就需要用到prompt template.

什么是prompt template

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

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

下面是一個prompt template的簡單例子:

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

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

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

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

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

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

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

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

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

template = "請告訴我一個關(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ù)可以接受兩個參數(shù):input_variables和template。

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

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

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

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

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

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

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

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

Chat特有的prompt template

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

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

這樣做雖然復(fù)雜了一點,但是可以更好的對消息進(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"],
)

有了一個或者多個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)文章

最新評論

襄垣县| 五大连池市| 郧西县| 和林格尔县| 淮安市| 景德镇市| 垫江县| 堆龙德庆县| 高淳县| 界首市| 克山县| 佳木斯市| 兴城市| 佛教| 波密县| 晋宁县| 苍溪县| 伊川县| 福建省| 武夷山市| 阿拉善盟| 胶州市| 二手房| 西峡县| 固安县| 中宁县| 衡山县| 前郭尔| 襄城县| 历史| 枣庄市| 凯里市| 图木舒克市| 儋州市| 凌海市| 福泉市| 板桥市| 青岛市| 苍溪县| 宜君县| 云林县|