groq-module 1.0.0 → 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 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
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/package.json
CHANGED