File size: 1,448 Bytes
bbef364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { OLLAMA_HOST } from '@/utils/app/const';

import { OllamaModel, OllamaModelID, OllamaModels } from '@/types/ollama';

export const config = {
  runtime: 'edge',
};

const handler = async (req: Request): Promise<Response> => {
  try {
    let url = `${OLLAMA_HOST}/api/tags`;

    const response = await fetch(url, {
      headers: {
        'Content-Type': 'application/json',
      },
    });

    if (response.status === 401) {
      return new Response(response.body, {
        status: 500,
        headers: response.headers,
      });
    } else if (response.status !== 200) {
      console.error(
        `Ollama API returned an error ${
          response.status
        }: ${await response.text()}`,
      );
      throw new Error('Ollama API returned an error');
    }

    const json = await response.json();

    const models: OllamaModel[] = json.models
      .map((model: any) => {
        const model_name = model.name;
        for (const [key, value] of Object.entries(OllamaModelID)) {
          {
            return {
              id: model.name,
              name: model.name,
              modified_at: model.modified_at,
              size: model.size,
            };
          }
        }
      })
      .filter(Boolean);

    return new Response(JSON.stringify(models), { status: 200 });
  } catch (error) {
    console.error(error);
    return new Response('Error', { status: 500 });
  }
};

export default handler;