mirror of
https://github.com/therootcompany/golib.git
synced 2025-12-23 13:58:46 +00:00
f(ai): add WithFormat to change format without changing Generate signature
This commit is contained in:
parent
45ed7ae848
commit
4aeabd1ddb
17
ai/ai.go
17
ai/ai.go
@ -12,6 +12,7 @@ type API interface {
|
||||
Type() string
|
||||
Model() string
|
||||
WithModel(name string) API
|
||||
WithFormat(name string) API
|
||||
Generate(system, prompt string, ctx json.RawMessage) (string, error)
|
||||
}
|
||||
|
||||
@ -29,6 +30,7 @@ type OllamaAPI struct {
|
||||
APIKey string
|
||||
BasicAuth BasicAuth
|
||||
ModelName string
|
||||
formatName string
|
||||
}
|
||||
|
||||
type BasicAuth struct {
|
||||
@ -44,6 +46,12 @@ func (a *OllamaAPI) Model() string {
|
||||
return a.ModelName
|
||||
}
|
||||
|
||||
func (a *OllamaAPI) WithFormat(format string) API {
|
||||
a2 := *a
|
||||
a2.formatName = format
|
||||
return &a2
|
||||
}
|
||||
|
||||
func (a *OllamaAPI) WithModel(model string) API {
|
||||
a2 := *a
|
||||
a2.ModelName = model
|
||||
@ -62,6 +70,7 @@ func (a *OllamaAPI) Generate(system, prompt string, ctxU32s json.RawMessage) (st
|
||||
Context: context,
|
||||
Prompt: prompt,
|
||||
Stream: false,
|
||||
Format: a.formatName,
|
||||
// Options: &Options{
|
||||
// Temperature: 0.7, // Controls randomness (0.0 to 1.0)
|
||||
// TopP: 0.8, // Controls diversity (0.0 to 1.0)
|
||||
@ -105,6 +114,7 @@ type OpenAiAPI struct {
|
||||
BaseURL string
|
||||
APIKey string
|
||||
ModelName string
|
||||
formatName string
|
||||
}
|
||||
|
||||
func (a *OpenAiAPI) Type() string {
|
||||
@ -169,6 +179,13 @@ type OpenAIResponse struct {
|
||||
} `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 {
|
||||
a2 := *a
|
||||
a2.ModelName = model
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user