Skip to content

Commit d474851

Browse files
committed
example: TextEmbedding
1 parent 5619dd2 commit d474851

File tree

6 files changed

+79
-22
lines changed

6 files changed

+79
-22
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ go get -u github.com/devinyf/dashscopego
2424
- [x] [自定义工具调用](./example/function_call/main.go)
2525
#### 通义万相
2626
- [x] [文本生成图像](./example/wanx/img_generation.go)
27+
- [ ] 涂鸦作画
28+
- [ ] AnyText图文融合
2729
- [ ] 人像风格重绘
2830
- [ ] 图像背景生成
29-
#### Paraformer(语音识别)
31+
#### Paraformer(语音识别文字)
3032
- [x] [实时语音识别](./example/paraformer/speech2text.go)
3133
- [ ] 录音文件识别
34+
#### 语音合成
35+
- [ ] 文本至语音的实时流式合成
36+
#### 通用文本向量 Embedding
37+
- [x] [同步接口](./example/embedding/main.go)
38+
- [ ] 批处理接口
3239

3340
#### langchaingo-Agent
3441
- TODO...

embedding/dtypes.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
package embedding
22

33
type Request struct {
4-
Model string `json:"model"`
5-
Input struct {
6-
Texts []string `json:"texts"`
7-
} `json:"input"`
8-
Params struct {
9-
TextType string `json:"text_type"` // query or document
10-
} `json:"parameters"`
4+
Model string `json:"model"`
5+
Input Input `json:"input"`
6+
Params Params `json:"parameters"`
7+
}
8+
9+
type Input struct {
10+
Texts []string `json:"texts"`
11+
}
12+
13+
type Params struct {
14+
TextType string `json:"text_type"` // query or document
1115
}
1216

1317
type Response struct {
1418
Output Output `json:"output"`
19+
Usgae struct {
20+
TotalTokens int `json:"total_tokens"`
21+
} `json:"usage"`
22+
RequestID string `json:"request_id"`
1523
}
1624

1725
type Embedding struct {
@@ -21,8 +29,4 @@ type Embedding struct {
2129

2230
type Output struct {
2331
Embeddings []Embedding `json:"embeddings"`
24-
Usgae struct {
25-
TotalTokens int `json:"total_tokens"`
26-
} `json:"usage"`
27-
RequestID string `json:"request_id"`
2832
}

embedding/embeddings.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ import (
99
//nolint:lll
1010
func CreateEmbedding(ctx context.Context, req *Request, cli httpclient.IHttpClient, token string) (*Response, error) {
1111
if req.Model == "" {
12-
req.Model = EmbeddingV2
12+
req.Model = TextEmbeddingV2
1313
}
1414
if req.Params.TextType == "" {
1515
req.Params.TextType = TypeDocument
1616
}
1717

1818
resp := Response{}
1919
tokenOption := httpclient.WithTokenHeaderOption(token)
20-
err := cli.Post(ctx, embeddingURL, req, &resp, tokenOption)
20+
headerOption := httpclient.WithHeader(httpclient.HeaderMap{"content-type": "application/json"})
21+
err := cli.Post(ctx, embeddingURL, req, &resp, tokenOption, headerOption)
2122
if err != nil {
2223
return nil, err
2324
}

embedding/params.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const (
77
type ModelEmbedding = string
88

99
const (
10-
EmbeddingV1 = "text-embedding-v1"
11-
EmbeddingAsyncV1 = "text-embedding-async-v1"
12-
EmbeddingV2 = "text-embedding-v2"
13-
EmbeddingAsyncV2 = "text-embedding-async-v2"
10+
TextEmbeddingV1 = "text-embedding-v1"
11+
TextEmbeddingAsyncV1 = "text-embedding-async-v1"
12+
TextEmbeddingV2 = "text-embedding-v2"
13+
TextEmbeddingAsyncV2 = "text-embedding-async-v2"
1414
)
1515

1616
type TextType = string

example/embedding/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/devinyf/dashscopego"
9+
"github.com/devinyf/dashscopego/embedding"
10+
)
11+
12+
func main() {
13+
model := embedding.TextEmbeddingV2
14+
token := os.Getenv("DASHSCOPE_API_KEY")
15+
if token == "" {
16+
panic("token is empty")
17+
}
18+
19+
textInputs := []string{"风急天高猿啸哀", "渚清沙白鸟飞回", "无边落木萧萧下", "不尽长江滚滚来"}
20+
21+
cli := dashscopego.NewTongyiClient(model, token)
22+
ctx := context.TODO()
23+
24+
req := &embedding.Request{
25+
Model: model,
26+
Params: embedding.Params{
27+
TextType: embedding.TypeQuery,
28+
},
29+
Input: embedding.Input{
30+
Texts: textInputs,
31+
},
32+
}
33+
34+
embeddings, totalToken, err := cli.CreateEmbedding(ctx, req)
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
//nolint:all
40+
fmt.Println("embeddings:", embeddings)
41+
//nolint:all
42+
fmt.Println("used tokens:", totalToken)
43+
}

tongyiclient.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,22 @@ func (q *TongyiClient) CreateSpeechToTextGeneration(ctx context.Context, request
224224
return nil
225225
}
226226

227-
func (q *TongyiClient) CreateEmbedding(ctx context.Context, r *embedding.Request) ([][]float32, error) {
227+
func (q *TongyiClient) CreateEmbedding(ctx context.Context, r *embedding.Request) ([][]float32, int, error) {
228228
resp, err := embedding.CreateEmbedding(ctx, r, q.httpCli, q.token)
229229
if err != nil {
230-
return nil, err
230+
return nil, 0, err
231231
}
232+
233+
totslTokens := resp.Usgae.TotalTokens
232234
if len(resp.Output.Embeddings) == 0 {
233-
return nil, ErrEmptyResponse
235+
return nil, 0, ErrEmptyResponse
234236
}
235237

236238
embeddings := make([][]float32, 0)
237239
for i := 0; i < len(resp.Output.Embeddings); i++ {
238240
embeddings = append(embeddings, resp.Output.Embeddings[i].Embedding)
239241
}
240-
return embeddings, nil
242+
return embeddings, totslTokens, nil
241243
}
242244

243245
func paylosdPreCheck[T qwen.IQwenContent](q *TongyiClient, payload *qwen.Request[T]) *qwen.Request[T] {

0 commit comments

Comments
 (0)