You need to enable JavaScript to run this app.
导航
BotChatCompletions
最近更新时间:2025.01.16 23:13:56首次发布时间:2024.08.12 14:15:42

前提条件

示例代码

说明

示例代码中 <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{
        // 将<YOUR_BOT_ID>替换为您的应用ID
        Model: "<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{
        // 将<YOUR_BOT_ID>替换为您的应用ID 
        Model: "<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{
        // 将<YOUR_BOT_ID>替换为您的应用ID 
        Model: "<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{ 
        // 将<YOUR_BOT_ID>替换为您的应用ID 
        Model: "<YOUR_BOT_ID>", 
        Messages: []*model.ChatCompletionMessage{ 
            { 
                Role: model.ChatMessageRoleUser, 
                Content: &model.ChatCompletionMessageContent{ 
                    StringValue: volcengine.String("你是谁?"), 
                }, 
            }, 
        }, 
        Metadata: map[string]interface{}{
            "target_character_name": "群聊角色",
            "group_chat_config": map[string]interface{}{
               "user_name":   "用户角色名",
               "description": "场景描述",
               "characters": []map[string]interface{}{
                  {
                     "name":          "群聊角色",
                     "system_prompt": "群聊角色设定",
                     "model_desc": map[string]interface{}{
                        "endpoint_id": "<YOUR_ENDPOINT_ID>",
                     },
                  },
               },
            },
        },
    } 
 
    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) 
        } 
    } 
} 

设置自定义 header

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{
        // 将<YOUR_BOT_ID>替换为您的应用ID 
        Model: "<YOUR_BOT_ID>",
        Messages: []*model.ChatCompletionMessage{
            {
                Role: model.ChatMessageRoleUser,
                Content: &model.ChatCompletionMessageContent{
                    StringValue: volcengine.String("北京今天的天气如何?"),
                },
            },
        },
    }

    resp, err := client.CreateBotChatCompletion(
        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{
        // 将<YOUR_BOT_ID>替换为您的应用ID
        Model: "<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)
                }
            }
        }
    }
}

图片理解

应用已经能够支持视觉理解模型。您可以在应用中配置视觉理解模型的推理接入点,使应用具备理解图片、视频能力,处理如图片文字内容识别,根据图片绘制表格、开发代码等任务。

更详细的视觉理解模型使用教程请参见视觉理解

package main

import (
    "context"
    "fmt"
    "os"
    "github.com/volcengine/volcengine-go-sdk/service/arkruntime"
    "github.com/volcengine/volcengine-go-sdk/service/arkruntime/model"
)

func main() {
    client := arkruntime.NewClientWithApiKey(
        os.Getenv("ARK_API_KEY"),
    )
    ctx := context.Background()
    req := model.BotChatCompletionRequest{
        // 将<YOUR_BOT_ID>替换为您的应用ID
        Model: "<YOUR_BOT_ID>",
        Messages: []*model.ChatCompletionMessage{
            {
                Role: model.ChatMessageRoleUser,
                Content: &model.ChatCompletionMessageContent{
                    ListValue: []*model.ChatCompletionMessageContentPart{
                        {
                            Type: model.ChatCompletionMessageContentPartTypeText,
                            Text: "对美国人来说最安全的支付APP是什么",
                        },
                        {
                            Type: model.ChatCompletionMessageContentPartTypeImageURL,
                            ImageURL: &model.ChatMessageImageURL{
                                URL: "https://p9-arcosite.byteimg.com/tos-cn-i-goo7wpa0wc/7d2e7e810f054952aa8854fc99577260~tplv-goo7wpa0wc-image.image",
                            },
                        },
                    },
                },
            },
        },
    }
    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)
        }
    }
}

Bot SDK与Chat SDK差异对比

对比项

Bot SDK

Chat SDK

单轮

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{// 注意此处存在差异
        // 将<YOUR_BOT_ID>替换为您的应用ID
        Model: "<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("----- standard request -----")
    req := model.ChatCompletionRequest{// 注意此处存在差异
       Model: "<YOUR_ENDPOINT_ID>",// 注意此处为您的推理接入点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"
    "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{// 注意此处存在差异
        // 将<YOUR_BOT_ID>替换为您的应用ID
        Model: "<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"
    "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>",// 注意此处为您的推理接入点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.BotChatCompletionRequest{// 注意此处存在差异
        // 将<YOUR_BOT_ID>替换为您的应用ID
        Model: "<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("----- streaming request -----")
    req := model.ChatCompletionRequest{// 注意此处存在差异
       Model: "<YOUR_ENDPOINT_ID>",// 注意此处为您的推理接入点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)
       }
    }
}

自定义header

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{// 注意此处存在差异
        // 将<YOUR_BOT_ID>替换为您的应用ID
        Model: "<YOUR_BOT_ID>",
        Messages: []*model.ChatCompletionMessage{
            {
                Role: model.ChatMessageRoleUser,
                Content: &model.ChatCompletionMessageContent{
                    StringValue: volcengine.String("北京今天的天气如何?"),
                },
            },
        },
    }
    // 自定义request id
    resp, err := client.CreateBotChatCompletion(// 注意此处存在差异
        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("----- standard request -----")
    req := model.ChatCompletionRequest{// 注意此处存在差异
       Model: "<YOUR_ENDPOINT_ID>",// 注意此处为您的推理接入点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"
    "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{// 注意此处存在差异
        // 将<YOUR_BOT_ID>替换为您的应用ID
        Model: "<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)
                }
            }
        }
    }
}
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>",// 注意此处为您的推理接入点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)
       }
    }
}