Skip to content

Commit

Permalink
Manage models
Browse files Browse the repository at this point in the history
  • Loading branch information
n4ze3m committed May 2, 2024
1 parent efc922d commit dedf3c5
Show file tree
Hide file tree
Showing 30 changed files with 825 additions and 679 deletions.
82 changes: 79 additions & 3 deletions app/ui/src/routes/settings/application.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Form, InputNumber, Switch, notification, Select } from "antd";
import { Form, InputNumber, Switch, notification, Select, Input } from "antd";
import React from "react";
import api from "../../services/api";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
Expand All @@ -25,6 +25,9 @@ export default function SettingsApplicationRoot() {
defaultChunkOverlap: number;
defaultChatModel: string;
defaultEmbeddingModel: string;
hideDefaultModels: boolean;
dynamicallyFetchOllamaModels: boolean;
ollamaURL: string;
};
});

Expand Down Expand Up @@ -63,6 +66,22 @@ export default function SettingsApplicationRoot() {
}
);

const { mutateAsync: updateModelSettings, isLoading: isModelLoading } =
useMutation(onUpdateApplicatoon, {
onSuccess: (data) => {
queryClient.invalidateQueries(["fetchBotCreateConfig"]);
notification.success({
message: "Success",
description: data.message,
});
},
onError: (error: any) => {
notification.error({
message: "Error",
description: error?.response?.data?.message || "Something went wrong",
});
},
});
const { mutateAsync: updateRagSettings, isLoading: isRagLoading } =
useMutation(onRagApplicationUpdate, {
onSuccess: (data) => {
Expand Down Expand Up @@ -135,6 +154,33 @@ export default function SettingsApplicationRoot() {
>
<Switch />
</Form.Item>
</div>
<div className="bg-gray-50 border-x border-b rounded-b-md rounded-x-md px-4 py-3 text-right sm:px-6 dark:bg-[#141414] dark:border-gray-600">
<button
disabled={isLoading}
type="submit"
className="inline-flex justify-center rounded-md border border-transparent bg-indigo-600 py-2 px-4 text-sm font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
>
{isLoading ? "Saving..." : "Save"}
</button>
</div>
</div>
</Form>
</ApplicationCard>

<ApplicationCard
title="Models Settings"
description="Configure your models settings"
>
<Form
initialValues={{
...data,
}}
layout="vertical"
onFinish={updateModelSettings}
>
<div className="sm:overflow-hidden ">
<div className="space-y-6 border-t border rounded-t-md bg-white px-4 py-5 sm:p-6 dark:bg-[#171717] dark:border-gray-600">
<Form.Item
label="Default Chat Model"
name="defaultChatModel"
Expand Down Expand Up @@ -195,19 +241,49 @@ export default function SettingsApplicationRoot() {
loading={modeStatus === "loading"}
/>
</Form.Item>
<Form.Item
label="Ollama URL"
name="ollamaURL"
rules={[
{
required: true,
message: "Please input ollama url!",
},
]}
>
<Input size="large" placeholder="Enter ollama url" />
</Form.Item>

<Form.Item
label="Hide Default Models"
name="hideDefaultModels"
valuePropName="checked"
help="This will hide all the default models and only show the models that are locally added or from ollama."
>
<Switch />
</Form.Item>
<Form.Item
label="Dynamically Fetch Ollama Models"
name="dynamicallyFetchOllamaModels"
valuePropName="checked"
help="This will dynamically fetch the models from ollama. You don't need to manually add the models."
>
<Switch />
</Form.Item>
</div>
<div className="bg-gray-50 border-x border-b rounded-b-md rounded-x-md px-4 py-3 text-right sm:px-6 dark:bg-[#141414] dark:border-gray-600">
<button
disabled={isLoading}
disabled={isModelLoading}
type="submit"
className="inline-flex justify-center rounded-md border border-transparent bg-indigo-600 py-2 px-4 text-sm font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
>
{isLoading ? "Saving..." : "Save"}
{isModelLoading ? "Saving..." : "Save"}
</button>
</div>
</div>
</Form>
</ApplicationCard>

<ApplicationCard
title="RAG Settings"
description="Configure your RAG settings"
Expand Down
2 changes: 2 additions & 0 deletions server/prisma/migrations/q_12_3/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "DialoqbaseSettings" ADD COLUMN "ollamaURL" TEXT DEFAULT 'http://host.docker.internal:11434';
1 change: 1 addition & 0 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ model DialoqbaseSettings {
hideDefaultModels Boolean? @default(false)
defaultChatModel String @default("gpt-3.5-turbo-dbase")
defaultEmbeddingModel String @default("dialoqbase_eb_text-embedding-ada-002")
ollamaURL String? @default("http://host.docker.internal:11434")
}

model BotIntegration {
Expand Down
35 changes: 22 additions & 13 deletions server/src/handlers/api/v1/admin/model.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "./type";
import axios from "axios";
import { removeTrailingSlash } from "../../../../utils/url";
import { getSettings } from "../../../../utils/common";

const _getModelFromUrl = async (url: string, apiKey?: string) => {
try {
Expand Down Expand Up @@ -130,21 +131,29 @@ export const getAllModelsHandler = async (
request: FastifyRequest,
reply: FastifyReply
) => {
try {
const prisma = request.server.prisma;
const user = request.user;
const prisma = request.server.prisma;
const user = request.user;

if (!user.is_admin) {
return reply.status(403).send({
message: "Forbidden",
});
}
const allModels = await prisma.dialoqbaseModels.findMany({
where: {
deleted: false,
},
if (!user.is_admin) {
return reply.status(403).send({
message: "Forbidden",
});
}

const settings = await getSettings(prisma);

const not_to_hide_providers = settings?.hideDefaultModels
? [ "Local", "local", "ollama", "transformer", "Transformer"]
: undefined;
const allModels = await prisma.dialoqbaseModels.findMany({
where: {
deleted: false,
model_provider: {
in: not_to_hide_providers,
},
},
});
try {
return {
data: allModels.filter((model) => model.model_type !== "embedding"),
embedding: allModels.filter((model) => model.model_type === "embedding"),
Expand Down Expand Up @@ -245,7 +254,7 @@ export const saveModelFromInputedUrlHandler = async (
});
}

let newModelId = model_id.trim() + `_custom_${new Date().getTime()}`;
let newModelId = model_id.trim() + `_dialoqbase_${new Date().getTime()}`;
await prisma.dialoqbaseModels.create({
data: {
name: isModelExist.name,
Expand Down
36 changes: 10 additions & 26 deletions server/src/handlers/api/v1/bot/bot/api.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
uniqueNamesGenerator,
} from "unique-names-generator";
import { validateDataSource } from "../../../../../utils/datasource-validation";
import { getModelInfo } from "../../../../../utils/get-model-info";

export const createBotAPIHandler = async (
request: FastifyRequest<CreateBotAPIRequest>,
Expand Down Expand Up @@ -55,19 +56,11 @@ export const createBotAPIHandler = async (
message: `Reach maximum limit of ${maxBotsAllowed} bots per user`,
});
}
const modelInfo = await prisma.dialoqbaseModels.findFirst({
where: {
hide: false,
deleted: false,
OR: [
{
model_id: model,
},
{
model_id: `${model}-dbase`,
},
],
},

const modelInfo = await getModelInfo({
model,
prisma,
type: "chat",
});

if (!modelInfo) {
Expand All @@ -76,19 +69,10 @@ export const createBotAPIHandler = async (
});
}

const embeddingInfo = await prisma.dialoqbaseModels.findFirst({
where: {
OR: [
{
model_id: embedding,
},
{
model_id: `dialoqbase_eb_${embedding}`,
},
],
hide: false,
deleted: false,
},
const embeddingInfo = await getModelInfo({
model: embedding,
prisma,
type: "embedding",
});

if (!embeddingInfo) {
Expand Down

0 comments on commit dedf3c5

Please sign in to comment.