基于任何數(shù)據(jù)集創(chuàng)建LLM(Large Language Models)機(jī)器人
今天偶然翻到一個(gè)倉(cāng)庫(kù) Embedchain,覺得很實(shí)用,分享給大家。倉(cāng)庫(kù)地址如下:
https://github.com/embedchain/embedchain
它是基于 OpenAI 的,但是你可以添加自己的數(shù)據(jù)集,然后生成一個(gè)對(duì)話機(jī)器人,使用方法簡(jiǎn)單,很容易上手。
Embedchain 簡(jiǎn)介
Embedchain 是一個(gè)可以方便地基于任何數(shù)據(jù)集創(chuàng)建 LLM(Large Language Models)機(jī)器人的框架。它抽象了加載數(shù)據(jù)集、分塊、創(chuàng)建嵌入向量以及存儲(chǔ)在向量數(shù)據(jù)庫(kù)中的整個(gè)過程。你可以使用 .add 和 .add_local 函數(shù)添加單個(gè)或多個(gè)數(shù)據(jù)集,然后使用 .query 函數(shù)從添加的數(shù)據(jù)集中查找答案。
假如你崇拜一個(gè)很厲害的人 - Naval Ravikant,你想把他的知識(shí)做成一個(gè)對(duì)話機(jī)器人,你可以把他的 Youtube 視頻、PDF 書籍、博客文章,以及你提供的一個(gè)問題和答案對(duì),添加到 Embedchain,Embedchain 將為你創(chuàng)建一個(gè)機(jī)器人。這是一個(gè)例子:
from embedchain import App
naval_chat_bot = App()
# 嵌入在線資源
naval_chat_bot.add("youtube_video", "https://www.youtube.com/watch?v=3qHkcs3kG44")
naval_chat_bot.add("pdf_file", "https://navalmanack.s3.amazonaws.com/Eric-Jorgenson_The-Almanack-of-Naval-Ravikant_Final.pdf")
naval_chat_bot.add("web_page", "https://nav.al/feedback")
naval_chat_bot.add("web_page", "https://nav.al/agi")
# 嵌入本地資源
naval_chat_bot.add_local("qna_pair", ("Who is Naval Ravikant?", "Naval Ravikant is an Indian-American entrepreneur and investor."))
naval_chat_bot.query("What unique capacity does Naval argue humans possess when it comes to understanding explanations or concepts?")
# 答案:Naval 認(rèn)為,人類在理解解釋或概念方面擁有獨(dú)特的能力,這是在這個(gè)物理現(xiàn)實(shí)中可能的最大程度。
Embedchain 使用
要開始使用 Embedchain,首先確保你已經(jīng)安裝了該包。如果還沒有安裝,可以使用 pip 進(jìn)行安裝:
pip install embedchain
Embedchain 使用 OpenAI 的嵌入模型創(chuàng)建塊的嵌入,使用 ChatGPT API 作為 LLM,給出相關(guān)文檔的答案。確保你有一個(gè) OpenAI 帳戶和 API 密鑰。如果你沒有 API 密鑰,可以通過訪問 此鏈接 [1] 創(chuàng)建一個(gè)。
一旦你有了 API 密鑰,將其設(shè)置在一個(gè)名為 OPENAI_API_KEY 的環(huán)境變量中
import os
os.environ["OPENAI_API_KEY"] = "sk-xxxx"
接下來,從 embedchain 中導(dǎo)入 App 類并使用 .add 函數(shù)添加任何數(shù)據(jù)集。
from embedchain import App
naval_chat_bot = App()
# 嵌入在線資源
naval_chat_bot.add("youtube_video", "https://www.youtube.com/watch?v=3qHkcs3kG44")
naval_chat_bot.add("pdf_file", "https://navalmanack.s3.amazonaws.com/Eric-Jorgenson_The-Almanack-of-Naval-Ravikant_Final.pdf")
naval_chat_bot.add("web_page", "https://nav.al/feedback")
naval_chat_bot.add("web_page", "https://nav.al/agi")
# 嵌入本地資源
naval_chat_bot.add_local("qna_pair", ("Who is Naval Ravikant?", "Naval Ravikant is an Indian-American entrepreneur and investor."))
如果在你的腳本或應(yīng)用中有任何其他的應(yīng)用實(shí)例,你可以更改導(dǎo)入如下
from embedchain import App as EmbedChainApp
# 或者
from embedchain import App as ECApp
現(xiàn)在你的應(yīng)用已經(jīng)創(chuàng)建好了。可以使用 .query 函數(shù)獲得任何查詢的答案。
print(naval_chat_bot.query("What unique capacity does Naval argue humans possess when it comes to understanding explanations or concepts?"))
# answer: Naval argues that humans possess the unique capacity to understand explanations or concepts to the maximum extent possible in this physical reality.
支持的格式
支持以下格式:
Youtube 視頻
要將任何 Youtube 視頻添加到你的應(yīng)用中,使用數(shù)據(jù)類型(.add 的第一個(gè)參數(shù))為 youtube_video。例如:
app.add('youtube_video', 'a_valid_youtube_url_here')
PDF 文件
要添加任何 PDF 文件,使用數(shù)據(jù)類型為 pdf_file。例如:
app.add('pdf_file', 'a_valid_url_where_pdf_file_can_be_accessed')
注意,不支持密碼保護(hù)的 PDF。
網(wǎng)頁
要添加任何網(wǎng)頁,使用數(shù)據(jù)類型為 web_page。例如:
app.add('web_page', 'a_valid_web_page_url')
文本
要提供你自己的文本,使用數(shù)據(jù)類型為 text 并輸入一個(gè)字符串。文本不會(huì)被處理,這可以非常多樣化。例如:
app.add_local('text', 'Seek wealth, not money or status. Wealth is having assets that earn while you sleep. Money is how we transfer time and wealth. Status is your place in the social hierarchy.')
注意:這在示例中沒有使用,因?yàn)樵诖蠖鄶?shù)情況下,你將提供整個(gè)段落或文件。