{"_id":"@ag076810/ai.js","name":"@ag076810/ai.js","dist-tags":{"latest":"0.1.5"},"versions":{"0.1.5":{"name":"@ag076810/ai.js","version":"0.1.5","description":"Effortlessly integrate AI text and image models in your node applications","main":"src/index.js","scripts":{"start":"node src/index.js","test":"DEBUG=ai.js:* mocha","test:watch":"DEBUG=ai.js:* mocha --watch","examples":"bash examples/run_all.sh"},"repository":{"type":"git","url":"git+https://github.com/ag076810/ai.js.git"},"bin":{"ai":"src/cli.js"},"keywords":["ai","llm","openai","anthropic","gpt-3.5-turbo","gpt-4","claude-v1","stablediffusion","riffusion"],"author":{"name":"The Maximalist"},"license":"MIT","bugs":{"url":"https://github.com/ag076810/ai.js/issues"},"homepage":"https://github.com/ag076810/ai.js#readme","dependencies":{"@anthropic-ai/sdk":"^0.4.3","commander":"^10.0.1","debug":"^4.3.4","dotenv":"^16.0.3","node-fetch":"^2.6.9","openai":"^3.2.1","prompt-sync":"^4.2.0","prompt-sync-history":"^1.0.1","replicate":"^0.11.1"},"devDependencies":{"mocha":"^10.2.0"},"directories":{"example":"examples","test":"test"},"gitHead":"5f84c6555d39171d97bdad8dd5306269962ce415","_id":"@ag076810/ai.js@0.1.5","_nodeVersion":"17.3.0","_npmVersion":"8.3.0","dist":{"integrity":"sha512-6uWLO3PwmCMuSrGUuc0S7+CMx0z0rGlvibHrlynsm6KUjzwCVREao5D3e7H4vGUqQmQkoP2FEPHd2KdP+E3Y4A==","shasum":"a11b93b29b8e69b74262a77f9f2103e6e63b18a8","tarball":"https://registry.npmjs.org/@ag076810/ai.js/-/ai.js-0.1.5.tgz","fileCount":42,"unpackedSize":51865,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQChbqJ5ogG60dOn1Hm2TH3FNv6MIMahVYN+HoeCXKpPiQIgCuHiXcbLTmWLy9nqwyF+Sqkbv9RQQu/PyWDMlkYtm8Y="}]},"_npmUser":{"name":"ag076810","email":"ag0768101@gmail.com"},"maintainers":[{"name":"ag076810","email":"ag0768101@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ai.js_0.1.5_1691404814012_0.6179544113317641"},"_hasShrinkwrap":false}},"time":{"created":"2023-08-07T10:40:13.948Z","0.1.5":"2023-08-07T10:40:14.159Z","modified":"2023-08-07T10:40:14.340Z"},"maintainers":[{"name":"ag076810","email":"ag0768101@gmail.com"}],"description":"Effortlessly integrate AI text and image models in your node applications","homepage":"https://github.com/ag076810/ai.js#readme","keywords":["ai","llm","openai","anthropic","gpt-3.5-turbo","gpt-4","claude-v1","stablediffusion","riffusion"],"repository":{"type":"git","url":"git+https://github.com/ag076810/ai.js.git"},"author":{"name":"The Maximalist"},"bugs":{"url":"https://github.com/ag076810/ai.js/issues"},"license":"MIT","readme":"# AI.js\n\n`AI.js` is the easiest way to add AI text and image capabilities to your node applications:\n\n```javascript\nawait AI(\"the color of the sky is\"); // blue\n\nawait AI.Image(\"a red rose\"); // <image buffer: red rose>\n\nawait AI.Image.Concept(\"a red rose\"); // <image buffer: a red rose in realist style, watercolor>\n```\n\nUnder the hood `AI.js` seamlessly integrates all the best AI APIs:\n\n* **Text**: OpenAI, Anthropic\n* **Image**: Replicate, StabilityAI\n* **Music:** *Coming Soon*\n* **Video:** *Coming Soon*\n* **Embeddings:** *Coming Soon*\n\nWe're constantly looking for new models and APIs to add. In a future update, `AI.js` will support local models like `Stable Diffusion` and `LLaMA/Alpaca` with the same easy-to-use interface.\n\n*AI.js is under heavy development and still subject to breaking changes.*\n\n\n\n## Features\n\n* Easy to use\n* Best LLM chat completion models (`gpt-3.5-turbo`, `gpt-4`, `claude-v1`, `claude-instant-v1`)\n* Best hosted image generation APIs (`stable-diffusion-xl-beta-v2-2-2`, many other Stable Diffusion models)\n* Same interface across all services\n* Streaming is easy as `{stream: true}`\n* `ai` CLI interface in your shell\n* MIT license\n\n\n\n## Installation\n\nInstall `AI.js` via npm\n\n```bash\nnpm install @themaximalist/ai.js\n```\n\nEnable at least one service by setting its environment `API_KEY`\n\n```bash\nexport OPENAI_API_KEY=sk-...\nexport ANTHROPIC_API_KEY=sk-ant-...\nexport STABILITY_API_KEY=sk-...\nexport REPLICATE_API_KEY=sk-....\n```\n\n\n\n## Usage\n\nThe default interface is text. `AI.js` lets you send one-off requests or build up complex message histories with the Large Language Model (LLM).\n\n```javascript\nconst AI = require(\"@themaximalist/ai.js\");\nawait AI(\"what is the codeword?\"); // i don't know any codewords\n\nconst ai = new AI(\"the codeword is blue\");\nawait ai.chat(\"what is the codeword?\"); // blue\n```\n\n\n\n### System and User Prompts\n\nGiving the `LLM` a role can help improve performance, this can be done through `system` and `user` prompts.\n\n```javascript\nawait AI.system(\"I am HexBot—I generate beautiful hex color schemes\", \"autumn tree\");\n// #F2AF80 (soft peach), #E78C4D (burnt orange), #C86018 (rusty red), #5B3B0B (deep brown), #252527 (dark grey)\n```\n\nOpenAI has mentioned `user` prompts may be more strongly enforced than `system` prompts.\n\n```javascript\nawait AI.user(\"You are HexBot—you generate beautiful hex color schemes\", \"autumn tree\");\n// #F2AF80 (soft peach), #E78C4D (burnt orange), #C86018 (rusty red), #5B3B0B (deep brown), #252527 (dark grey)\n```\n\nProviding `system` and `user` roles can also be used with message history.\n\n```javascript\nconst ai = new AI();\nai.system(\"I am HexBot—I generate beautiful hex color schemes\");\nawait ai.chat(\"autumn tree\");\n// #663300 (dark brown), #CC6600 (deep orange), #FF9900 (bright orange), #FFCC00 (golden yellow), #663399 (deep purple)\nawait ai.chat(\"make it lighter\");\n// #885533 (light brown), #FF9933 (pale orange), #FFCC66 ( light orange), #FFEE99 (pale yellow), #9966CC (light purple)\n```\n\n\n\n## Message History\n\nChat history can build up over time, or you can initialize with an existing history.\n\n```javascript\nawait AI([\n    { role: \"user\", content: \"remember the secret codeword is blue\" },\n    { role: \"user\", content: \"what is the secret codeword I just told you?\" },\n]); // blue\n```\nOr initialize with an existing history, and continue the conversation.\n\n```javascript\nconst ai = new AI([\n    { role: \"user\", content: \"remember the secret codeword is blue\" },\n    { role: \"user\", content: \"what is the secret codeword I just told you?\" },\n]);\nawait ai.send(); // blue\nawait ai.chat(\"now the codeword is red\");\nawait ai.chat(\"what is the codeword?\"); // red\n```\n\nNote: Anthropic's message format is text-based and less convenient, so `AI.js` uses the OpenAI message format, and it's converted automatically on-the-fly for Anthropic.\n\n\n\n### Streaming\n\nStreaming is as easy as passing `{stream: true}` as the second options parameter. A generator is returned that yields the completion tokens in real-time.\n\n```javascript\nconst stream = await AI(\"the color of the sky is\", { stream: true });\nfor await (const message of stream) {\n    process.stdout.write(message);\n}\n```\n\n\n\n### AI() Interface\n\nThe `AI()` interface is the same whether you're using `new` or `await`. Also `options` aim to be the same everywhere you can pass them, and use the most local scope possible, falling back to more global defaults and environment variables when needed.\n\n```javascript\nawait|new AI(\n    string | array,\n    options = {\n        service: \"openai\", // openai, anthropic\n        model: \"gpt-3.5-turbo\", // gpt-3.5-turbo, gpt-4, claude-v1, claude-instant-v1\n        parser: null, // optional content parser or stream parser\n        stream: false,\n        temperature: null, // optional, can be 0-2 for openai, 0-1 for anthropic\n        max_tokens: null, // optional for openai, defaults to 2,000 for anthropic because its required\n        partial: false, // specify whether last response is a partial response \"putting words in the AI's mouth\"\n        context: AI.CONTEXT_FULL, // slices of message history can be sent, but by default send everything\n    }\n);\n```\n\n\n\n* **Static Methods**\n  * `AI.system(prompt, input, option=null)` one-time use system prompt\n  * `AI.user(prompt, input, options=null`) one-time use user prompt\n\n* **Instance Methods**\n  * `ai.send(options=null)` send chat completion request to network\n  * `ai.chat(content, options=null)` add user message and send chat completion request to network\n  * `ai.user(content)` add user message\n  * `ai.system(content)` add system message\n  * `ai.assistant(content)` add assistant message\n* **Instance Properties**\n  * `ai.messages[]` message history\n  * `ai.lastMessage` last message\n\n\n\n## Image Generation\n\n`AI.js` provides powerful image generation functions through `StabilityAI` and `Replicate`. To get started, make sure you've set the `STABILITY_API_KEY` or `REPLICATE_API_KEY` environment variable.\n\n```javascript\nawait AI.Image(\"a red rose\"); // <image buffer: red rose>\n```\n\n`AI.js` also provides a concept generator—a way of using chat completion to generate a great image prompt. Using a concept generator can result in significantly better generated images.\n\n```javascript\nawait AI.Image.Concept(\"a red rose\"); // <image buffer: a red rose in realist style, watercolor>\n```\n\nThis hits your `LLM` provider and generates a complex image prompt before sending it off to the image generation service. \n\nYou can also create an `AI.Image` object to retrieve properties like `generated_prompt` from `AI.Image.Concept`.\n\n```javascript\nconst image = new AI.Image(\"a red rose\");\nawait image.concept(); // <image buffer: a red rose in realist style, watercolor>\nconsole.log(image.generated_prompt); // a red rose in the realist style, watercolor\n```\n\n\n\n### AI.Image() Interface\n\nThe `AI.Image()` interface takes an input prompt and a few options.\n\n```javascript\nawait|new Image(\n    string,\n    options = {\n        service: \"stability\", // stability, replicate\n        model: \"stable-diffusion-xl-beta-v2-2-2\",\n        concept_service: \"openai\", // openai, anthropic\n        concept_model: \"gpt-3.5-turbo\", // gpt-3.5-turbo, gpt-4, claude-v1, claude-instant-v1\n        concept_prompt: \"I am Concept2Prompt. My task is to generate rich and dynamic scenes ...\",\n    }\n);\n```\n\n* **Static Methods**\n  * `Image.Concept(string, options=null)` generate a concept and send image completion request to network\n* **Instance Methods**\n  * `image.send(options=null)` send image completion request to network\n  * `image.concept(prompt, options=null)` generate concept prompt and then send image completion request to network\n* **Instance Properties**\n  * `image.prompt` prompt supplied by user\n  * `image.generated_prompt` prompt created from `concept()`\n  * `service` image service\n  * `model` image model\n  * `concept_model` LLM concept model\n  * `concept_service` LLM concept service\n  * `concept_prompt` LLM concept prompt (uses default but can be overridden)\n\n\n\n## Environment Variables\n\n`AI.js` supports configuration through environment variables.\n\n##### Configure API keys\n\n```bash\nexport OPENAI_API_KEY=sk-...\nexport ANTHROPIC_API_KEY=sk-ant-...\nexport STABILITY_API_KEY=sk-...\nexport REPLICATE_API_KEY=sk-....\n```\n\n##### Configure Service\n\n```bash\nexport AI_SERVICE=openai\nexport AI_SERVICE=anthropic\n\nexport AI_IMAGE_SERVICE=stability\nexport AI_IMAGE_SERVICE=replicate\n```\n\n##### Configure Model\n\n```bash\nexport AI_MODEL=gpt-3.5-turbo\nexport AI_MODEL=gpt-4\nexport AI_MODEL=claude-v1\nexport AI_MODEL=claude-instant-v1\n\nexport AI_IMAGE_MODEL=stable-diffusion-xl-beta-v2-2-2\n```\n\n`AI.js` will make some effort to make sure the right models are used with the right service if no defaults are set. So you can switch to `anthropic` and `AI.js` will automatically use `claude-v1`.\n\n\n\n## `AI` Command in your Shell\n\n`AI.js` provides a handy `ai` command that can be invoked from your shell. This is an extremely convenient way to call models and services with the full power of `AI.js`. Access it globally by installing `npm install @themaximalist/ai.js -g` or setting up an `nvm` environment.\n\n```bash\n> ai the color of the sky is\nblue\n```\n\nMessages are streamed back in real time.\n\nYou can also initiate a `--chat` to remember message history and continue your conversation. `Ctrl-C` to quit.\n\n```bash\n> ai remember the codeword is blue. say ok if you understand --chat\nOK, I understand.\n> what is the codeword?\nThe codeword is blue.\n```\n\nModel and service can be specified on the fly\n\n```bash\n> ai the color of the sky is --service anthropic --model claude-v1\nblue\n```\n\nOr `ai` will fallback to `$AI_SERVICE` and `$AI_MODEL` environment variables.\n\n```bash\n> export AI_SERVICE=anthropic\n> export AI_MODEL=claude-v1\n> ai the color of the sky is\nblue # claude-v1 response\n```\n\nImage and concept generation also work\n\n```bash\n> ai a red rose --image # opens generated image of a red rose\n> ai a red rose --concept # opens a generated image of a close up of a red rose, its velvety petals shining under the golden hour light. The dew on its petals captures the tranquil beauty of nature in a refreshing way. The rose stands out against the blurred background with its deep crimson red color. Perfect for a romantic gesture or a symbolic display of love. Shot in Ultra HD (4K) with a shallow depth of field\n```\n\n\n\nHere's the help interface\n\n```bash\n> ai\nUsage: ai [options] [input]\n\nAI cli interface to generate chat completions and images\n\nArguments:\n  input                    Input to send to AI service\n\nOptions:\n  -V, --version            output the version number\n  -s, --service <service>  AI Service (default: openai)\n  -m, --model <model>      Completion Model (default: gpt-3.5-turbo)\n  -c, --chat               Chat Mode\n  -i, --image              Image Mode\n  --concept                Concept Image Mode\n  -h, --help               display help for command\n```\n\n\n\n## Debug\n\n`AI.js` and `ai` use the `debug` npm module with the `ai.js` namespace, so you can view debug logs by setting the `DEBUG` environment variable.\n\n```bash\n> DEBUG=ai.js:* ai the color of the sky is\n# debug logs\nblue\n> export DEBUG=ai.js:*\n> ai the color of the sky is\n# debug logs\nblue\n```\n\n\n\n## Examples\n\nView [examples](https://github.com/themaximal1st/ai.js/tree/main/examples) on how to use `AI.js`.\n\n\n\n## Projects\n\n`AI.js` is currently used in the following projects:\n\n-   [Infinity Arcade](https://infinityarcade.com)\n\n\n\n## Author\n\n-   [The Maximalist](https://themaximalist.com/)\n-   [@themaximal1st](https://twitter.com/themaximal1st)\n\n\n\n## License\n\nMIT\n","readmeFilename":"README.md"}