watsonx go
v1.0.1
watsonx-go
ist ein watsonx-Client für Go
go get -u github.com/IBM/watsonx-go
import (
wx "github.com/IBM/watsonx-go/pkg/models"
)
export WATSONX_API_KEY= " YOUR WATSONX API KEY "
export WATSONX_PROJECT_ID= " YOUR WATSONX PROJECT ID "
Erstellen Sie einen Kunden:
client , _ := wx . NewClient ()
Oder geben Sie die erforderlichen Geheimnisse direkt ein:
client , err := wx . NewClient (
wx . WithWatsonxAPIKey ( apiKey ),
wx . WithWatsonxProjectID ( projectID ),
)
Generation:
result , _ := client . GenerateText (
"meta-llama/llama-3-1-8b-instruct" ,
"Hi, who are you?" ,
wx . WithTemperature ( 0.4 ),
wx . WithMaxNewTokens ( 512 ),
)
println ( result . Text )
Stream-Generierung:
dataChan , _ := client . GenerateTextStream (
"meta-llama/llama-3-1-8b-instruct" ,
"Hi, who are you?" ,
wx . WithTemperature ( 0.4 ),
wx . WithMaxNewTokens ( 512 ),
)
for data := range dataChan {
print ( data . Text ) // print the result as it's being generated
}
Einbetten | Einzelabfrage:
result , _ := client . EmbedQuery (
"ibm/slate-30m-english-rtrvr" ,
"Hello, world!" ,
wx . WithEmbeddingTruncateInputTokens ( 2 ),
wx . WithEmbeddingReturnOptions ( true ),
)
embeddingVector := result . Results [ 0 ]. Embedding
Einbetten | Mehrere Dokumente:
result , _ := clientl . EmbedDocuments (
"ibm/slate-30m-english-rtrvr" ,
[] string { "Hello, world!" , "Goodbye, world!" },
wx . WithEmbeddingTruncateInputTokens ( 2 ),
wx . WithEmbeddingReturnOptions ( true ),
)
for _ , doc := range result . Results {
fmt . Println ( doc . Embedding )
}
export WATSONX_API_KEY= " YOUR WATSONX API KEY "
export WATSONX_PROJECT_ID= " YOUR WATSONX PROJECT ID "
go test ./...
Führen Sie den folgenden Befehl aus, um die Pre-Commit-Formatierung auszuführen:
git config --local core.hooksPath .githooks/
Es gibt zwei Methoden zum Konfigurieren des Watsonx-Clients für die Verwendung mit der Testumgebung:
Geben Sie die Watsonx-URL und den IAM-Endpunkt mithilfe von Umgebungsvariablen an:
export WATSONX_URL_HOST= " us-south.ml.test.cloud.ibm.com "
export WATSONX_IAM_HOST= " iam.test.cloud.ibm.com "
NewClient
-FunktionsparameterGeben Sie die Watsonx-URL und den IAM-Endpunkt über die Parameter der NewClient-Funktion an:
client , err := wx . NewClient (
wx . WithURL ( "us-south.ml.test.cloud.ibm.com" ),
wx . WithIAM ( "iam.test.cloud.ibm.com" ),
wx . WithWatsonxAPIKey ( apiKey ),
wx . WithWatsonxProjectID ( projectID ),
)