Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adjust readme.md for beginners #71

Merged
merged 11 commits into from
Jun 21, 2024
134 changes: 116 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
</p>

<p align="center">
<a href="https://assistant.ai.boldare.dev/" target="_blank">demo</a> 🔹
<a href="https://assistant.ai.boldare.dev/api/docs" target="_blank">api docs</a> 🔹
<a href="https://assistant.ai.boldare.dev/" target="_blank">Demo</a> 🔹
<a href="https://assistant.ai.boldare.dev/api/docs" target="_blank">API docs</a> 🔹
<a href="https://www.npmjs.com/package/@boldare/openai-assistant" target="_blank">npm</a> 🔹
<a href="https://github.com/boldare/openai-assistant" target="_blank">github</a>
<a href="https://github.com/boldare/openai-assistant" target="_blank">Github</a> 🔹
<a href="https://www.boldare.com/services/ai-software-development-consulting/" target="_blank">How we can help you</a>
</p>

# 🤖 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.
`@boldare/openai-assistant` - library to kickstart your AI Assistant development under 15 minutes.

Introducing the NestJS library. Whether you're building a virtual assistant, or an interactive chatbot for engaging user experiences, our library empowers you to leverage cutting-edge AI capabilities with minimal effort.

**The library provides ready-to-use API endpoints** handling your assistant and WebSocket server for real-time communication between the client and the assistant. Install the library and paste the config to get it running.

## 🚀 Features

#### AI Assistant library features
### AI Assistant library features

- **Function calling**: The library provides a way to create functions, which allows you to extend the assistant's capabilities with custom logic.
- **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.
Expand All @@ -28,28 +33,34 @@ Introducing the NestJS library, designed to harness the power of OpenAI's Assist

#### Additional features in the repository

The repository not only contains a library but also provides additional features. You don't have to build everything from scratch. You can just clone the repository and run it to take advantage of the features above and below:

- **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: Prerequisites
<!-- The information that after this steps we will have the endpoints ready -->

Install Node.js which includes Node Package Manager (`^20.0.0` version).
### Step 0: Prerequisites

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/).
- Node.js (`^20.0.0` version)
- npm (`^10.0.0` version)
- NestJS (`^10.0.0` version)
- OpenAI (`^4.51.0` version)
- OpenAI API key

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:
Open or create your NestJS application where you would like to integrate the AI Assistant. To create a new NestJS application, use the following command:

```bash
nest new project-name
```

### Step 1: Installation

Install the library using npm:
Install the library and `openai` package using npm:

```bash
npm i @boldare/openai-assistant openai --save
Expand All @@ -71,38 +82,125 @@ Add the following content to the `.env` file:
# OpenAI API Key
OPENAI_API_KEY=

# Assistant ID - leave it empty if you don't have an assistant yet
# Assistant ID - leave it empty if you don't have an assistant to reuse
ASSISTANT_ID=
```

Please note that the `.env` file should not be committed to the repository. Add it to the `.gitignore` file to prevent it from being committed.
Please note that the `.env` file should not be committed to the repository. *Add the `.env` file to the `.gitignore`* file to prevent it from being committed.

### Step 3: Configuration

The library provides a way to configure the assistant with the `AssistantModule.forRoot` method. The method takes a configuration object as an argument. Create a new configuration file like in a [sample configuration file (chat.config.ts)](apps%2Fapi%2Fsrc%2Fapp%2Fchat%2Fchat.config.ts) and fill it with the necessary configuration.

More details about the configuration with code examples can be found in the [wiki](https://github.com/boldare/openai-assistant/wiki/%F0%9F%A4%96-AI-Assistant#step-3-configuration).
```typescript
// chat.config.ts file
import { AssistantConfigParams } from '@boldare/openai-assistant';
import { AssistantCreateParams } from 'openai/resources/beta';

// 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' },
sebastianmusial marked this conversation as resolved.
Show resolved Hide resolved
],
model: 'gpt-4-turbo',
temperature: 0.05,
};

// Additional configuration for our assistant
export const assistantConfig: AssistantConfigParams = {
id: process.env['ASSISTANT_ID'],
params: assistantParams,
filesDir: './apps/api/src/app/knowledge',
toolResources: {
codeInterpreter: {
sebastianmusial marked this conversation as resolved.
Show resolved Hide resolved
fileNames: [],
},
},
};
```
olivierhalupczok marked this conversation as resolved.
Show resolved Hide resolved


More details about the configuration can be found in the [wiki](https://github.com/boldare/openai-assistant/wiki/%F0%9F%A4%96-AI-Assistant#step-3-configuration).

#### What is this step for?
From now you can run your application and call the assistant.


### Step 4: Function calling

Function calling allows you to extend the assistant's capabilities with custom logic. **If you are not going to use function calling you can jump to: [Step 5: Testing](#step-5-testing).**
olivierhalupczok marked this conversation as resolved.
Show resolved Hide resolved

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.
- The `output` method is the main method that will be called when the function is invoked by the assistant.
- The `definition` property is an object that describes the function and its parameters so the assistant can understand how to call it.

For more information about function calling, you can refer to the [OpenAI documentation](https://platform.openai.com/docs/assistants/tools/defining-functions).

The instructions for creating a function can be found in the [wiki](https://github.com/boldare/openai-assistant/wiki/%F0%9F%A4%96-AI-Assistant#step-4-function-calling), while examples can be found in the [agents](apps/api/src/app/chat/agents) directory.

#### What is this step for?

If you've defined a function and the output method, you can now call it from the assistant just by asking him to do the action described in the function definition.


### Step 5: Running the application and testing

Run your application and this will allow you to test the assistant.

```bash
# use this if you are using the repository:
npm run start:dev

# if you are using NestJS please check the npm scripts in the package.json file
olivierhalupczok marked this conversation as resolved.
Show resolved Hide resolved
# defualt command for NestJS is:
npm run start
```

Then you can test the assistant.
1. First, you need to create a thread. You can create one
by sending a POST request to the `/assistant/threads` endpoint with the empty body:
```
{}
```
olivierhalupczok marked this conversation as resolved.
Show resolved Hide resolved
2. Then you can send a message to the assistant by sending a POST request to the `/assistant/chat` endpoint with the following body.
```json
{
"threadId": "your-thread-id",
"content": "Hello, how are you?"
}
```

---


## 🤔 Are you stuck?

Boldare's engineers are here to help you. If you have any questions or need help with the implementation, feel free to **[click here to book a call with one of our engineers.](https://calendly.com/olivier-halupczok/30min)**

**Learn more how [Boldare can help you with AI development on our website](https://www.boldare.com/services/ai-software-development-consulting/).**

You can also ask questions in the [GitHub Discussions](https://github.com/boldare/openai-assistant/discussions) section.

---

# 👨‍💻 Repository
## 👨‍💻 Repository

The complete documentation on how to run the demo with all applications and libraries from the repository can be found in the [wiki](https://github.com/boldare/openai-assistant/wiki/%F0%9F%91%A8%E2%80%8D%F0%9F%92%BB-Repository).

---

# License
## Contributions

Would you like to see new features in the library?
- You can freely contribute to the project! Just create a pull request with your changes.
- [Talk your idea over with one of our engineers.](https://calendly.com/olivier-halupczok/30min)
- You can also [post your idea here](https://github.com/boldare/openai-assistant/discussions).

---

## License

`@boldare/openai-assistant` is MIT licensed
`@boldare/openai-assistant` and this repository is MIT licensed