@mzhub/promptc 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/dist/src/providers/cerebras.d.ts +2 -1
- package/dist/src/providers/cerebras.d.ts.map +1 -1
- package/dist/src/providers/cerebras.js +23 -22
- package/dist/src/providers/cerebras.js.map +1 -1
- package/dist/src/providers/groq.d.ts +3 -2
- package/dist/src/providers/groq.d.ts.map +1 -1
- package/dist/src/providers/groq.js +38 -27
- package/dist/src/providers/groq.js.map +1 -1
- package/package.json +16 -6
- package/src/providers/cerebras.ts +26 -27
- package/src/providers/groq.ts +42 -32
package/README.md
CHANGED
|
@@ -14,10 +14,12 @@ Install your preferred LLM provider (optional peer dependencies):
|
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
16
|
# Pick one or more
|
|
17
|
-
npm install openai
|
|
18
|
-
npm install @anthropic-ai/sdk
|
|
19
|
-
npm install @google/generative-ai
|
|
20
|
-
npm install
|
|
17
|
+
npm install openai # OpenAI
|
|
18
|
+
npm install @anthropic-ai/sdk # Anthropic (Claude)
|
|
19
|
+
npm install @google/generative-ai # Google (Gemini)
|
|
20
|
+
npm install groq-sdk # Groq (fast inference)
|
|
21
|
+
npm install @cerebras/cerebras_cloud_sdk # Cerebras (fast inference)
|
|
22
|
+
npm install ollama # Ollama (local)
|
|
21
23
|
```
|
|
22
24
|
|
|
23
25
|
## Quick Start
|
|
@@ -222,4 +224,3 @@ The compiler returns a serializable artifact:
|
|
|
222
224
|
## License
|
|
223
225
|
|
|
224
226
|
MIT
|
|
225
|
-
|
|
@@ -2,9 +2,10 @@ import type { LLMProvider, CompletionParams, CompletionResult, ProviderConfig }
|
|
|
2
2
|
export declare class CerebrasProvider implements LLMProvider {
|
|
3
3
|
name: string;
|
|
4
4
|
defaultModel: string;
|
|
5
|
+
private client;
|
|
5
6
|
private apiKey;
|
|
6
|
-
private baseUrl;
|
|
7
7
|
constructor(config: ProviderConfig);
|
|
8
|
+
private ensureClient;
|
|
8
9
|
complete(params: CompletionParams): Promise<CompletionResult>;
|
|
9
10
|
}
|
|
10
11
|
//# sourceMappingURL=cerebras.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cerebras.d.ts","sourceRoot":"","sources":["../../../src/providers/cerebras.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,qBAAa,gBAAiB,YAAW,WAAW;IAClD,IAAI,SAAc;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"cerebras.d.ts","sourceRoot":"","sources":["../../../src/providers/cerebras.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,qBAAa,gBAAiB,YAAW,WAAW;IAClD,IAAI,SAAc;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,MAAM,CAAM;IACpB,OAAO,CAAC,MAAM,CAAS;gBAEX,MAAM,EAAE,cAAc;YAMpB,YAAY;IAcpB,QAAQ,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAkBpE"}
|
|
@@ -1,37 +1,38 @@
|
|
|
1
1
|
export class CerebrasProvider {
|
|
2
2
|
name = "cerebras";
|
|
3
3
|
defaultModel;
|
|
4
|
+
client;
|
|
4
5
|
apiKey;
|
|
5
|
-
baseUrl;
|
|
6
6
|
constructor(config) {
|
|
7
7
|
this.apiKey = config.apiKey || process.env.CEREBRAS_API_KEY || "";
|
|
8
|
-
this.baseUrl = config.baseUrl || "https://api.cerebras.ai/v1";
|
|
9
8
|
this.defaultModel = config.defaultModel || "llama3.1-8b";
|
|
9
|
+
this.client = null;
|
|
10
|
+
}
|
|
11
|
+
async ensureClient() {
|
|
12
|
+
if (!this.client) {
|
|
13
|
+
try {
|
|
14
|
+
const Cerebras = (await import("@cerebras/cerebras_cloud_sdk")).default;
|
|
15
|
+
this.client = new Cerebras({ apiKey: this.apiKey });
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
throw new Error("@cerebras/cerebras_cloud_sdk is not installed. Install it with: npm install @cerebras/cerebras_cloud_sdk");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return this.client;
|
|
10
22
|
}
|
|
11
23
|
async complete(params) {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
body: JSON.stringify({
|
|
19
|
-
model: params.model || this.defaultModel,
|
|
20
|
-
messages: [{ role: "user", content: params.prompt }],
|
|
21
|
-
temperature: params.temperature ?? 0.7,
|
|
22
|
-
max_tokens: params.maxTokens ?? 1024,
|
|
23
|
-
}),
|
|
24
|
+
const client = await this.ensureClient();
|
|
25
|
+
const response = await client.chat.completions.create({
|
|
26
|
+
model: params.model || this.defaultModel,
|
|
27
|
+
messages: [{ role: "user", content: params.prompt }],
|
|
28
|
+
temperature: params.temperature ?? 0.7,
|
|
29
|
+
max_completion_tokens: params.maxTokens ?? 1024,
|
|
24
30
|
});
|
|
25
|
-
if (!response.ok) {
|
|
26
|
-
const error = await response.text();
|
|
27
|
-
throw new Error(`Cerebras API error: ${response.status} - ${error}`);
|
|
28
|
-
}
|
|
29
|
-
const data = (await response.json());
|
|
30
31
|
return {
|
|
31
|
-
content:
|
|
32
|
+
content: response.choices[0].message.content || "",
|
|
32
33
|
usage: {
|
|
33
|
-
inputTokens:
|
|
34
|
-
outputTokens:
|
|
34
|
+
inputTokens: response.usage?.prompt_tokens || 0,
|
|
35
|
+
outputTokens: response.usage?.completion_tokens || 0,
|
|
35
36
|
},
|
|
36
37
|
};
|
|
37
38
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cerebras.js","sourceRoot":"","sources":["../../../src/providers/cerebras.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,gBAAgB;IAC3B,IAAI,GAAG,UAAU,CAAC;IAClB,YAAY,CAAS;IACb,MAAM,
|
|
1
|
+
{"version":3,"file":"cerebras.js","sourceRoot":"","sources":["../../../src/providers/cerebras.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,gBAAgB;IAC3B,IAAI,GAAG,UAAU,CAAC;IAClB,YAAY,CAAS;IACb,MAAM,CAAM;IACZ,MAAM,CAAS;IAEvB,YAAY,MAAsB;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,EAAE,CAAC;QAClE,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,IAAI,aAAa,CAAC;QACzD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;IACrB,CAAC;IAEO,KAAK,CAAC,YAAY;QACxB,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,IAAI,CAAC;gBACH,MAAM,QAAQ,GAAG,CAAC,MAAM,MAAM,CAAC,8BAA8B,CAAC,CAAC,CAAC,OAAO,CAAC;gBACxE,IAAI,CAAC,MAAM,GAAG,IAAI,QAAQ,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;YACtD,CAAC;YAAC,MAAM,CAAC;gBACP,MAAM,IAAI,KAAK,CACb,0GAA0G,CAC3G,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,MAAwB;QACrC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,YAAY,EAAE,CAAC;QAEzC,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;YACpD,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,IAAI,CAAC,YAAY;YACxC,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;YACpD,WAAW,EAAE,MAAM,CAAC,WAAW,IAAI,GAAG;YACtC,qBAAqB,EAAE,MAAM,CAAC,SAAS,IAAI,IAAI;SAChD,CAAC,CAAC;QAEH,OAAO;YACL,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE;YAClD,KAAK,EAAE;gBACL,WAAW,EAAE,QAAQ,CAAC,KAAK,EAAE,aAAa,IAAI,CAAC;gBAC/C,YAAY,EAAE,QAAQ,CAAC,KAAK,EAAE,iBAAiB,IAAI,CAAC;aACrD;SACF,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -2,9 +2,10 @@ import type { LLMProvider, CompletionParams, CompletionResult, ProviderConfig }
|
|
|
2
2
|
export declare class GroqProvider implements LLMProvider {
|
|
3
3
|
name: string;
|
|
4
4
|
defaultModel: string;
|
|
5
|
-
private
|
|
6
|
-
private baseUrl;
|
|
5
|
+
private client;
|
|
7
6
|
constructor(config: ProviderConfig);
|
|
7
|
+
private initClient;
|
|
8
|
+
private ensureClient;
|
|
8
9
|
complete(params: CompletionParams): Promise<CompletionResult>;
|
|
9
10
|
}
|
|
10
11
|
//# sourceMappingURL=groq.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"groq.d.ts","sourceRoot":"","sources":["../../../src/providers/groq.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,qBAAa,YAAa,YAAW,WAAW;IAC9C,IAAI,SAAU;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"groq.d.ts","sourceRoot":"","sources":["../../../src/providers/groq.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,qBAAa,YAAa,YAAW,WAAW;IAC9C,IAAI,SAAU;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,MAAM,CAAM;gBAER,MAAM,EAAE,cAAc;YASpB,UAAU;YASV,YAAY;IAgBpB,QAAQ,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAoBpE"}
|
|
@@ -1,40 +1,51 @@
|
|
|
1
1
|
export class GroqProvider {
|
|
2
2
|
name = "groq";
|
|
3
3
|
defaultModel;
|
|
4
|
-
|
|
5
|
-
baseUrl;
|
|
4
|
+
client;
|
|
6
5
|
constructor(config) {
|
|
7
|
-
|
|
8
|
-
this.baseUrl = config.baseUrl || "https://api.groq.com/openai/v1";
|
|
6
|
+
const apiKey = config.apiKey || process.env.GROQ_API_KEY || "";
|
|
9
7
|
this.defaultModel = config.defaultModel || "llama-3.3-70b-versatile";
|
|
8
|
+
// Dynamic import to avoid requiring groq-sdk if not used
|
|
9
|
+
this.client = null;
|
|
10
|
+
this.initClient(apiKey);
|
|
11
|
+
}
|
|
12
|
+
async initClient(apiKey) {
|
|
13
|
+
try {
|
|
14
|
+
const Groq = (await import("groq-sdk")).default;
|
|
15
|
+
this.client = new Groq({ apiKey });
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// Will throw on complete() if SDK not installed
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async ensureClient() {
|
|
22
|
+
if (!this.client) {
|
|
23
|
+
try {
|
|
24
|
+
const Groq = (await import("groq-sdk")).default;
|
|
25
|
+
this.client = new Groq({
|
|
26
|
+
apiKey: process.env.GROQ_API_KEY || "",
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
throw new Error("groq-sdk is not installed. Install it with: npm install groq-sdk");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return this.client;
|
|
10
34
|
}
|
|
11
35
|
async complete(params) {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
model: params.model || this.defaultModel,
|
|
20
|
-
messages: [{ role: "user", content: params.prompt }],
|
|
21
|
-
temperature: params.temperature ?? 0.7,
|
|
22
|
-
max_tokens: params.maxTokens ?? 1024,
|
|
23
|
-
response_format: params.responseFormat === "json"
|
|
24
|
-
? { type: "json_object" }
|
|
25
|
-
: undefined,
|
|
26
|
-
}),
|
|
36
|
+
const client = await this.ensureClient();
|
|
37
|
+
const response = await client.chat.completions.create({
|
|
38
|
+
model: params.model || this.defaultModel,
|
|
39
|
+
messages: [{ role: "user", content: params.prompt }],
|
|
40
|
+
temperature: params.temperature ?? 0.7,
|
|
41
|
+
max_tokens: params.maxTokens ?? 1024,
|
|
42
|
+
response_format: params.responseFormat === "json" ? { type: "json_object" } : undefined,
|
|
27
43
|
});
|
|
28
|
-
if (!response.ok) {
|
|
29
|
-
const error = await response.text();
|
|
30
|
-
throw new Error(`Groq API error: ${response.status} - ${error}`);
|
|
31
|
-
}
|
|
32
|
-
const data = (await response.json());
|
|
33
44
|
return {
|
|
34
|
-
content:
|
|
45
|
+
content: response.choices[0].message.content || "",
|
|
35
46
|
usage: {
|
|
36
|
-
inputTokens:
|
|
37
|
-
outputTokens:
|
|
47
|
+
inputTokens: response.usage?.prompt_tokens || 0,
|
|
48
|
+
outputTokens: response.usage?.completion_tokens || 0,
|
|
38
49
|
},
|
|
39
50
|
};
|
|
40
51
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"groq.js","sourceRoot":"","sources":["../../../src/providers/groq.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,YAAY;IACvB,IAAI,GAAG,MAAM,CAAC;IACd,YAAY,CAAS;IACb,MAAM,
|
|
1
|
+
{"version":3,"file":"groq.js","sourceRoot":"","sources":["../../../src/providers/groq.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,YAAY;IACvB,IAAI,GAAG,MAAM,CAAC;IACd,YAAY,CAAS;IACb,MAAM,CAAM;IAEpB,YAAY,MAAsB;QAChC,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,EAAE,CAAC;QAC/D,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,IAAI,yBAAyB,CAAC;QAErE,yDAAyD;QACzD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IAC1B,CAAC;IAEO,KAAK,CAAC,UAAU,CAAC,MAAc;QACrC,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,CAAC,MAAM,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC;YAChD,IAAI,CAAC,MAAM,GAAG,IAAI,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QACrC,CAAC;QAAC,MAAM,CAAC;YACP,gDAAgD;QAClD,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,YAAY;QACxB,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,IAAI,CAAC;gBACH,MAAM,IAAI,GAAG,CAAC,MAAM,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC;gBAChD,IAAI,CAAC,MAAM,GAAG,IAAI,IAAI,CAAC;oBACrB,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,EAAE;iBACvC,CAAC,CAAC;YACL,CAAC;YAAC,MAAM,CAAC;gBACP,MAAM,IAAI,KAAK,CACb,kEAAkE,CACnE,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,MAAwB;QACrC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,YAAY,EAAE,CAAC;QAEzC,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;YACpD,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,IAAI,CAAC,YAAY;YACxC,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;YACpD,WAAW,EAAE,MAAM,CAAC,WAAW,IAAI,GAAG;YACtC,UAAU,EAAE,MAAM,CAAC,SAAS,IAAI,IAAI;YACpC,eAAe,EACb,MAAM,CAAC,cAAc,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC,CAAC,CAAC,SAAS;SACzE,CAAC,CAAC;QAEH,OAAO;YACL,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE;YAClD,KAAK,EAAE;gBACL,WAAW,EAAE,QAAQ,CAAC,KAAK,EAAE,aAAa,IAAI,CAAC;gBAC/C,YAAY,EAAE,QAAQ,CAAC,KAAK,EAAE,iBAAiB,IAAI,CAAC;aACrD;SACF,CAAC;IACJ,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mzhub/promptc",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Compile and optimize LLM prompts in JavaScript using type-safe schemas and examples.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -31,14 +31,16 @@
|
|
|
31
31
|
"author": "MZ Hub",
|
|
32
32
|
"license": "MIT",
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"
|
|
35
|
-
"
|
|
34
|
+
"p-limit": "^6.1.0",
|
|
35
|
+
"zod": "^3.24.0"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"openai": "^4.0.0",
|
|
39
38
|
"@anthropic-ai/sdk": "^0.30.0",
|
|
39
|
+
"@cerebras/cerebras_cloud_sdk": "^1.0.0",
|
|
40
40
|
"@google/generative-ai": "^0.21.0",
|
|
41
|
-
"
|
|
41
|
+
"groq-sdk": "^0.8.0",
|
|
42
|
+
"ollama": "^0.5.0",
|
|
43
|
+
"openai": "^4.0.0"
|
|
42
44
|
},
|
|
43
45
|
"peerDependenciesMeta": {
|
|
44
46
|
"openai": {
|
|
@@ -52,11 +54,19 @@
|
|
|
52
54
|
},
|
|
53
55
|
"ollama": {
|
|
54
56
|
"optional": true
|
|
57
|
+
},
|
|
58
|
+
"groq-sdk": {
|
|
59
|
+
"optional": true
|
|
60
|
+
},
|
|
61
|
+
"@cerebras/cerebras_cloud_sdk": {
|
|
62
|
+
"optional": true
|
|
55
63
|
}
|
|
56
64
|
},
|
|
57
65
|
"devDependencies": {
|
|
58
|
-
"
|
|
66
|
+
"@cerebras/cerebras_cloud_sdk": "^1.59.0",
|
|
59
67
|
"@types/node": "^22.0.0",
|
|
68
|
+
"groq-sdk": "^0.37.0",
|
|
69
|
+
"typescript": "^5.7.0",
|
|
60
70
|
"vitest": "^2.0.0"
|
|
61
71
|
}
|
|
62
72
|
}
|
|
@@ -8,45 +8,44 @@ import type {
|
|
|
8
8
|
export class CerebrasProvider implements LLMProvider {
|
|
9
9
|
name = "cerebras";
|
|
10
10
|
defaultModel: string;
|
|
11
|
+
private client: any;
|
|
11
12
|
private apiKey: string;
|
|
12
|
-
private baseUrl: string;
|
|
13
13
|
|
|
14
14
|
constructor(config: ProviderConfig) {
|
|
15
15
|
this.apiKey = config.apiKey || process.env.CEREBRAS_API_KEY || "";
|
|
16
|
-
this.baseUrl = config.baseUrl || "https://api.cerebras.ai/v1";
|
|
17
16
|
this.defaultModel = config.defaultModel || "llama3.1-8b";
|
|
17
|
+
this.client = null;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
async
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
temperature: params.temperature ?? 0.7,
|
|
31
|
-
max_tokens: params.maxTokens ?? 1024,
|
|
32
|
-
}),
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
if (!response.ok) {
|
|
36
|
-
const error = await response.text();
|
|
37
|
-
throw new Error(`Cerebras API error: ${response.status} - ${error}`);
|
|
20
|
+
private async ensureClient(): Promise<any> {
|
|
21
|
+
if (!this.client) {
|
|
22
|
+
try {
|
|
23
|
+
const Cerebras = (await import("@cerebras/cerebras_cloud_sdk")).default;
|
|
24
|
+
this.client = new Cerebras({ apiKey: this.apiKey });
|
|
25
|
+
} catch {
|
|
26
|
+
throw new Error(
|
|
27
|
+
"@cerebras/cerebras_cloud_sdk is not installed. Install it with: npm install @cerebras/cerebras_cloud_sdk"
|
|
28
|
+
);
|
|
29
|
+
}
|
|
38
30
|
}
|
|
31
|
+
return this.client;
|
|
32
|
+
}
|
|
39
33
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
async complete(params: CompletionParams): Promise<CompletionResult> {
|
|
35
|
+
const client = await this.ensureClient();
|
|
36
|
+
|
|
37
|
+
const response = await client.chat.completions.create({
|
|
38
|
+
model: params.model || this.defaultModel,
|
|
39
|
+
messages: [{ role: "user", content: params.prompt }],
|
|
40
|
+
temperature: params.temperature ?? 0.7,
|
|
41
|
+
max_completion_tokens: params.maxTokens ?? 1024,
|
|
42
|
+
});
|
|
44
43
|
|
|
45
44
|
return {
|
|
46
|
-
content:
|
|
45
|
+
content: response.choices[0].message.content || "",
|
|
47
46
|
usage: {
|
|
48
|
-
inputTokens:
|
|
49
|
-
outputTokens:
|
|
47
|
+
inputTokens: response.usage?.prompt_tokens || 0,
|
|
48
|
+
outputTokens: response.usage?.completion_tokens || 0,
|
|
50
49
|
},
|
|
51
50
|
};
|
|
52
51
|
}
|
package/src/providers/groq.ts
CHANGED
|
@@ -8,49 +8,59 @@ import type {
|
|
|
8
8
|
export class GroqProvider implements LLMProvider {
|
|
9
9
|
name = "groq";
|
|
10
10
|
defaultModel: string;
|
|
11
|
-
private
|
|
12
|
-
private baseUrl: string;
|
|
11
|
+
private client: any;
|
|
13
12
|
|
|
14
13
|
constructor(config: ProviderConfig) {
|
|
15
|
-
|
|
16
|
-
this.baseUrl = config.baseUrl || "https://api.groq.com/openai/v1";
|
|
14
|
+
const apiKey = config.apiKey || process.env.GROQ_API_KEY || "";
|
|
17
15
|
this.defaultModel = config.defaultModel || "llama-3.3-70b-versatile";
|
|
16
|
+
|
|
17
|
+
// Dynamic import to avoid requiring groq-sdk if not used
|
|
18
|
+
this.client = null;
|
|
19
|
+
this.initClient(apiKey);
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
async
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
model: params.model || this.defaultModel,
|
|
29
|
-
messages: [{ role: "user", content: params.prompt }],
|
|
30
|
-
temperature: params.temperature ?? 0.7,
|
|
31
|
-
max_tokens: params.maxTokens ?? 1024,
|
|
32
|
-
response_format:
|
|
33
|
-
params.responseFormat === "json"
|
|
34
|
-
? { type: "json_object" }
|
|
35
|
-
: undefined,
|
|
36
|
-
}),
|
|
37
|
-
});
|
|
22
|
+
private async initClient(apiKey: string) {
|
|
23
|
+
try {
|
|
24
|
+
const Groq = (await import("groq-sdk")).default;
|
|
25
|
+
this.client = new Groq({ apiKey });
|
|
26
|
+
} catch {
|
|
27
|
+
// Will throw on complete() if SDK not installed
|
|
28
|
+
}
|
|
29
|
+
}
|
|
38
30
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
31
|
+
private async ensureClient(): Promise<any> {
|
|
32
|
+
if (!this.client) {
|
|
33
|
+
try {
|
|
34
|
+
const Groq = (await import("groq-sdk")).default;
|
|
35
|
+
this.client = new Groq({
|
|
36
|
+
apiKey: process.env.GROQ_API_KEY || "",
|
|
37
|
+
});
|
|
38
|
+
} catch {
|
|
39
|
+
throw new Error(
|
|
40
|
+
"groq-sdk is not installed. Install it with: npm install groq-sdk"
|
|
41
|
+
);
|
|
42
|
+
}
|
|
42
43
|
}
|
|
44
|
+
return this.client;
|
|
45
|
+
}
|
|
43
46
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
async complete(params: CompletionParams): Promise<CompletionResult> {
|
|
48
|
+
const client = await this.ensureClient();
|
|
49
|
+
|
|
50
|
+
const response = await client.chat.completions.create({
|
|
51
|
+
model: params.model || this.defaultModel,
|
|
52
|
+
messages: [{ role: "user", content: params.prompt }],
|
|
53
|
+
temperature: params.temperature ?? 0.7,
|
|
54
|
+
max_tokens: params.maxTokens ?? 1024,
|
|
55
|
+
response_format:
|
|
56
|
+
params.responseFormat === "json" ? { type: "json_object" } : undefined,
|
|
57
|
+
});
|
|
48
58
|
|
|
49
59
|
return {
|
|
50
|
-
content:
|
|
60
|
+
content: response.choices[0].message.content || "",
|
|
51
61
|
usage: {
|
|
52
|
-
inputTokens:
|
|
53
|
-
outputTokens:
|
|
62
|
+
inputTokens: response.usage?.prompt_tokens || 0,
|
|
63
|
+
outputTokens: response.usage?.completion_tokens || 0,
|
|
54
64
|
},
|
|
55
65
|
};
|
|
56
66
|
}
|