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
- Fix usage example comments
- Remove silly test.mp3
  • Loading branch information
jackspirou committed Mar 14, 2024
1 parent 699f397 commit 3d041d3
Show file tree
Hide file tree
Showing 3 changed files with 66 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.Mixtral8x7b, openai.Groq.LLaMA270b, openai.Groq.Gemma7bIT.
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("> ")
}
}

0 comments on commit 3d041d3

Please sign in to comment.