-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents.go
More file actions
121 lines (99 loc) · 3.59 KB
/
agents.go
File metadata and controls
121 lines (99 loc) · 3.59 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
package hbcgo
import (
"context"
"encoding/base64"
"fmt"
"os"
)
// Chat sends a message to the AI model and returns the model's response.
// It takes a context for cancellation/timeout and a slice of messages containing
// the conversation history. Returns the model's response as a string or an error.
// Messages must not be empty. Uses the Meta-Llama-3.1-70B-Instruct model.
func (c *Client) Chat(ctx context.Context, messages []Message) (string, error) {
if len(messages) == 0 {
return "", fmt.Errorf("messages cannot be empty")
}
requestData := ChatRequest{
Messages: messages,
Model: "meta-llama/Meta-Llama-3.1-70B-Instruct", // Updated to supported model
PresencePenalty: 0,
Temperature: 0.7,
TopP: 0.95,
Stream: false,
}
req, err := c.newRequest(ctx, "POST", "/chat/completions", requestData)
if err != nil {
return "", fmt.Errorf("error creating request: %w", err)
}
var response ChatResponse
if err := c.doRequest(req, &response); err != nil {
return "", fmt.Errorf("chat request failed: %w", err)
}
if len(response.Choices) == 0 {
return "", fmt.Errorf("no choices in response")
}
return response.Choices[0].Message.Content, nil
}
// GenerateImage creates an image based on the given text prompt.
// Height and width parameters determine the image dimensions in pixels.
// Returns the path to the generated image file (result.jpg) or an error.
// Uses the SDXL1.0-base model for image generation.
func (c *Client) GenerateImage(ctx context.Context, prompt string, height, width int) (string, error) {
if prompt == "" {
return "", fmt.Errorf("prompt cannot be empty")
}
requestData := ImageRequest{
ModelName: "SDXL1.0-base",
Prompt: prompt,
Height: height,
Width: width,
Backend: "auto",
}
req, err := c.newRequest(ctx, "POST", "/image/generation", requestData)
if err != nil {
return "", fmt.Errorf("error creating request: %w", err)
}
var response ImageResponse
if err := c.doRequest(req, &response); err != nil {
return "", fmt.Errorf("image generation failed: %w", err)
}
if len(response.Images) == 0 {
return "", fmt.Errorf("no images returned from API")
}
imageData, err := base64.StdEncoding.DecodeString(response.Images[0].Image)
if err != nil {
return "", fmt.Errorf("error decoding base64 image data: %w", err)
}
outputPath := "result.jpg"
if err := os.WriteFile(outputPath, imageData, 0644); err != nil {
return "", fmt.Errorf("error writing image file: %w", err)
}
return outputPath, nil
}
// GenerateAudio converts the given text to speech and saves it as an audio file.
// Takes a context for cancellation/timeout and the text to convert.
// Returns the path to the generated audio file (result.mp3) or an error.
// The text must not be empty. Uses default voice settings.
func (c *Client) GenerateAudio(ctx context.Context, text string) (string, error) {
if text == "" {
return "", fmt.Errorf("text cannot be empty")
}
requestData := AudioRequest{Text: text}
req, err := c.newRequest(ctx, "POST", "/audio/generation", requestData)
if err != nil {
return "", fmt.Errorf("error creating request: %w", err)
}
var response AudioResponse
if err := c.doRequest(req, &response); err != nil {
return "", fmt.Errorf("audio generation failed: %w", err)
}
audioData, err := base64.StdEncoding.DecodeString(response.Audio)
if err != nil {
return "", fmt.Errorf("error decoding base64 audio data: %w", err)
}
outputPath := "result.mp3"
if err := os.WriteFile(outputPath, audioData, 0644); err != nil {
return "", fmt.Errorf("error writing audio file: %w", err)
}
return outputPath, nil
}