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{
CharacterId: xingchen.PtrString("40f70d5466e1429ba9aa755842b35d9f"),
Name: xingchen.PtrString("小婉"),
},
ModelParameters: &xingchen.ModelParameters{
Seed: xingchen.PtrInt64(1683806810),
IncrementalOutput: xingchen.PtrBool(false),
},
UserProfile: xingchen.UserProfile{
UserId: "1234",
},
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"),
},
},
}
return chatReqParam
}
固定角色对话
流式输出
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{
CharacterId: xingchen.PtrString("40f70d5466e1429ba9aa755842b35d9f"),
Name: xingchen.PtrString("小婉"),
},
ModelParameters: &xingchen.ModelParameters{
Seed: xingchen.PtrInt64(1683806810),
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"),
},
},
}
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{
CharacterId: xingchen.PtrString("40f70d5466e1429ba9aa755842b35d9f"),
Name: xingchen.PtrString("小婉"),
},
ModelParameters: &xingchen.ModelParameters{
Seed: xingchen.PtrInt64(1683806810),
},
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"),
},
},
}
return chatReqParam
}
我今年17岁了呢
角色管理
创建角色
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_character_create(t *testing.T) {
apiClient, ctx := initClient()
characterCreateDTO := xingchen.CharacterCreateDTO{
Name: "满腔忠义的关云长",
Type: xingchen.PtrString("virtual"),
Avatar: &xingchen.FileInfoVO{
FileUrl: xingchen.PtrString("https://gw.alicdn.com/imgextra/i3/O1CN01iVuwX11jCXzNxM4v9_!!6000000004512-0-tps-3000-2920.jpg"),
Filename: xingchen.PtrString("关云长.jpg"),
},
Introduction: "满腔忠义的关云长",
Traits: xingchen.PtrString("请在对话时尽可能的展现你的性格、感情, 用文言文回答, 并使用古人的语气和用词。"),
OpeningLine: "我是「关云长」,很高心与你玩游戏",
BasicInformation: "我们来玩一个角色扮演的游戏, 你是「满腔忠义的关云长」。",
ChatExample: xingchen.PtrString("{{user}}:敢问阁下尊姓大名。\\\\n{{char}}:吾姓关名羽,字长生,后改云长,河东解良人也。"),
PermConfig: &xingchen.CharacterPermissionConfig{
AllowChat: 0,
AllowApi: 0,
IsPublic: 0,
},
RoleTypes: []string{
"ACG",
"accompany",
"movie",
},
}
resp, httpRes, err := apiClient.CharacterApiSub.Create(ctx).CharacterCreateDTO(characterCreateDTO).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_character_update(t *testing.T) {
apiClient, ctx := initClient()
characterUpdateDTO := xingchen.CharacterUpdateDTO{
CharacterId: "ecb6cb93e7464a5cb6c1e32a51a42e29",
Name: "满腔忠义的关云长",
Type: xingchen.PtrString("virtual"),
Avatar: &xingchen.FileInfoVO{
FileUrl: xingchen.PtrString("https://gw.alicdn.com/imgextra/i3/O1CN01iVuwX11jCXzNxM4v9_!!6000000004512-0-tps-3000-2920.jpg"),
Filename: xingchen.PtrString("关云长.jpg"),
},
Introduction: "满腔忠义的关云长",
Traits: xingchen.PtrString("请在对话时尽可能的展现你的性格、感情, 用文言文回答, 并使用古人的语气和用词。"),
OpeningLine: "我是「关云长」,很高心与你玩游戏",
BasicInformation: "我们来玩一个角色扮演的游戏, 你是「满腔忠义的关云长」。",
ChatExample: xingchen.PtrString("{{user}}:敢问阁下尊姓大名。\\\\n{{char}}:吾姓关名羽,字长生,后改云长,河东解良人也。"),
}
resp, httpRes, err := apiClient.CharacterApiSub.Update(ctx).CharacterUpdateDTO(characterUpdateDTO).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_character_detail(t *testing.T) {
apiClient, ctx := initClient()
resp, httpRes, err := apiClient.CharacterApiSub.CharacterDetails(ctx).CharacterId("ecb6cb93e7464a5cb6c1e32a51a42e29").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_character_search(t *testing.T) {
apiClient, ctx := initClient()
query := xingchen.CharacterQueryDTO{
Where: &xingchen.CharacterQueryWhere{
Scope: xingchen.PtrString("my"),
RoleTypes: []string{
"ACG",
},
},
PageNum: xingchen.PtrInt64(1),
PageSize: xingchen.PtrInt64(10),
}
resp, httpRes, err := apiClient.CharacterApiSub.Search(ctx).CharacterQueryDTO(query).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_character_delete(t *testing.T) {
apiClient, ctx := initClient()
resp, httpRes, err := apiClient.CharacterApiSub.Delete(ctx).CharacterId("ecb6cb93e7464a5cb6c1e32a51a42e29").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"
"time"
)
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_chat_histories(t *testing.T) {
apiClient, ctx := initClient()
query := xingchen.ChatHistoryQueryDTO{
Where: &xingchen.ChatHistoryQueryWhere{
CharacterId: xingchen.PtrString("ecb6cb93e7464a5cb6c1e32a51a42e29"),
BizUserId: xingchen.PtrString("1234"),
EndTime: xingchen.PtrInt64(time.Now().Unix()),
},
PageSize: xingchen.PtrInt32(10),
}
resp, httpRes, err := apiClient.ChatMessageApiSub.ChatHistories(ctx).ChatHistoryQueryDTO(query).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_rate_message(t *testing.T) {
apiClient, ctx := initClient()
messageRatingRequest := xingchen.MessageRatingRequest{
MessageId: "b886b7790f044ac087602037717f210b",
Rating: 0,
}
resp, httpRes, err := apiClient.ChatMessageApiSub.RateMessage(ctx).MessageRatingRequest(messageRatingRequest).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_sys_reminder(t *testing.T) {
apiClient, ctx := initClient()
systemReminderRequest := xingchen.SysReminderRequest{
CharacterId: "ecb6cb93e7464a5cb6c1e32a51a42e29",
Content: "你在干嘛",
BizUserId: "fcf76e7b02914fa0b321cbf5662f3385",
}
resp, httpRes, err := apiClient.ChatMessageApiSub.SysReminder(ctx).SysReminderRequest(systemReminderRequest).Execute()
testAssert := assert.New(t)
testAssert.Nil(err)
testAssert.NotNil(resp)
testAssert.Equal(httpRes.StatusCode, 200)
}
文档内容是否对您有帮助?