-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroq.go
138 lines (110 loc) · 3.08 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
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
// Package groq is an unofficial API wrapper for GroqCloud https://groq.com/
//
// groq requires Go 1.14 or newer
package groq
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
const (
MessageRoleUser = "user"
MessageRoleSystem = "system"
MessageRoleAssistant = "assistant"
)
// GroqClient is the main client that interacts with the GroqCloud API
type GroqClient struct {
apiKey string
}
// a struct that represents the exact response returned by Groq's API
type modelsResponse struct {
Data []Model `json:"data"`
}
// Model represents the metadata for an LLM hosted on Groqcloud
type Model struct {
// the model's ID
Id string `json:"id"`
Object string `json:"object"`
// Unix timestamp when the model was created
Created int `json:"created"`
// Who owns this model
OwnedBy string `json:"owned_by"`
// Is the model currently active?
Active bool `json:"active"`
// How many context window tokens the model supports
ContextWindow int `json:"context_window"`
}
// creates an http.Request with the API key added to it and the URL set
func createGroqRequest(endpoint string, apiKey string, method string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, fmt.Sprintf("https://api.groq.com/openai/v1/%s", endpoint), body)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiKey))
return req, nil
}
// Creates a new Groq client. Returns an error if the API key given is invalid
func NewGroqClient(apiKey string) (*GroqClient, error) {
// test the API key
req, err := createGroqRequest("/models", apiKey, "GET", nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
// if the API key is invalid return an error
if resp.StatusCode == 401 {
return nil, errors.New("invalid API key")
}
// return the client
return &GroqClient{
apiKey,
}, nil
}
// returns all models available on GroqCloud
func (g *GroqClient) GetModels() ([]Model, error) {
req, err := createGroqRequest("/models", g.apiKey, "GET", nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request failed with status: %s", resp.Status)
}
var response modelsResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, err
}
return response.Data, nil
}
// returns the information for a specific model
func (g *GroqClient) GetModel(modelId string) (Model, error) {
req, err := createGroqRequest(fmt.Sprintf("/models/%s", modelId), g.apiKey, "GET", nil)
if err != nil {
return Model{}, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return Model{}, err
}
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == 404 {
return Model{}, errors.New("invalid model id")
}
return Model{}, fmt.Errorf("request failed with status: %s", resp.Status)
}
var model Model
err = json.NewDecoder(resp.Body).Decode(&model)
if err != nil {
return Model{}, err
}
return model, nil
}