micro--go-micro
081e375f29
* docs: add AI provider integration guide and Supported AI Providers section Add a step-by-step guide for AI infrastructure companies to implement ai.Model and contribute a provider to go-micro. Covers the full lifecycle: skeleton, tool call handling, tests, registration, and PR checklist. Add a "Supported AI Providers" section to the project README that lists current providers (Anthropic, OpenAI) in a table and links to the integration guide with a call-to-action for new providers and sponsors. Streamline the "Adding a New Provider" section in ai/README.md to point to the new guide instead of duplicating a full code listing. * fix: remove nonexistent Discord link from README * fix(website): set content container width to 800px on desktop Move the 800px max-width from .markdown-body up to .content so the entire content pane (not just the inner body) is sized correctly. The container now fills up to 800px beside the sidebar. * feat(ai): wire Atlas Cloud into server and auto-detection Import atlascloud provider in the micro server so it is available when running micro run / micro server. Add atlascloud to AutoDetectProvider so --ai_base_url with an atlascloud domain selects the right provider automatically. * feat(ai): add Google Gemini provider Add ai/gemini implementing ai.Model for Google's Gemini API. Uses the native generateContent endpoint with system_instruction, contents/parts, and functionDeclarations — not an OpenAI shim. Default model gemini-2.5-flash, auth via x-goog-api-key header. Wire into micro server imports and AutoDetectProvider (matches googleapis.com and google in base URL). Update README.md and ai/README.md with provider listing. * feat(ai): add Groq, Mistral, and Together AI providers Add three new OpenAI-compatible providers: - ai/groq: ultra-fast inference, default model llama-3.3-70b-versatile - ai/mistral: Mistral AI, default model mistral-large-latest - ai/together: Together AI, default model Llama-3.3-70B-Instruct-Turbo All three are wired into the micro server imports and AutoDetectProvider. README and ai/README updated with the full provider table. * feat(ai): add ai/tools helper and 'micro chat' interactive agent Extract the registry-discovery + RPC-execution loop from the web agent playground into a reusable ai/tools package: - tools.New(reg) creates a Set bound to a registry - Set.Discover() walks the registry and returns []ai.Tool with LLM-safe (underscored) names, remembering the mapping back to the original dotted form - Set.Handler(client) returns an ai.ToolHandler that resolves the safe name and issues the RPC Add cmd/micro/chat — an interactive 'micro chat' REPL that uses ai/tools to let users talk to their services through any registered AI provider. Supports --prompt for single-shot use, auto-detects the provider from --base_url, and falls back to the provider's conventional env var (ANTHROPIC_API_KEY, etc). Update README with the new command and the programmatic example. * feat(examples): add gRPC interop example Add examples/grpc-interop showing that any standard gRPC client can call a go-micro service — no go-micro SDK required on the client side. Includes: - proto/greeter.proto with generated Go, gRPC, and micro stubs - server/ using go-micro gRPC transport - client/ using stock google.golang.org/grpc (no go-micro imports) - README with Python example and explanation of how routing works Addresses the confusion from issue #2818 where users didn't know that go-micro gRPC services are callable by any gRPC client. --------- Co-authored-by: Claude <noreply@anthropic.com>
142 行
4.0 KiB
Go
142 行
4.0 KiB
Go
// Package ai provides abstraction for AI model providers
|
|
package ai
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
)
|
|
|
|
// Model provides an interface for interacting with AI model providers
|
|
type Model interface {
|
|
// Init initializes the model with options
|
|
Init(...Option) error
|
|
// Options returns the model options
|
|
Options() Options
|
|
// Generate generates a response from the model
|
|
Generate(ctx context.Context, req *Request, opts ...GenerateOption) (*Response, error)
|
|
// Stream generates a streaming response (for future implementation)
|
|
Stream(ctx context.Context, req *Request, opts ...GenerateOption) (Stream, error)
|
|
// String returns the name of the provider
|
|
String() string
|
|
}
|
|
|
|
// Tool represents a tool/function that can be called by the model
|
|
type Tool struct {
|
|
Name string // LLM-safe name (e.g., "greeter_Greeter_Hello")
|
|
OriginalName string // Original name (e.g., "greeter.Greeter.Hello")
|
|
Description string
|
|
Properties map[string]any // JSON schema for tool parameters
|
|
}
|
|
|
|
// Request represents a request to generate content from a model
|
|
type Request struct {
|
|
// Prompt is the user's message/prompt
|
|
Prompt string
|
|
// SystemPrompt is the system instruction for the model
|
|
SystemPrompt string
|
|
// Tools available for the model to use
|
|
Tools []Tool
|
|
// Messages for continuing a conversation (optional)
|
|
Messages []Message
|
|
}
|
|
|
|
// Message represents a conversation message
|
|
type Message struct {
|
|
Role string // "user", "assistant", "system", "tool"
|
|
Content any // Can be string or structured content
|
|
}
|
|
|
|
// Response represents the response from a model
|
|
type Response struct {
|
|
// Reply is the text response from the model
|
|
Reply string
|
|
// ToolCalls are tool calls requested by the model
|
|
ToolCalls []ToolCall
|
|
// Answer is the final answer after tool execution (if tools were used)
|
|
Answer string
|
|
}
|
|
|
|
// ToolCall represents a request to call a tool
|
|
type ToolCall struct {
|
|
ID string // Tool call ID (for correlation)
|
|
Name string // Tool name
|
|
Input map[string]any // Tool input arguments
|
|
}
|
|
|
|
// ToolResult represents the result of a tool execution
|
|
type ToolResult struct {
|
|
ID string // Tool call ID (for correlation)
|
|
Content string // Tool execution result (JSON string)
|
|
}
|
|
|
|
// Stream is the interface for streaming responses (future implementation)
|
|
type Stream interface {
|
|
// Recv receives the next chunk of the response
|
|
Recv() (*Response, error)
|
|
// Close closes the stream
|
|
Close() error
|
|
}
|
|
|
|
// ToolHandler is a function that handles tool calls
|
|
type ToolHandler func(name string, input map[string]any) (result any, content string)
|
|
|
|
// NewFunc creates a new Model instance
|
|
type NewFunc func(...Option) Model
|
|
|
|
var providers = make(map[string]NewFunc)
|
|
|
|
// Register registers a model provider
|
|
func Register(name string, fn NewFunc) {
|
|
providers[name] = fn
|
|
}
|
|
|
|
// New creates a new Model instance based on the provider name
|
|
func New(provider string, opts ...Option) Model {
|
|
if fn, ok := providers[provider]; ok {
|
|
return fn(opts...)
|
|
}
|
|
|
|
// Default to first registered provider
|
|
if len(providers) > 0 {
|
|
for _, fn := range providers {
|
|
return fn(opts...)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// AutoDetectProvider attempts to detect the provider from the base URL
|
|
func AutoDetectProvider(baseURL string) string {
|
|
if baseURL == "" {
|
|
return "openai"
|
|
}
|
|
switch {
|
|
case strings.Contains(baseURL, "anthropic"):
|
|
return "anthropic"
|
|
case strings.Contains(baseURL, "atlascloud"):
|
|
return "atlascloud"
|
|
case strings.Contains(baseURL, "googleapis.com"), strings.Contains(baseURL, "google"):
|
|
return "gemini"
|
|
case strings.Contains(baseURL, "groq"):
|
|
return "groq"
|
|
case strings.Contains(baseURL, "mistral"):
|
|
return "mistral"
|
|
case strings.Contains(baseURL, "together"):
|
|
return "together"
|
|
default:
|
|
return "openai"
|
|
}
|
|
}
|
|
|
|
// DefaultModel is a default model instance
|
|
var DefaultModel Model
|
|
|
|
// Generate generates a response using the default model
|
|
func Generate(ctx context.Context, req *Request, opts ...GenerateOption) (*Response, error) {
|
|
if DefaultModel == nil {
|
|
return nil, nil
|
|
}
|
|
return DefaultModel.Generate(ctx, req, opts...)
|
|
}
|