Skip to content

Commit

Permalink
Add getAPIKey method that can get api key from env
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jun 7, 2024
1 parent 87285c7 commit 00ab3a9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,21 @@ const response = await client.models();
## API
- `chat()`
- `models()`
- `putFile()`
- `putFileStream()`
- `files()`
- `getFile()`
- `getFileContent()`
- `deleteFile()`
- `estimateTokenCount()`
- `getBalance()`
- Chat
- `chat()`
- `models()`
- `estimateTokenCount()`
- Files
- `putFile()`
- `putFileStream()`
- `files()`
- `getFile()`
- `getFileContent()`
- `deleteFile()`
- Others
- `getBalance()`
The detail of parameters can be found at <https://platform.moonshot.cn/docs/api-reference> or [`test/kimi.test.js`](./test/kimi.test.js).
Expand Down
11 changes: 6 additions & 5 deletions bin/kimi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import Kimi from '../lib/kimi.js';
import { loadConfig, saveConfig } from '../lib/config.js';

const KIMI_RC_PATH = path.join(homedir(), '.moonshot_ai_rc');
const rcPath = KIMI_RC_PATH;

const config = await loadConfig(KIMI_RC_PATH);
const config = await loadConfig(rcPath);

async function question(prompt) {
const answers = await inquirer.prompt([
Expand Down Expand Up @@ -57,7 +58,7 @@ async function chooseAPIKey() {
mask: '*'
});
config.api_key = apikey.trim();
await saveConfig(config, KIMI_RC_PATH);
await saveConfig(config, rcPath);
}

if (!config.api_key) {
Expand Down Expand Up @@ -96,7 +97,7 @@ async function chooseModel() {
if (model) {
config.model = model;
}
await saveConfig(config, KIMI_RC_PATH);
await saveConfig(config, rcPath);
}

if (!config.model) {
Expand Down Expand Up @@ -152,7 +153,7 @@ while (true) {
});

config.api_key = apikey;
await saveConfig(config, KIMI_RC_PATH);
await saveConfig(config, rcPath);
console.log('The new API key is set.');
continue;
}
Expand All @@ -169,7 +170,7 @@ while (true) {
});

config.verbose = verbose === 'true';
await saveConfig(config, KIMI_RC_PATH);
await saveConfig(config, rcPath);
console.log(`The verbose mode is turned ${config.verbose ? 'on' : 'off'} now.`);
continue;
}
Expand Down
18 changes: 18 additions & 0 deletions lib/apikey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { homedir } from 'os';
import { join } from 'path';
import { loadConfig } from './config.js';

const KIMI_RC_PATH = join(homedir(), '.moonshot_ai_rc');

export async function getAPIKey(rcPath = KIMI_RC_PATH) {
if (process.env.MOONSHOT_API_KEY) {
return process.env.MOONSHOT_API_KEY;
}

const config = await loadConfig(rcPath);
if (config && config.api_key) {
return config.api_key;
}

return '';
}

0 comments on commit 00ab3a9

Please sign in to comment.