ai-openai 1.0.14 → 1.0.16
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/README.md +24 -22
- package/index.js +27 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,49 +13,51 @@ Simplifies calling AI models for chat responses, learning advice, and more.
|
|
|
13
13
|
- Easy integration with websites, Discord bots, or other apps
|
|
14
14
|
|
|
15
15
|
---
|
|
16
|
+
|
|
16
17
|
## Setup
|
|
17
18
|
|
|
18
|
-
1. Get your free HuggingFace token at https://huggingface.co/settings/tokens
|
|
19
|
-
2. Create a
|
|
19
|
+
1. Get your free HuggingFace token at https://huggingface.co/settings/tokens
|
|
20
|
+
2. Create a `.env` file in your project root and add your HuggingFace token:
|
|
20
21
|
HUGGINGFACE=your_token_here
|
|
21
22
|
3. Then run your project.
|
|
22
23
|
|
|
24
|
+
---
|
|
25
|
+
|
|
23
26
|
## Usage
|
|
27
|
+
|
|
24
28
|
```js
|
|
25
29
|
const { chat } = require("ai-openai"); // import from npm package
|
|
26
30
|
require("dotenv").config();
|
|
27
31
|
|
|
28
32
|
(async () => {
|
|
29
33
|
try {
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
// Default usage
|
|
35
|
+
const reply = await chat("What is x if y = 2x + 3 and y = 7?");
|
|
36
|
+
console.log("Default model reply:", reply);
|
|
37
|
+
|
|
38
|
+
// Using a custom model
|
|
39
|
+
const reply2 = await chat("Explain black holes briefly.", {
|
|
40
|
+
model: "mistralai/Mistral-7B-Instruct-v0.2"
|
|
41
|
+
});
|
|
42
|
+
console.log("Custom model reply:", reply2);
|
|
43
|
+
|
|
44
|
+
// Using a custom system prompt
|
|
45
|
+
const reply3 = await chat("Explain black holes briefly.", {
|
|
46
|
+
system: "You are a physics professor explaining to a high school student."
|
|
47
|
+
});
|
|
48
|
+
console.log("Custom system prompt reply:", reply3);
|
|
49
|
+
|
|
32
50
|
} catch (err) {
|
|
33
51
|
console.error(err);
|
|
34
52
|
}
|
|
35
53
|
})();
|
|
36
54
|
```
|
|
37
|
-
|
|
38
|
-
The default model used is openai/gpt-oss-120b
|
|
55
|
+
- **Note:** The default model is openai/gpt-oss-120b.
|
|
39
56
|
|
|
40
57
|
---
|
|
41
58
|
|
|
42
|
-
## Using a Custom Model (optional)
|
|
43
|
-
```js
|
|
44
|
-
const reply = await chat("Explain black holes", {
|
|
45
|
-
model: "mistralai/Mistral-7B-Instruct-v0.2"
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## Using a custom token (optional)
|
|
51
|
-
If you don't want to use a .env file:
|
|
52
|
-
```js
|
|
53
|
-
const reply = await chat("Hello!", {
|
|
54
|
-
token: "hf_your_token_here"
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
```
|
|
58
59
|
## Installation
|
|
59
60
|
|
|
60
61
|
```bash
|
|
61
62
|
npm install ai-openai
|
|
63
|
+
|
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) {
|