Skip to content

Commit 59acb72

Browse files
authored
feat: support iFlytek spark model (#635)
1 parent d62d609 commit 59acb72

5 files changed

Lines changed: 102 additions & 0 deletions

File tree

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ require (
4343
github.com/golang/snappy v0.0.4 // indirect
4444
github.com/gomodule/redigo v2.0.0+incompatible // indirect
4545
github.com/google/go-cmp v0.5.9 // indirect
46+
github.com/gorilla/websocket v1.5.0 // indirect
4647
github.com/hashicorp/golang-lru v0.5.4 // indirect
4748
github.com/henomis/cohere-go v1.0.1 // indirect
4849
github.com/henomis/restclientgo v1.0.5 // indirect
@@ -64,6 +65,9 @@ require (
6465
github.com/rogpeppe/go-internal v1.9.0 // indirect
6566
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
6667
github.com/syndtr/goleveldb v1.0.0 // indirect
68+
github.com/vogo/gorun v1.1.0 // indirect
69+
github.com/vogo/logger v1.5.1 // indirect
70+
github.com/vogo/xfspark v0.1.2 // indirect
6771
golang.org/x/crypto v0.7.0 // indirect
6872
golang.org/x/exp v0.0.0-20220827204233-334a2380cb91 // indirect
6973
golang.org/x/mod v0.8.0 // indirect

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51
253253
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
254254
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
255255
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
256+
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
257+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
256258
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
257259
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
258260
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
@@ -579,6 +581,12 @@ github.com/unidoc/unioffice v1.4.0 h1:yl+TbZJu2GTVYAYvu51wppj0R+fPC67xzVcy91qgrz
579581
github.com/unidoc/unioffice v1.4.0/go.mod h1:7wl8btOkZW1TfqfpDWoujRXkUpowwisGRYDo7COHBiI=
580582
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
581583
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
584+
github.com/vogo/gorun v1.1.0 h1:i/8HhmNyjMa79bFn1ZHGTO3KyiWQcZPBsFGpvgkcap8=
585+
github.com/vogo/gorun v1.1.0/go.mod h1:MyOjF/DbZSz40GQ5657Ou9NEfNlUbMhvpXjDvh4L8OY=
586+
github.com/vogo/logger v1.5.1 h1:voyVY69TpM/x1lml4LYy4jMe5z0kDh5jW1oatcikajM=
587+
github.com/vogo/logger v1.5.1/go.mod h1:9U+qupncHpWpt4ptlxFm9bvDb9EjbGIA+cY/tYtW4Kg=
588+
github.com/vogo/xfspark v0.1.2 h1:QdW7jvFL6bgHQ74xTuAlP2IBj4sIVCpBE/LS0LW4ePE=
589+
github.com/vogo/xfspark v0.1.2/go.mod h1:KHvAxw98fpLsFdmFYm9ps99eGaLbuLBTBa9rVG6SFyo=
582590
github.com/wcharczuk/go-chart/v2 v2.1.0/go.mod h1:yx7MvAVNcP/kN9lKXM/NTce4au4DFN99j6i1OwDclNA=
583591
github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b/go.mod h1:Q12BUT7DqIlHRmgv3RskH+UCM/4eqVMgI0EMmlSpAXc=
584592
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
@@ -1037,6 +1045,7 @@ modernc.org/token v1.0.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
10371045
modernc.org/z v1.0.1/go.mod h1:8/SRk5C/HgiQWCgXdfpb+1RvhORdkz5sw72d3jjtyqA=
10381046
modernc.org/z v1.5.1 h1:RTNHdsrOpeoSeOF4FbzTo8gBYByaJ5xT7NgZ9ZqRiJM=
10391047
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
1048+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
10401049
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
10411050
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
10421051
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=

model/iflytek.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2023 The casbin Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package model
16+
17+
import (
18+
"fmt"
19+
"io"
20+
"net/http"
21+
"strings"
22+
23+
iflytek "github.com/vogo/xfspark/chat"
24+
)
25+
26+
type iFlytekModelProvider struct {
27+
subType string
28+
appID string
29+
apiKey string
30+
secretKey string
31+
}
32+
33+
func NewiFlytekModelProvider(subType string, secretKey string) (*iFlytekModelProvider, error) {
34+
p := &iFlytekModelProvider{
35+
subType: subType,
36+
appID: "",
37+
apiKey: "",
38+
secretKey: secretKey,
39+
}
40+
return p, nil
41+
}
42+
43+
func (p *iFlytekModelProvider) QueryText(question string, writer io.Writer, builder *strings.Builder) error {
44+
client := iflytek.NewServer(p.appID, p.apiKey, p.secretKey)
45+
flusher, ok := writer.(http.Flusher)
46+
if !ok {
47+
return fmt.Errorf("writer does not implement http.Flusher")
48+
}
49+
50+
session, err := client.GetSession("1")
51+
if err != nil {
52+
return fmt.Errorf("iflytek get session error: %v", err)
53+
}
54+
if session == nil {
55+
return fmt.Errorf("iflytek get session error: session is nil")
56+
}
57+
58+
response, err := session.Send(question)
59+
if err != nil {
60+
return fmt.Errorf("iflytek send error: %v", err)
61+
}
62+
63+
flushData := func(data string) error {
64+
if _, err := fmt.Fprintf(writer, "event: message\ndata: %s\n\n", data); err != nil {
65+
return err
66+
}
67+
flusher.Flush()
68+
builder.WriteString(data)
69+
return nil
70+
}
71+
72+
err = flushData(response)
73+
74+
if builder != nil {
75+
builder.WriteString(response)
76+
}
77+
78+
return nil
79+
}

model/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ func GetModelProvider(typ string, subType string, clientId string, clientSecret
3434
p, err = NewOpenRouterModelProvider(subType, clientSecret)
3535
} else if typ == "Ernie" {
3636
p, err = NewErnieModelProvider(subType, clientId, clientSecret)
37+
} else if typ == "iFlytek" {
38+
p, err = NewiFlytekModelProvider(subType, clientSecret)
3739
}
3840

3941
if err != nil {

web/src/Setting.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ export function getProviderTypeOptions(category) {
632632
{id: "Hugging Face", name: "Hugging Face"},
633633
{id: "OpenRouter", name: "OpenRouter"},
634634
{id: "Ernie", name: "Ernie"},
635+
{id: "iFlytek", name: "iFlytek"},
635636
]
636637
);
637638
} else if (category === "Embedding") {
@@ -776,6 +777,13 @@ export function getProviderSubTypeOptions(category, type) {
776777
{id: "embed-multilingual-v2.0", name: "embed-multilingual-v2.0"},
777778
]
778779
);
780+
} else if (type === "iFlytek") {
781+
return (
782+
[
783+
{id: "spark-v1.5", name: "spark-v1.5"},
784+
{id: "spark-v2.0", name: "spark-v2.0"},
785+
]
786+
);
779787
} else {
780788
return [];
781789
}

0 commit comments

Comments
 (0)