File size: 2,628 Bytes
755dd12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { fetchEventData } from 'fetch-sse';
import { IMessage, Provider, TSearCategory, TSearchMode } from './interface';
const BASE_URL = import.meta.env.MODE === 'development' ? 'http://127.0.0.1:3000' : '';

const SEARCH = '/api/search';
const MODEL = '/api/models';
const LOCAL_MODELS = '/api/local/models';
const CHAT = '/api/chat';
export interface IQueryOptions {
  ctrl?: AbortController
  stream?: boolean
  model?: string | null
  provider?: Provider | null
  engine?: string | null
  locally?: boolean
  system?: string
  mode?: TSearchMode
  language?: string
  categories?: TSearCategory[]
  reload?: boolean
  onMessage: (data: Record<string, any>) => void
  onOpen?: () => void
  onClose?: () => void
  onError?: (e: any) => void
}
export async function search(q: string, options: IQueryOptions) {
  const { ctrl, stream = true, model, provider, engine, reload = false, mode, categories, locally, language, onMessage, onOpen, onClose, onError } = options;
  const query = new URLSearchParams({
    q
  });
  const url = `${BASE_URL}${SEARCH}?${query.toString()}`;
  await fetchEventData(url, {
    method: 'POST',
    signal: ctrl?.signal,
    data: {
      stream,
      model,
      provider,
      mode,
      language,
      categories,
      engine,
      locally,
      reload
    },
    headers: {
      'Content-Type': 'application/json'
    },
    onOpen: async () => {
      onOpen?.();
    },
    onMessage: (e) => {
      console.log('[search]', e);
      try {
        if (e?.data) {
          const data = JSON.parse(e.data);
          onMessage(JSON.parse(data.data || '{}'));
        }
      } catch (err) {
        onError?.(err);
      }
    },
    onClose,
    onError
  });
}

export async function chat(messages: IMessage[], options: IQueryOptions) {
  const url = `${BASE_URL}${CHAT}`;
  const { ctrl, model, locally, system, onMessage, onError } = options;
  await fetchEventData(url, {
    method: 'POST',
    signal: ctrl?.signal,
    data: {
      model,
      system,
      locally,
      messages
    },
    headers: {
      'Content-Type': 'application/json'
    },
    onMessage: (e) => {
      try {
        if (e?.data) {
          const data = JSON.parse(e.data);
          console.log('[chat]', data);
          onMessage(data);
        }
      } catch (err) {
        onError?.(err);
      }
    },
  });
}

export async function getModels() {
  const res = await fetch(`${BASE_URL}${MODEL}`);
  return res.json();
}

export async function getLocalModels(provider: Provider) {
  const res = await fetch(`${BASE_URL}${LOCAL_MODELS}?provider=${provider}`);
  return res.json();
}