搜尋
首頁後端開發Golang計算 Go 中發送給 LLM 的 Token 數量(第 2 部分)

Counting the number of Tokens sent to a LLM in Go (part 2)

介紹

這是編寫 Go 應用程式的第二部分,該應用程式用於根據所選文字確定使用者發送給 LLM 的令牌數量。

在上一篇文章中,我提到我只想建立一些僅用 Golang 寫的東西,在我查看的 Github 儲存庫中,這個似乎非常好:go-hggingface。該代碼似乎很新,但它“有點”適合我。

執行

首先,程式碼存取 Hugginface 以取得所有與 LLM 相關的「標記器」列表,因此使用者應該擁有 HF 標記。因此,我將令牌放入 .env 檔案中,如圖所示。

HF_TOKEN="your-huggingface-token"

然後使用下頁中提供的範例 (https://github.com/gomlx/go-huggingface?tab=readme-ov-file),我圍繞它建立了自己的程式碼。

package main

import (
 "bytes"
 "fmt"
 "log"
 "os"
 "os/exec"
 "runtime"

 "github.com/gomlx/go-huggingface/hub"
 "github.com/gomlx/go-huggingface/tokenizers"

 "github.com/joho/godotenv"
 "github.com/sqweek/dialog"

 "fyne.io/fyne/v2"
 "fyne.io/fyne/v2/app"
 "fyne.io/fyne/v2/container"
 "fyne.io/fyne/v2/widget"
 //"github.com/inancgumus/scree"
)

var (
 // Model IDs we use for testing.
 hfModelIDs = []string{
  "ibm-granite/granite-3.1-8b-instruct",
  "meta-llama/Llama-3.3-70B-Instruct",
  "mistralai/Mistral-7B-Instruct-v0.3",
  "google/gemma-2-2b-it",
  "sentence-transformers/all-MiniLM-L6-v2",
  "protectai/deberta-v3-base-zeroshot-v1-onnx",
  "KnightsAnalytics/distilbert-base-uncased-finetuned-sst-2-english",
  "KnightsAnalytics/distilbert-NER",
  "SamLowe/roberta-base-go_emotions-onnx",
 }
)

func runCmd(name string, arg ...string) {
 cmd := exec.Command(name, arg...)
 cmd.Stdout = os.Stdout
 cmd.Run()
}

func ClearTerminal() {
 switch runtime.GOOS {
 case "darwin":
  runCmd("clear")
 case "linux":
  runCmd("clear")
 case "windows":
  runCmd("cmd", "/c", "cls")
 default:
  runCmd("clear")
 }
}

func FileSelectionDialog() string {
 // Open a file dialog box and let the user select a text file
 filePath, err := dialog.File().Filter("Text Files", "txt").Load()
 if err != nil {
  if err.Error() == "Cancelled" {
   fmt.Println("File selection was cancelled.")
  }
  log.Fatalf("Error selecting file: %v", err)
 }

 // Output the selected file name
 fmt.Printf("Selected file: %s\n", filePath)
 return filePath
}

func main() {

 var filePath string

 // read the '.env' file
 err := godotenv.Load()
 if err != nil {
  log.Fatal("Error loading .env file")
 }
 // get the value of the 'HF_TOKEN' environment variable
 hfAuthToken := os.Getenv("HF_TOKEN")
 if hfAuthToken == "" {
  log.Fatal("HF_TOKEN environment variable is not set")
 }

 // to display a list of LLMs to determine the # of tokens later on regarding the given text
 var llm string = ""
 var modelID string = ""
 myApp := app.New()
 myWindow := myApp.NewWindow("Select a LLM in the list")
 items := hfModelIDs
 // Label to display the selected item
 selectedItem := widget.NewLabel("Selected LLM: None")
 // Create a list widget
 list := widget.NewList(
  func() int {
   // Return the number of items in the list
   return len(items)
  },
  func() fyne.CanvasObject {
   // Template for each list item
   return widget.NewLabel("Template")
  },
  func(id widget.ListItemID, obj fyne.CanvasObject) {
   // Update the template with the actual data
   obj.(*widget.Label).SetText(items[id])
  },
 )
 // Handle list item selection
 list.OnSelected = func(id widget.ListItemID) {
  selectedItem.SetText("Selected LLM:" + items[id])
  llm = items[id]
 }

 // Layout with the list and selected item label
 content := container.NewVBox(
  list,
  selectedItem,
 )

 // Set the content of the window
 myWindow.SetContent(content)
 myWindow.Resize(fyne.NewSize(300, 400))
 myWindow.ShowAndRun()
 ClearTerminal()
 fmt.Printf("Selected LLM: %s\n", llm)
 //////

 //List files for the selected model
 for _, modelID := range hfModelIDs {
  if modelID == llm {
   fmt.Printf("\n%s:\n", modelID)
   repo := hub.New(modelID).WithAuth(hfAuthToken)
   for fileName, err := range repo.IterFileNames() {
    if err != nil {
     panic(err)
    }
    fmt.Printf("fileName\t%s\n", fileName)
    fmt.Printf("repo\t%s\n", repo)
    fmt.Printf("modelID\t%s\n", modelID)
   }
  }
 }

 //List tokenizer classes for the selected model
 for _, modelID := range hfModelIDs {
  if modelID == llm {
   fmt.Printf("\n%s:\n", modelID)
   repo := hub.New(modelID).WithAuth(hfAuthToken)
   fmt.Printf("\trepo=%s\n", repo)
   config, err := tokenizers.GetConfig(repo)
   if err != nil {
    panic(err)
   }
   fmt.Printf("\ttokenizer_class=%s\n", config.TokenizerClass)
  }
 }

 // Models URL -> "https://huggingface.co/api/models"
 repo := hub.New(modelID).WithAuth(hfAuthToken)
 tokenizer, err := tokenizers.New(repo)
 if err != nil {
  panic(err)
 }

 // call file selection dialogbox
 filePath = FileSelectionDialog()

 // Open the file
 filerc, err := os.Open(filePath)
 if err != nil {
  fmt.Printf("Error opening file: %v\n", err)
  return
 }
 defer filerc.Close()

 // Put the text file content into a buffer and convert it to a string.
 buf := new(bytes.Buffer)
 buf.ReadFrom(filerc)
 sentence := buf.String()

 tokens := tokenizer.Encode(sentence)
 fmt.Println("Sentence:\n", sentence)

 fmt.Printf("Tokens:  \t%v\n", tokens)
}

在「hfModelIDs」的「var」部分中,我加入了一些新的引用,例如IBM 的Granite、Meta 的LLama 以及Mistral模型。

Huggingface 令牌也直接在 Go 程式碼中取得和讀取。

我添加了一個對話框來顯示法學碩士列表(我最終會更改),一個對話框來添加文件中的文本(我喜歡這種東西?)以及一些要清除和刪除的代碼行清潔屏幕? !

輸入文字如下;

The popularity of the Rust language continues to explode; yet, many critical codebases remain authored in C, and cannot be realistically rewritten by hand. Automatically translating C to Rust is thus an appealing course of action. Several works have gone down this path, handling an ever-increasing subset of C through a variety of Rust features, such as unsafe. While the prospect of automation is appealing, producing code that relies on unsafe negates the memory safety guarantees offered by Rust, and therefore the main advantages of porting existing codebases to memory-safe languages.
We instead explore a different path, and explore what it would take to translate C to safe Rust; that is, to produce code that is trivially memory safe, because it abides by Rust's type system without caveats. Our work sports several original contributions: a type-directed translation from (a subset of) C to safe Rust; a novel static analysis based on "split trees" that allows expressing C's pointer arithmetic using Rust's slices and splitting operations; an analysis that infers exactly which borrows need to be mutable; and a compilation strategy for C's struct types that is compatible with Rust's distinction between non-owned and owned allocations.
We apply our methodology to existing formally verified C codebases: the HACL* cryptographic library, and binary parsers and serializers from EverParse, and show that the subset of C we support is sufficient to translate both applications to safe Rust. Our evaluation shows that for the few places that do violate Rust's aliasing discipline, automated, surgical rewrites suffice; and that the few strategic copies we insert have a negligible performance impact. Of particular note, the application of our approach to HACL* results in a 80,000 line verified cryptographic library, written in pure Rust, that implements all modern algorithms - the first of its kind.

檢定
執行後的程式碼會顯示對話方塊 bx,您可以在其中選擇所需的 LLM。

Counting the number of Tokens sent to a LLM in Go (part 2)

如果一切順利,下一步是在本機下載「tokenizer」檔案(請參閱 Github 儲存庫的說明),然後會顯示一個對話框,選擇包含要評估的內容的文字檔案令牌數量。

到目前為止,我已要求訪問 Meta LLama 和 Google“google/gemma-2–2b-it”模型,並正在等待訪問權限被授予。

google/gemma-2-2b-it:
        repo=google/gemma-2-2b-it
panic: request for metadata from "https://huggingface.co/google/gemma-2-2b-it/resolve/299a8560bedf22ed1c72a8a11e7dce4a7f9f51f8/tokenizer_config.json" failed with the following message: "403 Forbidden"

Counting the number of Tokens sent to a LLM in Go (part 2)

結論

我認為實現我想要的目標的正確途徑是,一個能夠確定代幣數量的 Golang 程式是用戶發送到 LLM 的查詢。

這個專案的唯一目的是了解針對各種 LLM 的查詢中確定令牌數量背後的內部系統,並發現它們是如何計算的。

感謝您的閱讀並歡迎評論。

最終結論之前,敬請期待…?

以上是計算 Go 中發送給 LLM 的 Token 數量(第 2 部分)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
學習GO二進制編碼/解碼:使用'編碼/二進制”軟件包學習GO二進制編碼/解碼:使用'編碼/二進制”軟件包May 08, 2025 am 12:13 AM

Go語言使用"encoding/binary"包進行二進制編碼與解碼。 1)該包提供binary.Write和binary.Read函數,用於數據的寫入和讀取。 2)需要注意選擇正確的字節序(如BigEndian或LittleEndian)。 3)數據對齊和錯誤處理也是關鍵,確保數據的正確性和性能。

GO:帶有標準'字節”軟件包的字節切​​片操作GO:帶有標準'字節”軟件包的字節切​​片操作May 08, 2025 am 12:09 AM

1)usebybytes.joinforconcatenatinges,2)bytes.bufferforincrementalwriting,3)bytes.indexorbytes.indexorbytes.indexbyteforsearching bytes.bytes.readereforrednorederencretingnchunknunknchunknunk.sss.inc.softes.4)

進行編碼/二進制包:優化二進制操作的性能進行編碼/二進制包:優化二進制操作的性能May 08, 2025 am 12:06 AM

theencoding/binarypackageingoiseforporptimizingBinaryBinaryOperationsDuetoitssupportforendiannessessandefficityDatahandling.toenhancePerformance:1)usebinary.nativeendiandiandiandiandiandiandiandian nessideendian toavoid avoidByteByteswapping.2)

Go Bytes軟件包:簡短的參考和提示Go Bytes軟件包:簡短的參考和提示May 08, 2025 am 12:05 AM

Go的bytes包主要用於高效處理字節切片。 1)使用bytes.Buffer可以高效進行字符串拼接,避免不必要的內存分配。 2)bytes.Equal函數用於快速比較字節切片。 3)bytes.Index、bytes.Split和bytes.ReplaceAll函數可用於搜索和操作字節切片,但需注意性能問題。

Go Bytes軟件包:字節切片操縱的實例Go Bytes軟件包:字節切片操縱的實例May 08, 2025 am 12:01 AM

字節包提供了多種功能來高效處理字節切片。 1)使用bytes.Contains檢查字節序列。 2)用bytes.Split分割字節切片。 3)通過bytes.Replace替換字節序列。 4)用bytes.Join連接多個字節切片。 5)利用bytes.Buffer構建數據。 6)結合bytes.Map進行錯誤處理和數據驗證。

進行二進制編碼/解碼:實踐指南進行二進制編碼/解碼:實踐指南May 07, 2025 pm 05:37 PM

Go的encoding/binary包是處理二進制數據的工具。 1)它支持小端和大端字節序,可用於網絡協議和文件格式。 2)可以通過Read和Write函數處理複雜結構的編碼和解碼。 3)使用時需注意字節序和數據類型的一致性,尤其在不同系統間傳輸數據時。該包適合高效處理二進制數據,但需謹慎管理字節切片和長度。

Go'字節”軟件包:比較,加入,分裂及更多Go'字節”軟件包:比較,加入,分裂及更多May 07, 2025 pm 05:29 PM

“字節”包裝封裝becapeitoffersefficerSoperationsOnbyteslices,cocialforbinarydatahandling,textPrococessing,andnetworkCommunications.byteslesalemutable,允許forforforforforformance-enhangingin-enhangingin-placemodifications,makaythisspackage

GO弦套件:您需要知道的基本功能GO弦套件:您需要知道的基本功能May 07, 2025 pm 04:57 PM

go'sstringspackageIncludeSessentialFunctionsLikeContains,trimspace,split,andreplaceAll.1)contunsefefitedsseffitedsfificeCheckSforSubStrings.2)trimspaceRemovesWhitespaceToeensuredity.3)splitparsentertparsentertparsentertparsentertparstructedtextlikecsv.4)report textlikecsv.4)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。