-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckSimilarityGoClient.go
80 lines (66 loc) · 1.81 KB
/
checkSimilarityGoClient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"flag"
"fmt"
"github.com/jina-ai/client-go"
"github.com/jina-ai/client-go/docarray"
"github.com/jina-ai/client-go/jina"
)
// Create a Document
func getDoc(sentence string) *docarray.DocumentProto {
return &docarray.DocumentProto{
Content: &docarray.DocumentProto_Text{
Text: sentence,
},
}
}
// Create a DocumentArray with 3 Documents
func getDocarrays(s1, s2 string) *docarray.DocumentArrayProto {
var docs []*docarray.DocumentProto
docs = append(docs, getDoc(s1))
docs = append(docs, getDoc(s2))
return &docarray.DocumentArrayProto{
Docs: docs,
}
}
// Create DataRequest with a DocumentArray
func getDataRequest(s1, s2 string) *jina.DataRequestProto {
return &jina.DataRequestProto{
Data: &jina.DataRequestProto_DataContentProto{
Documents: &jina.DataRequestProto_DataContentProto_Docs{
Docs: getDocarrays(s1, s2),
},
},
}
}
// Generate a stream of requests
func generateDataRequests(s1, s2 string) <-chan *jina.DataRequestProto {
requests := make(chan *jina.DataRequestProto)
go func() {
requests <- getDataRequest(s1, s2)
defer close(requests)
}()
return requests
}
func OnDone(resp *jina.DataRequestProto) {
fmt.Println("resp.Data:", resp.GetData().GetDocs().Docs[0].GetText()) //注意返回的数据是array,所以用了Docs[0]
fmt.Println("服务调用成功!:")
}
func OnError(resp *jina.DataRequestProto) {
fmt.Println("Got an error for request", resp)
}
func main() {
host := flag.String("host", "", "host of the gateway")
flag.Parse()
if *host == "" {
panic("Please pass a host to check the health of")
}
s1 := "我认为你很优秀"
s2 := "我不得不承认你很优秀"
GRPCClient, err := client.NewGRPCClient(*host)
if err != nil {
panic(err)
}
request := generateDataRequests(s1, s2)
GRPCClient.POST(request, OnDone, OnError, nil)
}