Go에서 LLM으로 전송된 토큰 수 계산(1부)
소개
몇 주 전 저는 비즈니스 파트너 회사의 CFO와 자사 솔루션 내에서 watsonx.ai 기능 구현에 관해 논의했습니다. 비용에 대한 논의 중에 "토큰"이라는 단어를 발음했는데 갑자기 패닉이 일어났습니다 ?
토큰이 무엇인지 설명한 후 질문이 생겼습니다. “우리가 보내고 받는 토큰을 어떻게 계산하나요? 비용이 얼마나 드나요?”
답은 아주 쉬웠습니다. 우리는 watsonx.ai 스튜디오 프롬프트 랩에 가서 몇 가지 간단한 프롬프트를 가지고 왔다 갔다 했고 거기에서 토큰의 수를 확인했습니다. 또한 간단한 입력을 통해 LLM에 보내는 토큰 수를 확인할 수 있는 매우 멋진 웹사이트도 보여주었습니다.
나중에 저는 스스로 토큰 카운터 애플리케이션을 만들어 보는 것이 어떨까요(그리고 오랫동안 Golang을 사용하지 않았기 때문에 Go 언어로 작성하려는 의도였습니다!)라고 생각했습니다. 글쎄요, 그보다 좀 더 복잡할 것 같은데요?
첫 번째 시도 — Regex 사용
처음에는 Regex를 사용하면 어느 정도 만족스러운 결과를 얻을 수 있겠다는 생각이 들었습니다.
다음 Go 앱을 설정했습니다.
package main import ( "bufio" "fmt" "log" "os" "regexp" "strings" "github.com/sqweek/dialog" ) // countTokens approximates the number of tokens in a text based on whitespace and punctuation. func countTokens(text string) int { // A simple regex to split text into words and punctuation tokenizer := regexp.MustCompile(`\w+|[^\w\s]`) tokens := tokenizer.FindAllString(text, -1) return len(tokens) } func main() { // 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.") return } log.Fatalf("Error selecting file: %v", err) } // Output the selected file name fmt.Printf("Selected file: %s\n", filePath) // Specify the file to read //filePath := "input.txt" // Open the file file, err := os.Open(filePath) if err != nil { fmt.Printf("Error opening file: %v\n", err) return } defer file.Close() // Read the file line by line var content strings.Builder scanner := bufio.NewScanner(file) for scanner.Scan() { content.WriteString(scanner.Text()) content.WriteString("\n") } if err := scanner.Err(); err != nil { fmt.Printf("Error reading file: %v\n", err) return } // Get the text content text := content.String() // Count the tokens tokenCount := countTokens(text) // Output the result fmt.Printf("The file contains approximately %d tokens.\n", tokenCount) }
아시겠지만 제가 GUI와 대화 상자를 좋아해서 입력 텍스트 파일을 선택할 수 있는 대화 상자를 구현했습니다.
여기 텍스트 파일이 있습니다(내가 찾은 임의의 텍스트?).
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.
코드를 실행하면 다음과 같은 결과가 나타납니다.
The file contains approximately 359 tokens.
괜찮을 것 같긴 한데, 음… 그렇긴 한데… 어떤 모델과 비교하면?? 그리고 Regex를 구현하는 방법도 다양하므로 이 방법은 전혀 포함되지 않습니다 ?!
두 번째 시도 - 특정 모델에 대해 실행
내가 알아낸 것은 특정 LLM에 특정 "토크나이저"를 사용하지 않는 한 이전 방법은 정확하지 않다는 것입니다. 그래서 나는 현재 시중에 나와 있는 gpt 3.5와 같은 모델에 대해 어떻게 하면 정확한 결과를 얻을 수 있는지 알아보기 시작했습니다. 인터넷에서 좀 조사한 끝에 제가 만든 앱이 나왔습니다.
package main import ( "bufio" "bytes" "fmt" "log" "os" "os/exec" "github.com/joho/godotenv" "github.com/sqweek/dialog" ) func main() { // 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.") return } log.Fatalf("Error selecting file: %v", err) } // Output the selected file name fmt.Printf("Selected file: %s\n", filePath) // Open the file file, err := os.Open(filePath) if err != nil { fmt.Printf("Error opening file: %v\n", err) return } defer file.Close() // Read the file content var content bytes.Buffer scanner := bufio.NewScanner(file) for scanner.Scan() { content.WriteString(scanner.Text()) content.WriteString("\n") } if err := scanner.Err(); err != nil { fmt.Printf("Error reading file: %v\n", err) return } // Specify the model model := "gpt-3.5-turbo" // Execute the Python script cmd := exec.Command("python3", "tokenizer.py", model) cmd.Stdin = bytes.NewReader(content.Bytes()) output, err := cmd.Output() if err != nil { fmt.Printf("Error running tokenizer script: %v\n", err) return } // Print the token count fmt.Printf("Token count: %s", output) }
위 코드에서 볼 수 있듯이 Microsoft 사이트에서 찾은 Python 앱에 대한 호출이 있습니다. 이 앱은 (구현되었기 때문에) "tiktoken” 라이브러리를 사용하여 gpt에 대한 토큰 수를 결정합니다! 모델명도 하드코딩 되어있습니다.
import sys from tiktoken import encoding_for_model def count_tokens(model, text): enc = encoding_for_model(model) tokens = enc.encode(text) return len(tokens) if __name__ == "__main__": # Read model name and text from stdin model = sys.argv[1] # E.g., "gpt-3.5-turbo" text = sys.stdin.read() print(count_tokens(model, text))
잘 작동합니다. 이전에 제공된 동일한 텍스트에 대해 이제 내가 찾은 모든 웹사이트에 대해 정확한 366개의 토큰 수를 얻었고 모델을 GPT 3.5
로 설정했습니다.내가 작성하고 싶은 것은 "Golang"에 완전히 포함된 코드입니다... 그리고 Huggingface에서 찾을 수 있는 모든 모델(또는 거의 모든 모델)에서 실행할 수 있기를 원합니다. ibm-granite/granite-3.1–8b-instruct) ?
이 글의 2부(WIP)입니다.
지금까지 다음을 탐색 중입니다(좋죠?) Github repos;
- 토크나이저: https://github.com/sugarme/tokenizer
- 토크나이저: https://github.com/daulet/tokenizers
- 그리고 마지막으로 중요한 것은 -> 고허깅페이스: https://github.com/gomlx/go-huggingface?tab=readme-ov-file
결론
읽어주셔서 감사하고 의견을 남겨주세요.
그리고 두 번째 앱이 나올 때까지 지켜봐주세요... ?
위 내용은 Go에서 LLM으로 전송된 토큰 수 계산(1부)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Go Language는 효율적이고 확장 가능한 시스템을 구축하는 데 잘 작동합니다. 장점은 다음과 같습니다. 1. 고성능 : 기계 코드로 컴파일, 빠른 달리기 속도; 2. 동시 프로그래밍 : 고어 라틴 및 채널을 통한 멀티 태스킹 단순화; 3. 단순성 : 간결한 구문, 학습 및 유지 보수 비용 절감; 4. 크로스 플랫폼 : 크로스 플랫폼 컴파일, 쉬운 배포를 지원합니다.

Golang은 동시성에서 C보다 낫고 C는 원시 속도에서 Golang보다 낫습니다. 1) Golang은 Goroutine 및 Channel을 통해 효율적인 동시성을 달성하며, 이는 많은 동시 작업을 처리하는 데 적합합니다. 2) C 컴파일러 최적화 및 표준 라이브러리를 통해 하드웨어에 가까운 고성능을 제공하며 극도의 최적화가 필요한 애플리케이션에 적합합니다.

Golang은 성능과 확장 성 측면에서 Python보다 낫습니다. 1) Golang의 컴파일 유형 특성과 효율적인 동시성 모델은 높은 동시성 시나리오에서 잘 수행합니다. 2) 해석 된 언어로서 파이썬은 천천히 실행되지만 Cython과 같은 도구를 통해 성능을 최적화 할 수 있습니다.

Golang과 C는 각각 공연 경쟁에서 고유 한 장점을 가지고 있습니다. 1) Golang은 높은 동시성과 빠른 발전에 적합하며 2) C는 더 높은 성능과 세밀한 제어를 제공합니다. 선택은 프로젝트 요구 사항 및 팀 기술 스택을 기반으로해야합니다.

C는 하드웨어 리소스 및 고성능 최적화가 직접 제어되는 시나리오에 더 적합하지만 Golang은 빠른 개발 및 높은 동시성 처리가 필요한 시나리오에 더 적합합니다. 1.C의 장점은 게임 개발과 같은 고성능 요구에 적합한 하드웨어 특성 및 높은 최적화 기능에 가깝습니다. 2. Golang의 장점은 간결한 구문 및 자연 동시성 지원에 있으며, 이는 동시성 서비스 개발에 적합합니다.

Golang과 Python은 각각 고유 한 장점이 있습니다. Golang은 고성능 및 동시 프로그래밍에 적합하지만 Python은 데이터 과학 및 웹 개발에 적합합니다. Golang은 동시성 모델과 효율적인 성능으로 유명하며 Python은 간결한 구문 및 풍부한 라이브러리 생태계로 유명합니다.

goimpactsdevelopmentpositively throughlyspeed, 효율성 및 단순성.

Golang과 C의 성능 차이는 주로 메모리 관리, 컴파일 최적화 및 런타임 효율에 반영됩니다. 1) Golang의 쓰레기 수집 메커니즘은 편리하지만 성능에 영향을 줄 수 있습니다. 2) C의 수동 메모리 관리 및 컴파일러 최적화는 재귀 컴퓨팅에서 더 효율적입니다.
