Skip to content

Commit

Permalink
feat: add zhipuai embedding api & examples
Browse files Browse the repository at this point in the history
Signed-off-by: Lanture1064 <[email protected]>
  • Loading branch information
Lanture1064 committed Aug 31, 2023
1 parent 8e48dde commit f4d1495
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 29 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
// Should be only one word
type ConditionReason string

// Some common Condition resons.
// Some common Condition reasons.
const (
ReasonAvailable ConditionReason = "Available"
ReasonUnavailable ConditionReason = "Unavailable"
Expand Down
63 changes: 63 additions & 0 deletions examples/embedding/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2023 KubeAGI.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
llmszhipuai "github.com/kubeagi/arcadia/pkg/llms/zhipuai"
"k8s.io/klog/v2"
"os"
)

func main() {
// usage: go run main.go <apiKey> <testWord>
// apiKey == The apiKey of user, available on ZhiPuAI dashboard.
// testWord == The text to be embedded.
if len(os.Args) == 1 {
panic("api key is empty")
}
apiKey := os.Args[1]

if len(os.Args) == 2 {
panic("test word is empty")
}
testWord := os.Args[2]

klog.V(0).Info("try `Embedding`")
resp, err := sampleEmbedding(apiKey, testWord)

if err != nil {
panic(err)
}
klog.V(0).Info("Response: \n", resp.Success)
klog.V(0).Info("Usage: \n", resp.Data.Usage)
klog.V(0).Info("First 5 number of the Result: \n", resp.Data.Embedding[:5])
// resp.Data.Embedding is available.
}

func sampleEmbedding(apiKey, testWord string) (*llmszhipuai.EmbeddingResponse, error) {
ai := llmszhipuai.NewZhiPuAI(apiKey)
text := llmszhipuai.EmbeddingText{
Prompt: testWord,
RequestID: "1",
}

resp, err := ai.Embedding(text)
if err != nil {
return nil, err
}
return resp, nil
}
3 changes: 2 additions & 1 deletion examples/zhipuai/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package main
import (
"os"

"github.com/kubeagi/arcadia/pkg/llms/zhipuai"
"k8s.io/klog/v2"

"github.com/kubeagi/arcadia/pkg/llms/zhipuai"
)

func main() {
Expand Down
22 changes: 12 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ require (
)

require (
cloud.google.com/go v0.81.0 // indirect
cloud.google.com/go/compute v1.19.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.18 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.13 // indirect
Expand All @@ -35,20 +36,20 @@ require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-logr/zapr v1.2.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -60,24 +61,25 @@ require (
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.19.1 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.24.2 // indirect
k8s.io/component-base v0.24.2 // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
Expand Down
Loading

0 comments on commit f4d1495

Please sign in to comment.