ai-openai 1.0.13 → 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 -28
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,42 +5,41 @@ require("dotenv").config();
|
|
|
5
5
|
* Send a message to HuggingFace chat API and get a reply
|
|
6
6
|
* @param {string} message - The user's message
|
|
7
7
|
* @param {object} options - Optional settings
|
|
8
|
-
* @param {string} options.model -
|
|
9
|
-
* @param {string} options.
|
|
8
|
+
* @param {string} options.model - Chat-completions model (default: "openai/gpt-oss-120b")
|
|
9
|
+
* @param {string} options.system - System prompt (default: "You are a helpful assistant.")
|
|
10
10
|
* @returns {Promise<string>} - AI reply
|
|
11
11
|
*/
|
|
12
12
|
async function chat(message, options = {}) {
|
|
13
|
-
const {
|
|
14
|
-
model = "openai/gpt-oss-120b",
|
|
15
|
-
token = process.env.HUGGINGFACE
|
|
16
|
-
} = options;
|
|
17
|
-
|
|
18
|
-
if (!token) throw new Error("HuggingFace token is required");
|
|
19
13
|
if (!message) throw new Error("Message is required");
|
|
20
14
|
|
|
15
|
+
const token = process.env.HUGGINGFACE;
|
|
16
|
+
if (!token) throw new Error("HuggingFace token required in .env as HUGGINGFACE");
|
|
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.";
|
|
21
|
+
|
|
21
22
|
try {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
Authorization: `Bearer ${token}`,
|
|
37
|
-
"Content-Type": "application/json"
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
);
|
|
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 });
|
|
41
37
|
|
|
38
|
+
// HuggingFace sometimes wraps message content differently; fallback robust extraction
|
|
42
39
|
const aiText =
|
|
43
|
-
response.data?.choices?.[0]?.message?.content
|
|
40
|
+
response.data?.choices?.[0]?.message?.content ??
|
|
41
|
+
response.data?.choices?.[0]?.text ??
|
|
42
|
+
"No response";
|
|
44
43
|
|
|
45
44
|
return aiText;
|
|
46
45
|
|