groq-module 0.0.0-stage → 1.0.1
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 +188 -3
- package/groqModule.js +146 -0
- package/package.json +9 -4
package/README.md
CHANGED
|
@@ -1,3 +1,188 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
groq-module
|
|
2
|
+
|
|
3
|
+
A lightweight Node.js module for interacting with the Groq API. Easily send messages to AI models, manage conversation history, configure response formats, and process structured JSON responses with a simple interface.
|
|
4
|
+
|
|
5
|
+
Features
|
|
6
|
+
Simple integration with the Groq API
|
|
7
|
+
Support for multiple Groq models
|
|
8
|
+
Automatic language detection
|
|
9
|
+
Configurable response language
|
|
10
|
+
Optional plain-text response mode
|
|
11
|
+
JSON and JSON Schema response support
|
|
12
|
+
Conversation history management
|
|
13
|
+
Custom API key support
|
|
14
|
+
Environment variable support
|
|
15
|
+
Installation
|
|
16
|
+
bash
|
|
17
|
+
npm install groq-module
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
For environment variable management, you can optionally install dotenv:
|
|
21
|
+
|
|
22
|
+
bash
|
|
23
|
+
npm install dotenv
|
|
24
|
+
|
|
25
|
+
Configuration
|
|
26
|
+
|
|
27
|
+
Create a .env file in your project root:
|
|
28
|
+
|
|
29
|
+
env
|
|
30
|
+
GROQ_API_KEY=your_groq_api_key
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
You can obtain an API key from the Groq Console.
|
|
34
|
+
|
|
35
|
+
Usage
|
|
36
|
+
|
|
37
|
+
Import the askGroq function:
|
|
38
|
+
|
|
39
|
+
javascript
|
|
40
|
+
import { askGroq } from "groq-module";
|
|
41
|
+
|
|
42
|
+
Basic Example
|
|
43
|
+
javascript
|
|
44
|
+
const response = await askGroq("Hello, how are you?");
|
|
45
|
+
|
|
46
|
+
console.log(response);
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
The module automatically detects the language of the user's message and responds in the same language.
|
|
50
|
+
|
|
51
|
+
Custom Model
|
|
52
|
+
javascript
|
|
53
|
+
const response = await askGroq(
|
|
54
|
+
"Explain how APIs work.",
|
|
55
|
+
{
|
|
56
|
+
model: "openai/gpt-oss-120b"
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
console.log(response);
|
|
61
|
+
|
|
62
|
+
Custom Response Language
|
|
63
|
+
javascript
|
|
64
|
+
const response = await askGroq(
|
|
65
|
+
"Hello!",
|
|
66
|
+
{
|
|
67
|
+
lang: "German"
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
Plain Text Mode
|
|
72
|
+
|
|
73
|
+
Plain-text responses are enabled by default when no response format is specified.
|
|
74
|
+
|
|
75
|
+
javascript
|
|
76
|
+
const response = await askGroq(
|
|
77
|
+
"Explain JavaScript.",
|
|
78
|
+
{
|
|
79
|
+
plainText: true
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
JSON Responses
|
|
84
|
+
|
|
85
|
+
Request structured JSON responses using the responseFormat option:
|
|
86
|
+
|
|
87
|
+
javascript
|
|
88
|
+
const response = await askGroq(
|
|
89
|
+
"Generate information about a fictional user.",
|
|
90
|
+
{
|
|
91
|
+
plainText: false,
|
|
92
|
+
responseFormat: {
|
|
93
|
+
type: "json_object"
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
console.log(response);
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
JSON responses are automatically parsed into JavaScript objects.
|
|
102
|
+
|
|
103
|
+
Conversation History
|
|
104
|
+
|
|
105
|
+
Maintain conversations by passing previous messages through the chat option.
|
|
106
|
+
|
|
107
|
+
javascript
|
|
108
|
+
const [answer, chat] = await askGroq(
|
|
109
|
+
"My name is John.",
|
|
110
|
+
{
|
|
111
|
+
returnGroqChat: true
|
|
112
|
+
}
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
console.log(answer);
|
|
116
|
+
|
|
117
|
+
const [nextAnswer, updatedChat] = await askGroq(
|
|
118
|
+
"What is my name?",
|
|
119
|
+
{
|
|
120
|
+
chat,
|
|
121
|
+
returnGroqChat: true
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
console.log(nextAnswer);
|
|
126
|
+
|
|
127
|
+
Custom API Key
|
|
128
|
+
|
|
129
|
+
You can provide a custom API key instead of using the environment variable:
|
|
130
|
+
|
|
131
|
+
javascript
|
|
132
|
+
const response = await askGroq(
|
|
133
|
+
"Hello!",
|
|
134
|
+
{
|
|
135
|
+
apiKey: "your_groq_api_key"
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
API Reference
|
|
140
|
+
askGroq(messages, options)
|
|
141
|
+
|
|
142
|
+
Sends a request to the Groq Chat Completions API.
|
|
143
|
+
|
|
144
|
+
Parameter Type Default Description
|
|
145
|
+
messages string | Array Required User message or array of messages
|
|
146
|
+
apiKey string | null null Custom Groq API key
|
|
147
|
+
model string "openai/gpt-oss-120b" Groq model identifier
|
|
148
|
+
responseFormat object | null null Requested response format
|
|
149
|
+
lang string | null null Preferred response language
|
|
150
|
+
plainText boolean true Requests plain-text responses
|
|
151
|
+
returnGroqChat boolean false Returns the updated conversation history
|
|
152
|
+
chat Array [] Previous conversation messages
|
|
153
|
+
Return Values
|
|
154
|
+
|
|
155
|
+
By default, askGroq() returns the assistant response.
|
|
156
|
+
|
|
157
|
+
When returnGroqChat is enabled, it returns:
|
|
158
|
+
|
|
159
|
+
javascript
|
|
160
|
+
[
|
|
161
|
+
answer,
|
|
162
|
+
updatedChat
|
|
163
|
+
]
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
When JSON response formatting is enabled, the answer is automatically parsed into a JavaScript object.
|
|
167
|
+
|
|
168
|
+
Error Handling
|
|
169
|
+
|
|
170
|
+
The module throws errors when requests fail or invalid parameters are provided.
|
|
171
|
+
|
|
172
|
+
javascript
|
|
173
|
+
try {
|
|
174
|
+
const response = await askGroq("Hello!");
|
|
175
|
+
|
|
176
|
+
console.log(response);
|
|
177
|
+
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.error("Groq API Error:", error.message);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
Requirements
|
|
183
|
+
Node.js 18 or newer
|
|
184
|
+
A valid Groq API key
|
|
185
|
+
Internet connection
|
|
186
|
+
License
|
|
187
|
+
|
|
188
|
+
MIT License
|
package/groqModule.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
|
|
3
|
+
async function modelExists(modelName) {
|
|
4
|
+
const response = await fetch(
|
|
5
|
+
"https://api.groq.com/openai/v1/models",
|
|
6
|
+
{
|
|
7
|
+
headers: {
|
|
8
|
+
Authorization: `Bearer ${process.env.GROQ_API_KEY}`
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
if (!response.ok) {
|
|
14
|
+
throw new Error("Modelle konnten nicht abgerufen werden: " + response.status);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const data = await response.json();
|
|
18
|
+
|
|
19
|
+
return data.data.some(model => model.id === modelName);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function askGroq(
|
|
23
|
+
messages,
|
|
24
|
+
{
|
|
25
|
+
apiKey = null,
|
|
26
|
+
model = "openai/gpt-oss-120b",
|
|
27
|
+
responseFormat = null,
|
|
28
|
+
lang = null,
|
|
29
|
+
plainText = true,
|
|
30
|
+
returnGroqChat = false,
|
|
31
|
+
chat = []
|
|
32
|
+
} = {}
|
|
33
|
+
) {
|
|
34
|
+
try {
|
|
35
|
+
const key = apiKey || process.env.GROQ_API_KEY;
|
|
36
|
+
|
|
37
|
+
if (!key) {
|
|
38
|
+
throw new Error("GROQ_API_KEY wurde nicht angegeben!");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// String in Array umwandeln
|
|
42
|
+
if (typeof messages === "string") {
|
|
43
|
+
messages = [
|
|
44
|
+
{ role: "user", content: messages }
|
|
45
|
+
];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!Array.isArray(messages) || messages.length === 0) {
|
|
49
|
+
throw new Error("Ungültige Nachrichten!");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Chatverlauf überprüfen
|
|
53
|
+
if (!Array.isArray(chat)) {
|
|
54
|
+
throw new Error("Chat muss ein Array sein!");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// System-Anweisungen erstellen
|
|
58
|
+
const instructions = [
|
|
59
|
+
lang
|
|
60
|
+
? `Always respond in ${lang}.`
|
|
61
|
+
: "Detect the language of the user's message and respond in that same language.",
|
|
62
|
+
|
|
63
|
+
plainText && !responseFormat
|
|
64
|
+
? "Use plain text only. Never use Markdown or LaTeX."
|
|
65
|
+
: "",
|
|
66
|
+
|
|
67
|
+
responseFormat
|
|
68
|
+
? "Return the response as valid JSON matching the requested format."
|
|
69
|
+
: "",
|
|
70
|
+
|
|
71
|
+
"Always prioritize the most recent information available to you.",
|
|
72
|
+
"Keep answers concise. Avoid unnecessarily long sentences for simple questions."
|
|
73
|
+
].filter(Boolean).join(" ");
|
|
74
|
+
|
|
75
|
+
// Gesamte Nachrichten zusammenstellen
|
|
76
|
+
const apiMessages = [
|
|
77
|
+
{
|
|
78
|
+
role: "system",
|
|
79
|
+
content: instructions
|
|
80
|
+
},
|
|
81
|
+
...chat,
|
|
82
|
+
...messages
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
// API-Anfrage erstellen
|
|
86
|
+
const body = {
|
|
87
|
+
model,
|
|
88
|
+
messages: apiMessages
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
if (responseFormat) {
|
|
92
|
+
body.response_format = responseFormat;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Anfrage senden
|
|
96
|
+
const response = await fetch(
|
|
97
|
+
"https://api.groq.com/openai/v1/chat/completions",
|
|
98
|
+
{
|
|
99
|
+
method: "POST",
|
|
100
|
+
headers: {
|
|
101
|
+
"Content-Type": "application/json",
|
|
102
|
+
"Authorization": `Bearer ${key}`
|
|
103
|
+
},
|
|
104
|
+
body: JSON.stringify(body)
|
|
105
|
+
}
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const data = await response.json();
|
|
109
|
+
|
|
110
|
+
if (!response.ok) {
|
|
111
|
+
throw new Error(
|
|
112
|
+
data.error?.message || "Groq API Fehler"
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const content = data.choices[0].message.content;
|
|
117
|
+
|
|
118
|
+
// Antwort verarbeiten
|
|
119
|
+
const answer = (
|
|
120
|
+
responseFormat?.type === "json_schema" ||
|
|
121
|
+
responseFormat?.type === "json_object"
|
|
122
|
+
)
|
|
123
|
+
? JSON.parse(content)
|
|
124
|
+
: content;
|
|
125
|
+
|
|
126
|
+
// Antwort und Chatverlauf zurückgeben
|
|
127
|
+
if (returnGroqChat) {
|
|
128
|
+
const updatedChat = [
|
|
129
|
+
...chat,
|
|
130
|
+
...messages,
|
|
131
|
+
data.choices[0].message
|
|
132
|
+
];
|
|
133
|
+
|
|
134
|
+
return [
|
|
135
|
+
answer,
|
|
136
|
+
updatedChat
|
|
137
|
+
];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return answer;
|
|
141
|
+
|
|
142
|
+
} catch (error) {
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "groq-module",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Ein einfaches Groq Node.js Modul",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "groqModule.js",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"dotenv": "^16.4.5"
|
|
9
|
+
},
|
|
10
|
+
"exports": "./groqModule.js"
|
|
11
|
+
}
|