Go SDK
更新时间:
环境依赖
Go 1.18+
安装
go get github.com/tongyi-xingchen/xingchen-sdk-go@v1.0.14
非固定角色对话
流式调用
package main
import (
"context"
"fmt"
"io"
)
import xingchen "github.com/tongyi-xingchen/xingchen-sdk-go/xingchen"
func main() {
configuration := xingchen.NewConfiguration()
apiClient := xingchen.NewAPIClient(configuration)
brearer := "{API-KEY}"
ctx := context.WithValue(context.Background(), xingchen.ContextAccessToken, brearer)
chatReqParams := buildChatReqParams()
chatReqParams.Streaming = xingchen.PtrBool(true)
chatResultStream, err := apiClient.ChatApiSub.Chat(ctx).ChatReqParams(chatReqParams).StreamExecute()
if err != nil {
return
}
defer chatResultStream.Close()
for {
resp, err := chatResultStream.Recv()
if err == io.EOF {
break
}
fmt.Println(*resp.Data.Choices[0].Messages[0].Content)
}
}
func buildChatReqParams() xingchen.ChatReqParams {
chatReqParam := xingchen.ChatReqParams{
BotProfile: xingchen.BotProfile{
Name: xingchen.PtrString("小婉"),
Content: xingchen.PtrString("【你的人设】\\n"),
},
ModelParameters: &xingchen.ModelParameters{
Seed: xingchen.PtrInt64(1683806810),
TopP: xingchen.PtrFloat64(0.8),
Temperature: xingchen.PtrFloat64(0.8),
IncrementalOutput: xingchen.PtrBool(false),
},
UserProfile: xingchen.UserProfile{
UserId: "1234",
},
ChatSamples: []xingchen.ChatSampleItem{
{
Name: xingchen.PtrString("小明"),
Role: xingchen.PtrString("user"),
Content: xingchen.PtrString("你在干嘛啊"),
},
{
Name: xingchen.PtrString("小婉"),
Role: xingchen.PtrString("assistant"),
Content: xingchen.PtrString("在想你啊~[[想你]] 你呢?"),
},
{
Name: xingchen.PtrString("小明"),
Role: xingchen.PtrString("user"),
Content: xingchen.PtrString("我在吃饭呢"),
},
{
Name: xingchen.PtrString("小婉"),
Role: xingchen.PtrString("assistant"),
Content: xingchen.PtrString("我也是!你看我在吃沙拉~"),
},
},
Messages: []xingchen.Message{
{
Name: xingchen.PtrString("小明"),
Content: xingchen.PtrString("你叫什么名字"),
Role: xingchen.PtrString("user"),
},
{
Name: xingchen.PtrString("小婉"),
Content: xingchen.PtrString("我叫小婉啊"),
Role: xingchen.PtrString("assistant"),
},
{
Name: xingchen.PtrString("小明"),
Content: xingchen.PtrString("你今年多大"),
Role: xingchen.PtrString("user"),
},
{
Name: xingchen.PtrString("小婉"),
Content: xingchen.PtrString("我今年17岁了"),
Role: xingchen.PtrString("assistant"),
},
{
Name: xingchen.PtrString("小明"),
Content: xingchen.PtrString("你今年多大"),
Role: xingchen.PtrString("user"),
},
},
AdvancedSettings: &xingchen.AdvancedSettings{
EnableWebSearch: xingchen.PtrBool(true),
SearchEnhancedKeyword: xingchen.PtrString("开启网络搜索关键字"),
},
Memory: &xingchen.Memory{
Summaries: []string{
"user和assistant计划一起去打篮球",
},
Originals: []xingchen.Message{
{
Name: xingchen.PtrString("小明"),
Role: xingchen.PtrString("user"),
Content: xingchen.PtrString("你叫什么名字"),
},
{
Name: xingchen.PtrString("小婉"),
Role: xingchen.PtrString("assistant"),
Content: xingchen.PtrString("我叫小婉啊"),
},
},
Tags: []xingchen.MemorySchema{
{
Role: xingchen.PtrString("user"),
Key: xingchen.PtrString("职业"),
Value: []string{
"武术家",
},
},
{
Role: xingchen.PtrString("assistant"),
Key: xingchen.PtrString("喜欢"),
Value: []string{
"武术",
},
},
},
},
PlatformPlugins: []interface{}{
*xingchen.NewRejectAnswerPlugin(
[]xingchen.RejectCondition{
{
Enabled: xingchen.PtrBool(true),
ConditionType: xingchen.PtrString("reject_rule"),
Keywords: []xingchen.Keyword{
{
Value: xingchen.PtrString("政治"),
},
{
Value: xingchen.PtrString("宗教"),
},
{
Value: xingchen.PtrString("敏感事件"),
},
},
},
{
Enabled: xingchen.PtrBool(true),
ConditionType: xingchen.PtrString("passive_rule"),
Keywords: []xingchen.Keyword{
{
Value: xingchen.PtrString("年龄"),
},
},
},
{
Enabled: xingchen.PtrBool(true),
ConditionType: xingchen.PtrString("knowledge_domain_rule"),
SubRejectCondition: &xingchen.RejectCondition{
ConditionType: xingchen.PtrString("ancient"),
Keywords: []xingchen.Keyword{
{
Value: xingchen.PtrString("古代"),
},
},
},
},
}),
},
}
return chatReqParam
}
非增量输出
我今年
我今年17岁了,很快就要
我今年17岁了,很快就要步入大学了。
我今年17岁了,很快就要步入大学了。
增量输出
我今年
17岁了,很快就要
步入大学了。
非流式调用
package main
import (
"context"
"fmt"
)
import xingchen "github.com/tongyi-xingchen/xingchen-sdk-go/xingchen"
func main() {
configuration := xingchen.NewConfiguration()
apiClient := xingchen.NewAPIClient(configuration)
brearer := "{API-KEY}"
ctx := context.WithValue(context.Background(), xingchen.ContextAccessToken, brearer)
chatReqParams := buildChatReqParams()
resp, httpRes, err := apiClient.ChatApiSub.Chat(ctx).ChatReqParams(chatReqParams).Execute()
if err != nil || httpRes == nil {
return
}
fmt.Println(*resp.Data.Choices[0].Messages[0].Content)
}
func buildChatReqParams() xingchen.ChatReqParams {
chatReqParam := xingchen.ChatReqParams{
BotProfile: xingchen.BotProfile{
Name: xingchen.PtrString("小婉"),
Content: xingchen.PtrString("【你的人设】\\n"),
},
ModelParameters: &xingchen.ModelParameters{
Seed: xingchen.PtrInt64(1683806810),
TopP: xingchen.PtrFloat64(0.8),
Temperature: xingchen.PtrFloat64(0.8),
IncrementalOutput: xingchen.PtrBool(false),
},
UserProfile: xingchen.UserProfile{
UserId: "1234",
},
ChatSamples: []xingchen.ChatSampleItem{
{
Name: xingchen.PtrString("小明"),
Role: xingchen.PtrString("user"),
Content: xingchen.PtrString("你在干嘛啊"),
},
{
Name: xingchen.PtrString("小婉"),
Role: xingchen.PtrString("assistant"),
Content: xingchen.PtrString("在想你啊~[[想你]] 你呢?"),
},
{
Name: xingchen.PtrString("小明"),
Role: xingchen.PtrString("user"),
Content: xingchen.PtrString("我在吃饭呢"),
},
{
Name: xingchen.PtrString("小婉"),
Role: xingchen.PtrString("assistant"),
Content: xingchen.PtrString("我也是!你看我在吃沙拉~"),
},
},
Messages: []xingchen.Message{
{
Name: xingchen.PtrString("小明"),
Content: xingchen.PtrString("你叫什么名字"),
Role: xingchen.PtrString("user"),
},
{
Name: xingchen.PtrString("小婉"),
Content: xingchen.PtrString("我叫小婉啊"),
Role: xingchen.PtrString("assistant"),
},
{
Name: xingchen.PtrString("小明"),
Content: xingchen.PtrString("你今年多大"),
Role: xingchen.PtrString("user"),
},
{
Name: xingchen.PtrString("小婉"),
Content: xingchen.PtrString("我今年17岁了"),
Role: xingchen.PtrString("assistant"),
},
{
Name: xingchen.PtrString("小明"),
Content: xingchen.PtrString("你今年多大"),
Role: xingchen.PtrString("user"),
},
},
AdvancedSettings: &xingchen.AdvancedSettings{
EnableWebSearch: xingchen.PtrBool(true),
SearchEnhancedKeyword: xingchen.PtrString("开启网络搜索关键字"),
},
Memory: &xingchen.Memory{
Summaries: []string{
"user和assistant计划一起去打篮球",
},
Originals: []xingchen.Message{
{
Name: xingchen.PtrString("小明"),
Role: xingchen.PtrString("user"),
Content: xingchen.PtrString("你叫什么名字"),
},
{
Name: xingchen.PtrString("小婉"),
Role: xingchen.PtrString("assistant"),
Content: xingchen.PtrString("我叫小婉啊"),
},
},
Tags: []xingchen.MemorySchema{
{
Role: xingchen.PtrString("user"),
Key: xingchen.PtrString("职业"),
Value: []string{
"武术家",
},
},
{
Role: xingchen.PtrString("assistant"),
Key: xingchen.PtrString("喜欢"),
Value: []string{
"武术",
},
},
},
},
PlatformPlugins: []interface{}{
*xingchen.NewRejectAnswerPlugin(
[]xingchen.RejectCondition{
{
Enabled: xingchen.PtrBool(true),
ConditionType: xingchen.PtrString("reject_rule"),
Keywords: []xingchen.Keyword{
{
Value: xingchen.PtrString("政治"),
},
{
Value: xingchen.PtrString("宗教"),
},
{
Value: xingchen.PtrString("敏感事件"),
},
},
},
{
Enabled: xingchen.PtrBool(true),
ConditionType: xingchen.PtrString("passive_rule"),
Keywords: []xingchen.Keyword{
{
Value: xingchen.PtrString("年龄"),
},
},
},
{
Enabled: xingchen.PtrBool(true),
ConditionType: xingchen.PtrString("knowledge_domain_rule"),
SubRejectCondition: &xingchen.RejectCondition{
ConditionType: xingchen.PtrString("ancient"),
Keywords: []xingchen.Keyword{
{
Value: xingchen.PtrString("古代"),
},
},
},
},
}),
},
}
return chatReqParam
}
其他
kv抽取
package test
import (
"context"
"github.com/tongyi-xingchen/xingchen-sdk-go/xingchen"
"github.com/stretchr/testify/assert"
"testing"
)
func initClient() (*xingchen.APIClient, context.Context) {
configuration := xingchen.NewConfiguration()
apiClient := xingchen.NewAPIClient(configuration)
brearer := "{API-KEY}"
ctx := context.WithValue(context.Background(), xingchen.ContextAccessToken, brearer)
return apiClient, ctx
}
func Test_desc_generate(t *testing.T) {
apiClient, ctx := initClient()
req := openapiclient.CharacterDescGenerateRequest{
Type: openapiclient.PtrString("file"),
FileName: openapiclient.PtrString("test.txt"),
FileUrl: openapiclient.PtrString("https://lang.alicdn.com/xingchen/guanyu.txt"),
ModelParameters: &openapiclient.ModelParameters{
ModelName: openapiclient.PtrString("xingchen-base"),
},
}
resp, httpRes, err := apiClient.CharacterApiSub.CharacterDescGenerate(ctx).
CharacterDescGenerate(req).Execute()
testAssert := assert.New(t)
testAssert.Nil(err)
testAssert.NotNil(resp)
testAssert.Equal(httpRes.StatusCode, 200)
}
summary抽取
package test
import (
"context"
"github.com/tongyi-xingchen/xingchen-sdk-go/xingchen"
"github.com/stretchr/testify/assert"
"testing"
)
func initClient() (*xingchen.APIClient, context.Context) {
configuration := xingchen.NewConfiguration()
apiClient := xingchen.NewAPIClient(configuration)
brearer := "{API-KEY}"
ctx := context.WithValue(context.Background(), xingchen.ContextAccessToken, brearer)
return apiClient, ctx
}
func Test_extract_memory_ky(t *testing.T) {
apiClient, ctx := initClient()
request := openapiclient.ExtractMemoryKVRequest{
Messages: []openapiclient.Message{
{
Content: openapiclient.PtrString("你好"),
Role: openapiclient.PtrString("user"),
},
{
Content: openapiclient.PtrString("你好呀"),
Role: openapiclient.PtrString("assistant"),
},
{
Content: openapiclient.PtrString("我们喜欢打篮球,你呢"),
Role: openapiclient.PtrString("user"),
},
{
Content: openapiclient.PtrString("我喜欢踢足球"),
Role: openapiclient.PtrString("assistant"),
},
},
ModelParameters: &openapiclient.ModelParameters{
ModelName: openapiclient.PtrString("xingchen-base"),
},
KVMemoryConfigs: []openapiclient.KVMemoryConfig{
{
Enabled: openapiclient.PtrBool(true),
MemoryText: openapiclient.PtrString("职业"),
},
{
Enabled: openapiclient.PtrBool(true),
MemoryText: openapiclient.PtrString("喜好"),
},
{
Enabled: openapiclient.PtrBool(true),
MemoryText: openapiclient.PtrString("性别"),
},
},
}
resp, httpRes, err := apiClient.ChatExtractMessageApiSubService.ExtractMemoryKV(ctx).ExtractMemoryKVRequest(request).Execute()
testAssert := assert.New(t)
testAssert.Nil(err)
testAssert.NotNil(resp)
testAssert.Equal(httpRes.StatusCode, 200)
}
角色描述自动生成
package test
import (
"context"
"github.com/tongyi-xingchen/xingchen-sdk-go/xingchen"
"github.com/stretchr/testify/assert"
"testing"
)
func initClient() (*xingchen.APIClient, context.Context) {
configuration := xingchen.NewConfiguration()
apiClient := xingchen.NewAPIClient(configuration)
brearer := "{API-KEY}"
ctx := context.WithValue(context.Background(), xingchen.ContextAccessToken, brearer)
return apiClient, ctx
}
func Test_desc_generate(t *testing.T) {
apiClient, ctx := initClient()
req := openapiclient.CharacterDescGenerateRequest{
Type: openapiclient.PtrString("file"),
FileName: openapiclient.PtrString("test.txt"),
FileUrl: openapiclient.PtrString("https://lang.alicdn.com/xingchen/guanyu.txt"),
ModelParameters: &openapiclient.ModelParameters{
ModelName: openapiclient.PtrString("xingchen-base"),
},
}
resp, httpRes, err := apiClient.CharacterApiSub.CharacterDescGenerate(ctx).
CharacterDescGenerate(req).Execute()
testAssert := assert.New(t)
testAssert.Nil(err)
testAssert.NotNil(resp)
testAssert.Equal(httpRes.StatusCode, 200)
}
终止对话
package test
import (
"context"
"github.com/tongyi-xingchen/xingchen-sdk-go/xingchen"
"github.com/stretchr/testify/assert"
"testing"
)
func initClient() (*xingchen.APIClient, context.Context) {
configuration := xingchen.NewConfiguration()
apiClient := xingchen.NewAPIClient(configuration)
brearer := "{API-KEY}"
ctx := context.WithValue(context.Background(), xingchen.ContextAccessToken, brearer)
return apiClient, ctx
}
func Test_stop_chat(t *testing.T) {
apiClient, ctx := initClient()
stopChatReq := openapiclient.StopChatRequest{
SessionId: openapiclient.PtrString("xxx"),
RequestId: openapiclient.PtrString("xxx"),
Content: openapiclient.PtrString("测试终止对话"),
}
resp, httpRes, err := apiClient.ChatApiSub.StopChat(ctx).StopChatRequest(stopChatReq).Execute()
testAssert := assert.New(t)
testAssert.Nil(err)
testAssert.NotNil(resp)
testAssert.Equal(httpRes.StatusCode, 200)
}
文档内容是否对您有帮助?