ask-my-llm 1.0.6 → 1.0.7
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 +12 -1
- package/index.js +56 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,10 +24,14 @@ Config saved to `~/.askairc`.
|
|
|
24
24
|
## Usage
|
|
25
25
|
|
|
26
26
|
```js
|
|
27
|
-
const { ask } = require("ask-my-llm");
|
|
27
|
+
const { ask, askAsync } = require("ask-my-llm");
|
|
28
28
|
|
|
29
|
+
// sync
|
|
29
30
|
const response = ask("Hello");
|
|
30
31
|
console.log(response);
|
|
32
|
+
|
|
33
|
+
// async
|
|
34
|
+
askAsync("Hello").then(console.log);
|
|
31
35
|
```
|
|
32
36
|
|
|
33
37
|
## API
|
|
@@ -38,3 +42,10 @@ Sends a prompt to the configured AI and returns the response synchronously.
|
|
|
38
42
|
|
|
39
43
|
- `prompt` - The message to send
|
|
40
44
|
- Returns: AI response as string
|
|
45
|
+
|
|
46
|
+
### askAsync(prompt: string): Promise<string>
|
|
47
|
+
|
|
48
|
+
Sends a prompt to the configured AI and returns a Promise.
|
|
49
|
+
|
|
50
|
+
- `prompt` - The message to send
|
|
51
|
+
- Returns: Promise that resolves to AI response as string
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { spawnSync } = require('child_process');
|
|
1
|
+
const { spawnSync, spawn } = require('child_process');
|
|
2
2
|
const fs = require('fs');
|
|
3
3
|
const path = require('path');
|
|
4
4
|
|
|
@@ -48,12 +48,64 @@ function ask(prompt) {
|
|
|
48
48
|
if (result.error) throw result.error;
|
|
49
49
|
if (result.status !== 0) throw new Error('curl failed: ' + (result.stderr || result.stdout));
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
return parseResponse(result.stdout);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function askAsync(prompt) {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const config = loadConfig();
|
|
57
|
+
|
|
58
|
+
const postData = JSON.stringify({
|
|
59
|
+
model: config.model,
|
|
60
|
+
messages: [{ role: 'user', content: prompt }],
|
|
61
|
+
temperature: 0.7
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
let baseUrl = config.baseApi.replace(/\/$/, '');
|
|
65
|
+
if (!baseUrl.endsWith('/v1')) {
|
|
66
|
+
baseUrl += '/v1';
|
|
67
|
+
}
|
|
68
|
+
const url = baseUrl + '/chat/completions';
|
|
69
|
+
|
|
70
|
+
const headerArgs = [
|
|
71
|
+
'-s', '-S', '-X', 'POST',
|
|
72
|
+
'-H', 'Content-Type: application/json',
|
|
73
|
+
'-H', 'HTTP-Referer: https://cows.info.gf',
|
|
74
|
+
'-H', 'X-OpenRouter-Title: ask-my-llm'
|
|
75
|
+
];
|
|
76
|
+
if (config.apiKey) {
|
|
77
|
+
headerArgs.push('-H', `Authorization: Bearer ${config.apiKey}`);
|
|
78
|
+
}
|
|
79
|
+
headerArgs.push('-d', postData, url);
|
|
80
|
+
|
|
81
|
+
const child = spawn('curl', headerArgs, { encoding: 'utf8' });
|
|
82
|
+
let data = '';
|
|
83
|
+
let error = '';
|
|
84
|
+
|
|
85
|
+
child.stdout.on('data', chunk => data += chunk);
|
|
86
|
+
child.stderr.on('data', chunk => error += chunk);
|
|
87
|
+
child.on('close', code => {
|
|
88
|
+
if (code !== 0) {
|
|
89
|
+
reject(new Error('curl failed: ' + (error || data)));
|
|
90
|
+
} else {
|
|
91
|
+
try {
|
|
92
|
+
resolve(parseResponse(data));
|
|
93
|
+
} catch (e) {
|
|
94
|
+
reject(e);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
child.on('error', reject);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function parseResponse(stdout) {
|
|
103
|
+
const parsed = JSON.parse(stdout);
|
|
52
104
|
if (parsed.choices && parsed.choices[0]) {
|
|
53
105
|
return parsed.choices[0].message.content;
|
|
54
106
|
} else {
|
|
55
|
-
throw new Error('API Error: ' +
|
|
107
|
+
throw new Error('API Error: ' + stdout);
|
|
56
108
|
}
|
|
57
109
|
}
|
|
58
110
|
|
|
59
|
-
module.exports = { ask };
|
|
111
|
+
module.exports = { ask, askAsync };
|