├── .gitignore ├── library ├── index.js └── assistant.js ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | .env -------------------------------------------------------------------------------- /library/index.js: -------------------------------------------------------------------------------- 1 | import ChatAssistant from "./assistant.js"; 2 | 3 | export {ChatAssistant}; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ai-customer-assistant", 3 | "version": "1.1.1", 4 | "description": "A simple chat assistant package.", 5 | "main": "library/index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "keywords": [], 10 | "license": "ISC", 11 | "dependencies": { 12 | "@google/generative-ai": "^0.16.0", 13 | "axios": "^1.7.2", 14 | "dotenv": "^16.4.5" 15 | }, 16 | "devDependencies": { 17 | "chai": "^5.1.1", 18 | "mocha": "^10.7.0", 19 | "sinon": "^18.0.0" 20 | }, 21 | "type": "module" 22 | } 23 | -------------------------------------------------------------------------------- /library/assistant.js: -------------------------------------------------------------------------------- 1 | import { GoogleGenerativeAI } from "@google/generative-ai"; 2 | import "dotenv/config"; 3 | 4 | const API = process.env.API_KEY; 5 | const genAI = new GoogleGenerativeAI(API); 6 | 7 | const model = genAI.getGenerativeModel({ model: "gemini-pro" }); 8 | 9 | async function generate(prompt, userQuery) { 10 | try { 11 | // Prepend the prompt to the user's query 12 | const fullQuery = `${prompt} ${userQuery}`; 13 | 14 | // Get the response from the AI model 15 | const result = await model.generateContent(fullQuery); 16 | const response = result.response; 17 | const text = response.text(); 18 | 19 | // Return the AI's response 20 | return text; 21 | } catch (error) { 22 | console.error("Error generating content:", error); 23 | throw error; 24 | } 25 | } 26 | 27 | // Exported module to handle chat messages 28 | export default class ChatAssistant { 29 | async sendMessage(message) { 30 | try { 31 | const prompt = `You are a helpful and knowledgeable customer support assistant. Respond to users' questions and issues with accurate, polite, and concise answers. Keep your responses to 1-2 lines.`; 32 | console.log(message); 33 | const responseText = await generate(prompt, message); 34 | return responseText; 35 | } catch (error) { 36 | console.error("Error sending message:", error); 37 | throw error; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AI Customer Assistant 2 | 3 | `ai-customer-assistant` is a customer support chat bot powered by Google Generative AI. This package allows you to easily integrate an AI-powered customer support assistant into your projects. 4 | 5 | ## Features 6 | 7 | - Responds to user queries with accurate, polite, and concise answers. 8 | - Uses Google Generative AI to generate responses. 9 | - Easy to integrate into any Node.js project. 10 | 11 | ## Installation 12 | 13 | Install the package using npm: 14 | 15 | ```bash 16 | npm install ai-customer-assistant 17 | ``` 18 | 19 | ## Setup 20 | 21 | Before using the package, you need to set up your Gemini API key: 22 | 23 | 1. Create a `.env` file in your project's root directory. 24 | 2. Add your Gemini API key to the `.env` file: 25 | 26 | ``` 27 | API_KEY=your_api_key_here 28 | ``` 29 | 30 | Make sure to replace `your_api_key_here` with your actual Gemini API key. 31 | 32 | ## Usage 33 | 34 | ### ES Module 35 | 36 | If you are using ES modules, ensure your package.json includes `"type": "module"`: 37 | 38 | ```json 39 | { 40 | "name": "your-project-name", 41 | "version": "1.0.0", 42 | "description": "", 43 | "main": "index.js", 44 | "type": "module", 45 | "scripts": { 46 | "test": "echo \"Error: no test specified\" && exit 1" 47 | }, 48 | "author": "", 49 | "license": "ISC", 50 | "dependencies": { 51 | "ai-customer-assistant": "^1.0.0" 52 | } 53 | } 54 | ``` 55 | 56 | Then, you can import and use the `ChatAssistant` class: 57 | 58 | ```javascript 59 | import { ChatAssistant } from 'ai-customer-assistant'; 60 | 61 | const chatAssistant = new ChatAssistant(); 62 | 63 | (async () => { 64 | try { 65 | const response = await chatAssistant.sendMessage("What are you doing?"); 66 | console.log("Response from AI Customer Assistant:", response); 67 | } catch (error) { 68 | console.error("Error:", error); 69 | } 70 | })(); 71 | ``` 72 | 73 | ### CommonJS 74 | 75 | If you are using CommonJS, you will need to dynamically import the package: 76 | 77 | ```javascript 78 | (async () => { 79 | const { ChatAssistant } = await import('ai-customer-assistant'); 80 | 81 | const chatAssistant = new ChatAssistant(); 82 | 83 | try { 84 | const response = await chatAssistant.sendMessage("What are you doing?"); 85 | console.log("Response from AI Customer Assistant:", response); 86 | } catch (error) { 87 | console.error("Error:", error); 88 | } 89 | })(); 90 | ``` 91 | 92 | ## API 93 | 94 | ### ChatAssistant 95 | 96 | #### `sendMessage(message: string): Promise` 97 | - `message` - The user's query. 98 | - Returns a Promise that resolves to the AI-generated response. 99 | 100 | ## Example 101 | 102 | ```javascript 103 | import { ChatAssistant } from 'ai-customer-assistant'; 104 | 105 | const chatAssistant = new ChatAssistant(); 106 | 107 | (async () => { 108 | try { 109 | const response = await chatAssistant.sendMessage("How can I reset my password?"); 110 | console.log("Response from AI Customer Assistant:", response); 111 | } catch (error) { 112 | console.error("Error:", error); 113 | } 114 | })(); 115 | ``` 116 | 117 | ## Contributing 118 | 119 | Contributions are welcome! Please open an issue or submit a pull request. 120 | 121 | ## License 122 | 123 | This project is licensed under the ISC License - see the LICENSE file for details. --------------------------------------------------------------------------------