-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
165 lines (139 loc) · 4.54 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/joho/godotenv"
"github.com/wansatya/groq-go/pkg/groq"
)
func main() {
// Load .env file
err := godotenv.Load()
if err != nil {
log.Println("Warning: Error loading .env file")
}
apiKey := os.Getenv("GROQ_API_KEY")
if apiKey == "" {
log.Fatal("GROQ_API_KEY not found in environment variables")
}
modelID := os.Getenv("GROQ_MODEL")
if modelID == "" {
modelID = "mixtral-8x7b-32768" // Default model if not specified
fmt.Printf("GROQ_MODEL not set, using default: %s\n", modelID)
}
client := groq.NewClient(apiKey)
// Set base prompts
client.SetBasePrompt("You are a helpful assistant. Always be polite and concise.")
client.SetBasePrompt("Provide examples when explaining concepts.")
ctx := context.Background()
// List models
listModels(ctx, client)
// Get model details
getModel(ctx, client, modelID)
// Create chat completion
chatCompletionCreate(ctx, client, modelID)
// Create chat completion with JSON response
chatCompletionCreateJSON(ctx, client, modelID)
// Create streaming chat completion
chatCompletionCreateStream(ctx, client, modelID)
}
func listModels(ctx context.Context, client *groq.Client) {
modelList, err := client.ListModels(ctx)
if err != nil {
log.Fatalf("Error listing models: %v", err)
}
fmt.Println("Available Models:")
for _, model := range modelList.Data {
fmt.Printf("- %s\n", model.ID)
}
}
func getModel(ctx context.Context, client *groq.Client, modelID string) {
model, err := client.GetModel(ctx, modelID)
if err != nil {
log.Fatalf("Error fetching model %s: %v", modelID, err)
}
fmt.Printf("\nModel Details for %s:\n", modelID)
fmt.Printf("ID: %s\n", model.ID)
fmt.Printf("Object: %s\n", model.Object)
fmt.Printf("Created: %d\n", model.Created)
fmt.Printf("Owned By: %s\n", model.OwnedBy)
isValid, err := client.IsValidModel(ctx, modelID)
if err != nil {
log.Fatalf("Error checking model validity: %v", err)
}
fmt.Printf("Is model %s valid: %v\n", modelID, isValid)
}
func chatCompletionCreate(ctx context.Context, client *groq.Client, modelID string) {
req := groq.ChatCompletionRequest{
Model: modelID,
Messages: []groq.Message{
{Role: "user", Content: "What is Golang?"},
},
MaxTokens: 100,
Temperature: 0.7,
}
resp, err := client.CreateChatCompletion(ctx, req)
if err != nil {
log.Fatalf("Error creating chat completion: %v", err)
}
if len(resp.Choices) > 0 {
fmt.Println("\nText Response from Groq API:\n")
fmt.Println(resp.Choices[0].Message.Content)
}
}
func chatCompletionCreateJSON(ctx context.Context, client *groq.Client, modelID string) {
req := groq.ChatCompletionRequest{
Model: modelID,
Messages: []groq.Message{
{Role: "user", Content: "What is Golang? Respond in JSON format with keys: 'name', 'description', and 'key_features'."},
},
MaxTokens: 150,
Temperature: 0.7,
ResponseFormat: &groq.ResponseFormat{
Type: "json_object",
},
}
resp, err := client.CreateChatCompletion(ctx, req)
if err != nil {
log.Fatalf("Error creating chat completion (JSON): %v", err)
}
if len(resp.Choices) > 0 {
fmt.Println("\nJSON Response from Groq API:\n")
fmt.Println(resp.Choices[0].Message.Content)
} else {
fmt.Println("No response received from API")
}
}
func chatCompletionCreateStream(ctx context.Context, client *groq.Client, modelID string) {
req := groq.ChatCompletionRequest{
Model: modelID,
Messages: []groq.Message{
{Role: "user", Content: "Tell me a short story about a Tesla Optimus."},
},
MaxTokens: 150,
Temperature: 0.7,
Stream: true,
}
fmt.Println("\nStreaming Response from Groq API:\n")
chunkChan, errChan := client.CreateChatCompletionStream(ctx, req)
for {
select {
case chunk, ok := <-chunkChan:
if !ok {
return
}
for _, choice := range chunk.Choices {
fmt.Print(choice.Delta.Content)
}
case err, ok := <-errChan:
if !ok {
return
}
if err != nil {
log.Fatalf("Error in stream: %v", err)
}
return
}
}
}