embedder

package
v1.37.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Hostname = embedderapi.Hostname
	Version  = 2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Intermediate

type Intermediate struct {
	*connector.Connector
	ToDo
}

Intermediate extends and customizes the generic base connector.

func NewIntermediate

func NewIntermediate(impl ToDo) *Intermediate

NewIntermediate creates a new instance of the intermediate.

func (*Intermediate) MaxWorkers

func (svc *Intermediate) MaxWorkers() (value int)

MaxWorkers caps how many calls into the Python venv may run concurrently.

func (*Intermediate) SetMaxWorkers

func (svc *Intermediate) SetMaxWorkers(value int) (err error)

SetMaxWorkers sets the value of the configuration property.

type Mock

type Mock struct {
	*Intermediate
	// contains filtered or unexported fields
}

Mock is a mockable version of the microservice, allowing functions, event sinks and web handlers to be mocked.

func NewMock

func NewMock() *Mock

NewMock creates a new mockable version of the microservice.

func (*Mock) Demo

func (svc *Mock) Demo(w http.ResponseWriter, r *http.Request) (err error)

Demo executes the mock handler.

func (*Mock) DemoInit

func (svc *Mock) DemoInit(w http.ResponseWriter, r *http.Request) (err error)

DemoInit executes the mock handler.

func (*Mock) DemoStatus

func (svc *Mock) DemoStatus(w http.ResponseWriter, r *http.Request) (err error)

DemoStatus executes the mock handler.

func (*Mock) Embed

func (svc *Mock) Embed(ctx context.Context, text string) (vector []float64, err error)

Embed executes the mock handler.

func (*Mock) MockDemo

func (svc *Mock) MockDemo(handler func(w http.ResponseWriter, r *http.Request) (err error)) *Mock

MockDemo sets up a mock handler for Demo.

func (*Mock) MockDemoInit

func (svc *Mock) MockDemoInit(handler func(w http.ResponseWriter, r *http.Request) (err error)) *Mock

MockDemoInit sets up a mock handler for DemoInit.

func (*Mock) MockDemoStatus

func (svc *Mock) MockDemoStatus(handler func(w http.ResponseWriter, r *http.Request) (err error)) *Mock

MockDemoStatus sets up a mock handler for DemoStatus.

func (*Mock) MockEmbed

func (svc *Mock) MockEmbed(handler func(ctx context.Context, text string) (vector []float64, err error)) *Mock

MockEmbed sets up a mock handler for Embed.

func (*Mock) MockSimilarity

func (svc *Mock) MockSimilarity(handler func(ctx context.Context, a string, b string) (score float64, err error)) *Mock

MockSimilarity sets up a mock handler for Similarity.

func (*Mock) OnShutdown

func (svc *Mock) OnShutdown(ctx context.Context) (err error)

OnShutdown is called when the microservice is shut down.

func (*Mock) OnStartup

func (svc *Mock) OnStartup(ctx context.Context) (err error)

OnStartup is called when the microservice is started up.

func (*Mock) Similarity

func (svc *Mock) Similarity(ctx context.Context, a string, b string) (score float64, err error)

Similarity executes the mock handler.

type Service

type Service struct {
	*Intermediate // IMPORTANT: Do not remove
	// contains filtered or unexported fields
}

Service implements the embedder.example microservice.

Embedder loads a sentence-transformers model in an in-process Python virtual environment via the github.com/microbus-io/pyvenv module and exposes two typed Go endpoints that delegate to it: Embed returns the vector for one string; Similarity returns the cosine similarity between two strings. The model loads on first Start of the venv and stays warm for subsequent calls.

func NewService

func NewService() *Service

NewService creates a new instance of the microservice.

func (*Service) Demo

func (svc *Service) Demo(w http.ResponseWriter, r *http.Request) (err error)

Demo serves the interactive demo page for the embedder.

func (*Service) DemoInit

func (svc *Service) DemoInit(w http.ResponseWriter, r *http.Request) (err error)

DemoInit kicks off Python venv Start in the background. Idempotent: a second invocation while initialization is pending or already ready returns the current status without restarting.

func (*Service) DemoStatus

func (svc *Service) DemoStatus(w http.ResponseWriter, r *http.Request) (err error)

DemoStatus is a long-poll endpoint. The client passes its last-seen ETag via If-None-Match; the server snapshots the current status + tailed logs, hashes them into an ETag, and:

  • returns 200 with the new ETag and body if the snapshot differs;
  • holds the connection (polling every 500ms) until the snapshot changes;
  • returns 204 No Content if the request context is within ~1s of its deadline without any change, so the client can immediately re-issue the request with the same ETag.

A fresh client (no If-None-Match) gets the current snapshot back on the first iteration.

func (*Service) Embed

func (svc *Service) Embed(ctx context.Context, text string) (vector []float64, err error)

Embed returns the sentence-embedding vector for the input text.

func (*Service) Init

func (svc *Service) Init(initializer func(svc *Service) (err error)) *Service

Init enables a single-statement pattern for initializing the microservice.

func (*Service) OnShutdown

func (svc *Service) OnShutdown(ctx context.Context) (err error)

OnShutdown is called when the microservice is shut down.

func (*Service) OnStartup

func (svc *Service) OnStartup(ctx context.Context) (err error)

OnStartup is called when the microservice is started up. The Python venv is constructed but not started; the demo page's "Initialize Python VM" button triggers the actual Start in the background so the multi-second pip install + model download is visible to the user rather than buried in the startup banner.

func (*Service) Similarity

func (svc *Service) Similarity(ctx context.Context, a string, b string) (score float64, err error)

Similarity returns the cosine similarity between the embeddings of strings a and b.

func (*Service) StartPyVenv

func (svc *Service) StartPyVenv(ctx context.Context) error

StartPyVenv synchronously starts the Python venv: pip install (skipped if requirements haven't changed), spawn the worker, load the embedded sources. Returns when the worker is Ready or on error. Tests opt in to running real Python by calling this directly.

type ToDo

type ToDo interface {
	OnStartup(ctx context.Context) (err error)
	OnShutdown(ctx context.Context) (err error)
	Embed(ctx context.Context, text string) (vector []float64, err error)          // MARKER: Embed
	Similarity(ctx context.Context, a string, b string) (score float64, err error) // MARKER: Similarity
	Demo(w http.ResponseWriter, r *http.Request) (err error)                       // MARKER: Demo
	DemoInit(w http.ResponseWriter, r *http.Request) (err error)                   // MARKER: DemoInit
	DemoStatus(w http.ResponseWriter, r *http.Request) (err error)                 // MARKER: DemoStatus
}

ToDo is implemented by the service or mock. The intermediate delegates handling to this interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL