-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllm.go
142 lines (119 loc) · 3.5 KB
/
llm.go
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
package main
import (
"context"
"encoding/json"
"log"
"os"
"github.com/4nkitd/sapiens"
)
type AiResponse struct {
Response string `json:"response"`
Command string `json:"command"`
}
func AskQuery(query string, imageBytes [][]byte) AiResponse {
apiKey := os.Getenv("GENAI_API_KEY")
if apiKey == "" {
log.Fatal("GENAI_API_KEY environment variable is not set")
}
defaultModel := os.Getenv("GENAI_DEFAULT_MODEL")
if defaultModel == "" {
log.Fatal("GENAI_DEFAULT_MODEL environment variable is not set")
}
// Initialize the Sapiens LLM client
llm := sapiens.NewGoogleGenAI(apiKey, defaultModel)
if err := llm.Initialize(); err != nil {
log.Fatalf("Failed to initialize LLM: %v", err)
}
// Create a Sapiens agent
agent := sapiens.NewAgent("GemaCLI", llm, apiKey, defaultModel, "google")
// Define system info tool
sysInfoTool := sapiens.Tool{
Name: "get_system_info",
Description: "Get detailed system information including hardware, OS, memory usage, and running processes",
}
// Add the system info tool to the agent
agent.AddTools(sysInfoTool)
// Register tool implementation
agent.RegisterToolImplementation("get_system_info", func(params map[string]interface{}) (interface{}, error) {
return GetSystemInfo(params)
})
// Add system prompt (without system info directly embedded)
agent.AddSystemPrompt(SystemInstruction, "1.0")
// Define schema for structured output
schema := sapiens.Schema{
Type: "object",
Properties: map[string]sapiens.Schema{
"response": {
Type: "string",
Description: "The response from the AI",
},
"command": {
Type: "string",
Description: "Command to execute on the system",
},
},
Required: []string{"response"},
}
// Set the schema on your agent
agent.SetStructuredResponseSchema(schema)
// Create context and run the query
ctx := context.Background()
// Handle image attachments if present
if len(imageBytes) > 0 {
for _, imgBytes := range imageBytes {
if imgBytes != nil {
agent.AddImageContent(imgBytes, "image/jpeg")
}
}
}
// Run the agent with the query
response, err := agent.Run(ctx, query)
if err != nil {
log.Fatalf("Error from agent: %v", err)
}
// fmt.Println("Response:", response.Content)
// Create a result object with default values
result := AiResponse{
Response: "",
Command: "",
}
// Access the structured data from the response
if response.Structured != nil {
structuredData, ok := response.Structured.(map[string]interface{})
if ok {
// Extract response field
if responseVal, exists := structuredData["response"]; exists {
if responseStr, ok := responseVal.(string); ok {
result.Response = responseStr
}
}
// Extract command field
if commandVal, exists := structuredData["command"]; exists {
if commandStr, ok := commandVal.(string); ok {
result.Command = commandStr
}
}
} else {
log.Printf("Failed to cast structured data to map[string]interface{}")
}
} else if response.Content != "" {
// As fallback, try to parse the content as JSON
var jsonResult AiResponse
if json.Valid([]byte(response.Content)) {
if err := json.Unmarshal([]byte(response.Content), &jsonResult); err == nil {
result = jsonResult
}
} else {
result.Response = response.Content
}
}
// Validate that we have at least a response
if result.Response == "" {
log.Fatal("Response field is missing or not a string")
}
errDb := StoreCommandHistory(query, result.Response)
if errDb != nil {
log.Fatal(errDb)
}
return result
}