助手
扣子 AI 帮助与支持
你好,我是 扣子 文档问答助手 🎉 你在阅读当前文档的过程中,无论对文档概念的解释,还是文档内容方面的疑问,都可以随时向我提问,我会全力为你解答
推荐问题
如何快速了解这个空间的核心内容?
有哪些近期更新的重点文档?
我应该从哪些文档开始阅读?
文档反馈

调用 Prompt

更新于: 2026-06-24 15:45:09

扣子罗盘 SDK 支持调用预定义的 Prompt 来与 AI 模型交互,文档介绍如何通过 Go SDK 实现这一功能。

场景介绍

通过扣子罗盘 Go SDK 调用预定义的 Prompt 模板时,可以选择以下两种调用方式:

  • 非流式调用(nonStream):一次性获取完整的 AI 响应结果。
  • 流式调用(stream):逐段获取 AI 的响应内容,类似实时对话的打字效果。

准备工作

  • 已集成 v0.1.12 及以上版本的扣子罗盘 Go SDK。
  • 已创建 Prompt 模板并提交版本。操作步骤可参考开发提示词
  • 获取以下配置信息:
    • COZELOOP_WORKSPACE_ID:扣子罗盘 Prompt 所在工作空间的 ID。你可以在 Prompt 详情页 URL 的 space 关键词之后获取工作空间 ID,例如 https://loop.coze.cn/console/enterprise/personal/space/73917415734092****/pe/prompts/75115667262537**** 中,73917415734092****为工作空间 ID。
    • COZELOOP_API_TOKEN:扣子罗盘访问密钥,应具备 executePrompt 权限。获取密钥及授权方式可参考SDK 鉴权

示例代码

以下示例代码演示如何通过扣子罗盘 Go SDK 调用一个预定义的 Prompt 模板。调用模板时传入具体参数,然后获取 AI 模型对 “解释机器学习” 这个问题的简短回答,同时演示了同步获取完整结果和异步流式获取结果两种方式。
关于调用 Prompt 的更多使用示例,例如指定版本标识执行 Prompt、多模态 Prompt、传递 Prompt 变量、超时设置等,可参考 Github

package main

import (
        "context"
        "fmt"
        "io"

        "github.com/coze-dev/cozeloop-go"
        "github.com/coze-dev/cozeloop-go/entity"
        "github.com/coze-dev/cozeloop-go/internal/util"
)

func main() {
        // 1.Create a prompt on the platform
        // Create a Prompt on the platform's Prompt development page (set Prompt Key to 'ptaas_demo'),
        // add the following messages to the template, submit a version.
        // System: You are a helpful assistant for {{topic}}.
        // User: Please help me with {{user_request}}
        ctx := context.Background()

        // Set the following environment variables first.
        // COZELOOP_WORKSPACE_ID=your workspace id
        // COZELOOP_API_TOKEN=your token
        // 2.New loop client
        client, err := cozeloop.NewClient()
        if err != nil {
                panic(err)
        }
        defer client.Close(ctx)

        // 3. Execute prompt
        executeRequest := &entity.ExecuteParam{
                PromptKey: "ptaas_demo",
                Version:   "0.0.1",
                VariableVals: map[string]any{
                        "topic":        "artificial intelligence",
                        "user_request": "explain what is machine learning",
                },
                // You can also append messages to the prompt.
                Messages: []*entity.Message{
                        {
                                Role:    entity.RoleUser,
                                Content: util.Ptr("Keep the answer brief."),
                        },
                },
        }
        // 3.1 non stream
        nonStream(ctx, client, executeRequest)
        // 3.2 stream
        stream(ctx, client, executeRequest)
}

func nonStream(ctx context.Context, client cozeloop.Client, executeRequest *entity.ExecuteParam) {
        result, err := client.Execute(ctx, executeRequest)
        if err != nil {
                panic(err)
        }
        printExecuteResult(result)
}

func stream(ctx context.Context, client cozeloop.Client, executeRequest *entity.ExecuteParam) {
        streamReader, err := client.ExecuteStreaming(ctx, executeRequest)
        if err != nil {
                panic(err)
        }
        for {
                result, err := streamReader.Recv()
                if err != nil {
                        if err == io.EOF {
                                fmt.Println("\nStream finished.")
                                break
                        }
                        panic(err)
                }
                printExecuteResult(result)
        }
}

func printExecuteResult(result entity.ExecuteResult) {
        if result.Message != nil {
                fmt.Printf("Message: %s\n", util.ToJSON(result.Message))
        }
        if util.PtrValue(result.FinishReason) != "" {
                fmt.Printf("FinishReason: %s\n", util.PtrValue(result.FinishReason))
        }
        if result.Usage != nil {
                fmt.Printf("Usage: %s\n", util.ToJSON(result.Usage))
        }
}