说明
示例代码中 <YOUR_ENDPOINT_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.ChatCompletionRequest{ Model: "<YOUR_ENDPOINT_ID>", Messages: []*model.ChatCompletionMessage{ { Role: model.ChatMessageRoleSystem, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("你是豆包,是由字节跳动开发的 AI 人工智能助手"), }, }, { Role: model.ChatMessageRoleUser, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("常见的十字花科植物有哪些?"), }, }, }, } resp, err := client.CreateChatCompletion(ctx, req) if err != nil { fmt.Printf("standard chat error: %v\n", err) return } fmt.Println(*resp.Choices[0].Message.Content.StringValue) }
package main import ( "context" "fmt" "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.ChatCompletionRequest{ Model: "<YOUR_ENDPOINT_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.CreateChatCompletion(ctx, req) if err != nil { fmt.Printf("standard chat error: %v\n", err) return } fmt.Println(*resp.Choices[0].Message.Content.StringValue) }
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.ChatCompletionRequest{ Model: "<YOUR_ENDPOINT_ID>", Messages: []*model.ChatCompletionMessage{ { Role: model.ChatMessageRoleSystem, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("你是豆包,是由字节跳动开发的 AI 人工智能助手"), }, }, { Role: model.ChatMessageRoleUser, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("常见的十字花科植物有哪些?"), }, }, }, } stream, err := client.CreateChatCompletionStream(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) } } }
package main import ( "context" "encoding/json" "fmt" "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("----- function call request -----") req := model.ChatCompletionRequest{ Model: "<YOUR_ENDPOINT_ID>", Messages: []*model.ChatCompletionMessage{ { Role: model.ChatMessageRoleUser, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("北京今天天气如何?"), }, }, }, Tools: []*model.Tool{ { Type: model.ToolTypeFunction, Function: &model.FunctionDefinition{ Name: "get_current_weather", Description: "获取给定地点的天气", Parameters: map[string]interface{}{ "type": "object", "properties": map[string]interface{}{ "location": map[string]interface{}{ "type": "string", "description": "地点的位置信息,比如北京", }, "unit": map[string]interface{}{ "type": "string", "enum": []string{"摄氏度", "华氏度"}, }, }, "required": []string{"location"}, }, }, }, }, } resp, err := client.CreateChatCompletion(ctx, req) if err != nil { fmt.Printf("standard chat error: %v\n", err) return } s, _ := json.Marshal(resp) fmt.Println(string(s)) }
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.ChatCompletionRequest{ Model: "<YOUR_ENDPOINT_ID>", Messages: []*model.ChatCompletionMessage{ { Role: model.ChatMessageRoleSystem, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("你是豆包,是由字节跳动开发的 AI 人工智能助手"), }, }, { Role: model.ChatMessageRoleUser, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("常见的十字花科植物有哪些?"), }, }, }, } // 自定义request id 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) }
package main import ( "context" "errors" "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.ChatCompletionRequest{ Model: "<YOUR_ENDPOINT_ID>", Messages: []*model.ChatCompletionMessage{ { Role: model.ChatMessageRoleSystem, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("你是豆包,是由字节跳动开发的 AI 人工智能助手"), }, }, { Role: model.ChatMessageRoleUser, Content: &model.ChatCompletionMessageContent{ StringValue: volcengine.String("常见的十字花科植物有哪些?"), }, }, }, } stream, err := client.CreateChatCompletionStream(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 { apiErr := &model.APIError{} if errors.As(err, &apiErr) { fmt.Printf("stream chat error: %v\n", apiErr) } return } if len(recv.Choices) > 0 { fmt.Print(recv.Choices[0].Delta.Content) } } }