first-update
This commit is contained in:
75
easy-dataset-main/app/api/llm/fetch-models/route.js
Normal file
75
easy-dataset-main/app/api/llm/fetch-models/route.js
Normal file
@@ -0,0 +1,75 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import axios from 'axios';
|
||||
|
||||
// Fetch model list from provider
|
||||
export async function POST(request) {
|
||||
try {
|
||||
const { endpoint, providerId, apiKey } = await request.json();
|
||||
|
||||
if (!endpoint) {
|
||||
return NextResponse.json({ error: 'Missing required parameter: endpoint' }, { status: 400 });
|
||||
}
|
||||
|
||||
let url = endpoint.replace(/\/$/, ''); // Remove trailing slash
|
||||
|
||||
// Handle Ollama endpoint
|
||||
if (providerId === 'ollama') {
|
||||
// Remove possible /v1 or other version suffix
|
||||
url = url.replace(/\/v\d+$/, '');
|
||||
|
||||
// Append /api if missing
|
||||
if (!url.includes('/api')) {
|
||||
url += '/api';
|
||||
}
|
||||
url += '/tags';
|
||||
} else {
|
||||
url += '/models';
|
||||
}
|
||||
|
||||
const headers = {};
|
||||
if (apiKey) {
|
||||
headers.Authorization = `Bearer ${apiKey}`;
|
||||
}
|
||||
|
||||
const response = await axios.get(url, { headers });
|
||||
|
||||
// Format response per provider
|
||||
let formattedModels = [];
|
||||
if (providerId === 'ollama') {
|
||||
// Ollama /api/tags format: { models: [{ name: 'model-name', ... }] }
|
||||
if (response.data.models && Array.isArray(response.data.models)) {
|
||||
formattedModels = response.data.models.map(item => ({
|
||||
modelId: item.name,
|
||||
modelName: item.name,
|
||||
providerId
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
// Default handling (OpenAI-compatible)
|
||||
if (response.data.data && Array.isArray(response.data.data)) {
|
||||
formattedModels = response.data.data.map(item => ({
|
||||
modelId: item.id,
|
||||
modelName: item.id,
|
||||
providerId
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(formattedModels);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch model list:', String(error));
|
||||
|
||||
// Handle known error shapes
|
||||
if (error.response) {
|
||||
if (error.response.status === 401) {
|
||||
return NextResponse.json({ error: 'Invalid API key' }, { status: 401 });
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ error: `Failed to fetch model list: ${error.response.statusText}` },
|
||||
{ status: error.response.status }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: `Failed to fetch model list: ${error.message}` }, { status: 500 });
|
||||
}
|
||||
}
|
||||
39
easy-dataset-main/app/api/llm/model/route.js
Normal file
39
easy-dataset-main/app/api/llm/model/route.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getLlmModelsByProviderId } from '@/lib/db/llm-models';
|
||||
|
||||
// Get LLM models
|
||||
export async function GET(request) {
|
||||
try {
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
let providerId = searchParams.get('providerId');
|
||||
if (!providerId) {
|
||||
return NextResponse.json({ error: 'Invalid parameters' }, { status: 400 });
|
||||
}
|
||||
const models = await getLlmModelsByProviderId(providerId);
|
||||
if (!models) {
|
||||
return NextResponse.json({ error: 'LLM provider not found' }, { status: 404 });
|
||||
}
|
||||
return NextResponse.json(models);
|
||||
} catch (error) {
|
||||
console.error('Database query error:', String(error));
|
||||
return NextResponse.json({ error: 'Database query failed' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// Sync latest model list
|
||||
export async function POST(request) {
|
||||
try {
|
||||
const { newModels, providerId } = await request.json();
|
||||
const models = await getLlmModelsByProviderId(providerId);
|
||||
const existingModelIds = models.map(model => model.modelId);
|
||||
const diffModels = newModels.filter(item => !existingModelIds.includes(item.modelId));
|
||||
if (diffModels.length > 0) {
|
||||
// return NextResponse.json(await createLlmModels(diffModels));
|
||||
return NextResponse.json({ message: 'No new models to insert' }, { status: 200 });
|
||||
} else {
|
||||
return NextResponse.json({ message: 'No new models to insert' }, { status: 200 });
|
||||
}
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Database insert failed' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
26
easy-dataset-main/app/api/llm/ollama/models/route.js
Normal file
26
easy-dataset-main/app/api/llm/ollama/models/route.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
const OllamaClient = require('@/lib/llm/core/providers/ollama');
|
||||
|
||||
// Force dynamic route to prevent static generation
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function GET(request) {
|
||||
try {
|
||||
// Read host and port from query params
|
||||
const { searchParams } = new URL(request.url);
|
||||
const host = searchParams.get('host') || '127.0.0.1';
|
||||
const port = searchParams.get('port') || '11434';
|
||||
|
||||
// Create Ollama API client
|
||||
const ollama = new OllamaClient({
|
||||
endpoint: `http://${host}:${port}/api`
|
||||
});
|
||||
// Fetch model list
|
||||
const models = await ollama.getModels();
|
||||
return NextResponse.json(models);
|
||||
} catch (error) {
|
||||
// console.error('fetch Ollama models error:', error);
|
||||
return NextResponse.json({ error: 'fetch Models failed' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
14
easy-dataset-main/app/api/llm/providers/route.js
Normal file
14
easy-dataset-main/app/api/llm/providers/route.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getLlmProviders } from '@/lib/db/llm-providers';
|
||||
import { sortProvidersByPriority } from '@/lib/util/providerLogo';
|
||||
|
||||
// Get LLM provider data
|
||||
export async function GET() {
|
||||
try {
|
||||
const result = await getLlmProviders();
|
||||
return NextResponse.json(sortProvidersByPriority(result, item => item.id));
|
||||
} catch (error) {
|
||||
console.error('Database query error:', String(error));
|
||||
return NextResponse.json({ error: 'Database query failed' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user