f(ai): add WithFormat to change format without changing Generate signature

This commit is contained in:
AJ ONeal 2025-12-17 03:33:27 -07:00
parent 45ed7ae848
commit 4aeabd1ddb
No known key found for this signature in database

View File

@ -12,6 +12,7 @@ type API interface {
Type() string Type() string
Model() string Model() string
WithModel(name string) API WithModel(name string) API
WithFormat(name string) API
Generate(system, prompt string, ctx json.RawMessage) (string, error) Generate(system, prompt string, ctx json.RawMessage) (string, error)
} }
@ -25,10 +26,11 @@ type API interface {
// var modelName = "gpt-oss:20b" // var modelName = "gpt-oss:20b"
// var ollamaBaseURL = "http://localhost:11434" // var ollamaBaseURL = "http://localhost:11434"
type OllamaAPI struct { type OllamaAPI struct {
BaseURL string BaseURL string
APIKey string APIKey string
BasicAuth BasicAuth BasicAuth BasicAuth
ModelName string ModelName string
formatName string
} }
type BasicAuth struct { type BasicAuth struct {
@ -44,6 +46,12 @@ func (a *OllamaAPI) Model() string {
return a.ModelName return a.ModelName
} }
func (a *OllamaAPI) WithFormat(format string) API {
a2 := *a
a2.formatName = format
return &a2
}
func (a *OllamaAPI) WithModel(model string) API { func (a *OllamaAPI) WithModel(model string) API {
a2 := *a a2 := *a
a2.ModelName = model a2.ModelName = model
@ -62,6 +70,7 @@ func (a *OllamaAPI) Generate(system, prompt string, ctxU32s json.RawMessage) (st
Context: context, Context: context,
Prompt: prompt, Prompt: prompt,
Stream: false, Stream: false,
Format: a.formatName,
// Options: &Options{ // Options: &Options{
// Temperature: 0.7, // Controls randomness (0.0 to 1.0) // Temperature: 0.7, // Controls randomness (0.0 to 1.0)
// TopP: 0.8, // Controls diversity (0.0 to 1.0) // TopP: 0.8, // Controls diversity (0.0 to 1.0)
@ -102,9 +111,10 @@ func (a *OllamaAPI) Generate(system, prompt string, ctxU32s json.RawMessage) (st
// var gptModel = "gpt-4o" // var gptModel = "gpt-4o"
// var openAIBaseURL = "https://api.openai.com/v1" // var openAIBaseURL = "https://api.openai.com/v1"
type OpenAiAPI struct { type OpenAiAPI struct {
BaseURL string BaseURL string
APIKey string APIKey string
ModelName string ModelName string
formatName string
} }
func (a *OpenAiAPI) Type() string { func (a *OpenAiAPI) Type() string {
@ -169,6 +179,13 @@ type OpenAIResponse struct {
} `json:"choices"` } `json:"choices"`
} }
func (a *OpenAiAPI) WithFormat(format string) API {
a2 := *a
a2.formatName = format
// TODO how to set JSON format for OpenAi?
return &a2
}
func (a *OpenAiAPI) WithModel(model string) API { func (a *OpenAiAPI) WithModel(model string) API {
a2 := *a a2 := *a
a2.ModelName = model a2.ModelName = model