ask-my-llm 1.0.0
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 +40 -0
- package/bin/ask-ai +35 -0
- package/index.js +48 -0
- package/package.json +17 -0
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# ask-my-llm
|
|
2
|
+
|
|
3
|
+
Oversimplified AI usage npm module.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install ask-my-llm
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Configure
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npx ask-my-llm
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Prompts for:
|
|
18
|
+
- **Base API** - OpenAI compatible endpoint (e.g., `https://api.openai.com/v1`)
|
|
19
|
+
- **API Key** - Your API key (can be empty)
|
|
20
|
+
- **Model** - Model name (e.g., `gpt-3.5-turbo`)
|
|
21
|
+
|
|
22
|
+
Config saved to `~/.askairc`.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
const { ask } = require("ask-my-llm");
|
|
28
|
+
|
|
29
|
+
const response = ask("Hello");
|
|
30
|
+
console.log(response);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## API
|
|
34
|
+
|
|
35
|
+
### ask(prompt: string): string
|
|
36
|
+
|
|
37
|
+
Sends a prompt to the configured AI and returns the response synchronously.
|
|
38
|
+
|
|
39
|
+
- `prompt` - The message to send
|
|
40
|
+
- Returns: AI response as string
|
package/bin/ask-ai
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const inquirer = require('inquirer');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
const CONFIG_PATH = path.join(process.env.HOME || process.env.USERPROFILE, '.askairc');
|
|
8
|
+
|
|
9
|
+
async function setup() {
|
|
10
|
+
const answers = await inquirer.prompt([
|
|
11
|
+
{
|
|
12
|
+
type: 'input',
|
|
13
|
+
name: 'baseApi',
|
|
14
|
+
message: 'Base API URL (e.g., https://api.openai.com/v1):',
|
|
15
|
+
default: 'https://api.openai.com/v1'
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
type: 'input',
|
|
19
|
+
name: 'apiKey',
|
|
20
|
+
message: 'API Key (leave empty for none):',
|
|
21
|
+
default: ''
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
type: 'input',
|
|
25
|
+
name: 'model',
|
|
26
|
+
message: 'Model (e.g., gpt-3.5-turbo):',
|
|
27
|
+
default: 'gpt-3.5-turbo'
|
|
28
|
+
}
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
fs.writeFileSync(CONFIG_PATH, JSON.stringify(answers, null, 2));
|
|
32
|
+
console.log('Configuration saved to ~/.askairc');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
setup();
|
package/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const { spawnSync } = require('child_process');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
const CONFIG_PATH = path.join(process.env.HOME || process.env.USERPROFILE, '.askairc');
|
|
6
|
+
|
|
7
|
+
function loadConfig() {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
|
|
10
|
+
} catch (e) {
|
|
11
|
+
throw new Error('Not configured. Run `npx ask-ai` first.');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function ask(prompt) {
|
|
16
|
+
const config = loadConfig();
|
|
17
|
+
|
|
18
|
+
const postData = JSON.stringify({
|
|
19
|
+
model: config.model,
|
|
20
|
+
messages: [{ role: 'user', content: prompt }],
|
|
21
|
+
temperature: 0.7
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const url = new URL('/v1/chat/completions', config.baseApi);
|
|
25
|
+
const isHttps = url.protocol === 'https:';
|
|
26
|
+
|
|
27
|
+
const args = [
|
|
28
|
+
'-s', '-X', 'POST',
|
|
29
|
+
'-H', 'Content-Type: application/json',
|
|
30
|
+
'-H', config.apiKey ? `Authorization: Bearer ${config.apiKey}` : '',
|
|
31
|
+
'-d', postData,
|
|
32
|
+
`${url.protocol}//${url.host}${url.pathname}`
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
const result = spawnSync('curl', args, { encoding: 'utf8' });
|
|
36
|
+
|
|
37
|
+
if (result.error) throw result.error;
|
|
38
|
+
if (result.status !== 0) throw new Error('curl failed: ' + result.stderr);
|
|
39
|
+
|
|
40
|
+
const parsed = JSON.parse(result.stdout);
|
|
41
|
+
if (parsed.choices && parsed.choices[0]) {
|
|
42
|
+
return parsed.choices[0].message.content;
|
|
43
|
+
} else {
|
|
44
|
+
throw new Error('Unexpected response: ' + result.stdout);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = { ask };
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ask-my-llm",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Oversimplified AI usage npm module",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"ask-ai": "./bin/ask-ai"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"keywords": ["ai", "openai", "chatgpt", "llm"],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"inquirer": "^8.2.5"
|
|
16
|
+
}
|
|
17
|
+
}
|