ai-openai 1.0.13 → 1.0.14
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 +11 -16
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,17 +5,16 @@ 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
|
-
|
|
15
|
-
|
|
16
|
-
} = options;
|
|
13
|
+
const model = options.model || "openai/gpt-oss-120b";
|
|
14
|
+
const system = options.system || "You are a helpful assistant.";
|
|
15
|
+
const token = process.env.HUGGINGFACE;
|
|
17
16
|
|
|
18
|
-
if (!token) throw new Error("HuggingFace token
|
|
17
|
+
if (!token) throw new Error("HuggingFace token required in .env as HUGGINGFACE");
|
|
19
18
|
if (!message) throw new Error("Message is required");
|
|
20
19
|
|
|
21
20
|
try {
|
|
@@ -24,24 +23,20 @@ async function chat(message, options = {}) {
|
|
|
24
23
|
{
|
|
25
24
|
model,
|
|
26
25
|
messages: [
|
|
27
|
-
{
|
|
28
|
-
role: "system",
|
|
29
|
-
content: `You are a helpful assistant.`
|
|
30
|
-
},
|
|
26
|
+
{ role: "system", content: system },
|
|
31
27
|
{ role: "user", content: message }
|
|
32
|
-
]
|
|
28
|
+
],
|
|
33
29
|
},
|
|
34
30
|
{
|
|
35
31
|
headers: {
|
|
36
32
|
Authorization: `Bearer ${token}`,
|
|
37
33
|
"Content-Type": "application/json"
|
|
38
|
-
}
|
|
34
|
+
},
|
|
39
35
|
}
|
|
40
36
|
);
|
|
41
37
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
38
|
+
// Extract reply text from router response
|
|
39
|
+
const aiText = response.data?.choices?.[0]?.message?.content || "No response";
|
|
45
40
|
return aiText;
|
|
46
41
|
|
|
47
42
|
} catch (err) {
|