mirror of
https://github.com/therootcompany/golib.git
synced 2025-11-20 05:54:29 +00:00
f(ai): fix interface / add type guard
This commit is contained in:
parent
64fb761615
commit
8155fa44fe
15
ai/ai.go
15
ai/ai.go
@ -11,7 +11,7 @@ import (
|
|||||||
type API interface {
|
type API interface {
|
||||||
Type() string
|
Type() string
|
||||||
Model() string
|
Model() string
|
||||||
Generate(system, prompt string) (string, error)
|
Generate(system, prompt string, ctx json.RawMessage) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ollama show --parameters gpt-oss:20b
|
// ollama show --parameters gpt-oss:20b
|
||||||
@ -43,7 +43,12 @@ func (a *OllamaAPI) Model() string {
|
|||||||
return a.ModelName
|
return a.ModelName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *OllamaAPI) Generate(system, prompt string, context []uint32) (string, error) {
|
func (a *OllamaAPI) Generate(system, prompt string, ctxU32s json.RawMessage) (string, error) {
|
||||||
|
var context []uint32
|
||||||
|
// for type safety while maintaining interface
|
||||||
|
if err := json.Unmarshal(ctxU32s, &context); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
reqBody := OllamaGenerate{
|
reqBody := OllamaGenerate{
|
||||||
Model: a.ModelName,
|
Model: a.ModelName,
|
||||||
System: system,
|
System: system,
|
||||||
@ -157,7 +162,7 @@ type OpenAIResponse struct {
|
|||||||
} `json:"choices"`
|
} `json:"choices"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *OpenAiAPI) Generate(system, prompt string) (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
|
||||||
Messages: []OpenAIMessage{
|
Messages: []OpenAIMessage{
|
||||||
@ -196,3 +201,7 @@ func (a *OpenAiAPI) Generate(system, prompt string) (string, error) {
|
|||||||
|
|
||||||
return openAIResp.Choices[0].Message.Content, nil
|
return openAIResp.Choices[0].Message.Content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// interface guards
|
||||||
|
var _ API = (*OllamaAPI)(nil)
|
||||||
|
var _ API = (*OpenAiAPI)(nil)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user