项目文件夹

文件
Asim Aslam c26d74a8a1 Add CRUD, pub/sub, and API gateway templates; update AI features (#2909)
* feat(website): redesign docs and blog layouts, add blog header images

Redesign both layouts to match the new landing page:
- Consistent nav bar with logo, Docs, Blog, GitHub, Reference, Home
- Consistent footer with copyright and links
- CSS custom properties for theming
- Updated typography, spacing, and code block styling
- Active sidebar link highlighting in docs
- Dark mode support preserved

Generate 4 blog header images via Atlas Cloud:
- blog-deploy.png for post 1 (micro deploy)
- blog-mcp.png for posts 2, 3, 7 (MCP-related)
- blog-agents-demo.png for post 4 (agents demo)
- blog-dx.png for post 5 (DX cleanup)
- Reuse data-model.png for post 6 (model package)

All 7 existing blog posts now have header images.

* fix(website): prevent horizontal scroll on mobile landing page

Add overflow-x: hidden on html and body. Set max-width: 100% and
height: auto on all section and two-col images. Add overflow:
hidden to .two-col grid. Constrain hero pre with max-width and
overflow-x. Reduce font sizes and padding at mobile breakpoint.

* feat(website): add images to remaining core doc pages

Generate 5 more images via Atlas Cloud for docs:
- registry.png: service discovery diagram
- broker.png: pub/sub message broker pattern
- transport.png: multi-transport layers (HTTP, gRPC, NATS)
- config.png: dynamic configuration from multiple sources
- observability.png: monitoring dashboard with metrics/traces

Add images to registry.md, broker.md, transport.md, config.md,
observability.md, and architecture.md. All 11 main doc pages
now have header images.

* feat: add sponsor logos to landing page, README images, and flows blog post

Add Anthropic and Atlas Cloud sponsor logos to the landing page
with links to their respective blog posts. Logos display at 0.7
opacity with hover effect.

Add architecture and MCP agent images to the GitHub README for
the Overview and MCP sections.

Write blog post 9: "From Chat to Flows" — explores the concept
of LLM-powered service orchestration. Compares micro chat's
interactive model with persistent event-driven flows, shows how
the existing building blocks (ai/tools, History, broker) could
compose into a flow engine, discusses tradeoffs vs traditional
orchestration (Step Functions, Temporal), and includes a working
15-line code example. Explicitly positions it as a concept for
community feedback, not an announcement.

* feat(website): add animated hero video to landing page

Generate a 6-second hero video via Atlas Cloud's image-to-video
API (gemini-omni-flash). Shows the microservices network diagram
animating with data flowing between nodes.

Replace the static hero image with an autoplay muted looping
video element. Falls back to the static image via poster
attribute and img fallback for browsers without video support.

* feat(ai): add VideoModel interface with Atlas Cloud provider

Add ai.VideoModel interface for video generation alongside Model
and ImageModel. Supports text-to-video and image-to-video via
VideoRequest with prompt, reference images, duration, aspect
ratio, and resolution fields.

Implement GenerateVideo for Atlas Cloud using their async API:
POST /api/v1/model/generateVideo → poll /api/v1/model/prediction.
Default model is gemini-omni-flash image-to-video. Polls every
5 seconds until completion or context cancellation.

Register Atlas Cloud as a video provider via ai.RegisterVideo.
Add 3 tests: registration, no-key error, compile-time interface
check. Update ai/README.md with VideoModel docs.

The ai package now covers all three modalities:
- Model (text) — 7 providers
- ImageModel (image) — 2 providers (Atlas Cloud, OpenAI)
- VideoModel (video) — 1 provider (Atlas Cloud)

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-29 15:52:35 +01:00
..

AI Package

The ai package provides simple, high-level interfaces for AI model providers. It supports text generation (Model), image generation (ImageModel), and video generation (VideoModel).

Interfaces

Text Generation (Model)

The Model interface follows the same patterns as other go-micro packages (Registry, Client, Broker):

type Model interface {
    Init(...Option) error
    Options() Options
    Generate(ctx context.Context, req *Request, opts ...GenerateOption) (*Response, error)
    Stream(ctx context.Context, req *Request, opts ...GenerateOption) (Stream, error)
    String() string
}

Quick Start

import (
    "context"
    "go-micro.dev/v5/ai"
    _ "go-micro.dev/v5/ai/anthropic"
    _ "go-micro.dev/v5/ai/openai"
)

// Create a model
m := ai.New("openai",
    ai.WithAPIKey("your-api-key"),
    ai.WithModel("gpt-4o"),
)

// Generate a response
req := &ai.Request{
    Prompt:       "What is Go?",
    SystemPrompt: "You are a helpful programming assistant",
}

resp, err := m.Generate(context.Background(), req)
if err != nil {
    log.Fatal(err)
}

fmt.Println(resp.Reply)

Image Generation (ImageModel)

type ImageModel interface {
    GenerateImage(ctx context.Context, req *ImageRequest, opts ...GenerateOption) (*ImageResponse, error)
    String() string
}
import (
    "go-micro.dev/v5/ai"
    _ "go-micro.dev/v5/ai/atlascloud"
)

ig := ai.NewImage("atlascloud",
    ai.WithAPIKey("your-api-key"),
)

resp, err := ig.GenerateImage(context.Background(), &ai.ImageRequest{
    Prompt: "A Go gopher in space",
    Size:   "1024x1024",
})

fmt.Println(resp.Images[0].URL)

Providers that support image generation: Atlas Cloud, OpenAI.

Video Generation (VideoModel)

type VideoModel interface {
    GenerateVideo(ctx context.Context, req *VideoRequest, opts ...GenerateOption) (*VideoResponse, error)
    String() string
}
import (
    "go-micro.dev/v5/ai"
    _ "go-micro.dev/v5/ai/atlascloud"
)

vg := ai.NewVideo("atlascloud",
    ai.WithAPIKey("your-api-key"),
)

resp, err := vg.GenerateVideo(context.Background(), &ai.VideoRequest{
    Prompt:   "Microservices nodes animating with data flowing between them",
    Images:   []string{"https://example.com/diagram.png"}, // optional: image-to-video
    Duration: 6,
})

fmt.Println(resp.URL)

Providers that support video generation: Atlas Cloud.

Options

Configure the model using functional options:

m := ai.New("anthropic",
    ai.WithAPIKey("your-key"),              // Required
    ai.WithModel("claude-sonnet-4-20250514"), // Optional, uses provider default
    ai.WithBaseURL("https://api.anthropic.com"), // Optional, uses provider default
)

You can also update options after creation:

m.Init(
    ai.WithModel("gpt-4o-mini"),
    ai.WithAPIKey("new-key"),
)

Using Tools

The model can automatically execute tool calls when provided with a tool handler:

// Define a tool handler
toolHandler := func(name string, input map[string]any) (result any, content string) {
    // Execute the tool and return results
    switch name {
    case "get_weather":
        return map[string]string{"temp": "72F"}, `{"temp": "72F"}`
    default:
        return nil, `{"error": "unknown tool"}`
    }
}

// Create model with tool handler
m := ai.New("openai",
    ai.WithAPIKey("your-key"),
    ai.WithToolHandler(toolHandler),
)

// Provide tools in the request
req := &ai.Request{
    Prompt: "What's the weather?",
    SystemPrompt: "You are a helpful assistant",
    Tools: []ai.Tool{
        {
            Name:        "get_weather",
            Description: "Get current weather",
            Properties: map[string]any{
                "location": map[string]any{
                    "type": "string",
                    "description": "City name",
                },
            },
        },
    },
}

// Generate will automatically call tools and return final answer
resp, err := m.Generate(context.Background(), req)
fmt.Println(resp.Answer) // Final answer after tool execution

Response Structure

type Response struct {
    Reply     string      // Initial reply from model
    ToolCalls []ToolCall  // Tools the model wants to call
    Answer    string      // Final answer (after tool execution if handler provided)
}
  • Reply: The model's first response
  • ToolCalls: List of tools the model requested (if any)
  • Answer: The final answer after tools are executed (only set if ToolHandler is provided)

Supported Providers

Anthropic Claude

m := ai.New("anthropic",
    ai.WithAPIKey("sk-ant-..."),
    ai.WithModel("claude-sonnet-4-20250514"), // default
)

Default model: claude-sonnet-4-20250514 Default base URL: https://api.anthropic.com

OpenAI GPT

m := ai.New("openai",
    ai.WithAPIKey("sk-..."),
    ai.WithModel("gpt-4o"), // default
)

Default model: gpt-4o Default base URL: https://api.openai.com

Google Gemini

m := ai.New("gemini",
    ai.WithAPIKey("your-key"),
    ai.WithModel("gemini-2.5-flash"), // default
)

Default model: gemini-2.5-flash Default base URL: https://generativelanguage.googleapis.com

Google Gemini uses its own API format with system_instruction, contents (not messages), and functionDeclarations for tool calling. The provider handles the translation automatically.

Groq

m := ai.New("groq",
    ai.WithAPIKey("your-key"),
    ai.WithModel("llama-3.3-70b-versatile"), // default
)

Default model: llama-3.3-70b-versatile Default base URL: https://api.groq.com/openai

Groq provides ultra-fast inference for open-weight models via an OpenAI-compatible endpoint.

Mistral

m := ai.New("mistral",
    ai.WithAPIKey("your-key"),
    ai.WithModel("mistral-large-latest"), // default
)

Default model: mistral-large-latest Default base URL: https://api.mistral.ai

Mistral AI is a European AI company offering high-performance models via an OpenAI-compatible endpoint.

Together AI

m := ai.New("together",
    ai.WithAPIKey("your-key"),
    ai.WithModel("meta-llama/Llama-3.3-70B-Instruct-Turbo"), // default
)

Default model: meta-llama/Llama-3.3-70B-Instruct-Turbo Default base URL: https://api.together.xyz

Together AI provides fast inference for open-weight models via an OpenAI-compatible endpoint.

Atlas Cloud

m := ai.New("atlascloud",
    ai.WithAPIKey("your-key"),
    ai.WithModel("llama-3.3-70b"), // default
)

Default model: llama-3.3-70b Default base URL: https://api.atlascloud.ai

Atlas Cloud is an enterprise AI infrastructure platform offering high-performance LLM APIs. It exposes an OpenAI-compatible chat completions endpoint with tool calling support.

Auto-Detection

Use AutoDetectProvider() to detect the provider from a base URL:

provider := ai.AutoDetectProvider("https://api.anthropic.com")
// Returns "anthropic"

m := ai.New(provider, ai.WithAPIKey("..."))

Adding a New Provider

See the full AI Provider Integration Guide for a step-by-step walkthrough, checklist, and design notes.

Quick summary:

  1. Create ai/yourprovider/yourprovider.go implementing ai.Model.
  2. Call ai.Register("yourprovider", ...) in init().
  3. Add tests in ai/yourprovider/yourprovider_test.go.
  4. Users enable the provider with a blank import:
import _ "go-micro.dev/v5/ai/yourprovider"

We welcome contributions and sponsorships from AI infrastructure companies — see the guide for details.

Comparison with Other Packages

The ai package follows the same patterns as other go-micro packages:

Registry:

r := registry.NewRegistry(registry.Addrs("..."))
r.Register(service)

Client:

c := client.NewClient(client.Retries(3))
c.Call(ctx, req, rsp)

AI:

m := ai.New("openai", ai.WithAPIKey("..."))
m.Generate(ctx, req)

All use:

  • Init() to update options
  • Options() to get current options
  • String() to get the implementation name
  • Functional options pattern

Testing

go test ./ai/...

Examples

See the server implementation for a complete example of using the ai package with tool execution.