-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.go
More file actions
262 lines (227 loc) · 6.46 KB
/
Copy pathagent.go
File metadata and controls
262 lines (227 loc) · 6.46 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
"time"
)
const (
defaultBaseURL = "http://localhost:11434/v1"
defaultModel = "llama3"
)
// systemPrompt wird dynamisch aus L.SystemPrompt gelesen (siehe callLLM).
// Message repräsentiert eine Nachricht in der Konversation
type Message struct {
Role string `json:"role"`
Content interface{} `json:"content"` // string oder null
Name string `json:"name,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function FunctionCall `json:"function"`
}
type FunctionCall struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}
type chatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Tools []toolDef `json:"tools,omitempty"`
}
type toolDef struct {
Type string `json:"type"`
Function functionDef `json:"function"`
}
type functionDef struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]interface{} `json:"parameters"`
}
type chatResponse struct {
Choices []struct {
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Error *struct {
Message string `json:"message"`
} `json:"error,omitempty"`
}
// ToolCallRequest ist eine aufgelöste Tool-Anfrage
type ToolCallRequest struct {
ID string
Command string
Explanation string
}
// AgentResponse ist die aufgelöste Antwort des LLM
type AgentResponse struct {
Text string
ToolCalls []ToolCallRequest
}
// availableTools wird über rebuildTools() aus L.* befüllt.
var availableTools []toolDef
func rebuildTools() {
availableTools = []toolDef{
{
Type: "function",
Function: functionDef{
Name: "execute_command",
Description: L.ToolDesc,
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"command": map[string]interface{}{
"type": "string",
"description": L.ToolArgCmd,
},
"explanation": map[string]interface{}{
"type": "string",
"description": L.ToolArgExpl,
},
},
"required": []string{"command", "explanation"},
},
},
},
}
}
// Agent verwaltet die Konversation mit dem LLM
type Agent struct {
baseURL string
model string
apiKey string
customPrompt string // Zusatzanweisungen, die dem System-Prompt vorangestellt werden
history []Message
httpClient *http.Client
}
func newAgent() *Agent {
return &Agent{
baseURL: defaultBaseURL,
model: defaultModel,
history: []Message{{Role: "system", Content: L.SystemPrompt}},
httpClient: &http.Client{Timeout: 60 * time.Second},
}
}
// SetTimeout setzt das HTTP-Timeout für LLM-Anfragen (30–300 Sekunden).
func (a *Agent) SetTimeout(seconds int) {
if seconds < 30 {
seconds = 30
} else if seconds > 300 {
seconds = 300
}
a.httpClient = &http.Client{Timeout: time.Duration(seconds) * time.Second}
}
// SendMessage schickt eine Nutzernachricht und gibt die Antwort zurück
func (a *Agent) SendMessage(ctx context.Context, userMsg string) (*AgentResponse, error) {
a.history = append(a.history, Message{
Role: "user",
Content: userMsg,
})
return a.callLLM(ctx)
}
// SendToolResult schickt das Ergebnis einer Tool-Ausführung
func (a *Agent) SendToolResult(ctx context.Context, callID, result string) (*AgentResponse, error) {
a.history = append(a.history, Message{
Role: "tool",
Content: result,
ToolCallID: callID,
})
return a.callLLM(ctx)
}
// PopLastMessage entfernt die zuletzt hinzugefügte Nachricht aus der History (für Abbruch).
func (a *Agent) PopLastMessage() {
if len(a.history) > 1 {
a.history = a.history[:len(a.history)-1]
}
}
// Reset leert den Gesprächsverlauf (außer System-Prompt)
func (a *Agent) Reset() {
a.history = []Message{
{Role: "system", Content: L.SystemPrompt},
}
}
func (a *Agent) callLLM(ctx context.Context) (*AgentResponse, error) {
// System-Prompt dynamisch aufbauen (customPrompt voranstellen wenn gesetzt)
msgs := make([]Message, len(a.history))
copy(msgs, a.history)
if len(msgs) > 0 && msgs[0].Role == "system" {
sysContent := L.SystemPrompt
if a.customPrompt != "" {
sysContent = fmt.Sprintf(L.ExtraPromptFmt, a.customPrompt, L.SystemPrompt)
}
msgs[0].Content = sysContent
}
reqBody := chatRequest{
Model: a.model,
Messages: msgs,
Tools: availableTools,
}
data, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("JSON-Fehler: %w", err)
}
req, err := http.NewRequestWithContext(ctx, "POST", a.baseURL+"/chat/completions", bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("Request-Fehler: %w", err)
}
req.Header.Set("Content-Type", "application/json")
if a.apiKey != "" {
req.Header.Set("Authorization", "Bearer "+a.apiKey)
}
resp, err := a.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("Verbindungsfehler zum LLM: %w", err)
}
defer resp.Body.Close()
var chatResp chatResponse
if err := json.NewDecoder(resp.Body).Decode(&chatResp); err != nil {
return nil, fmt.Errorf("Antwort-Fehler: %w", err)
}
if chatResp.Error != nil {
return nil, fmt.Errorf("LLM-Fehler: %s", chatResp.Error.Message)
}
if len(chatResp.Choices) == 0 {
return nil, fmt.Errorf("Keine Antwort vom LLM erhalten")
}
msg := chatResp.Choices[0].Message
// Antwort zur History hinzufügen
a.history = append(a.history, msg)
// Tool-Calls verarbeiten
if len(msg.ToolCalls) > 0 {
var calls []ToolCallRequest
for _, tc := range msg.ToolCalls {
var args struct {
Command string `json:"command"`
Explanation string `json:"explanation"`
}
if err := json.Unmarshal([]byte(tc.Function.Arguments), &args); err != nil {
continue
}
calls = append(calls, ToolCallRequest{
ID: tc.ID,
Command: args.Command,
Explanation: args.Explanation,
})
}
return &AgentResponse{ToolCalls: calls}, nil
}
// Text-Antwort
text := ""
switch v := msg.Content.(type) {
case string:
text = cleanResponse(v)
}
return &AgentResponse{Text: text}, nil
}
var thinkRegex = regexp.MustCompile(`(?s)<think>.*?</think>`)
func cleanResponse(s string) string {
s = thinkRegex.ReplaceAllString(s, "")
return strings.TrimSpace(s)
}