说明
示例代码中 <YOUR_BOT_ID>
需要替换为您在平台上创建的智能体 ID。
package main import ( "context" "fmt" "io" "os" "github.com/volcengine/volcengine-go-sdk/service/arkruntime" "github.com/volcengine/volcengine-go-sdk/service/arkruntime/model" "github.com/volcengine/volcengine-go-sdk/volcengine" ) func main() { client := arkruntime.NewClientWithApiKey(os.Getenv("ARK_API_KEY")) ctx := context.Background() fmt.Println("----- standard request -----") req := model.BotChatCompletionRequest{ BotId: "<YOUR_BOT_ID>", Messages: []*model.ChatCompletionMessage{ { Role: model.ChatMessageRoleUser, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("北京今天的天气如何?"), }, }, }, } resp, err := client.CreateBotChatCompletion(ctx, req) if err != nil { fmt.Printf("standard chat error: %v\n", err) return } fmt.Println(*resp.Choices[0].Message.Content.StringValue) if resp.References != nil { for _, ref := range resp.References { fmt.Printf("reference url: %s\n", ref.Url) } } }
package main import ( "context" "fmt" "io" "os" "github.com/volcengine/volcengine-go-sdk/service/arkruntime" "github.com/volcengine/volcengine-go-sdk/service/arkruntime/model" "github.com/volcengine/volcengine-go-sdk/volcengine" ) func main() { client := arkruntime.NewClientWithApiKey(os.Getenv("ARK_API_KEY")) ctx := context.Background() fmt.Println("----- multiple rounds request -----") req := model.BotChatCompletionRequest{ BotId: "<YOUR_BOT_ID>", Messages: []*model.ChatCompletionMessage{ { Role: model.ChatMessageRoleSystem, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("你是豆包,字节跳动开发的 AI 人工智能助手"), }, }, { Role: model.ChatMessageRoleUser, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("花椰菜是什么?"), }, }, { Role: model.ChatMessageRoleAssistant, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("花椰菜又称菜花、花菜,是一种常见的蔬菜。"), }, }, { Role: model.ChatMessageRoleUser, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("再详细点"), }, }, }, } resp, err := client.CreateBotChatCompletion(ctx, req) if err != nil { fmt.Printf("standard chat error: %v\n", err) return } fmt.Println(*resp.Choices[0].Message.Content.StringValue) if resp.References != nil { for _, ref := range resp.References { fmt.Printf("reference url: %s\n", ref.Url) } } }
package main import ( "context" "fmt" "io" "os" "github.com/volcengine/volcengine-go-sdk/service/arkruntime" "github.com/volcengine/volcengine-go-sdk/service/arkruntime/model" "github.com/volcengine/volcengine-go-sdk/volcengine" ) func main() { client := arkruntime.NewClientWithApiKey(os.Getenv("ARK_API_KEY")) ctx := context.Background() fmt.Println("----- streaming request -----") req = model.BotChatCompletionRequest{ BotId: "<YOUR_BOT_ID>", Messages: []*model.ChatCompletionMessage{ { Role: model.ChatMessageRoleUser, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("北京今天的天气如何?"), }, }, }, } stream, err := client.CreateBotChatCompletionStream(ctx, req) if err != nil { fmt.Printf("stream chat error: %v\n", err) return } defer stream.Close() for { recv, err := stream.Recv() if err == io.EOF { return } if err != nil { fmt.Printf("Stream chat error: %v\n", err) return } if len(recv.Choices) > 0 { fmt.Print(recv.Choices[0].Delta.Content) if recv.References != nil { for _, ref := range recv.References { fmt.Printf("reference url: %s\n", ref.Url) } } } } }
package main import ( "context" "fmt" "io" "os" "github.com/volcengine/volcengine-go-sdk/service/arkruntime" "github.com/volcengine/volcengine-go-sdk/service/arkruntime/model" "github.com/volcengine/volcengine-go-sdk/volcengine" ) func main() { client := arkruntime.NewClientWithApiKey(os.Getenv("ARK_API_KEY")) ctx := context.Background() fmt.Println("----- standard request -----") req := model.BotChatCompletionRequest{ BotId: "<YOUR_BOT_ID>", Messages: []*model.ChatCompletionMessage{ { Role: model.ChatMessageRoleUser, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("北京今天的天气如何?"), }, }, }, } resp, err := client.CreateChatCompletion( ctx, req, arkruntime.WithCustomHeader(model.ClientRequestHeader, "20240627112200D3FE1CFF83C5D5523085") ), if err != nil { fmt.Printf("standard chat error: %v\n", err) return } fmt.Println(*resp.Choices[0].Message.Content.StringValue) if resp.References != nil { for _, ref := range resp.References { fmt.Printf("reference url: %s\n", ref.Url) } } }
package main import ( "context" "fmt" "io" "os" "github.com/volcengine/volcengine-go-sdk/service/arkruntime" "github.com/volcengine/volcengine-go-sdk/service/arkruntime/model" "github.com/volcengine/volcengine-go-sdk/volcengine" ) func main() { client := arkruntime.NewClientWithApiKey(os.Getenv("ARK_API_KEY")) ctx := context.Background() fmt.Println("----- streaming request -----") req = model.BotChatCompletionRequest{ BotId: "<YOUR_BOT_ID>", Messages: []*model.ChatCompletionMessage{ { Role: model.ChatMessageRoleUser, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("北京今天的天气如何?"), }, }, }, } stream, err := client.CreateBotChatCompletionStream(ctx, req) if err != nil { apiErr := &model.APIError{} if errors.As(err, &apiErr) { fmt.Printf("stream chat error: %v\n", apiErr) } return } defer stream.Close() for { recv, err := stream.Recv() if err == io.EOF { return } if err != nil { fmt.Printf("Stream chat error: %v\n", err) return } if len(recv.Choices) > 0 { fmt.Print(recv.Choices[0].Delta.Content) if recv.References != nil { for _, ref := range recv.References { fmt.Printf("reference url: %s\n", ref.Url) } } } } }