fix(ai): ollama fetch models

This commit is contained in:
Vyacheslav Scherbinin
2025-08-19 23:56:54 +07:00
parent 72fca80047
commit 07c23292da
4 changed files with 28 additions and 8 deletions
@@ -38,7 +38,7 @@ import { api } from "@/utils/api";
const Schema = z.object({ const Schema = z.object({
name: z.string().min(1, { message: "Name is required" }), name: z.string().min(1, { message: "Name is required" }),
apiUrl: z.string().url({ message: "Please enter a valid URL" }), apiUrl: z.string().url({ message: "Please enter a valid URL" }),
apiKey: z.string().min(1, { message: "API Key is required" }), apiKey: z.string(),
model: z.string().min(1, { message: "Model is required" }), model: z.string().min(1, { message: "Model is required" }),
isEnabled: z.boolean(), isEnabled: z.boolean(),
}); });
@@ -53,6 +53,7 @@ export const HandleAi = ({ aiId }: Props) => {
const utils = api.useUtils(); const utils = api.useUtils();
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [fetchModels, setFetchModels] = useState(false);
const { data, refetch } = api.ai.one.useQuery( const { data, refetch } = api.ai.one.useQuery(
{ {
aiId: aiId || "", aiId: aiId || "",
@@ -71,7 +72,7 @@ export const HandleAi = ({ aiId }: Props) => {
name: "", name: "",
apiUrl: "", apiUrl: "",
apiKey: "", apiKey: "",
model: "gpt-3.5-turbo", model: "",
isEnabled: true, isEnabled: true,
}, },
}); });
@@ -81,7 +82,7 @@ export const HandleAi = ({ aiId }: Props) => {
name: data?.name ?? "", name: data?.name ?? "",
apiUrl: data?.apiUrl ?? "https://api.openai.com/v1", apiUrl: data?.apiUrl ?? "https://api.openai.com/v1",
apiKey: data?.apiKey ?? "", apiKey: data?.apiKey ?? "",
model: data?.model ?? "gpt-3.5-turbo", model: data?.model ?? "",
isEnabled: data?.isEnabled ?? true, isEnabled: data?.isEnabled ?? true,
}); });
}, [aiId, form, data]); }, [aiId, form, data]);
@@ -96,13 +97,18 @@ export const HandleAi = ({ aiId }: Props) => {
apiKey: apiKey ?? "", apiKey: apiKey ?? "",
}, },
{ {
enabled: !!apiUrl && !!apiKey, enabled: fetchModels,
onError: (error) => { onError: (error) => {
setError(`Failed to fetch models: ${error.message}`); setError(`Failed to fetch models: ${error.message}`);
}, },
}, },
); );
useEffect(() => {
const isOllama = apiUrl.includes(":11434") || apiUrl.includes("ollama");
setFetchModels(!!apiUrl && isOllama || !!apiKey);
}, [apiUrl, apiKey]);
useEffect(() => { useEffect(() => {
const apiUrl = form.watch("apiUrl"); const apiUrl = form.watch("apiUrl");
const apiKey = form.watch("apiKey"); const apiKey = form.watch("apiKey");
+16 -2
View File
@@ -20,6 +20,7 @@ import {
} from "@dokploy/server/services/user"; } from "@dokploy/server/services/user";
import { import {
getProviderHeaders, getProviderHeaders,
getProviderName,
type Model, type Model,
} from "@dokploy/server/utils/ai/select-ai-provider"; } from "@dokploy/server/utils/ai/select-ai-provider";
import { TRPCError } from "@trpc/server"; import { TRPCError } from "@trpc/server";
@@ -47,11 +48,24 @@ export const aiRouter = createTRPCRouter({
}), }),
getModels: protectedProcedure getModels: protectedProcedure
.input(z.object({ apiUrl: z.string().min(1), apiKey: z.string().min(1) })) .input(z.object({ apiUrl: z.string().min(1), apiKey: z.string() }))
.query(async ({ input }) => { .query(async ({ input }) => {
try { try {
const providerName = getProviderName(input.apiUrl);
const headers = getProviderHeaders(input.apiUrl, input.apiKey); const headers = getProviderHeaders(input.apiUrl, input.apiKey);
const response = await fetch(`${input.apiUrl}/models`, { headers }); let response = null;
switch (providerName) {
case "ollama":
response = await fetch(`${input.apiUrl}/api/tags`, { headers });
break;
default:
if (!input.apiKey)
throw new TRPCError({
code: "BAD_REQUEST",
message: "API key must contain at least 1 character(s)",
});
response = await fetch(`${input.apiUrl}/models`, { headers });
}
if (!response.ok) { if (!response.ok) {
const errorText = await response.text(); const errorText = await response.text();
+1 -1
View File
@@ -32,7 +32,7 @@ export const aiRelations = relations(ai, ({ one }) => ({
const createSchema = createInsertSchema(ai, { const createSchema = createInsertSchema(ai, {
name: z.string().min(1, { message: "Name is required" }), name: z.string().min(1, { message: "Name is required" }),
apiUrl: z.string().url({ message: "Please enter a valid URL" }), apiUrl: z.string().url({ message: "Please enter a valid URL" }),
apiKey: z.string().min(1, { message: "API Key is required" }), apiKey: z.string(),
model: z.string().min(1, { message: "Model is required" }), model: z.string().min(1, { message: "Model is required" }),
isEnabled: z.boolean().optional(), isEnabled: z.boolean().optional(),
}); });
@@ -7,7 +7,7 @@ import { createOpenAI } from "@ai-sdk/openai";
import { createOpenAICompatible } from "@ai-sdk/openai-compatible"; import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { createOllama } from "ollama-ai-provider"; import { createOllama } from "ollama-ai-provider";
function getProviderName(apiUrl: string) { export function getProviderName(apiUrl: string) {
if (apiUrl.includes("api.openai.com")) return "openai"; if (apiUrl.includes("api.openai.com")) return "openai";
if (apiUrl.includes("azure.com")) return "azure"; if (apiUrl.includes("azure.com")) return "azure";
if (apiUrl.includes("api.anthropic.com")) return "anthropic"; if (apiUrl.includes("api.anthropic.com")) return "anthropic";