This repository has been archived on 2026-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
ollie-core/backend/oneshot.go

47 lines
1.2 KiB
Go

// Package backend — stateless one-shot operations.
// These functions provide request/response LLM access without session state.
package backend
import (
"context"
"fmt"
"strings"
)
// GenerateRequest is the input for a stateless one-shot generation.
type GenerateRequest struct {
Prompt string `json:"prompt"`
System string `json:"system"`
Backend string `json:"backend"`
Model string `json:"model"`
}
// Generate performs a stateless one-shot LLM generation with no session context.
// Returns the full model response text.
func Generate(ctx context.Context, req GenerateRequest) (string, error) {
if req.Prompt == "" {
return "", fmt.Errorf("empty prompt")
}
be, err := NewWithName(req.Backend)
if err != nil {
return "", err
}
if req.Model != "" {
be.SetModel(req.Model)
}
var msgs []Message
if req.System != "" {
msgs = append(msgs, Message{Role: "system", Content: req.System})
}
msgs = append(msgs, Message{Role: "user", Content: req.Prompt})
ch, err := be.ChatStream(ctx, msgs, nil, GenerationParams{})
if err != nil {
return "", err
}
var sb strings.Builder
for ev := range ch {
sb.WriteString(ev.Content)
}
return sb.String(), nil
}