Skip to content

Commit e2206db

Browse files
authored
Merge pull request #135 from n4ze3m/next
v1.3.0
2 parents 78dd134 + ad4be18 commit e2206db

File tree

22 files changed

+521
-235
lines changed

22 files changed

+521
-235
lines changed

.github/workflows/build.yml

+11-10
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ jobs:
99
docker:
1010
runs-on: ubuntu-latest
1111
steps:
12-
-
13-
name: Set up QEMU
14-
uses: docker/setup-qemu-action@v2
15-
-
16-
name: Set up Docker Buildx
17-
uses: docker/setup-buildx-action@v2
18-
-
19-
name: Login to Docker Hub
20-
uses: docker/login-action@v2
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
- name: Set up QEMU
15+
uses: docker/setup-qemu-action@v3
16+
- name: Set up Docker Buildx
17+
uses: docker/setup-buildx-action@v3
18+
- name: Login to Docker Hub
19+
uses: docker/login-action@v3
2120
with:
2221
username: ${{ secrets.DOCKERHUB_USERNAME }}
2322
password: ${{ secrets.DOCKERHUB_PWD }}
2423
- name: Build and push
25-
uses: docker/build-push-action@v3
24+
uses: docker/build-push-action@v5
2625
with:
2726
tags: |
2827
n4z3m/dialoqbase:latest
2928
n4z3m/dialoqbase:${{ github.ref_name}}
3029
push: true
30+
cache-from: type=registry,ref=n4z3m/dialoqbase:latest
31+
cache-to: type=inline

.github/workflows/next.yml

+12-11
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ jobs:
88
docker:
99
runs-on: ubuntu-latest
1010
steps:
11-
-
12-
name: Set up QEMU
13-
uses: docker/setup-qemu-action@v2
14-
-
15-
name: Set up Docker Buildx
16-
uses: docker/setup-buildx-action@v2
17-
-
18-
name: Login to Docker Hub
19-
uses: docker/login-action@v2
11+
- name: Checkout
12+
uses: actions/checkout@v4
13+
- name: Set up QEMU
14+
uses: docker/setup-qemu-action@v3
15+
- name: Set up Docker Buildx
16+
uses: docker/setup-buildx-action@v3
17+
- name: Login to Docker Hub
18+
uses: docker/login-action@v3
2019
with:
2120
username: ${{ secrets.DOCKERHUB_USERNAME }}
2221
password: ${{ secrets.DOCKERHUB_PWD }}
2322
- name: Build and push
24-
uses: docker/build-push-action@v3
23+
uses: docker/build-push-action@v5
2524
with:
2625
tags: |
2726
n4z3m/dialoqbase-next:latest
28-
push: true
27+
push: true
28+
cache-from: type=registry,ref=n4z3m/dialoqbase-next:latest
29+
cache-to: type=inline

app/ui/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "app",
33
"private": true,
4-
"version": "1.2.1",
4+
"version": "1.3.0",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",

app/ui/src/@types/bot.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type BotSettings = {
1313
use_hybrid_search: boolean;
1414
bot_protect: boolean;
1515
use_rag: boolean;
16+
bot_model_api_key: string;
1617
},
1718
chatModel: {
1819
label: string;

app/ui/src/components/Bot/Playground/Form.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const PlaygroundgForm = () => {
3030
stop,
3131
} = useSpeechRecognition();
3232

33-
3433
React.useEffect(() => {
3534
const defaultLanguageFromLocalStorage = localStorage.getItem(
3635
"defaultSpeechToTextLanguage"

app/ui/src/components/Bot/Settings/SettingsCard.tsx

+18-6
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export const SettingsCard: React.FC<BotSettings> = ({
126126
use_hybrid_search: data.use_hybrid_search,
127127
bot_protect: data.bot_protect,
128128
use_rag: data.use_rag,
129+
bot_model_api_key: data.bot_model_api_key,
129130
}}
130131
form={form}
131132
requiredMark={false}
@@ -304,28 +305,39 @@ export const SettingsCard: React.FC<BotSettings> = ({
304305

305306
<Form.Item
306307
name="use_hybrid_search"
307-
label="Use Hybrid Search Retrieval (Beta)"
308+
label="Use Hybrid Search Retrieval"
308309
valuePropName="checked"
309310
tooltip="This will use the hybrid search retrieval method instead of the default semantic search retrieval method. Only work on playground ui."
310311
>
311312
<Switch />
312313
</Form.Item>
314+
<Form.Item
315+
name="use_rag"
316+
label="Use Retrieval Augmented Generation (RAG)"
317+
valuePropName="checked"
318+
>
319+
<Switch />
320+
</Form.Item>
313321

314322
<Form.Item
315323
name="bot_protect"
316-
label="Activate Public Bot Protection (Beta)"
324+
label="Activate Public Bot Protection"
317325
valuePropName="checked"
318326
tooltip="This will activate the public bot protection using session to avoid misuse of the bot"
319327
>
320328
<Switch />
321329
</Form.Item>
322330

323331
<Form.Item
324-
name="use_rag"
325-
label="Use Retrieval Augmented Generation (RAG)"
326-
valuePropName="checked"
332+
name="bot_model_api_key"
333+
label="Chat Model API Key"
334+
help="Enter your API key here. If you don't have one, you can leave this field blank."
335+
tooltip="Enter your API key to use your own chat model. Currently, only OpenAI API keys are supported."
327336
>
328-
<Switch />
337+
<input
338+
type="password"
339+
className="mt-1 block w-full sm:text-sm focus:ring-indigo-500 focus:border-indigo-500 border-gray-300 rounded-md"
340+
/>
329341
</Form.Item>
330342
</div>
331343
</div>

app/ui/src/routes/settings/model.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export default function SettingsModelRoot() {
7575
const response = await api.post("/admin/models", {
7676
...values,
7777
url: fetchUrlForm.getFieldValue("url"),
78+
api_key: fetchUrlForm.getFieldValue("api_key"),
7879
});
7980
return response.data;
8081
};
@@ -311,6 +312,14 @@ export default function SettingsModelRoot() {
311312
/>
312313
</Form.Item>
313314

315+
<Form.Item name="api_key" label="API Key (Optional)">
316+
<input
317+
type="text"
318+
className="border border-gray-200 rounded-md px-3 py-2 w-full"
319+
placeholder="API Key (Optional)"
320+
/>
321+
</Form.Item>
322+
314323
<button
315324
type="submit"
316325
disabled={isSaveModel}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dialoqbase",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"description": "Create chatbots with ease",
55
"scripts": {
66
"ui:dev": "pnpm run --filter ui dev",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "Bot" ADD COLUMN "bot_model_api_key" TEXT;

server/prisma/schema.prisma

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ model Bot {
3434
use_rag Boolean @default(false)
3535
bot_protect Boolean @default(false)
3636
bot_api_key String?
37+
bot_model_api_key String?
3738
options Json? @default("{}") @db.Json
3839
BotAppearance BotAppearance[]
3940
document BotDocument[]

server/src/integration/handlers/discord.handler.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ let history = chat_history
6969
}
7070
}
7171

72-
const botConfig = (modelinfo.config as {}) || {};
72+
const botConfig: any = (modelinfo.config as {}) || {};
73+
if (bot.provider.toLowerCase() === "openai") {
74+
if (bot.bot_model_api_key && bot.bot_model_api_key.trim() !== "") {
75+
botConfig.configuration = {
76+
apiKey: bot.bot_model_api_key,
77+
};
78+
}
79+
}
7380

7481
const model = chatModelProvider(bot.provider, bot.model, temperature, {
7582
...botConfig,

server/src/integration/handlers/telegram.handler.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ export const telegramBotHandler = async (
6767
return "Unable to find model";
6868
}
6969

70-
const botConfig = (modelinfo.config as {}) || {};
70+
const botConfig: any = (modelinfo.config as {}) || {};
71+
if (bot.provider.toLowerCase() === "openai") {
72+
if (bot.bot_model_api_key && bot.bot_model_api_key.trim() !== "") {
73+
botConfig.configuration = {
74+
apiKey: bot.bot_model_api_key,
75+
};
76+
}
77+
}
78+
7179
const model = chatModelProvider(bot.provider, bot.model, temperature, {
7280
...botConfig,
7381
});

server/src/integration/handlers/whatsapp.handler.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const whatsappBotHandler = async (
99
bot_id: string,
1010
hash: string,
1111
from: string,
12-
message: string,
12+
message: string
1313
) => {
1414
try {
1515
await prisma.$connect();
@@ -45,9 +45,11 @@ export const whatsappBotHandler = async (
4545
chat_history.splice(0, chat_history.length - 10);
4646
}
4747

48-
let history = chat_history.map((chat) => {
49-
return `Human: ${chat.human}\nAssistant: ${chat.bot}`;
50-
}).join("\n");
48+
let history = chat_history
49+
.map((chat) => {
50+
return `Human: ${chat.human}\nAssistant: ${chat.bot}`;
51+
})
52+
.join("\n");
5153

5254
const temperature = bot.temperature;
5355

@@ -59,7 +61,7 @@ export const whatsappBotHandler = async (
5961
{
6062
botId: bot.id,
6163
sourceId: null,
62-
},
64+
}
6365
);
6466

6567
const modelinfo = await prisma.dialoqbaseModels.findFirst({
@@ -74,16 +76,18 @@ export const whatsappBotHandler = async (
7476
return "Unable to find model";
7577
}
7678

77-
const botConfig = (modelinfo.config as {}) || {};
79+
const botConfig: any = (modelinfo.config as {}) || {};
80+
if (bot.provider.toLowerCase() === "openai") {
81+
if (bot.bot_model_api_key && bot.bot_model_api_key.trim() !== "") {
82+
botConfig.configuration = {
83+
apiKey: bot.bot_model_api_key,
84+
};
85+
}
86+
}
7887

79-
const model = chatModelProvider(
80-
bot.provider,
81-
bot.model,
82-
temperature,
83-
{
84-
...botConfig,
85-
},
86-
);
88+
const model = chatModelProvider(bot.provider, bot.model, temperature, {
89+
...botConfig,
90+
});
8791

8892
const chain = ConversationalRetrievalQAChain.fromLLM(
8993
model,
@@ -92,7 +96,7 @@ export const whatsappBotHandler = async (
9296
qaTemplate: bot.qaPrompt,
9397
questionGeneratorTemplate: bot.questionGeneratorPrompt,
9498
returnSourceDocuments: true,
95-
},
99+
}
96100
);
97101

98102
const response = await chain.call({
@@ -121,12 +125,8 @@ export const whatsappBotHandler = async (
121125
}
122126
};
123127

124-
export const clearHistoryWhatsapp = async (
125-
bot_id: string,
126-
from: string,
127-
) => {
128+
export const clearHistoryWhatsapp = async (bot_id: string, from: string) => {
128129
try {
129-
130130
await prisma.$connect();
131131

132132
const bot = await prisma.bot.findFirst({

server/src/routes/api/v1/admin/handlers/model.handler.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ import {
88
} from "./type";
99
import axios from "axios";
1010

11-
const _getModelFromUrl = async (url: string) => {
11+
const _getModelFromUrl = async (url: string, apiKey?: string) => {
1212
try {
13-
const response = await axios.get(`${url}/models`);
13+
const response = await axios.get(`${url}/models`, {
14+
headers: {
15+
"HTTP-Referer":
16+
process.env.LOCAL_REFER_URL || "https://dialoqbase.n4ze3m.com/",
17+
"X-Title": process.env.LOCAL_TITLE || "Dialoqbase",
18+
Authorization: apiKey && `Bearer ${apiKey}`,
19+
},
20+
});
1421
return response.data as {
1522
type: string;
1623
data: {
@@ -58,15 +65,15 @@ export const fetchModelFromInputedUrlHandler = async (
5865
reply: FastifyReply
5966
) => {
6067
try {
61-
const { url } = request.body;
68+
const { url, api_key } = request.body;
6269
const user = request.user;
6370
if (!user.is_admin) {
6471
return reply.status(403).send({
6572
message: "Forbidden",
6673
});
6774
}
6875

69-
const model = await _getModelFromUrl(url);
76+
const model = await _getModelFromUrl(url, api_key);
7077

7178
if (!model) {
7279
return reply.status(404).send({
@@ -100,7 +107,7 @@ export const saveModelFromInputedUrlHandler = async (
100107
});
101108
}
102109

103-
const { url, model_id, name, stream_available } = request.body;
110+
const { url, api_key, model_id, name, stream_available } = request.body;
104111

105112
const modelExist = await prisma.dialoqbaseModels.findFirst({
106113
where: {
@@ -125,6 +132,7 @@ export const saveModelFromInputedUrlHandler = async (
125132
model_provider: "local",
126133
config: {
127134
baseURL: url,
135+
apiKey: api_key,
128136
},
129137
},
130138
});

server/src/routes/api/v1/admin/handlers/type.ts

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type RegisterUserbyAdminRequestBody = {
2525
export type FetchModelFromInputedUrlRequest = {
2626
Body: {
2727
url: string;
28+
api_key?: string;
2829
};
2930
};
3031
export type SaveModelFromInputedUrlRequest = {
@@ -33,6 +34,7 @@ export type SaveModelFromInputedUrlRequest = {
3334
model_id: string;
3435
name: string;
3536
stream_available: boolean;
37+
api_key?: string;
3638
};
3739
};
3840

server/src/routes/api/v1/bot/handlers/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@ export interface UpdateBotById {
6666
use_hybrid_search: boolean;
6767
bot_protect: boolean;
6868
use_rag: boolean;
69+
bot_model_api_key: string
6970
};
7071
}

0 commit comments

Comments
 (0)