Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.25.0",
"packages": [
"plugins/*",
"docs",
"examples/*"
]
}
"packages": ["plugins/*", "docs", "examples/*"]
}
44 changes: 22 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion plugins/groq/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
lib/
node_modules/
node_modules/
.env
6 changes: 6 additions & 0 deletions plugins/groq/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/', '/lib/'],
};
6 changes: 4 additions & 2 deletions plugins/groq/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
"groq-sdk": "^0.19.0"
},
"peerDependencies": {
"genkit": "^0.9.0 || ^1.0.0"
"genkit": "^1.19.3"
},
"devDependencies": {
"@types/hast": "^3.0.4",
"@types/mdast": "^4.0.4",
"@types/node": "^20.11.16",
"jest": "^30.2.0",
"npm-run-all": "^4.1.5",
"rimraf": "^6.0.1",
"ts-jest": "^29.1.0",
"tsup": "^8.0.2",
"tsx": "^4.7.0",
"typescript": "^4.9.5"
Expand All @@ -61,6 +63,6 @@
"build:clean": "rimraf ./lib",
"build": "npm-run-all build:clean check compile",
"build:watch": "tsup-node --watch",
"test": "tsx \"./tests/\""
"test": "jest"
}
}
28 changes: 14 additions & 14 deletions plugins/groq/src/groq_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { ChatCompletion } from 'groq-sdk/resources/chat/index.mjs';
import {
GenerateRequest,
GenerationCommonConfigSchema,
Genkit,
Message,
MessageData,
Part,
Expand All @@ -40,6 +39,7 @@ import {
modelRef,
ToolDefinition,
} from 'genkit/model';
import { model } from 'genkit/plugin';

export const GroqConfigSchema = GenerationCommonConfigSchema.extend({
stream: z.boolean().optional(),
Expand Down Expand Up @@ -572,29 +572,29 @@ export function toGroqRequestBody(
}

/**
* Defines a Groq model.
* Creates a Groq model action.
*
* @param name - The name of the model.
* @param client - The Groq client.
* @returns The model.
* @returns The model action.
*/
export function groqModel(ai: Genkit, name: string, client: Groq) {
const model = SUPPORTED_GROQ_MODELS[name];
if (!model) throw new Error(`Unsupported model: ${name}`);
const modelId = `groq/${name}`;
export function createGroqModel(name: string, client: Groq) {
const modelRef = SUPPORTED_GROQ_MODELS[name];
if (!modelRef) throw new Error(`Unsupported model: ${name}`);
const modelId = `${name}`;

return ai.defineModel(
return model(
{
name: modelId,
...model.info,
configSchema: model.configSchema,
...modelRef.info,
configSchema: modelRef.configSchema,
},
async (
request,
streamingCallback?: StreamingCallback<GenerateResponseChunkData>
) => {
async (request, options: any) => {
let response: ChatCompletion;
const body = toGroqRequestBody(name, request);
const streamingCallback:
| StreamingCallback<GenerateResponseChunkData>
| undefined = options?.streamingCallback;
if (streamingCallback) {
if (request.output?.format === 'json') {
throw new Error(
Expand Down
62 changes: 39 additions & 23 deletions plugins/groq/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// Import necessary types and functions for Groq SDK integration.
import Groq from 'groq-sdk';
import {
groqModel,
createGroqModel,
llama3x70b,
llama3x8b,
llamaGuard3x8b,
Expand All @@ -32,8 +32,7 @@ import {
deepseekR1DistillLlamax70b,
SUPPORTED_GROQ_MODELS,
} from './groq_models';
import { Genkit } from 'genkit';
import { genkitPlugin } from 'genkit/plugin';
import { genkitPluginV2 } from 'genkit/plugin';

// Export models for direct access
export {
Expand Down Expand Up @@ -92,28 +91,45 @@ export interface PluginOptions {
* @param options - Optional configuration settings for the plugin.
* @returns An object containing the models initialized with the Groq client.
*/
export const groq = (options?: PluginOptions) =>
genkitPlugin('groq', async (ai: Genkit) => {
const apiKey = options?.apiKey || process.env.GROQ_API_KEY;
if (!apiKey) {
throw new Error(
'Please provide the API key or set the GROQ_API_KEY environment variable'
);
}
export const groq = (options?: PluginOptions) => {
const apiKey = options?.apiKey || process.env.GROQ_API_KEY;
if (!apiKey) {
throw new Error(
'Please provide the API key or set the GROQ_API_KEY environment variable'
);
}

// Initialize Groq client
const client = new Groq({
baseURL: options?.baseURL || process.env.GROQ_BASE_URL, // Optional base URL with environment variable fallback
apiKey, // API key retrieved from options or environment
timeout: options?.timeout, // Optional timeout
maxRetries: options?.maxRetries, // Optional max retries
});
const client = new Groq({
baseURL: options?.baseURL || process.env.GROQ_BASE_URL,
apiKey,
timeout: options?.timeout,
maxRetries: options?.maxRetries,
});

// Register each model with the Genkit instance
for (const name of Object.keys(SUPPORTED_GROQ_MODELS)) {
groqModel(ai, name, client);
}
return genkitPluginV2({
name: 'groq',
init: async () => {
const models = Object.keys(SUPPORTED_GROQ_MODELS).map((name) =>
createGroqModel(name, client)
);

return models;
},
resolve: async (actionType, actionName) => {
if (actionType === 'model') {
return createGroqModel(actionName, client);
}
return undefined;
},
list: async () => {
return Object.keys(SUPPORTED_GROQ_MODELS).map((name) => ({
name,
namespace: 'groq',
type: 'model' as const,
info: SUPPORTED_GROQ_MODELS[name].info,
}));
},
});
};

// Default export for plugin usage
export default groq;
Loading