-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathai_bindings.go
More file actions
96 lines (88 loc) · 2.25 KB
/
Copy pathai_bindings.go
File metadata and controls
96 lines (88 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"adomnia/internal/ai"
"adomnia/internal/devlog"
"adomnia/internal/mock"
"context"
"encoding/json"
"fmt"
"sync"
)
type AIEngine struct {
mu sync.RWMutex
engine *ai.Engine
}
func NewAIEngine() *AIEngine { return &AIEngine{} }
var globalAIEngine *AIEngine
func (a *AIEngine) Configure(cfgJSON string) error {
var cfg ai.Config
if err := json.Unmarshal([]byte(cfgJSON), &cfg); err != nil {
return fmt.Errorf("invalid AI config: %w", err)
}
e, err := ai.New(cfg)
if err != nil {
return err
}
a.mu.Lock()
a.engine = e
a.mu.Unlock()
devlog.Info("AIEngine.Configure", "provider configurato: "+string(cfg.Provider), nil)
return nil
}
func (a *AIEngine) Complete(systemPrompt, userPrompt string, maxTokens int) (string, error) {
a.mu.RLock()
e := a.engine
a.mu.RUnlock()
if e == nil {
return "", fmt.Errorf("AI engine non configurato — imposta provider nelle Impostazioni > AI")
}
resp, err := e.Complete(context.Background(), ai.CompletionRequest{
SystemPrompt: systemPrompt,
UserPrompt: userPrompt,
MaxTokens: maxTokens,
})
if err != nil {
return "", err
}
return resp.Text, nil
}
func (a *AIEngine) TestConnection(cfgJSON string) (string, error) {
var cfg ai.Config
if err := json.Unmarshal([]byte(cfgJSON), &cfg); err != nil {
return "", fmt.Errorf("invalid config: %w", err)
}
e, err := ai.New(cfg)
if err != nil {
return "", err
}
resp, err := e.Complete(context.Background(), ai.CompletionRequest{
UserPrompt: "Rispondi solo con: OK",
MaxTokens: 10,
})
if err != nil {
return "", err
}
return resp.Text, nil
}
func (a *AIEngine) GenerateMockEndpoints(inputType, userInput string) (string, error) {
a.mu.RLock()
e := a.engine
a.mu.RUnlock()
if e == nil {
return "", fmt.Errorf("AI engine non configurato — imposta provider nelle Impostazioni > AI")
}
prompt := mock.BuildMockGenerationPrompt(inputType, userInput)
resp, err := e.Complete(context.Background(), ai.CompletionRequest{
UserPrompt: prompt,
MaxTokens: 2048,
})
if err != nil {
return "", err
}
endpoints, err := mock.ParseAIResponse(resp.Text)
if err != nil {
return "", fmt.Errorf("AI ha generato JSON non valido: %w\nRisposta raw: %s", err, resp.Text)
}
raw, _ := json.Marshal(endpoints)
return string(raw), nil
}