Skip to content

Commit

Permalink
Merge pull request #135 from n4ze3m/next
Browse files Browse the repository at this point in the history
v1.3.0
  • Loading branch information
n4ze3m authored Nov 2, 2023
2 parents 78dd134 + ad4be18 commit e2206db
Show file tree
Hide file tree
Showing 22 changed files with 521 additions and 235 deletions.
21 changes: 11 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Login to Docker Hub
uses: docker/login-action@v2
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PWD }}
- name: Build and push
uses: docker/build-push-action@v3
uses: docker/build-push-action@v5
with:
tags: |
n4z3m/dialoqbase:latest
n4z3m/dialoqbase:${{ github.ref_name}}
push: true
cache-from: type=registry,ref=n4z3m/dialoqbase:latest
cache-to: type=inline
23 changes: 12 additions & 11 deletions .github/workflows/next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Login to Docker Hub
uses: docker/login-action@v2
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PWD }}
- name: Build and push
uses: docker/build-push-action@v3
uses: docker/build-push-action@v5
with:
tags: |
n4z3m/dialoqbase-next:latest
push: true
push: true
cache-from: type=registry,ref=n4z3m/dialoqbase-next:latest
cache-to: type=inline
2 changes: 1 addition & 1 deletion app/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "app",
"private": true,
"version": "1.2.1",
"version": "1.3.0",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
1 change: 1 addition & 0 deletions app/ui/src/@types/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type BotSettings = {
use_hybrid_search: boolean;
bot_protect: boolean;
use_rag: boolean;
bot_model_api_key: string;
},
chatModel: {
label: string;
Expand Down
1 change: 0 additions & 1 deletion app/ui/src/components/Bot/Playground/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const PlaygroundgForm = () => {
stop,
} = useSpeechRecognition();


React.useEffect(() => {
const defaultLanguageFromLocalStorage = localStorage.getItem(
"defaultSpeechToTextLanguage"
Expand Down
24 changes: 18 additions & 6 deletions app/ui/src/components/Bot/Settings/SettingsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export const SettingsCard: React.FC<BotSettings> = ({
use_hybrid_search: data.use_hybrid_search,
bot_protect: data.bot_protect,
use_rag: data.use_rag,
bot_model_api_key: data.bot_model_api_key,
}}
form={form}
requiredMark={false}
Expand Down Expand Up @@ -304,28 +305,39 @@ export const SettingsCard: React.FC<BotSettings> = ({

<Form.Item
name="use_hybrid_search"
label="Use Hybrid Search Retrieval (Beta)"
label="Use Hybrid Search Retrieval"
valuePropName="checked"
tooltip="This will use the hybrid search retrieval method instead of the default semantic search retrieval method. Only work on playground ui."
>
<Switch />
</Form.Item>
<Form.Item
name="use_rag"
label="Use Retrieval Augmented Generation (RAG)"
valuePropName="checked"
>
<Switch />
</Form.Item>

<Form.Item
name="bot_protect"
label="Activate Public Bot Protection (Beta)"
label="Activate Public Bot Protection"
valuePropName="checked"
tooltip="This will activate the public bot protection using session to avoid misuse of the bot"
>
<Switch />
</Form.Item>

<Form.Item
name="use_rag"
label="Use Retrieval Augmented Generation (RAG)"
valuePropName="checked"
name="bot_model_api_key"
label="Chat Model API Key"
help="Enter your API key here. If you don't have one, you can leave this field blank."
tooltip="Enter your API key to use your own chat model. Currently, only OpenAI API keys are supported."
>
<Switch />
<input
type="password"
className="mt-1 block w-full sm:text-sm focus:ring-indigo-500 focus:border-indigo-500 border-gray-300 rounded-md"
/>
</Form.Item>
</div>
</div>
Expand Down
9 changes: 9 additions & 0 deletions app/ui/src/routes/settings/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default function SettingsModelRoot() {
const response = await api.post("/admin/models", {
...values,
url: fetchUrlForm.getFieldValue("url"),
api_key: fetchUrlForm.getFieldValue("api_key"),
});
return response.data;
};
Expand Down Expand Up @@ -311,6 +312,14 @@ export default function SettingsModelRoot() {
/>
</Form.Item>

<Form.Item name="api_key" label="API Key (Optional)">
<input
type="text"
className="border border-gray-200 rounded-md px-3 py-2 w-full"
placeholder="API Key (Optional)"
/>
</Form.Item>

<button
type="submit"
disabled={isSaveModel}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dialoqbase",
"version": "1.2.1",
"version": "1.3.0",
"description": "Create chatbots with ease",
"scripts": {
"ui:dev": "pnpm run --filter ui dev",
Expand Down
2 changes: 2 additions & 0 deletions server/prisma/migrations/20231102171748_q_17/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Bot" ADD COLUMN "bot_model_api_key" TEXT;
1 change: 1 addition & 0 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ model Bot {
use_rag Boolean @default(false)
bot_protect Boolean @default(false)
bot_api_key String?
bot_model_api_key String?
options Json? @default("{}") @db.Json
BotAppearance BotAppearance[]
document BotDocument[]
Expand Down
9 changes: 8 additions & 1 deletion server/src/integration/handlers/discord.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ let history = chat_history
}
}

const botConfig = (modelinfo.config as {}) || {};
const botConfig: any = (modelinfo.config as {}) || {};
if (bot.provider.toLowerCase() === "openai") {
if (bot.bot_model_api_key && bot.bot_model_api_key.trim() !== "") {
botConfig.configuration = {
apiKey: bot.bot_model_api_key,
};
}
}

const model = chatModelProvider(bot.provider, bot.model, temperature, {
...botConfig,
Expand Down
10 changes: 9 additions & 1 deletion server/src/integration/handlers/telegram.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ export const telegramBotHandler = async (
return "Unable to find model";
}

const botConfig = (modelinfo.config as {}) || {};
const botConfig: any = (modelinfo.config as {}) || {};
if (bot.provider.toLowerCase() === "openai") {
if (bot.bot_model_api_key && bot.bot_model_api_key.trim() !== "") {
botConfig.configuration = {
apiKey: bot.bot_model_api_key,
};
}
}

const model = chatModelProvider(bot.provider, bot.model, temperature, {
...botConfig,
});
Expand Down
40 changes: 20 additions & 20 deletions server/src/integration/handlers/whatsapp.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const whatsappBotHandler = async (
bot_id: string,
hash: string,
from: string,
message: string,
message: string
) => {
try {
await prisma.$connect();
Expand Down Expand Up @@ -45,9 +45,11 @@ export const whatsappBotHandler = async (
chat_history.splice(0, chat_history.length - 10);
}

let history = chat_history.map((chat) => {
return `Human: ${chat.human}\nAssistant: ${chat.bot}`;
}).join("\n");
let history = chat_history
.map((chat) => {
return `Human: ${chat.human}\nAssistant: ${chat.bot}`;
})
.join("\n");

const temperature = bot.temperature;

Expand All @@ -59,7 +61,7 @@ export const whatsappBotHandler = async (
{
botId: bot.id,
sourceId: null,
},
}
);

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

const botConfig = (modelinfo.config as {}) || {};
const botConfig: any = (modelinfo.config as {}) || {};
if (bot.provider.toLowerCase() === "openai") {
if (bot.bot_model_api_key && bot.bot_model_api_key.trim() !== "") {
botConfig.configuration = {
apiKey: bot.bot_model_api_key,
};
}
}

const model = chatModelProvider(
bot.provider,
bot.model,
temperature,
{
...botConfig,
},
);
const model = chatModelProvider(bot.provider, bot.model, temperature, {
...botConfig,
});

const chain = ConversationalRetrievalQAChain.fromLLM(
model,
Expand All @@ -92,7 +96,7 @@ export const whatsappBotHandler = async (
qaTemplate: bot.qaPrompt,
questionGeneratorTemplate: bot.questionGeneratorPrompt,
returnSourceDocuments: true,
},
}
);

const response = await chain.call({
Expand Down Expand Up @@ -121,12 +125,8 @@ export const whatsappBotHandler = async (
}
};

export const clearHistoryWhatsapp = async (
bot_id: string,
from: string,
) => {
export const clearHistoryWhatsapp = async (bot_id: string, from: string) => {
try {

await prisma.$connect();

const bot = await prisma.bot.findFirst({
Expand Down
18 changes: 13 additions & 5 deletions server/src/routes/api/v1/admin/handlers/model.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ import {
} from "./type";
import axios from "axios";

const _getModelFromUrl = async (url: string) => {
const _getModelFromUrl = async (url: string, apiKey?: string) => {
try {
const response = await axios.get(`${url}/models`);
const response = await axios.get(`${url}/models`, {
headers: {
"HTTP-Referer":
process.env.LOCAL_REFER_URL || "https://dialoqbase.n4ze3m.com/",
"X-Title": process.env.LOCAL_TITLE || "Dialoqbase",
Authorization: apiKey && `Bearer ${apiKey}`,
},
});
return response.data as {
type: string;
data: {
Expand Down Expand Up @@ -58,15 +65,15 @@ export const fetchModelFromInputedUrlHandler = async (
reply: FastifyReply
) => {
try {
const { url } = request.body;
const { url, api_key } = request.body;
const user = request.user;
if (!user.is_admin) {
return reply.status(403).send({
message: "Forbidden",
});
}

const model = await _getModelFromUrl(url);
const model = await _getModelFromUrl(url, api_key);

if (!model) {
return reply.status(404).send({
Expand Down Expand Up @@ -100,7 +107,7 @@ export const saveModelFromInputedUrlHandler = async (
});
}

const { url, model_id, name, stream_available } = request.body;
const { url, api_key, model_id, name, stream_available } = request.body;

const modelExist = await prisma.dialoqbaseModels.findFirst({
where: {
Expand All @@ -125,6 +132,7 @@ export const saveModelFromInputedUrlHandler = async (
model_provider: "local",
config: {
baseURL: url,
apiKey: api_key,
},
},
});
Expand Down
2 changes: 2 additions & 0 deletions server/src/routes/api/v1/admin/handlers/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type RegisterUserbyAdminRequestBody = {
export type FetchModelFromInputedUrlRequest = {
Body: {
url: string;
api_key?: string;
};
};
export type SaveModelFromInputedUrlRequest = {
Expand All @@ -33,6 +34,7 @@ export type SaveModelFromInputedUrlRequest = {
model_id: string;
name: string;
stream_available: boolean;
api_key?: string;
};
};

Expand Down
1 change: 1 addition & 0 deletions server/src/routes/api/v1/bot/handlers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ export interface UpdateBotById {
use_hybrid_search: boolean;
bot_protect: boolean;
use_rag: boolean;
bot_model_api_key: string
};
}
Loading

0 comments on commit e2206db

Please sign in to comment.