File size: 1,539 Bytes
813eca2
 
 
 
 
 
 
 
89e5382
 
813eca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import _ from 'lodash';

import Request from '@/lib/request/Request.ts';
import Response from '@/lib/response/Response.ts';
import chat from '@/api/controllers/chat.ts';

export default {

    // prefix: '/v1/chat',
    prefix: '/hf/v1/chat',

    post: {

        '/completions': async (request: Request) => {
            request
                .validate('body.conversation_id', v => _.isUndefined(v) || _.isString(v))
                .validate('body.messages', _.isArray)
                .validate('headers.authorization', _.isString)
            // token切分
            const tokens = chat.tokenSplit(request.headers.authorization);
            // 随机挑选一个token
            const token = _.sample(tokens);
            let { model, messages, stream } = request.body;
            if(['deepseek_chat', 'deepseek_code', 'deepseek-chat*', 'deepseek-chat', 'deepseek-coder'].includes(model))
                model = {
                    'deepseek-chat*': 'deepseek_chat',
                    'deepseek-chat': 'deepseek_chat',
                    'deepseek-coder': 'deepseek_code'
                }[model] || model;
            else
                model = 'deepseek_chat';
            if (stream) {
                const stream = await chat.createCompletionStream(model, messages, token);
                return new Response(stream, {
                    type: "text/event-stream"
                });
            }
            else
                return await chat.createCompletion(model, messages, token);
        }

    }

}