ai-openai 1.0.14 → 1.0.15
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/index.js +27 -23
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -10,33 +10,37 @@ require("dotenv").config();
|
|
|
10
10
|
* @returns {Promise<string>} - AI reply
|
|
11
11
|
*/
|
|
12
12
|
async function chat(message, options = {}) {
|
|
13
|
-
|
|
14
|
-
const system = options.system || "You are a helpful assistant.";
|
|
15
|
-
const token = process.env.HUGGINGFACE;
|
|
13
|
+
if (!message) throw new Error("Message is required");
|
|
16
14
|
|
|
15
|
+
const token = process.env.HUGGINGFACE;
|
|
17
16
|
if (!token) throw new Error("HuggingFace token required in .env as HUGGINGFACE");
|
|
18
|
-
|
|
17
|
+
|
|
18
|
+
// Ensure model & system prompt are set correctly
|
|
19
|
+
const model = options.model ? String(options.model).trim() : "openai/gpt-oss-120b";
|
|
20
|
+
const system = options.system ? String(options.system).trim() : "You are a helpful assistant.";
|
|
19
21
|
|
|
20
22
|
try {
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
{
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
23
|
+
const payload = {
|
|
24
|
+
model,
|
|
25
|
+
messages: [
|
|
26
|
+
{ role: "system", content: system },
|
|
27
|
+
{ role: "user", content: message }
|
|
28
|
+
]
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const headers = {
|
|
32
|
+
Authorization: `Bearer ${token}`,
|
|
33
|
+
"Content-Type": "application/json"
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const response = await axios.post("https://router.huggingface.co/v1/chat/completions", payload, { headers });
|
|
37
|
+
|
|
38
|
+
// HuggingFace sometimes wraps message content differently; fallback robust extraction
|
|
39
|
+
const aiText =
|
|
40
|
+
response.data?.choices?.[0]?.message?.content ??
|
|
41
|
+
response.data?.choices?.[0]?.text ??
|
|
42
|
+
"No response";
|
|
43
|
+
|
|
40
44
|
return aiText;
|
|
41
45
|
|
|
42
46
|
} catch (err) {
|