groq-module
v1.0.2
Published
Ein einfaches Groq Node.js Modul
Readme
groq-module
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.
Features
- Simple integration with the Groq API
- Support for multiple Groq models
- Automatic language detection
- Configurable response language
- Optional plain-text response mode
- JSON and JSON Schema response support
- Conversation history management
- Custom API key support
- Environment variable support
Installation
npm install groq-moduleFor environment variable management, you can optionally install dotenv:
npm install dotenvConfiguration
Create a .env file in your project root:
GROQ_API_KEY=your_groq_api_keyYou can obtain an API key from the Groq Console.
Usage
Import the askGroq function:
import { askGroq } from "groq-module";Basic Example
const response = await askGroq("Hello, how are you?");
console.log(response);The module automatically detects the language of the user's message and responds in the same language.
Custom Model
const response = await askGroq(
"Explain how APIs work.",
{
model: "openai/gpt-oss-120b"
}
);
console.log(response);Custom Response Language
const response = await askGroq(
"Hello!",
{
lang: "German"
}
);Plain Text Mode
Plain-text responses are enabled by default when no response format is specified.
const response = await askGroq(
"Explain JavaScript.",
{
plainText: true
}
);JSON Responses
Request structured JSON responses using the responseFormat option:
const response = await askGroq(
"Generate information about a fictional user.",
{
plainText: false,
responseFormat: {
type: "json_object"
}
}
);
console.log(response);JSON responses are automatically parsed into JavaScript objects.
Conversation History
Maintain conversations by passing previous messages through the chat option.
const [answer, chat] = await askGroq(
"My name is John.",
{
returnGroqChat: true
}
);
console.log(answer);
const [nextAnswer, updatedChat] = await askGroq(
"What is my name?",
{
chat,
returnGroqChat: true
}
);
console.log(nextAnswer);Custom API Key
You can provide a custom API key instead of using the environment variable:
const response = await askGroq(
"Hello!",
{
apiKey: "your_groq_api_key"
}
);API Reference
askGroq(messages, options)
Sends a request to the Groq Chat Completions API.
| Parameter | Type | Default | Description |
|---|---|---|---|
| messages | string \| Array | Required | User message or array of messages |
| apiKey | string \| null | null | Custom Groq API key |
| model | string | "openai/gpt-oss-120b" | Groq model identifier |
| responseFormat | object \| null | null | Requested response format |
| lang | string \| null | null | Preferred response language |
| plainText | boolean | true | Requests plain-text responses |
| returnGroqChat | boolean | false | Returns the updated conversation history |
| chat | Array | [] | Previous conversation messages |
Return Values
By default, askGroq() returns the assistant response.
When returnGroqChat is enabled, it returns:
[
answer,
updatedChat
]When JSON response formatting is enabled, the answer is automatically parsed into a JavaScript object.
Error Handling
The module throws errors when requests fail or invalid parameters are provided.
try {
const response = await askGroq("Hello!");
console.log(response);
} catch (error) {
console.error("Groq API Error:", error.message);
}Requirements
- Node.js 18 or newer
- A valid Groq API key
- Internet connection
License
MIT License
