Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cd8b724

Browse files
authoredAug 20, 2024
feat(groq): integrate native Groq SDK and update documentation (promptfoo#1479)
1 parent 63aac8f commit cd8b724

File tree

12 files changed

+664
-74
lines changed

12 files changed

+664
-74
lines changed
 

‎.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"editor.defaultFormatter": "esbenp.prettier-vscode",
33
"editor.formatOnSave": true,
4-
"cSpell.words": ["openai", "promptfoo", "promptfooconfig", "redteam"]
4+
"cSpell.words": ["Groq", "openai", "promptfoo", "promptfooconfig", "redteam"]
55
}

‎examples/groq/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
To get started, set your GROQ_API_KEY environment variable.
1+
To get started, set your `GROQ_API_KEY` environment variable.
22

33
Next, edit promptfooconfig.yaml.
44

‎examples/groq/promptfooconfig.yaml

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
prompts:
2-
- 'Answer this question as concisely as possible: {{question}}'
2+
- 'Answer this question as concisely as possible: {{calculation}}'
3+
- 'Calculate the following and provide a brief explanation: {{calculation}}'
34

45
providers:
5-
- id: openai:chat:mixtral-8x7b-32768
6+
- id: groq:gemma2-9b-it
7+
- id: groq:llama3-groq-8b-8192-tool-use-preview
68
config:
7-
apiBaseUrl: https://api.groq.com/openai/v1
8-
apiKeyEnvar: GROQ_API_KEY
9-
- id: openai:chat:llama3-8b-8192
10-
config:
11-
apiBaseUrl: https://api.groq.com/openai/v1
12-
apiKeyEnvar: GROQ_API_KEY
9+
tools:
10+
- type: function
11+
function:
12+
name: calculate
13+
description: 'Evaluate a mathematical expression'
14+
parameters:
15+
type: object
16+
properties:
17+
expression:
18+
type: string
19+
description: 'The mathematical expression to evaluate'
20+
required: ['expression']
21+
tool_choice: auto
1322

1423
tests:
1524
- vars:
16-
question: What is the capital of France?
17-
assert:
18-
- type: icontains
19-
value: Paris
25+
calculation: 25 * 4 + 10
26+
- vars:
27+
calculation: (8 + 2) * 5

‎examples/tool-use/external_tools.yaml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
- type: function
2+
function:
3+
name: get_weather
4+
description: Get the current weather in a given location
5+
input_schema:
6+
type: object
7+
properties:
8+
location:
9+
type: string
10+
description: The city and state, e.g. San Francisco, CA
11+
unit:
12+
type: string
13+
enum:
14+
- celsius
15+
- fahrenheit
16+
required:
17+
- location

‎examples/tool-use/promptfooconfig.yaml

+21-1
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,29 @@ providers:
4040
- id: openai:chat:gpt-4o-mini
4141
config:
4242
tools: file://external_tools.yaml
43+
- id: groq:llama3-groq-70b-8192-tool-use-preview
44+
config:
45+
tools:
46+
- type: function
47+
function:
48+
name: get_weather
49+
description: 'Get the current weather in a given location'
50+
parameters:
51+
type: object
52+
properties:
53+
location:
54+
type: string
55+
description: 'The city and state, e.g. San Francisco, CA'
56+
unit:
57+
type: string
58+
enum:
59+
- celsius
60+
- fahrenheit
61+
required: ['location']
62+
tool_choice: auto
4363

4464
prompts:
45-
- Tell me about the weather in {{city}}.
65+
- Tell me about the weather in {{city}} in the default unit for the location.
4666

4767
tests:
4868
- vars:

‎package-lock.json

+26-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
"fast-xml-parser": "^4.4.1",
148148
"fastest-levenshtein": "^1.0.16",
149149
"glob": "^10.4.3",
150+
"groq-sdk": "^0.5.0",
150151
"inquirer": "^10.1.8",
151152
"js-rouge": "3.0.0",
152153
"js-yaml": "^4.1.0",

‎site/docs/providers/groq.md

+84-58
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,124 @@
11
# Groq
22

3-
The [Groq API](https://wow.groq.com) supports the OpenAI format, which makes it easy to integrate with promptfoo as a variation of the `openai` provider.
3+
The [Groq API](https://wow.groq.com) is integrated into promptfoo using the Groq SDK, providing a native experience for using Groq models in your evaluations. Groq offers high-performance inference for various large language models.
44

5-
## Prerequisites
5+
## Setup
66

7-
Before you begin, make sure you have a Groq API key. You can obtain one from the [Groq Console](https://console.groq.com/) and set it as the `GROQ_API_KEY` environment variable.
7+
To use Groq, you need to set up your API key:
88

9-
## Configuration
10-
11-
To use the Groq API with Promptfoo, you need to configure the provider in your promptfoo configuration file.
9+
1. Create a Groq API key in the [Groq Console](https://console.groq.com/).
10+
2. Set the `GROQ_API_KEY` environment variable:
1211

13-
Here's an example configuration:
14-
15-
```yaml
16-
providers:
17-
- id: openai:chat:mixtral-8x7b-32768
18-
config:
19-
apiBaseUrl: https://api.groq.com/openai/v1
20-
apiKeyEnvar: GROQ_API_KEY
21-
- id: openai:chat:llama2-70b-4096
22-
config:
23-
apiBaseUrl: https://api.groq.com/openai/v1
24-
apiKeyEnvar: GROQ_API_KEY
12+
```sh
13+
export GROQ_API_KEY=your_api_key_here
2514
```
2615

27-
In this configuration:
16+
Alternatively, you can specify the `apiKey` in the provider configuration (see below).
2817

29-
- `id` specifies the provider ID in the format `openai:chat:<model>`. Replace `<model>` with the desired Groq model.
30-
- `config.apiBaseUrl` points it to the Groq API.
31-
- `config.apiKeyEnvar` specifies the environment variable that holds your Groq API key.
18+
## Configuration
3219

33-
You can also directly specify the API key in the configuration using the `apiKey` field instead of `apiKeyEnvar`:
20+
Configure the Groq provider in your promptfoo configuration file:
3421

3522
```yaml
3623
providers:
37-
- id: openai:chat:mixtral-8x7b-32768
24+
- id: groq:llama3-groq-70b-8192-tool-use-preview
3825
config:
39-
apiBaseUrl: https://api.groq.com/openai/v1
40-
apiKey: gsk_abc123_your_groq_api_key
26+
temperature: 0.7
27+
max_tokens: 100
28+
tools:
29+
- type: function
30+
function:
31+
name: get_weather
32+
description: 'Get the current weather in a given location'
33+
parameters:
34+
type: object
35+
properties:
36+
location:
37+
type: string
38+
description: 'The city and state, e.g. San Francisco, CA'
39+
unit:
40+
type: string
41+
enum: [celsius, fahrenheit]
42+
required: [location]
43+
tool_choice: auto
4144
```
4245
46+
Key configuration options:
47+
48+
- `temperature`: Controls randomness in output (0.0 to 1.0)
49+
- `max_tokens`: Maximum number of tokens in the response
50+
- `tools`: Defines functions the model can use (for tool use/function calling)
51+
- `tool_choice`: Specifies how the model should choose tools ('auto', 'none', or a specific tool)
52+
4353
## Supported Models
4454

45-
As of the time of writing, the Groq API supports the following models:
55+
Groq supports a variety of models, including:
4656

47-
- `mixtral-8x7b-32768`
57+
- `gemma-7b-it`
58+
- `gemma2-9b-it`
59+
- `llama-3.1-405b-reasoning`
60+
- `llama-3.1-70b-versatile`
61+
- `llama-3.1-8b-instant`
4862
- `llama2-70b-4096`
63+
- `llama3-70b-8192`
64+
- `llama3-8b-8192`
65+
- `llama3-groq-70b-8192-tool-use-preview` (recommended for tool use)
66+
- `llama3-groq-8b-8192-tool-use-preview` (recommended for tool use)
67+
- `mixtral-8x7b-32768`
4968

50-
You can find the latest list of supported models in the [Groq Console documentation](https://console.groq.com/docs/models).
69+
For the most up-to-date list and detailed information about each model, refer to the [Groq Console documentation](https://console.groq.com/docs/models).
5170

5271
## Using the Provider
5372

54-
Once you have configured the Groq provider, you can use it in your Promptfoo tests just like any other OpenAI-compatible provider. Specify the provider ID in your test configuration, and Promptfoo will send the requests to the Groq API.
55-
56-
Here's an example test configuration:
73+
Specify the Groq provider in your test configuration:
5774

5875
```yaml
59-
prompts:
60-
- 'Answer this as concisely as possible: {{question}}'
61-
6276
providers:
63-
- id: openai:chat:mixtral-8x7b-32768
64-
config:
65-
apiBaseUrl: https://api.groq.com/openai/v1
66-
apiKeyEnvar: GROQ_API_KEY
67-
- id: openai:chat:llama2-70b-4096
77+
- id: groq:llama3-groq-70b-8192-tool-use-preview
6878
config:
69-
apiBaseUrl: https://api.groq.com/openai/v1
70-
apiKeyEnvar: GROQ_API_KEY
79+
temperature: 0.5
80+
max_tokens: 150
81+
82+
prompts:
83+
- Tell me about the weather in {{city}} in the default unit for the location.
7184
7285
tests:
7386
- vars:
74-
question: What is the capital of France?
75-
assert:
76-
- type: equals
77-
value: Paris
87+
city: Boston
88+
- vars:
89+
city: New York
7890
```
7991

80-
In this example, the test will be run against both the `mixtral-8x7b-32768` and `llama2-70b-4096` models using the Groq API.
92+
## Tool Use (Function Calling)
8193

82-
## Additional Configuration
83-
84-
The Groq provider supports additional configuration options, such as `temperature`, `max_tokens`, etc. You can specify these options under the `config` field for each provider.
85-
86-
For example:
94+
Groq supports tool use, allowing models to call predefined functions. Configure tools in your provider settings:
8795

8896
```yaml
8997
providers:
90-
- id: openai:chat:mixtral-8x7b-32768
98+
- id: groq:llama3-groq-70b-8192-tool-use-preview
9199
config:
92-
apiBaseUrl: https://api.groq.com/openai/v1
93-
apiKeyEnvar: GROQ_API_KEY
94-
temperature: 0.7
95-
max_tokens: 100
100+
tools:
101+
- type: function
102+
function:
103+
name: get_weather
104+
description: 'Get the current weather in a given location'
105+
parameters:
106+
type: object
107+
properties:
108+
location:
109+
type: string
110+
description: 'The city and state, e.g. San Francisco, CA'
111+
unit:
112+
type: string
113+
enum: [celsius, fahrenheit]
114+
required: [location]
115+
tool_choice: auto
96116
```
97117

98-
Refer to the [Groq OpenAI compatibility docs](https://console.groq.com/docs/openai) as well as the [OpenAI documentation](https://platform.openai.com/docs/api-reference/completions/create) for the full list of supported configuration options.
118+
For complex tools or ambiguous queries, use the `llama3-groq-70b-8192-tool-use-preview` model.
119+
120+
## Additional Capabilities
121+
122+
- **Caching**: Groq provider caches previous LLM requests by default for improved performance.
123+
- **Token Usage Tracking**: Provides detailed information on token usage for each request.
124+
- **Cost Calculation**: Automatically calculates the cost of each request based on token usage and the specific model used.

‎src/providers.ts

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { BAMChatProvider, BAMEmbeddingProvider } from './providers/bam';
1515
import { AwsBedrockCompletionProvider, AwsBedrockEmbeddingProvider } from './providers/bedrock';
1616
import * as CloudflareAiProviders from './providers/cloudflare-ai';
1717
import { CohereChatCompletionProvider } from './providers/cohere';
18+
import { GroqProvider } from './providers/groq';
1819
import { HttpProvider } from './providers/http';
1920
import {
2021
HuggingfaceFeatureExtractionProvider,
@@ -363,6 +364,9 @@ export async function loadApiProvider(
363364
ret = new RedteamCrescendoProvider(providerOptions.config);
364365
} else if (providerPath === 'promptfoo:manual-input') {
365366
ret = new ManualInputProvider(providerOptions);
367+
} else if (providerPath.startsWith('groq:')) {
368+
const modelName = providerPath.split(':')[1];
369+
ret = new GroqProvider(modelName, providerOptions);
366370
} else {
367371
if (providerPath.startsWith('file://')) {
368372
providerPath = providerPath.slice('file://'.length);
@@ -439,5 +443,6 @@ export default {
439443
LocalAiChatProvider,
440444
BAMChatProvider,
441445
BAMEmbeddingProvider,
446+
GroqProvider,
442447
loadApiProvider,
443448
};
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.