本文將詳細(xì)介紹LangChain的輸出解析器OutputParser的使用方法,以及如何基于LangChain的LCEL構(gòu)建鏈。我們將通過實例來展示這些功能,幫助您更好地理解和使用LangChain。
一、輸出解析器OutputParser
1.1 為什么需要OutputParser
在使用LangChain構(gòu)建LLM應(yīng)用的常規(guī)流程中,我們需要進行Prompt輸入、調(diào)用LLM、以及處理LLM的輸出。有時候,我們期望LLM給到的數(shù)據(jù)是格式化的數(shù)據(jù),這樣可以方便我們進行后續(xù)的處理。
為了實現(xiàn)這個目標(biāo),我們需要在Prompt里設(shè)置好要求,然后LLM會在輸出內(nèi)容后,再將內(nèi)容傳給輸出解析器,輸出解析器會將內(nèi)容解析成我們預(yù)期的格式。
1.2 代碼實踐
1.2.1 調(diào)用系統(tǒng)自帶的輸出解析器
我們可以使用系統(tǒng)自帶的輸出解析器來解析LLM的結(jié)果。例如,我們可以將LLM的結(jié)果解析為逗號分隔的列表。以下是一個示例,它詢問某個城市有N個景點。
from langchain_openai import ChatOpenAI from langchain.output_parsers import CommaSeparatedListOutputParser from langchain.prompts import ChatPromptTemplate prompt = ChatPromptTemplate.from_messages([ ("system", "{parser_instructions}"), ("human", "列出{cityName}的{viewPointNum}個著名景點。") ]) output_parser = CommaSeparatedListOutputParser() parser_instructions = output_parser.get_format_instructions() # 查看解析器的指令內(nèi)容 print(parser_instructions) final_prompt = prompt.invoke({"cityName": "南京", "viewPointNum": 3, "parser_instructions": parser_instructions}) model = ChatOpenAI(model="gpt-3.5-turbo", openai_api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", openai_api_base="https://api.aigc369.com/v1") response = model.invoke(final_prompt) print(response.content) ret = output_parser.invoke(response) print(ret)
1.2.2 自定義格式的輸出解析器
除了使用系統(tǒng)自帶的輸出格式,我們還可以使用自定義的輸出格式。使用步驟如下:
定義數(shù)據(jù)結(jié)構(gòu)類,繼承pydantic的BaseModel
使用輸出解析器PydanticOutputParser
后續(xù)是常規(guī)操作:生成prompt、調(diào)用LLM執(zhí)行、將輸出按照Parser解析
以下是一個示例,它給LLM一段書籍的介紹,讓他按照指定的格式總結(jié)輸出。
from typing import List from langchain.output_parsers import PydanticOutputParser from langchain.prompts import ChatPromptTemplate from langchain.schema import HumanMessage from langchain_core.pydantic_v1 import BaseModel, Field from langchain_openai import ChatOpenAI class BookInfo(BaseModel): book_name: str = Field(description="書籍的名字") author_name: str = Field(description="書籍的作者") genres: List[str] = Field(description="書籍的體裁") output_parser = PydanticOutputParser(pydantic_object=BookInfo) # 查看輸出解析器的內(nèi)容,會被輸出成json格式 print(output_parser.get_format_instructions()) prompt = ChatPromptTemplate.from_messages([ ("system", "{parser_instructions} 你輸出的結(jié)果請使用中文。"), ("human", "請你幫我從書籍的概述中,提取書名、作者,以及書籍的體裁。書籍概述會被三個#符號包圍。\n###{book_introduction}###") ]) book_introduction = """ 《朝花夕拾》原名《舊事重提》,是現(xiàn)代文學(xué)家魯迅的散文集,收錄魯迅于1926年創(chuàng)作的10篇回憶性散文, [1]1928年由北京未名社出版,現(xiàn)編入《魯迅全集》第2卷。 此文集作為“回憶的記事”,多側(cè)面地反映了作者魯迅青少年時期的生活,形象地反映了他的性格和志趣的形成經(jīng)過。前七篇反映他童年時代在紹興的家庭和私塾中的生活情景,后三篇敘述他從家鄉(xiāng)到南京,又到日本留學(xué),然后回國教書的經(jīng)歷;揭露了半殖民地半封建社會種種丑惡的不合理現(xiàn)象,同時反映了有抱負(fù)的青年知識分子在舊中國茫茫黑夜中,不畏艱險,尋找光明的困難歷程,以及抒發(fā)了作者對往日親友、師長的懷念之情 [2]。 文集以記事為主,飽含著濃烈的抒情氣息,往往又夾以議論,做到了抒情、敘事和議論融為一體,優(yōu)美和諧,樸實感人。作品富有詩情畫意,又不時穿插著幽默和諷喻;形象生動,格調(diào)明朗,有強烈的感染力。 """ model = ChatOpenAI(model="gpt-3.5-turbo", openai_api_key="sk-BuQK7SGbqCZP2i2z7fF267AeD0004eF095AbC78d2f79E019", openai_api_base="https://api.aigc369.com/v1") final_prompt = prompt.invoke({"book_introduction": book_introduction, "parser_instructions": output_parser.get_format_instructions()}) response = model.invoke(final_prompt) print(response.content) result = output_parser.invoke(response) print(result)
二、利用LCEL構(gòu)建鏈
2.1 LCEL是啥
LCEL是LangChain 表達(dá)式語言(LangChain Expression Language)的簡稱。使用LCEL可以快速將各種鏈組合到一起,那鏈又是啥呢?
在LangChain里,只要一個類實現(xiàn)了Runnable接口,并且有invoke方法,都可以成為鏈。實現(xiàn)了Runnable接口的類,可以拿上一個鏈的輸出作為自己的輸入。
2.2 使用區(qū)別
2.2.1 不使用LCEL
如果我們不使用LCEL,代碼寫起來可能會比較繁瑣,invoke方法會滿天飛。比如這樣:
final_prompt = prompt.invoke({"book_introduction": book_introduction, "parser_instructions": output_parser.get_format_instructions()}) response = model.invoke(final_prompt) result = output_parser.invoke(response)
2.2.2 使用LCEL
如果我們使用LCEL,代碼會變得更加簡潔,表達(dá)力也會更強。比如這樣:文章來源:http://www.zghlxwxcb.cn/article/775.html
chain = prompt | model | output_parser ret = chain.invoke({"book_introduction": book_introduction, "parser_instructions": output_parser.get_format_instructions()})
文章來源地址http://www.zghlxwxcb.cn/article/775.html
到此這篇關(guān)于LangChain輸出解析器與鏈的深度解析的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!