groq-module 0.0.0-stage → 1.0.0

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.
Files changed (3) hide show
  1. package/groqModule.js +146 -0
  2. package/package.json +8 -3
  3. package/README.md +0 -3
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": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
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"
6
11
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.