LangGraph是LangChain、LangServe和LangSmith系列的最新成員,旨在使用LLM構建生成式人工智能應用程序。請記住,所有這些都是獨立的包,必須單獨進行pip安裝。
在深入學習LangGraph之前,需要了解LangChain的兩個主要概念。
1. 鏈:圍繞LLM編寫的程序,用于執行任務,例如自動SQL編寫或NER提取鏈等。請注意,鏈不能用于任何其他任務(甚至不能用于一般用例),如果嘗試這樣做,可能會損壞鏈。鏈中要遵循的步驟是預定義的,不可靈活調整。
2. 代理:鏈的更加靈活版本,代理通常是啟用第三方工具(例如谷歌搜索、YouTube)的LLM,由LLM本身決定下一步如何解決給定的查詢。
現在,當處理現實世界的問題時,一個常見的問題是希望找到介于鏈和代理之間的解決方案。即不像鏈那樣硬編碼,但也不像代理那樣完全由LLM驅動。
LangGraph是以LangChain為核心,用于創建工作流程中的循環圖的工具。因此,我們假設以下示例:
你希望在知識庫上搭建一個基于RAG的檢索系統。現在,你希望引入這樣一種情況:如果RAG的輸出未滿足特定質量要求,代理/鏈應該再次檢索數據,但這次是自行更改提示。并且重復此過程,直到達到質量閾值為止。
使用LangGraph可以實現這種循環邏輯。這只是一個示例,使用LangGraph還可以做更多事情。
注:可以將其視為向鏈中引入循環邏輯,使其成為循環鏈。
顧名思義,LangGraph具有一般圖形所具有的所有組件,例如節點、邊等,接下來通過一個示例來了解。
在此示例中,希望將RAG系統在數據庫中的最終輸出減少到不超過30個字符。如果輸出長度大于30個字符,則希望引入循環,使用不同的提示再次嘗試,直到長度小于30個字符為止。這是一個演示目的的基本邏輯。你甚至可以實現復雜的邏輯來改善RAG結果。
我們將創建的圖形如下所示。
圖片
此處使用的版本為 langchain===0.0.349, openai===1.3.8, langgraph===0.0.26。
from typing import Dict, TypedDict, Optionalfrom langgraph.graph import StateGraph, ENDfrom langchain.vectorstores import Chromafrom langchain.chains import RetrievalQAfrom langchain.llms import OpenAIfrom langchain.embeddings.openai import OpenAIEmbeddingsllm = OpenAI(openai_api_key='your API')
接下來,我們將定義一個StateGraph。
class GraphState(TypedDict): question: Optional[str] = None classification: Optional[str] = None response: Optional[str] = None length: Optional[int] = None greeting: Optional[str] = Noneworkflow = StateGraph(GraphState)
什么是StateGraph?
StateGraph是任何LangGraph流程的核心,它存儲了在執行工作流程時我們將存儲的各種變量的狀態。在本例中,我們有5個變量,其值在執行圖形時將進行更新,并將與所有邊和節點共享。
def retriever_qa_creation(): embeddings = OpenAIEmbeddings() db = Chroma(embedding_functinotallow=embeddings,persist_directory='/database',collection_name='details') qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=db.as_retriever()) return qarag_chain = retriever_qa_creation()
def classify(question): return llm("classify intent of given input as greeting or not_greeting. Output just the class.Input:{}".format(question)).strip()def classify_input_node(state): question = state.get('question', '').strip() classification = classify(question) return {"classification": classification}def handle_greeting_node(state): return {"greeting": "Hello! How can I help you today?"}def handle_RAG(state): question = state.get('question', '').strip() prompt = question if state.get("length")<30: search_result = rag_chain.run(prompt) else: search_result = rag_chain.run(prompt+'. Return total count only.') return {"response": search_result,"length":len(search_result)}def bye(state): return{"greeting":"The graph has finished"}workflow.add_node("classify_input", classify_input_node)workflow.add_node("handle_greeting", handle_greeting_node)workflow.add_node("handle_RAG", handle_RAG)workflow.add_node("bye", bye)
這需要一些解釋。
①讀取任何狀態變量。
②更新任何狀態變量。在這種情況下,每個節點的返回函數都會更新某個或某些狀態變量的狀態/值。
workflow.set_entry_point("classify_input")workflow.add_edge('handle_greeting', END)workflow.add_edge('bye', END)
在上述的代碼片段中,
def decide_next_node(state): return "handle_greeting" if state.get('classification') == "greeting" else "handle_RAG"def check_RAG_length(state): return "handle_RAG" if state.get("length")>30 else "bye"workflow.add_conditional_edges( "classify_input", decide_next_node, { "handle_greeting": "handle_greeting", "handle_RAG": "handle_RAG" })workflow.add_conditional_edges( "handle_RAG", check_RAG_length, { "bye": "bye", "handle_RAG": "handle_RAG" })
條件邊界可根據條件(例如if-else)在兩個節點之間進行選擇。在創建的兩個條件邊界中:
第一個條件邊界
當遇到classify_input時,根據decide_next_node函數的輸出選擇handle_greeting或handle_RAG。
第二個條件邊界
如果遇到handle_RAG,則根據check_RAG_length條件選擇handle_RAG或bye。
app = workflow.compile()app.invoke({'question':'Mehul developed which projects?','length':0})
# 輸出{'question': 'Mehul developed which projects?', 'classification': 'not_greeting', 'response': ' 4', 'length': 2, 'greeting': 'The graph has finished'}
對于上述提示,圖形流程如下所示:
classify_input: 情感將為not_greeting。
由于第一個條件邊界,移至handle_RAG。
由于length=0,使用第一個提示并檢索答案(總長度將大于30)。
由于第二個條件邊界,再次移至handle_RAG。
由于length>30,使用第二個提示符。
由于第二個條件邊界,移至bye。
END。
如果沒有使用LangGraph:
rag_chain.run("Mehul developed which projects?")# 輸出"Mehul developed projects like ABC, XYZ, QWERTY. Not only these, he has major contribution in many other projects as well at OOO organization"
app.invoke({'question':'Hello bot','length':0})# 輸出{'question': 'Hello bot', 'classification': 'greeting', 'response': None, 'length': 0, 'greeting': 'Hello! How can I help you today?'}
這里的流程會更簡單。
classify_input: 情感將為greeting。
由于第一個條件邊界,移至handle_greeting。
END。
雖然我在這里應用的條件相當簡單,但通過添加更復雜的條件,這個框架可以很容易地用于改進你的結果。
本文鏈接:http://www.www897cc.com/showinfo-26-79462-0.html使用LangChain和LangGraph大幅提升RAG效果
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com