Documentation
¶
Overview ¶
Package fmgo provides pure Go access to Apple's Foundation Models CLI.
fmgo invokes the native fm executable. It is not a Swift bridge, does not require cgo, does not access private Apple frameworks, and does not reimplement Foundation Models or provide its own CLI.
fmgo requires macOS 27 or later, the native fm executable, an available Foundation Model, and accepted Foundation Models CLI terms. New validates the platform and returns ErrUnsupportedPlatform or ErrUnsupportedVersion when the machine is incompatible. Operations return ErrFMNotFound when the executable cannot be located.
Create a Client with New, then use Respond or Stream for generation. SchemaFor and RespondAs support structured output. Client also exposes token counting, availability and terms inspection, interactive chat sessions, and lifecycle management for the native fm serve process. All blocking operations accept context.Context.
Example ¶
package main
import (
"context"
"fmt"
"github.com/ruanklein/fmgo"
)
func main() {
client, err := fmgo.New()
if err != nil {
return
}
response, err := client.Respond(context.Background(), fmgo.Request{
Prompt: "Explain goroutines.",
Instructions: "Be concise.",
})
if err != nil {
return
}
fmt.Println(response.Text)
}
Output:
Index ¶
- Variables
- func RespondAs[T any](ctx context.Context, client *Client, request Request) (T, error)
- type Availability
- type ChatOptions
- type ChatSession
- type Client
- func (c *Client) Available(ctx context.Context) (Availability, error)
- func (c *Client) CountTokens(ctx context.Context, request TokenRequest) (int, error)
- func (c *Client) GenerateSchema(ctx context.Context, object ObjectSchema) (Schema, error)
- func (c *Client) License(ctx context.Context) (LicenseStatus, error)
- func (c *Client) Respond(ctx context.Context, request Request) (Response, error)
- func (c *Client) Serve(ctx context.Context, options ServerOptions) (*Server, error)
- func (c *Client) ShowLicense(ctx context.Context) (string, error)
- func (c *Client) StartChat(ctx context.Context, options ChatOptions) (*ChatSession, error)
- func (c *Client) Stream(ctx context.Context, request Request) (*Stream, error)
- type CommandError
- type GuardrailLevel
- type LicenseStatus
- type Model
- type ModelAvailability
- type ObjectSchema
- type Option
- type Request
- type Response
- type Schema
- type SchemaProperty
- type SchemaType
- type Server
- type ServerOptions
- type Stream
- type TokenRequest
- type Tool
- type UseCase
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupportedPlatform reports a system other than macOS. ErrUnsupportedPlatform = platform.ErrUnsupportedPlatform // ErrUnsupportedVersion reports a macOS version earlier than 27. ErrUnsupportedVersion = platform.ErrUnsupportedVersion // ErrFMNotFound reports that the native fm executable could not be located. ErrFMNotFound = errors.New("fm executable not found") ErrModelUnavailable = errors.New("foundation model unavailable") // ErrLicenseRequired reports that the Foundation Models CLI terms have not been accepted. ErrLicenseRequired = errors.New("foundation models license not accepted") // ErrGuardrailViolation reports that the native model rejected content under its guardrails. ErrGuardrailViolation = errors.New("foundation model guardrail violation") // ErrUnexpectedOutput reports output that fmgo cannot safely interpret. ErrUnexpectedOutput = errors.New("unexpected fm output") )
Functions ¶
Types ¶
type Availability ¶
type Availability struct {
Models []ModelAvailability
}
Availability contains statuses reported by fm available.
type ChatOptions ¶
type ChatOptions struct {
Model Model
Instructions string
Resume string
Continue bool
SetDefaultModel Model
Tools []Tool
}
ChatOptions configures an interactive fm chat session.
type ChatSession ¶
type ChatSession struct {
// contains filtered or unexported fields
}
ChatSession owns the standard streams and process for an interactive chat.
func (*ChatSession) Close ¶
func (s *ChatSession) Close() error
Close terminates and reaps the chat process. It is safe to call more than once.
func (*ChatSession) Stderr ¶
func (s *ChatSession) Stderr() io.ReadCloser
Stderr returns the chat process error stream.
func (*ChatSession) Stdin ¶
func (s *ChatSession) Stdin() io.WriteCloser
Stdin returns the chat process input stream.
func (*ChatSession) Stdout ¶
func (s *ChatSession) Stdout() io.ReadCloser
Stdout returns the chat process output stream.
func (*ChatSession) Wait ¶
func (s *ChatSession) Wait() error
Wait waits for the chat process after its output streams have been consumed.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client invokes the native fm executable.
func (*Client) Available ¶
func (c *Client) Available(ctx context.Context) (Availability, error)
Available reports Foundation Models availability.
func (*Client) CountTokens ¶
CountTokens counts Foundation Models tokens.
func (*Client) GenerateSchema ¶
GenerateSchema creates a schema through fm schema object.
func (*Client) License ¶
func (c *Client) License(ctx context.Context) (LicenseStatus, error)
License reports whether the Foundation Models CLI terms were accepted.
func (*Client) Serve ¶
Serve starts fm serve and waits until its configured address accepts connections.
func (*Client) ShowLicense ¶
ShowLicense returns the Foundation Models CLI terms without prompting.
func (*Client) StartChat ¶
func (c *Client) StartChat(ctx context.Context, options ChatOptions) (*ChatSession, error)
StartChat starts an interactive chat session.
type CommandError ¶
type CommandError struct {
Command string
ExitCode int
Stderr string
// contains filtered or unexported fields
}
CommandError describes a failed native fm invocation.
func (*CommandError) Error ¶
func (e *CommandError) Error() string
func (*CommandError) Unwrap ¶
func (e *CommandError) Unwrap() error
Unwrap returns a recognized operational cause when one is available.
type GuardrailLevel ¶
type GuardrailLevel string
GuardrailLevel configures Apple's supported guardrail level.
const ( GuardrailDefault GuardrailLevel = "default" GuardrailPermissiveContentTransformations GuardrailLevel = "permissive-content-transformations" )
type LicenseStatus ¶
type LicenseStatus struct {
Accepted bool
}
LicenseStatus reports whether the local machine accepted the fm terms.
type Model ¶
type Model string
Model identifies a Foundation Model exposed by fm.
const ( // ModelSystem is the on-device Apple Foundation Model. ModelSystem Model = "system" )
type ModelAvailability ¶
ModelAvailability describes whether a model is ready to use.
type ObjectSchema ¶
type ObjectSchema struct {
Name string
Properties []SchemaProperty
}
ObjectSchema describes an object passed to fm schema object.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithExecutable ¶
WithExecutable overrides the fm executable path.
type Request ¶
type Request struct {
Prompt string
Instructions string
Model Model
Text []string
Images []string
ImageLabels []string
Tools []Tool
Schema Schema
SchemaFile string
Resume string
SaveTranscript string
Greedy bool
Verbose bool
UseCase UseCase
Guardrails GuardrailLevel
}
Request configures a Foundation Models response.
type Response ¶
type Response struct {
Text string
}
Response is a non-streaming Foundation Models response.
type Schema ¶
type Schema json.RawMessage
Schema is inline JSON Schema accepted by fm respond.
func ParseSchema ¶
ParseSchema validates inline JSON Schema.
func SchemaFor ¶
SchemaFor generates JSON Schema for T without invoking fm.
Example ¶
package main
import (
"github.com/ruanklein/fmgo"
)
func main() {
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
schema, err := fmgo.SchemaFor[Person]()
if err != nil {
return
}
_ = schema
}
Output:
func SchemaFrom ¶
SchemaFrom generates JSON Schema for a Go value's type.
type SchemaProperty ¶
type SchemaProperty struct {
Name string
Type SchemaType
Description string
Optional bool
Array bool
Schema Schema
Choices []Schema
}
SchemaProperty describes a native fm schema property.
type SchemaType ¶
type SchemaType string
SchemaType identifies a property declaration accepted by fm schema object.
const ( SchemaString SchemaType = "string" SchemaInteger SchemaType = "integer" SchemaDouble SchemaType = "double" SchemaBoolean SchemaType = "boolean" SchemaObject SchemaType = "object" SchemaAnyOf SchemaType = "anyOf" )
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server manages a native fm serve process.
type ServerOptions ¶
ServerOptions configures the native fm serve process.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream incrementally reads a response from fm. Text returns the latest read block.
type TokenRequest ¶
type TokenRequest struct {
Prompt string
Instructions string
Text []string
Images []string
Transcript string
}
TokenRequest describes content to count with fm count-tokens.