mirror of
https://github.com/therootcompany/golib.git
synced 2025-12-12 00:18:46 +00:00
f(ai): add WithModel to change model on-the-fly
This commit is contained in:
parent
e599539c9c
commit
076fe9ed1d
27
ai/ai.go
27
ai/ai.go
@ -11,6 +11,7 @@ import (
|
|||||||
type API interface {
|
type API interface {
|
||||||
Type() string
|
Type() string
|
||||||
Model() string
|
Model() string
|
||||||
|
WithModel(name string) API
|
||||||
Generate(system, prompt string, ctx json.RawMessage) (string, error)
|
Generate(system, prompt string, ctx json.RawMessage) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +44,12 @@ func (a *OllamaAPI) Model() string {
|
|||||||
return a.ModelName
|
return a.ModelName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *OllamaAPI) WithModel(model string) API {
|
||||||
|
a2 := *a
|
||||||
|
a2.ModelName = model
|
||||||
|
return &a2
|
||||||
|
}
|
||||||
|
|
||||||
func (a *OllamaAPI) Generate(system, prompt string, ctxU32s json.RawMessage) (string, error) {
|
func (a *OllamaAPI) Generate(system, prompt string, ctxU32s json.RawMessage) (string, error) {
|
||||||
var context []uint32
|
var context []uint32
|
||||||
// for type safety while maintaining interface
|
// for type safety while maintaining interface
|
||||||
@ -55,10 +62,10 @@ func (a *OllamaAPI) Generate(system, prompt string, ctxU32s json.RawMessage) (st
|
|||||||
Context: context,
|
Context: context,
|
||||||
Prompt: prompt,
|
Prompt: prompt,
|
||||||
Stream: false,
|
Stream: false,
|
||||||
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)
|
||||||
},
|
// },
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonData, _ := json.Marshal(reqBody)
|
jsonData, _ := json.Marshal(reqBody)
|
||||||
@ -162,6 +169,12 @@ type OpenAIResponse struct {
|
|||||||
} `json:"choices"`
|
} `json:"choices"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *OpenAiAPI) WithModel(model string) API {
|
||||||
|
a2 := *a
|
||||||
|
a2.ModelName = model
|
||||||
|
return &a2
|
||||||
|
}
|
||||||
|
|
||||||
func (a *OpenAiAPI) Generate(system, prompt string, ctxMessages json.RawMessage) (string, error) {
|
func (a *OpenAiAPI) Generate(system, prompt string, ctxMessages json.RawMessage) (string, error) {
|
||||||
reqBody := OpenAIRequest{
|
reqBody := OpenAIRequest{
|
||||||
Model: a.ModelName, // Default OpenAI model, adjust as needed
|
Model: a.ModelName, // Default OpenAI model, adjust as needed
|
||||||
@ -169,9 +182,9 @@ func (a *OpenAiAPI) Generate(system, prompt string, ctxMessages json.RawMessage)
|
|||||||
{Role: "system", Content: system},
|
{Role: "system", Content: system},
|
||||||
{Role: "user", Content: prompt},
|
{Role: "user", Content: prompt},
|
||||||
},
|
},
|
||||||
Stream: false,
|
Stream: false,
|
||||||
Temperature: 0.7,
|
// Temperature: 0.7,
|
||||||
TopP: 0.9,
|
// TopP: 0.9,
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonData, _ := json.Marshal(reqBody)
|
jsonData, _ := json.Marshal(reqBody)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user