モチベーション
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 中国語 Web サイトの他の関連記事を参照してください。

golangisidealforporformance-criticalapplicationsandconcurrentprogramming、whilepythonexcelsindatascience、rapyプロトタイプ、およびandversitielity.1)for-high-duetoitsefficiency and concurrencyfeatures.2

GolangはGoroutineとChannelを通じて効率的な並行性を実現します。1。Goroutineは、Goキーワードで始まる軽量のスレッドです。 2.チャンネルは、ゴルチン間の安全な通信に使用され、人種の状態を避けます。 3.使用例は、基本的および高度な使用法を示しています。 4.一般的なエラーには、ゴルンレースで検出できるデッドロックとデータ競争が含まれます。 5.パフォーマンスの最適化では、チャネルの使用を削減し、ゴルチンの数を合理的に設定し、Sync.poolを使用してメモリを管理することを示唆しています。

Golangは、システムプログラミングと高い並行性アプリケーションにより適していますが、Pythonはデータサイエンスと迅速な発展により適しています。 1)GolangはGoogleによって開発され、静的にタイピングし、シンプルさと効率を強調しており、高い並行性シナリオに適しています。 2)Pythonは、Guidovan Rossumによって作成され、動的に型付けられた簡潔な構文、幅広いアプリケーション、初心者やデータ処理に適しています。

Golangは、パフォーマンスとスケーラビリティの点でPythonよりも優れています。 1)Golangのコンピレーションタイプの特性と効率的な並行性モデルにより、高い並行性シナリオでうまく機能します。 2)Pythonは解釈された言語として、ゆっくりと実行されますが、Cythonなどのツールを介してパフォーマンスを最適化できます。

GO言語は、同時プログラミング、パフォーマンス、学習曲線などにユニークな利点を持っています。1。GoroutineとChannelを通じて同時プログラミングが実現されます。これは軽量で効率的です。 2。コンピレーション速度は高速で、操作性能はC言語のパフォーマンスに近いです。 3.文法は簡潔で、学習曲線は滑らかで、生態系は豊富です。

GolangとPythonの主な違いは、並行性モデル、タイプシステム、パフォーマンス、実行速度です。 1. GolangはCSPモデルを使用します。これは、同時タスクの高いタスクに適しています。 Pythonは、I/O集約型タスクに適したマルチスレッドとGILに依存しています。 2。Golangは静的なタイプで、Pythonは動的なタイプです。 3.ゴーランコンパイルされた言語実行速度は高速であり、Python解釈言語開発は高速です。

Golangは通常Cよりも遅くなりますが、Golangはプログラミングと開発効率の同時により多くの利点があります。1)Golangのゴミ収集と並行性モデルにより、同時性の高いシナリオではうまく機能します。 2)Cは、手動のメモリ管理とハードウェアの最適化により、より高いパフォーマンスを取得しますが、開発の複雑さが高くなります。

GolangはクラウドコンピューティングとDevOpsで広く使用されており、その利点はシンプルさ、効率性、および同時プログラミング機能にあります。 1)クラウドコンピューティングでは、GolangはGoroutineおよびチャネルメカニズムを介して同時リクエストを効率的に処理します。 2)DevOpsでは、Golangの高速コンピレーションとクロスプラットフォーム機能により、自動化ツールの最初の選択肢になります。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

WebStorm Mac版
便利なJavaScript開発ツール

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。
