使用Ollama和Go基于文本嵌入模型實(shí)現(xiàn)文本向量化
基于RAG+大模型的應(yīng)用已經(jīng)成為當(dāng)前AI應(yīng)用領(lǐng)域的一個(gè)熱門(mén)方向。RAG(Retrieval-Augmented Generation)將檢索和生成兩個(gè)步驟相結(jié)合,利用外部知識(shí)庫(kù)來(lái)增強(qiáng)生成模型的能力(如下圖來(lái)自網(wǎng)絡(luò))。
圖片
在RAG賦能的大模型應(yīng)用中,關(guān)鍵的一步是將文本數(shù)據(jù)向量化后存儲(chǔ)在向量數(shù)據(jù)庫(kù)中(如上圖的紅框),以實(shí)現(xiàn)快速的相似度搜索,從而檢索與輸入查詢相關(guān)的文本片段,再將檢索到的文本輸入給生成模型生成最終結(jié)果。
本文是我學(xué)習(xí)開(kāi)發(fā)大模型應(yīng)用的一篇小記,探討的是如何使用Ollama和Go語(yǔ)言實(shí)現(xiàn)文本數(shù)據(jù)的向量化處理,這是開(kāi)發(fā)基于RAG的大模型應(yīng)用的前提和基礎(chǔ)。
要進(jìn)行文本向量化,我們首先要了解一下文本向量化的方法以及發(fā)展。
縱觀文本向量化技術(shù)的發(fā)展歷程,我們可以看到從早期的詞袋模型(Bag-of-Words)、主題模型(Topic Models),到詞嵌入(Word Embedding)、句嵌入(Sentence Embedding),再到當(dāng)前基于預(yù)訓(xùn)練的文本嵌入模型(Pretrained Text Embedding Models),文本向量化的方法不斷演進(jìn),語(yǔ)義表達(dá)能力也越來(lái)越強(qiáng)。
但傳統(tǒng)的詞袋模型忽略了詞序和語(yǔ)義,主題模型又難以捕捉詞間的細(xì)粒度關(guān)系,詞嵌入模型(如Word2Vec、GloVe)雖然考慮了詞的上下文,但無(wú)法很好地表征整個(gè)句子或文檔的語(yǔ)義。近年來(lái),隨著預(yù)訓(xùn)練語(yǔ)言模型(如BERT、GPT等)的崛起,出現(xiàn)了一系列強(qiáng)大的文本嵌入模型,它們?cè)诖笠?guī)模語(yǔ)料上進(jìn)行預(yù)訓(xùn)練,能夠生成高質(zhì)量的句子/文檔嵌入向量,廣泛應(yīng)用于各類(lèi)NLP任務(wù)中。下圖是抱抱臉(https://huggingface.co/)的最新文本嵌入模型的排行榜[1]:
圖片
目前,基于大型預(yù)訓(xùn)練語(yǔ)言模型的文本嵌入已成為主流方法。這些模型在大規(guī)模無(wú)監(jiān)督語(yǔ)料上預(yù)訓(xùn)練,學(xué)習(xí)到豐富的語(yǔ)義知識(shí),生成的文本嵌入能較好地編碼詞語(yǔ)、短語(yǔ)和句子等多個(gè)層面的語(yǔ)義關(guān)系。Nomic AI[2]等組織發(fā)布了多種優(yōu)秀的預(yù)訓(xùn)練文本嵌入模型,應(yīng)用效果獲得了較大提升。這種基于預(yù)訓(xùn)練的文本嵌入模型來(lái)實(shí)現(xiàn)文本數(shù)據(jù)向量化的方法也緩解了Go語(yǔ)言生態(tài)中文本向量化的相關(guān)庫(kù)相對(duì)較少的尷尬,Gopher可以在預(yù)訓(xùn)練文本嵌入模型的幫助下將文本向量化。接下來(lái),我們就來(lái)看看如何基于Ollama和Go基于文本嵌入模型實(shí)現(xiàn)文本向量化。
考慮到實(shí)驗(yàn)環(huán)境資源有限,以及Ollama對(duì)Text Embedding模型的支持[3],這里我選擇了Nomic AI開(kāi)源發(fā)布的nomic-embed-text v1.5模型[4],雖然在抱抱臉上它的排名并不十分靠前。
下面我們就用ollama下載nomic-embed-text:v1.5模型:
$ollama pull nomic-embed-text:v1.5
pulling manifest
pulling manifest
pulling 970aa74c0a90... 100% ▕██████████████████████████████████████████████████████████████████▏ 274 MB
pulling c71d239df917... 100% ▕██████████████████████████████████████████████████████████████████▏ 11 KB
pulling ce4a164fc046... 100% ▕██████████████████████████████████████████████████████████████████▏ 17 B
pulling 31df23ea7daa... 100% ▕██████████████████████████████████████████████████████████████████▏ 420 B
verifying sha256 digest
writing manifest
removing any unused layers
success
算上之前的Llama3模型,目前本地已經(jīng)有了兩個(gè)模型:
$ollama list
NAME ID SIZE MODIFIED
llama3:latest 71a106a91016 4.7 GB 2 weeks ago
nomic-embed-text:v1.5 0a109f422b47 274 MB 3 seconds ago
不過(guò)與llama3的對(duì)話模型不同,nomic-embed-text:v1.5是用于本文嵌入的模型,我們不能使用命令行來(lái)run該模型并通過(guò)命令行與其交互:
$ollama run nomic-embed-text:v1.5
Error: embedding models do not support chat
一旦模型下載成功,我們就可以通過(guò)Ollama的HTTP API來(lái)訪問(wèn)該模型了,下面是通過(guò)curl將一段文本向量化的命令:
$curl http://localhost:11434/api/embeddings -d '{
"model": "nomic-embed-text:v1.5",
"prompt": "The sky is blue because of Rayleigh scattering"
}'
{"embedding":[-1.246808409690857,0.10344144701957703,0.6935597658157349,-0.6157534718513489,0.4244955778121948,-0.7677388191223145,1.4136837720870972,0.012530215084552765,0.007208258379250765,-0.858286440372467,1.02878999710083,0.6512939929962158,1.0005667209625244,1.4231345653533936,0.30222395062446594,-0.4343869090080261,-1.358498215675354,-1.0671193599700928,0.3035725951194763,-1.5876567363739014,-0.9811925888061523,-0.31766557693481445,-0.32180508971214294,0.5726669430732727,-1.4187577962875366,-0.23533311486244202,-0.3387795686721802,0.02435961365699768,-0.9517765641212463,0.4120883047580719,-0.4619484841823578,-0.6658303737640381,0.010240706615149975,0.7687620520591736,0.9147310853004456,-0.18446297943592072,1.6336615085601807,1.006791353225708,-0.7928107976913452,0.3333768844604492,-0.9133707880973816,-0.8000166416168213,-0.41302260756492615,0.32945334911346436,0.44106146693229675,-1.3581880331039429,-0.2830675542354584,-0.49363842606544495,0.20744864642620087,0.039297714829444885,-0.6562637686729431,-0.24374787509441376,-0.22294744849205017,-0.664574921131134,0.5489196181297302,1.0000559091567993,0.45487216114997864,0.5257866382598877,0.25838619470596313,0.8648120760917664,0.32076674699783325,1.79911208152771,-0.23030932247638702,0.27912014722824097,0.6304138898849487,-1.1762936115264893,0.2685599625110626,-0.6646256446838379,0.332780659198761,0.1742674708366394,-0.27117523550987244,-1.1485087871551514,0.07291799038648605,0.7712352275848389,...,]}
注意:如果curl請(qǐng)求得到的應(yīng)答是類(lèi)似{"error":"error starting the external llama server: exec: "ollama_llama_server": executable file not found in $PATH "},可以嘗試重啟Ollama服務(wù)來(lái)解決:systemctl restart ollama。
Ollama沒(méi)有提供sdk,我們就基于langchaingo[6]的ollama包訪問(wèn)ollama本地加載的nomic-embed-text:v1.5模型,實(shí)現(xiàn)文本的向量化。下面是示例的源碼:
// textembedding.go
package main
import (
"context"
"fmt"
"log"
"github.com/tmc/langchaingo/llms/ollama"
)
func main() {
llm, err := ollama.New(ollama.WithModel("nomic-embed-text:v1.5"))
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
inputText := "The sky is blue because of Rayleigh scattering"
result, err := llm.CreateEmbedding(ctx, []string{inputText})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", result)
fmt.Printf("%d\n", len(result[0]))
}
更新一下依賴:
# go mod tidy
go: finding module for package github.com/tmc/langchaingo/llms/ollama
go: toolchain upgrade needed to resolve github.com/tmc/langchaingo/llms/ollama
go: github.com/tmc/langchaingo@v0.1.9 requires go >= 1.22.0; switching to go1.22.3
go: downloading go1.22.3 (linux/amd64)
go: finding module for package github.com/tmc/langchaingo/llms/ollama
go: found github.com/tmc/langchaingo/llms/ollama in github.com/tmc/langchaingo v0.1.9
go: downloading github.com/stretchr/testify v1.9.0
go: downloading github.com/pkoukk/tiktoken-go v0.1.6
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading github.com/google/uuid v1.6.0
go: downloading github.com/dlclark/regexp2 v1.10.0
我本地的Go是1.21.4版本,但langchaingo需要1.22.0版本及以上,這里考慮向前兼容性[7],go下載了go1.22.3。
接下來(lái)運(yùn)行一下上述程序:
$go run textembedding.go
[][]float32{[]float32{-1.2468084, 0.10344145, 0.69355977, -0.6157535, 0.42449558, -0.7677388, 1.4136838, 0.012530215, 0.0072082584, -0.85828644, 1.02879, 0.651294, 1.0005667, 1.4231346, 0.30222395, -0.4343869, -1.3584982, -1.0671194, 0.3035726, -1.5876567, -0.9811926, -0.31766558, -0.3218051, 0.57266694, -1.4187578, -0.23533311, -0.33877957, 0.024359614, -0.95177656, 0.4120883, -0.46194848, -0.6658304, 0.010240707, 0.76876205, 0.9147311, -0.18446298, 1.6336615, 1.0067914, -0.7928108, 0.33337688, -0.9133708, -0.80001664, -0.4130226, 0.32945335, 0.44106147, -1.358188, -0.28306755, -0.49363843, 0.20744865, 0.039297715, -0.65626377, -0.24374788, -0.22294745, -0.6645749, 0.5489196, 1.0000559, 0.45487216, 0.52578664, 0.2583862, 0.8648121, 0.32076675, 1.7991121, -0.23030932, 0.27912015, 0.6304139, -1.1762936, 0.26855996, -0.66462564, 0.33278066, 0.17426747, -0.27117524, -1.1485088, 0.07291799, 0.7712352, -1.2570909, -0.6230442, 0.02963586, -0.4936177, -0.014295651, 0.5730515, ... , -0.5260737, -0.44808808, 0.9352375}}
768
我們看到輸入的文本成功地被向量化了,我們輸出了這個(gè)向量的維度:768。
注:文本向量維度的常見(jiàn)的值有200、300、768、1536等。
我們看到,基于Ollama加載的預(yù)訓(xùn)練文本嵌入模型,我們可以在Go語(yǔ)言中實(shí)現(xiàn)高效優(yōu)質(zhì)的文本向量化。將文本數(shù)據(jù)映射到語(yǔ)義向量空間,為基于RAG的知識(shí)庫(kù)應(yīng)用打下堅(jiān)實(shí)的基礎(chǔ)。有了向量后,我們便可以將其存儲(chǔ)在向量數(shù)據(jù)庫(kù)中備用,在后續(xù)的文章中,我會(huì)探討向量數(shù)據(jù)庫(kù)寫(xiě)入與檢索的實(shí)現(xiàn)方法。