Skip to content

Commit

Permalink
Update Groq model options and documentation
Browse files Browse the repository at this point in the history
- Add Groq model options via Groq's OpenAI API compatibility
- Update documentation accordingly
- Fix example
- Fix golangci-lint compliants
- fix comment typo
  • Loading branch information
jackspirou committed Mar 14, 2024
1 parent 699f397 commit 092df75
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
13 changes: 13 additions & 0 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ var disabledModelsForEndpoints = map[string]map[string]bool{
},
}

// Groq contains models that work with Groq's OpenAI Compatibility API.
//
// Usage Examples: openai.Groq.Mixtral_8x7b, openai.Groq.LLaMA2_70b, openai.Groq.Gemma_7b_IT.
var Groq = struct {
Mixtral8x7b string
LLaMA270b string
Gemma7bIT string
}{
Mixtral8x7b: "mixtral-8x7b-32768",
LLaMA270b: "llama2-70b-4096",
Gemma7bIT: "gemma-7b-it",
}

func checkEndpointSupportsModel(endpoint, model string) bool {
return !disabledModelsForEndpoints[endpoint][model]
}
Expand Down
19 changes: 18 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
)

const (
openaiAPIURLv1 = "https://api.openai.com/v1"
openaiAPIURLv1 = "https://api.openai.com/v1"
groqAPIURLv1 = "https://api.groq.com/openai/v1"

defaultEmptyMessagesLimit uint = 300

azureAPIPrefix = "openai"
Expand All @@ -19,6 +21,7 @@ const (
APITypeOpenAI APIType = "OPEN_AI"
APITypeAzure APIType = "AZURE"
APITypeAzureAD APIType = "AZURE_AD"
APITypeGroq APIType = "GROQ"
)

const AzureAPIKeyHeader = "api-key"
Expand Down Expand Up @@ -67,6 +70,20 @@ func DefaultAzureConfig(apiKey, baseURL string) ClientConfig {
}
}

// DefaultGroqConfig takes a Groq auth token and returns a ClientConfig that works with Groq's OpenAI Compatibility API.
func DefaultGroqConfig(authToken string) ClientConfig {
return ClientConfig{
authToken: authToken,
BaseURL: groqAPIURLv1,
APIType: APITypeGroq,
OrgID: "",

HTTPClient: &http.Client{},

EmptyMessagesLimit: defaultEmptyMessagesLimit,
}

Check warning on line 84 in config.go

View check run for this annotation

Codecov / codecov/patch

config.go#L74-L84

Added lines #L74 - L84 were not covered by tests
}

func (ClientConfig) String() string {
return "<OpenAI API ClientConfig>"
}
Expand Down
35 changes: 35 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,38 @@ func ExampleAPIError() {
}
}
}

// ExampleDefaultGroqConfig demonstrates how to create a new DefaultGroqConfig, create a Groq client,
// use a hosted Groq model, and create a chat completion.
func ExampleDefaultGroqConfig() {
config := openai.DefaultGroqConfig(os.Getenv("GROQ_API_KEY"))
client := openai.NewClientWithConfig(config)

req := openai.ChatCompletionRequest{
Model: openai.Groq.Mixtral8x7b,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "you are a helpful chatbot",
},
},
}
fmt.Println("Conversation")
fmt.Println("---------------------")
fmt.Print("> ")
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
req.Messages = append(req.Messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: s.Text(),
})
resp, err := client.CreateChatCompletion(context.Background(), req)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
continue
}
fmt.Printf("%s\n\n", resp.Choices[0].Message.Content)
req.Messages = append(req.Messages, resp.Choices[0].Message)
fmt.Print("> ")
}
}
1 change: 1 addition & 0 deletions test.mp3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello

0 comments on commit 092df75

Please sign in to comment.