|
| 1 | +# Creating a Tiny AI App Using Dialoqbase API |
| 2 | + |
| 3 | +Learn how to create a custom AI chatbot application using the Dialoqbase API. This guide covers essential features including user-specific bot management, knowledge base creation, and interactive chat functionality. |
| 4 | + |
| 5 | +## Key Features |
| 6 | + |
| 7 | +1. **User-Specific Bot Management**: Create and manage dedicated chatbots for individual users. |
| 8 | +2. **Bot Creation and Knowledge Integration**: Set up new bots and populate them with relevant information. |
| 9 | +3. **Interactive Chat Interface**: Engage with the bot using the OpenAI SDK. |
| 10 | + |
| 11 | +## Before You Begin |
| 12 | + |
| 13 | +Ensure you have: |
| 14 | + |
| 15 | +- Node.js (or your preferred programming language) |
| 16 | +- Dialoqbase running locally or on a cloud server |
| 17 | +- Dialoqbase API key (admin access recommended) |
| 18 | + |
| 19 | +This tutorial uses Node.js with Dialoqbase running locally at `http://localhost:3000`. The example API key is `dq_xxxyyyzzz`. |
| 20 | + |
| 21 | +## Step-by-Step Guide |
| 22 | + |
| 23 | +### 1. Implement User-Specific Bot Management |
| 24 | + |
| 25 | +Create a unique bot for each user, similar to Stripe's customer management system. |
| 26 | + |
| 27 | +```javascript |
| 28 | +const response = await fetch('http://localhost:3000/api/v1/admin/register-user', { |
| 29 | + method: 'POST', |
| 30 | + headers: { |
| 31 | + 'Content-Type': 'application/json', |
| 32 | + 'Authorization': 'Bearer dq_xxxyyyzzz' |
| 33 | + }, |
| 34 | + body: JSON.stringify({ |
| 35 | + email: 'darthvader@galacticempire.com', |
| 36 | + name: 'Darth Vader', |
| 37 | + password: 'password', |
| 38 | + return_id: true |
| 39 | + }) |
| 40 | +}); |
| 41 | + |
| 42 | +const userData = await response.json(); |
| 43 | +const userId = userData.user_id; |
| 44 | +const userApiKey = userData.api_key; |
| 45 | +``` |
| 46 | + |
| 47 | +Note: Set `return_id` to `true` to receive the `user_id` and `api_key`. Store these for future use. |
| 48 | + |
| 49 | +### 2. Create a Bot and Add Knowledge |
| 50 | + |
| 51 | +Now that we have a user, let's create their personalized bot and add information to it. |
| 52 | + |
| 53 | +```javascript |
| 54 | +const botResponse = await fetch('http://localhost:3000/api/v1/bot/api', { |
| 55 | + method: 'POST', |
| 56 | + headers: { |
| 57 | + 'Content-Type': 'application/json', |
| 58 | + 'Authorization': `Bearer ${userApiKey}` |
| 59 | + }, |
| 60 | + body: JSON.stringify({ |
| 61 | + name: 'Darth Vader Bot', |
| 62 | + model: "claude-3-opus-20240229", |
| 63 | + embedding: "nomic-ai/nomic-embed-text-v1.5" |
| 64 | + }) |
| 65 | +}); |
| 66 | + |
| 67 | +const botData = await botResponse.json(); |
| 68 | +const botId = botData.id; |
| 69 | + |
| 70 | +// Add knowledge to the bot |
| 71 | +await fetch(`http://localhost:3000/api/v1/bot/${botId}/source/bulk`, { |
| 72 | + method: 'POST', |
| 73 | + headers: { |
| 74 | + 'Content-Type': 'application/json', |
| 75 | + 'Authorization': `Bearer ${userApiKey}` |
| 76 | + }, |
| 77 | + body: JSON.stringify({ |
| 78 | + data: [ |
| 79 | + { |
| 80 | + "content": "https://n4ze3m.com", |
| 81 | + "type": "website" |
| 82 | + } |
| 83 | + ] |
| 84 | + }) |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +This example uses the `claude-3-opus-20240229` model and `nomic-ai/nomic-embed-text-v1.5` embedding. Choose any model and embedding enabled in your Dialoqbase instance. |
| 89 | + |
| 90 | +### 3. Interact with Your Bot |
| 91 | + |
| 92 | +Install the OpenAI SDK: |
| 93 | + |
| 94 | +```bash |
| 95 | +npm install openai |
| 96 | +``` |
| 97 | + |
| 98 | +Use the following code to chat with your bot: |
| 99 | + |
| 100 | +```javascript |
| 101 | +import { OpenAI } from "openai"; |
| 102 | + |
| 103 | +const dialoqbase = new OpenAI({ |
| 104 | + apiKey: userApiKey, |
| 105 | + baseURL: "http://localhost:3000/api/v1/openai", |
| 106 | +}); |
| 107 | + |
| 108 | +const chatResponse = await dialoqbase.chat.completions.create({ |
| 109 | + model: botId, |
| 110 | + messages: [ |
| 111 | + { role: "user", content: "Hello" }, |
| 112 | + ], |
| 113 | + stream: true, |
| 114 | +}); |
| 115 | + |
| 116 | +for await (const chunk of chatResponse) { |
| 117 | + process.stdout.write(chunk.choices[0]?.delta?.content || ''); |
| 118 | +} |
| 119 | +``` |
| 120 | +
|
| 121 | +## Conclusion |
| 122 | +
|
| 123 | +Congratulations! You've successfully created a personalized AI chatbot application using the Dialoqbase API. This foundation allows you to: |
| 124 | +
|
| 125 | +1. Create user-specific bots |
| 126 | +2. Add custom knowledge to each bot |
| 127 | +3. Engage in interactive conversations |
0 commit comments