開始使用免費開始

Runnable lambda

RunnableLambda 類別是在混合式擷取鏈(hybrid retrieval chain)中很重要的元件。在你用它來執行向量擷取器之前,先練習把一個 Python 函式轉換成 LCEL 的 runnable。

使用者自訂的 reverse() 函式會接收一個字串,並回傳其反轉結果。請更新程式碼,讓它回傳輸入字串的反轉。

本練習屬於課程

使用 LangChain 與 Neo4j 的 Graph RAG

檢視課程

練習說明

  • reverse() 函式轉換成 LCEL 的 runnable。
  • 呼叫 reverse_chain runnable,將提供的字串反轉。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

def reverse(input):
    return input[::-1]

# Convert reverse() into a runnable
reverse_chain = ____

# Reverse the input
output = reverse_chain.____("Hello, world!")
print(output)
編輯並執行程式碼