Go에서 LangChain 호출하기(1부)
동기 부여
Golang 및 LLM 사용에 대한 "휴일" 테스트(이전 게시물…)에 이어 Go에서 LangChain 호출을 구현하는 쉬운 방법을 찾고 있었고 가급적이면 watsonx.ai를 사용하는 것이 좋습니다.
다행히 다음 Github 저장소를 찾았습니다: https://github.com/tmc/langchaingo(Travis Cline https://github.com/tmc에 대한 간략한 설명).
그의 저장소에는 다음 특정 폴더가 있습니다. https://github.com/tmc/langchaingo/blob/main/examples/watsonx-llm-example/watsonx_example.go가 제 관심을 끌었습니다!
그래서 늘 그렇듯이 프로젝트를 하나 만들어서 구현해보기도 했고, 나름의 아이디어도 넣어봤습니다.
구현
평소처럼 환경 변수가 필요하므로 나중에 앱에서 사용할 .env 파일을 설정했습니다.
export WATSONX_API_KEY="your-watsonx-api-key" export WATSONX_PROJECT_ID="your-watsonx-projectid" # I used the US-SOUTH, could be any other region of IBM Cloud export SERVICE_URL="https://us-south.ml.cloud.ibm.com"
이전 게시물에서 LLM과 주고받은 토큰 수를 계산하는 방법에 대해 언급한 적이 있습니다. 그 작업은 아직 WIP이므로 (가까운 미래에?) 약간의 변경을 가할 생각으로 앱 내부의 "tiktoken-go" 라이브러리를 직접 사용했습니다. 아무튼 현재 진행 상황의 경우에는 실제로 작동하지는 않지만 거기에 있습니다.
앱 자체의 경우 Travis 저장소의 코드를 거의 그대로 사용하고 다음 기능을 추가하고 래핑했습니다.
- 프롬프트 입력을 위해 대화 상자 사용(? 저는 대화 상자를 좋아합니다 ?)
- "시도"하여 LLM으로 보내고 받은 "토큰"의 수를 계산합니다. 코드 자체는 다음과 같습니다.
package main import ( "context" "fmt" "log" "os" "os/exec" "runtime" "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/dialog" "fyne.io/fyne/v2/widget" "github.com/joho/godotenv" "github.com/pkoukk/tiktoken-go" "github.com/tmc/langchaingo/llms" "github.com/tmc/langchaingo/llms/watsonx" ) const ( _tokenApproximation = 4 ) const ( _gpt35TurboContextSize = 4096 _gpt432KContextSize = 32768 _gpt4ContextSize = 8192 _textDavinci3ContextSize = 4097 _textBabbage1ContextSize = 2048 _textAda1ContextSize = 2048 _textCurie1ContextSize = 2048 _codeDavinci2ContextSize = 8000 _codeCushman1ContextSize = 2048 _textBisonContextSize = 2048 _chatBisonContextSize = 2048 _defaultContextSize = 2048 ) // nolint:gochecknoglobals var modelToContextSize = map[string]int{ "gpt-3.5-turbo": _gpt35TurboContextSize, "gpt-4-32k": _gpt432KContextSize, "gpt-4": _gpt4ContextSize, "text-davinci-003": _textDavinci3ContextSize, "text-curie-001": _textCurie1ContextSize, "text-babbage-001": _textBabbage1ContextSize, "text-ada-001": _textAda1ContextSize, "code-davinci-002": _codeDavinci2ContextSize, "code-cushman-001": _codeCushman1ContextSize, } var tokens int 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 promptEntryDialog() string { var promptEntry string // Create a new Fyne application myApp := app.New() myWindow := myApp.NewWindow("Prompt Entry Dialog") // Variable to store user input var userInput string // Button to show the dialog button := widget.NewButton("Click to Enter your prompt's text", func() { entry := widget.NewEntry() dialog.ShowCustomConfirm("Input Dialog", "OK", "Cancel", entry, func(confirm bool) { if confirm { userInput = entry.Text promptEntry = userInput fmt.Println("User Input:", userInput) // Print to the console myWindow.Close() } }, myWindow) }) // Add the button to the window myWindow.SetContent(container.NewVBox( widget.NewLabel("Click the button below to enter text:"), button, )) // Set the window size and run the application myWindow.Resize(fyne.NewSize(400, 200)) myWindow.ShowAndRun() return promptEntry } func CountTokens(model, text string, inorout string) int { var txtLen int e, err := tiktoken.EncodingForModel(model) if err != nil { e, err = tiktoken.GetEncoding("gpt2") if err != nil { log.Printf("[WARN] Failed to calculate number of tokens for model, falling back to approximate count") txtLen = len([]rune(text)) fmt.Println("Guessed tokens for the "+inorout+" text:", txtLen/_tokenApproximation) return txtLen } } return len(e.Encode(text, nil, nil)) } func GetModelContextSize(model string) int { contextSize, ok := modelToContextSize[model] if !ok { return _defaultContextSize } return contextSize } func CalculateMaxTokens(model, text string) int { return GetModelContextSize(model) - CountTokens(model, text, text) } func main() { var prompt, model string // read the '.env' file err := godotenv.Load() if err != nil { log.Fatal("Error loading .env file") } ApiKey := os.Getenv("WATSONX_API_KEY") if ApiKey == "" { log.Fatal("WATSONX_API_KEY environment variable is not set") } ServiceURL := os.Getenv("SERVICE_URL") if ServiceURL == "" { log.Fatal("SERVICE_URL environment variable is not set") } ProjectID := os.Getenv("WATSONX_PROJECT_ID") if ProjectID == "" { log.Fatal("WATSONX_PROJECT_ID environment variable is not set") } // LLM from watsonx.ai model = "ibm/granite-13b-instruct-v2" // model = "meta-llama/llama-3-70b-instruct" llm, err := watsonx.New( model, //// Optional parameters: to be implemented if needed - Not used at this stage but all ready // wx.WithWatsonxAPIKey(ApiKey), // wx.WithWatsonxProjectID("YOUR WATSONX PROJECT ID"), ) if err != nil { log.Fatal(err) } ctx := context.Background() prompt = promptEntryDialog() // for the output visibility on the consol - getting rid of system messages ClearTerminal() // Use the entry variable here fmt.Println("Calling the llm with the user's prompt:", prompt) tokens = CountTokens(model, prompt, "input") completion, err := llms.GenerateFromSinglePrompt( ctx, llm, prompt, llms.WithTopK(10), llms.WithTopP(0.95), llms.WithSeed(25), ) // Check for errors if err != nil { log.Fatal(err) } fmt.Println(completion) tokens = CountTokens(model, completion, "output") }
아래 출력과 같이 제대로 작동합니다.
Calling the llm with the user's prompt: What is the distance in Kilmometers from Earth to Moon? 2024/12/31 11:08:04 [WARN] Failed to calculate number of tokens for model, falling back to approximate count Guessed tokens for the input text: 13 The distance from Earth to the Moon is about 384,400 kilometers. 2024/12/31 11:08:04 [WARN] Failed to calculate number of tokens for model, falling back to approximate count Guessed tokens for the output text: 16 ##### Calling the llm with the user's prompt: What is the name of the capital city of France? 2024/12/31 11:39:28 [WARN] Failed to calculate number of tokens for model, falling back to approximate count Guessed tokens for the input text: 11 Paris 2024/12/31 11:39:28 [WARN] Failed to calculate number of tokens for model, falling back to approximate count Guessed tokens for the output text: 1
짜잔!
다음 단계
버전 0.2에서는 다음 기능을 구현하겠습니다.
- 사용자가 사용하고 싶은 모델을 제안하고,
- 토큰 개수를 더욱 정확하게 파악하는 방법
- 실제 LangChain 구현
결론
이것은 Go 애플리케이션에서 LangChain을 호출하는 작업을 매우 간단하게 반영한 것입니다.
앞으로 나올 내용을 계속 지켜봐 주시기 바랍니다.
위 내용은 Go에서 LangChain 호출하기(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은 각각 고유 한 장점이 있습니다. Golang은 고성능 및 동시 프로그래밍에 적합하지만 Python은 데이터 과학 및 웹 개발에 적합합니다. Golang은 동시성 모델과 효율적인 성능으로 유명하며 Python은 간결한 구문 및 풍부한 라이브러리 생태계로 유명합니다.

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

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

goimpactsdevelopmentpositively throughlyspeed, 효율성 및 단순성.

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

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