You need to enable JavaScript to run this app.
导航
BotChatCompletions
最近更新时间:2024.08.12 14:15:42首次发布时间: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{
        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)
                }
            }
        }
    }
}

设置自定义 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{
        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)
                }
            }
        }
    }
}