From f05fb96dc9550beb4ccd0f841dc91bc1efc52372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Musia=C5=82?= Date: Thu, 14 Mar 2024 09:46:20 +0100 Subject: [PATCH 1/2] docs: license file added --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5e5f969 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Boldare + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 941a84714599ced7aade7c8eca51426c80fcd40f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Musia=C5=82?= Date: Thu, 14 Mar 2024 13:40:24 +0100 Subject: [PATCH 2/2] docs: added missing documentation --- .github/CODE_OF_CONDUCT.md | 133 +++++++++++++++++ .github/PULL_REQUEST_TEMPLATE.md | 34 +++++ README.md | 4 +- libs/ai-assistant/README.md | 246 ------------------------------- libs/ai-assistant/package.json | 26 +++- package.json | 3 +- 6 files changed, 196 insertions(+), 250 deletions(-) create mode 100644 .github/CODE_OF_CONDUCT.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 libs/ai-assistant/README.md diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..20bcc4f --- /dev/null +++ b/.github/CODE_OF_CONDUCT.md @@ -0,0 +1,133 @@ + +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, caste, color, religion, or sexual +identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall + community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or advances of + any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email address, + without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official email address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +[INSERT CONTACT METHOD]. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of +actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or permanent +ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the +community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.1, available at +[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. + +Community Impact Guidelines were inspired by +[Mozilla's code of conduct enforcement ladder][Mozilla CoC]. + +For answers to common questions about this code of conduct, see the FAQ at +[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at +[https://www.contributor-covenant.org/translations][translations]. + +[homepage]: https://www.contributor-covenant.org +[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html +[Mozilla CoC]: https://github.com/mozilla/diversity +[FAQ]: https://www.contributor-covenant.org/faq +[translations]: https://www.contributor-covenant.org/translations diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..dbe38b3 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,34 @@ +## PR Type +What kind of change does this PR introduce? + + +``` +[ ] Bugfix +[ ] Feature +[ ] Code style update (formatting, local variables) +[ ] Refactoring (no functional changes, no api changes) +[ ] Build related changes +[ ] CI related changes +[ ] Documentation content changes +[ ] Other... Please describe: +``` + +## What is the current behavior? + + +Issue Number: N/A + + +## What is the new behavior? + + +## Does this PR introduce a breaking change? +``` +[ ] Yes +[ ] No +``` + + + + +## Other information diff --git a/README.md b/README.md index 57e92aa..5685d7f 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,11 @@

demo 🔹 api docs 🔹 - npm + npm 🔹 + github

+ # 🤖 AI Assistant Introducing the NestJS library, designed to harness the power of OpenAI's Assistant, enabling developers to create highly efficient, scalable, and rapid AI assistants and chatbots. This library is tailored for seamless integration into the NestJS ecosystem, offering an intuitive API, WebSockets, and tools that streamline the development of AI-driven interactions. Whether you're building a customer service bot, a virtual assistant, or an interactive chatbot for engaging user experiences, our library empowers you to leverage cutting-edge AI capabilities with minimal effort. diff --git a/libs/ai-assistant/README.md b/libs/ai-assistant/README.md deleted file mode 100644 index 57e92aa..0000000 --- a/libs/ai-assistant/README.md +++ /dev/null @@ -1,246 +0,0 @@ -

- - Boldare - -

- -

- demo 🔹 - api docs 🔹 - npm -

- -# 🤖 AI Assistant - -Introducing the NestJS library, designed to harness the power of OpenAI's Assistant, enabling developers to create highly efficient, scalable, and rapid AI assistants and chatbots. This library is tailored for seamless integration into the NestJS ecosystem, offering an intuitive API, WebSockets, and tools that streamline the development of AI-driven interactions. Whether you're building a customer service bot, a virtual assistant, or an interactive chatbot for engaging user experiences, our library empowers you to leverage cutting-edge AI capabilities with minimal effort. - -## 🚀 Features - -#### AI Assistant library features - -- **WebSockets**: The library provides a WebSocket server for real-time communication between the client and the assistant. -- **REST API**: The library provides a REST API for communication with the assistant. -- **Function calling**: The library provides a way to create functions, which allows you to extend the assistant's capabilities with custom logic. -- **File support**: The library provides a way to add files to the assistant, which allows you to extend the assistant's knowledge base with custom data. -- **TTS (Text-to-Speech)**: The library provides a way to convert text to speech, which allows you to create voice-based interactions with the assistant. -- **STT (Speech-to-Text)**: The library provides a way to convert speech to text, which allows you to create voice-based interactions with the assistant. - -#### Additional features in the repository - -- **Embedded chatbot**: The library provides a way to embed the chatbot on various websites through JavaScript scripts. -- **Chatbot client application**: The repository includes an example client application (SPA) with a chatbot. - -## Getting started - -In this section, you will learn how to integrate the AI Assistant library into your NestJS application. The following steps will guide you through the process of setting up the library and creating simple functionalities. - -### Step 0: Prerequiring - -Before you start, you will need to have an account on the OpenAI platform and an API key. You can create an account [here](https://platform.openai.com/). - -Open or create your NestJS application where you would like to integrate the AI Assistant. If you don't have a NestJS application yet, you can create one using the following command: - -```bash -nest new project-name -``` - -### Step 1: Installation - -Install the library using npm: - -```bash -npm i @boldare/ai-assistant --save -``` - -### Step 2: Env variables - -Set up your environment variables, create environment variables in the `.env` file in the root directory of the project, and populate it with the necessary secrets. You will need to add the OpenAI API Key and the Assistant ID. The Assistant ID is optional, and you can leave it empty if you don't have an assistant yet. - -Create a `.env` file in the root directory of your project and populate it with the necessary secrets: - -```bash -touch .env -``` - -Add the following content to the `.env` file: - -```dotenv -# OpenAI API Key -OPENAI_API_KEY= - -# Assistant ID - leave it empty if you don't have an assistant yet -ASSISTANT_ID= -``` - -### Step 3: Configuration - -Configure the settings for your assistant. For more information about assistant parameters, you can refer to the [OpenAI documentation](https://platform.openai.com/docs/assistants/how-it-works/creating-assistants). A sample configuration can be found in ([chat.config.ts](apps%2Fapi%2Fsrc%2Fapp%2Fchat%2Fchat.config.ts)). - -```js -// chat.config.ts file - -// Default OpenAI configuration -export const assistantParams: AssistantCreateParams = { - name: 'Your assistant name', - instructions: `You are a chatbot assistant. Speak briefly and clearly.`, - tools: [ - { type: 'code_interpreter' }, - { type: 'retrieval' }, - // (...) function calling - functions are configured by extended services - ], - model: 'gpt-4-1106-preview', - metadata: {}, -}; - -// Additional configuration for our assistant -export const assistantConfig: AssistantConfigParams = { - id: process.env['ASSISTANT_ID'], // OpenAI API Key - params: assistantParams, // AssistantCreateParams - filesDir: './apps/api/src/app/knowledge', // Path to the directory with files (the final path is "fileDir" + "single file") - files: ['file1.txt', 'file2.json'], // List of file names (or paths if you didn't fill in the above parameter) -}; -``` - -Import the AI Assistant module with your configuration into the module file where you intend to use it: - -```js -@Module({ - imports: [AssistantModule.forRoot(assistantConfig)], -}) -export class ChatbotModule {} -``` - -Automatically, the library will add WebSockets ([chat.gateway.ts](libs/ai-assistant/src/lib/chat/chat.gateway.ts)) and a [REST API](https://assistant.ai.boldare.dev/api/docs) for the assistant. The WebSocket server will be available at the `/` endpoint, and the [REST API](https://assistant.ai.boldare.dev/api/docs) will be available at the `/api` endpoint (depending on the API prefix). - -#### Websockets events - -Currently, the library provides the following WebSocket events: - -| Event name | Description | -| ------------------ | -------------------------------------------------------- | -| `send_message` | The event is emitted when the user sends a message. | -| `message_received` | The event is emitted when the assistant sends a message. | - -### Step 4: Function calling - -Create a new service that extends the `AgentBase` class, fill the definition and implement the `output` method. - -- The `output` method is the main method that will be called when the function is invoked. -- The `definition` property is an object that describes the function and its parameters. - -For more information about function calling, you can refer to the [OpenAI documentation](https://platform.openai.com/docs/assistants/tools/defining-functions). -Below is an example of a service that extends the `AgentBase` class: - -```js -@Injectable() -export class GetNicknameAgent extends AgentBase { - definition: AssistantCreateParams.AssistantToolsFunction = { - type: 'function', - function: { - name: this.constructor.name, - description: `Get the nickname of a city`, - parameters: { - type: 'object', - properties: { - location: { - type: 'string', - description: 'The city and state e.g. San Francisco, CA', - }, - }, - required: ['location'], - }, - }, - }; - - constructor(protected readonly agentService: AgentService) { - super(agentService); - } - - async output(data: AgentData): Promise { - // TODO: Your logic here - return 'Your string value'; - } -} -``` - -More examples can be found in the [agents](apps/api/src/app/chat/agents) directory. - -Import the service into the module file where you intend to use it: - -```js -import { Module } from '@nestjs/common'; -import { AgentModule } from '@boldare/ai-assistant'; -import { GetNicknameAgent } from './get-nickname.agent'; - -@Module({ - imports: [AgentModule], - providers: [GetNicknameAgent], -}) -export class AgentsModule {} -``` - -and remember to add the `AgentsModule` above the `AssistantModule` in your main module file (e.g. `chat.module.ts`): - -```js -@Module({ - imports: [AgentsModule, AssistantModule.forRoot(assistantConfig)], -}) -export class ChatModule {} -``` - ---- - -# 👨‍💻 Repository - -The repository includes a library with an AI assistant as well as other useful parts: - -| Name | Type | Description | -| ----------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------- | -| `@boldare/ai-assistant` | `library` | A NestJS library based on the OpenAI Assistant for building efficient, scalable, and quick solutions for AI assistants/chatbots | -| `@boldare/ai-embedded` | `library` | The code enables embedding the chatbot on various websites through JavaScript scripts. | -| `api` | `application` | Example usage of the `@boldare/ai-assistant` library. | -| `spa` | `application` | Example client application (SPA) with a chatbot. | - -## Getting started - -### Step 1: Install dependencies - -```bash -npm install -``` - -### Step 2: Env variables - -Set up your environment variables, copy the `.env.dist` file to `.env` file in the root directory of the project, and populate it with the necessary secrets. - -```bash -cp .env.dist .env -``` - -### Step 3: Run applications - -```bash -# Start the app (api and spa) -npm run start:dev - -# Start the api -npm run start:api - -# Start the spa -npm run start:spa -``` - -Now you can open your browser and navigate to: - -| URL | Description | -| ------------------------------ | --------------------------------------- | -| http://localhost:4200/ | Client application (Angular) | -| http://localhost:3000/ | API application, WebSockets (socket.io) | -| http://localhost:3000/api/ | API endpoints | -| http://localhost:3000/api/docs | API documentation (swagger) | - -### 🎉 Happy coding 🎉 - -# License - -`@boldare/ai-assistant` is MIT licensed diff --git a/libs/ai-assistant/package.json b/libs/ai-assistant/package.json index 5953945..8c172d5 100644 --- a/libs/ai-assistant/package.json +++ b/libs/ai-assistant/package.json @@ -1,6 +1,6 @@ { "name": "@boldare/ai-assistant", - "version": "0.0.2", + "version": "0.0.2-dev.2", "dependencies": { "tslib": "^2.3.0", "openai": "^4.26.1", @@ -16,5 +16,27 @@ }, "type": "commonjs", "main": "./src/index.js", - "typings": "./src/index.d.ts" + "typings": "./src/index.d.ts", + "homepage": "https://github.com/boldare/ai-assistant", + "keywords": [ + "ai", + "assistant", + "boldare", + "nestjs", + "openai", + "chatbot", + "bot", + "assistant", + "ai-assistant", + "openai-assistant" + ], + "author": "boldare", + "license": "MIT", + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/boldare/ai-assistant.git" + } } diff --git a/package.json b/package.json index a4d8d0d..30fd19d 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,9 @@ "build:api": "nx build --prod api", "build:ai-assistant": "nx build --prod ai-assistant", "build:ai-embedded": "nx build --prod ai-embedded --no-cache && npm run cp:ai-embedded", - "publish": "npm run build && cd dist/libs/ai-assistant/ && npm publish --access=public && cd ../../..", + "publish": "npm run build:ai-assistant && npm run cp:readme && cd dist/libs/ai-assistant/ && npm publish --access=public && cd ../../..", "cp:ai-embedded": "cp ./dist/libs/ai-embedded/main.js ./apps/spa/src/assets/js/ai-embedded.js", + "cp:readme": "cp ./README.md ./dist/libs/ai-assistant/README.md", "lint": "nx run-many --parallel --target=lint --all", "test": "nx run-many --parallel --target=test --all", "format": "nx format:write"