mirror of
https://github.com/therootcompany/golib.git
synced 2025-12-23 22:08: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
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,6 +30,7 @@ type OllamaAPI struct {
|
|||||||
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)
|
||||||
@ -105,6 +114,7 @@ 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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user