-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroq.go
98 lines (86 loc) · 2.53 KB
/
groq.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
type Choice struct {
Index int `json:"index"`
Message struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"message"`
Logprobs interface{} `json:"logprobs"`
FinishReason string `json:"finish_reason"`
}
type Usage struct {
QueueTime float64 `json:"queue_time"`
PromptTokens int `json:"prompt_tokens"`
PromptTime float64 `json:"prompt_time"`
CompletionTokens int `json:"completion_tokens"`
CompletionTime float64 `json:"completion_time"`
TotalTokens int `json:"total_tokens"`
TotalTime float64 `json:"total_time"`
}
type Response struct {
ID string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
Model string `js on:"model"`
Choices []Choice `json:"choices"`
Usage Usage `json:"usage"`
SystemFingerprint string `json:"system_fingerprint"`
XGroq struct {
ID string `json:"id"`
} `json:"x_groq"`
}
func callGroqAPI(content string) (string, error) {
// Replace with your actual API key
apiKey := os.Getenv("GROQ_API_KEY")
if apiKey == "" {
return "", fmt.Errorf("API key not found in environment variables")
}
url := "https://api.groq.com/openai/v1/chat/completions"
requestBody, err := json.Marshal(map[string]interface{}{
"model": "llama-3.2-90b-vision-preview",
"messages": []map[string]string{
{"role": "user", "content": content},
},
})
if err != nil {
return "", fmt.Errorf("failed to marshal request body: %w", err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
// fmt.Printf("%v\n", resp)
if err != nil {
return "", fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("request failed with status: %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
var response Response
err = json.Unmarshal([]byte(string(body)), &response)
if err != nil {
return "", err
}
if len(response.Choices) <= 0 {
return "", err
}
content = response.Choices[0].Message.Content
return content, nil
}