Build Multi‑Modal AI Agents in Go with Kratos Blades: Quick Start Guide

This article introduces the open‑source Go AI Agent framework Blades, showing how to quickly set up a chat with a large model, enable streaming responses, and implement multi‑turn conversations using prompts, memory, and custom middleware, with complete code examples.

Go Programming World
Go Programming World
Go Programming World
Build Multi‑Modal AI Agents in Go with Kratos Blades: Quick Start Guide

Most Gophers are familiar with Kratos, Bilibili's open‑source Go microservice framework. Its ecosystem now includes Blades, an open‑source multi‑modal AI Agent framework that supports custom models, tools, memory, middleware, and is suitable for multi‑turn conversations, chain‑of‑thought reasoning, and structured output.

Quick Start

Chat with a large model:

package main

import (
    "context"
    "log"

    "github.com/go-kratos/blades"
    "github.com/go-kratos/blades/contrib/openai"
    "github.com/openai/openai-go/v2/option"
)

func main() {
    agent := blades.NewAgent(
        "Template Agent",
        blades.WithModel("deepseek-chat"),
        blades.WithProvider(openai.NewChatProvider(
            option.WithBaseURL("https://api.deepseek.com"),
            option.WithAPIKey("sk-xxx"),
        )),
    )
    // Define templates and params
    params := map[string]any{
        "topic":    "人工智能的未来",
        "audience": "普通读者",
    }
    // Build prompt using the template builder
    // Note: Use exported methods when calling from another package.
    prompt, err := blades.NewPromptTemplate().
        System("请将 {{.topic}} 总结为三个关键点。", params).
        User("对 {{.audience}} 受众做出简洁准确的回复。", params).
        Build()
    if err != nil {
        log.Fatal(err)
    }
    log.Println("Generated Prompt:", prompt.String())
    // Run the agent with the templated prompt
    result, err := agent.Run(context.Background(), prompt)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("Generated result:
%s", result.Text())
}
blades.NewAgent()

constructs an Agent object; the example uses the DeepSeek model. blades.NewPromptTemplate() builds a Prompt template; see the linked article for Builder pattern in Go. agent.Run() executes the Agent and returns the model’s output.

Running the program produces a prompt and a result summarizing “the future of AI” in three key points.

Streaming Output

Blades supports streaming output:

stream, err := agent.RunStream(context.Background(), prompt)
if err != nil {
    log.Fatal(err)
}
for stream.Next() {
    chunk, err := stream.Current()
    if err != nil {
        log.Fatalf("stream recv error: %v", err)
    }
    log.Print(chunk.Text())
}

Replace agent.Run() with agent.RunStream() to receive streamed chunks.

Multi‑turn Conversation

For multi‑turn dialogs, use memory to keep history:

package main

import (
    "context"
    "log"

    "github.com/go-kratos/blades"
    "github.com/go-kratos/blades/contrib/openai"
    "github.com/go-kratos/blades/memory"
    "github.com/openai/openai-go/v2/option"
)

func main() {
    agent := blades.NewAgent(
        "History Tutor",
        blades.WithModel("deepseek-chat"),
        blades.WithProvider(openai.NewChatProvider(
            option.WithBaseURL("https://api.deepseek.com"),
            option.WithAPIKey("sk-xxx"),
        )),
        blades.WithInstructions("你是一位知识渊博的历史导师。提供详细、准确的历史事件信息。"),
        blades.WithMemory(memory.NewInMemory(10)),
    )
    prompt := blades.NewConversation(
        "conversation_123",
        blades.UserMessage("你能告诉我第二次世界大战的起因吗?"),
    )
    result, err := agent.Run(context.Background(), prompt)
    if err != nil {
        log.Fatal(err)
    }
    log.Println(result.Text())

    prompt = blades.NewConversation(
        "conversation_123",
        blades.UserMessage("我刚刚问你的问题是什么?"),
    )
    result, err = agent.Run(context.Background(), prompt)
    if err != nil {
        log.Fatal(err)
    }
    log.Println(result.Text())
}

Use blades.WithMemory(memory.NewInMemory(10)) when creating the Agent and blades.NewConversation() with a shared conversation ID to enable multi‑turn dialogue.

Summary

Blades offers a new way to develop AI Agents in Go. This article presented three small examples—simple chat, streaming output, and multi‑turn conversation. More examples are available in the official GitHub repository.

Blades is still early stage; interested readers can explore the source code for deeper learning.

NOTE: The log omitted many streaming lines due to volume.

Blades GitHub source: https://github.com/go-kratos/blades

Builder pattern in Go: https://mp.weixin.qq.com/s/LRyb7tOTZmq33j9AwOd18g

Go 1.23 iterator usage article: https://mp.weixin.qq.com/s/t47eJ9rYK2CZ-hIbjx7kSg

Go 1.23 iterator theory article: https://mp.weixin.qq.com/s/Bgr4GSA2AivTMlnxd4UQiA

Article source code: https://github.com/jianghushinian/blog-go-example/tree/main/go-kratos/blades

Permanent article URL: https://jianghushinian.cn/2025/09/29/go-kratos-blades/

AIGoStreamingAgentPromptBladesconversation
Go Programming World
Written by

Go Programming World

Mobile version of tech blog https://jianghushinian.cn/, covering Golang, Docker, Kubernetes and beyond.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.