File size: 19,076 Bytes
e28221f
6e2fad5
bc384a3
e28221f
bc384a3
1ff3bbb
e28221f
6e2fad5
a1a3cef
40ba0ea
97b108f
6e2fad5
2da6968
3a09006
489b65b
deca16d
 
06e3150
97b108f
 
40ba0ea
3a09006
489b65b
97b108f
3125c87
06e3150
3125c87
3a09006
b4970eb
87bcf8e
3a09006
844397e
 
 
 
 
 
 
 
1ff3bbb
844397e
1ff3bbb
844397e
 
 
 
 
 
 
 
827aa76
6f4d127
844397e
6f4d127
1ff3bbb
 
 
 
3a09006
 
 
 
deca16d
3a09006
deca16d
3a09006
 
ecec9fc
827aa76
 
 
844397e
 
ecec9fc
b4970eb
 
 
3a09006
 
40ba0ea
3a09006
55b4f54
6072755
55b4f54
 
2da6968
97b108f
2da6968
8ab8ca6
2da6968
8ab8ca6
97b108f
 
8ab8ca6
97b108f
 
 
 
 
 
 
 
 
 
 
 
 
 
2da6968
3a09006
 
214fb7b
 
3a09006
 
 
 
 
a54e7a6
e2b245b
3a09006
 
403b8cf
 
 
 
a54e7a6
1b9f698
3a09006
 
e2b245b
 
 
 
3a09006
 
 
 
 
2da6968
 
 
97b108f
9dffcaf
765a3f6
97b108f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162773e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac2d561
 
 
 
c70c206
 
 
 
 
ac2d561
 
162773e
 
 
 
 
ee910d2
 
 
 
 
 
 
 
 
e37b4b3
ee910d2
 
9a9e4a4
ee910d2
 
f9ac435
 
 
 
 
 
 
 
28bce37
 
f9ac435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee910d2
 
 
 
 
765a3f6
ee910d2
 
 
 
 
 
 
 
 
 
 
 
f9ac435
ee910d2
 
f9ac435
4424f88
f9ac435
 
 
 
 
28bce37
 
 
 
 
ee910d2
 
 
 
1fcd6c7
ee910d2
 
 
 
4070db8
6fe3f86
 
 
 
 
 
 
 
 
 
 
4070db8
ee910d2
 
 
e68ef65
63e4b77
 
a4e2086
c83c114
198a4f7
63e4b77
ee910d2
 
 
 
 
 
 
3a09006
ecec9fc
 
bd6a555
87bcf8e
bd6a555
 
 
ecec9fc
 
 
 
33e0d19
fb1e6a9
 
 
87bcf8e
844397e
fb1e6a9
 
 
827aa76
fb1e6a9
 
 
 
 
 
 
bd6a555
ecec9fc
 
 
844397e
827aa76
ecec9fc
 
b4970eb
 
 
 
 
bb2a1eb
b4970eb
bb2a1eb
b4970eb
bb2a1eb
 
 
b4970eb
bb2a1eb
b4970eb
bb2a1eb
b4970eb
 
 
bb2a1eb
ecec9fc
 
6e2fad5
 
 
 
 
 
 
 
 
3a09006
245d9fd
a2d3414
 
 
 
 
3a09006
 
 
06a233d
3a09006
 
b4970eb
 
 
 
 
 
3a09006
 
ee910d2
06a233d
3a09006
ecec9fc
d98f847
 
ee910d2
d98f847
ee910d2
d98f847
 
 
ee910d2
d98f847
162773e
fb1e6a9
 
 
 
 
 
 
 
 
 
 
 
 
1fe16ba
 
 
 
feb8c06
55b4f54
1fe16ba
6e2fad5
 
 
 
 
 
3a09006
 
e28221f
 
 
 
 
 
deca16d
e28221f
deca16d
 
e28221f
 
 
 
 
deca16d
 
e28221f
 
 
 
 
 
 
 
 
 
 
 
 
3a09006
 
 
e28221f
 
deca16d
e28221f
deca16d
e28221f
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
import argparse
import markdown2
import os
import sys
import uvicorn
import requests

from pathlib import Path
from typing import Union, Optional

from fastapi import FastAPI, Depends, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel, Field
from sse_starlette.sse import EventSourceResponse, ServerSentEvent
from tclogger import logger

from constants.models import AVAILABLE_MODELS_DICTS, PRO_MODELS
from constants.envs import CONFIG, SECRETS
from networks.exceptions import HfApiException, INVALID_API_KEY_ERROR

from messagers.message_composer import MessageComposer
from mocks.stream_chat_mocker import stream_chat_mock

from networks.huggingface_streamer import HuggingfaceStreamer
from networks.huggingchat_streamer import HuggingchatStreamer
from networks.openai_streamer import OpenaiStreamer

from sentence_transformers import SentenceTransformer, CrossEncoder
import tiktoken

class EmbeddingsAPIInference:
    def __init__(self, model_name):
        self.model_name=model_name
    def encode(self, x:str, api_key=None):
        if api_key:
            headers = {"Authorization": f"Bearer {api_key}"}
        else:
            headers = None
                
        API_URL = "https://api-inference.huggingface.co/models/"+self.model_name
        
        payload = {
            "inputs": x,
            "options":{"wait_for_model":True}
        }
        
        return requests.post(API_URL, headers=headers, json=payload).json()


class SentenceTransformerLocal(SentenceTransformer):
    def encode(self, *args, **kwargs):
        kwargs.pop("api_key", None)
        return super().encode(*args, **kwargs).tolist()
        
        
        

class ChatAPIApp:
    def __init__(self):
        self.app = FastAPI(
            docs_url="/",
            title=CONFIG["app_name"],
            swagger_ui_parameters={"defaultModelsExpandDepth": -1},
            version=CONFIG["version"],
        )
        self.setup_routes()
        self.embeddings = {
            "mxbai-embed-large":SentenceTransformerLocal("mixedbread-ai/mxbai-embed-large-v1"),
            "nomic-embed-text": SentenceTransformerLocal("nomic-ai/nomic-embed-text-v1.5", trust_remote_code=True),
            "multilingual-e5-large-instruct":SentenceTransformerLocal("intfloat/multilingual-e5-large-instruct"),
            "intfloat/multilingual-e5-large-instruct":EmbeddingsAPIInference("intfloat/multilingual-e5-large-instruct"),
            "mixedbread-ai/mxbai-embed-large-v1":EmbeddingsAPIInference("mixedbread-ai/mxbai-embed-large-v1")
        }
        self.rerank = {
            "bge-reranker-v2-m3":CrossEncoder("BAAI/bge-reranker-v2-m3")
        }

    def get_available_models(self):
        return {"object": "list", "data": AVAILABLE_MODELS_DICTS}

    def get_available_models_ollama(self):
        ollama_models_dict = [{"name" if k == "id" else k:v for k,v in d.items()} for d in AVAILABLE_MODELS_DICTS.copy()]
        return {"object": "list", "models":ollama_models_dict}

    def extract_api_key(
        credentials: HTTPAuthorizationCredentials = Depends(HTTPBearer()),
    ):
        api_key = None
        if credentials:
            api_key = credentials.credentials
        env_api_key = SECRETS["HF_LLM_API_KEY"]
        return api_key

    def auth_api_key(self, api_key: str):
        env_api_key = SECRETS["HF_LLM_API_KEY"]

        # require no api_key
        if not env_api_key:
            return None
        # user provides HF_TOKEN
        if api_key and api_key.startswith("hf_"):
            return api_key
        # user provides correct API_KEY
        if str(api_key) == str(env_api_key):
            return None

        raise INVALID_API_KEY_ERROR

    class ChatCompletionsPostItem(BaseModel):
        model: str = Field(
            default="nous-mixtral-8x7b",
            description="(str) `nous-mixtral-8x7b`",
        )
        messages: list = Field(
            default=[{"role": "user", "content": "Hello, who are you?"}],
            description="(list) Messages",
        )
        temperature: Union[float, None] = Field(
            default=0.5,
            description="(float) Temperature",
        )
        top_p: Union[float, None] = Field(
            default=0.95,
            description="(float) top p",
        )
        max_tokens: Union[int, None] = Field(
            default=-1,
            description="(int) Max tokens",
        )
        use_cache: bool = Field(
            default=False,
            description="(bool) Use cache",
        )
        stream: bool = Field(
            default=True,
            description="(bool) Stream",
        )

    def chat_completions(
        self, item: ChatCompletionsPostItem, api_key: str = Depends(extract_api_key)
    ):
        try:
            print(item.messages)
            item.model = "llama3-8b" if item.model == "llama3" else item.model
            api_key = self.auth_api_key(api_key)

            if item.model == "gpt-3.5-turbo":
                streamer = OpenaiStreamer()
                stream_response = streamer.chat_response(messages=item.messages)
            elif item.model in PRO_MODELS:
                streamer = HuggingchatStreamer(model=item.model)
                stream_response = streamer.chat_response(
                    messages=item.messages,
                )
            else:
                streamer = HuggingfaceStreamer(model=item.model)
                composer = MessageComposer(model=item.model)
                composer.merge(messages=item.messages)
                stream_response = streamer.chat_response(
                    prompt=composer.merged_str,
                    temperature=item.temperature,
                    top_p=item.top_p,
                    max_new_tokens=item.max_tokens,
                    api_key=api_key,
                    use_cache=item.use_cache,
                )

            if item.stream:
                event_source_response = EventSourceResponse(
                    streamer.chat_return_generator(stream_response),
                    media_type="text/event-stream",
                    ping=2000,
                    ping_message_factory=lambda: ServerSentEvent(**{"comment": ""}),
                )
                return event_source_response
            else:
                data_response = streamer.chat_return_dict(stream_response)
                return data_response
        except HfApiException as e:
            raise HTTPException(status_code=e.status_code, detail=e.detail)
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))

    def chat_completions_ollama(
        self, item: ChatCompletionsPostItem, api_key: str = Depends(extract_api_key)
    ):
        try:
            print(item.messages)
            item.model = "llama3-8b" if item.model == "llama3" else item.model
            api_key = self.auth_api_key(api_key)

            if item.model == "gpt-3.5-turbo":
                streamer = OpenaiStreamer()
                stream_response = streamer.chat_response(messages=item.messages)
            elif item.model in PRO_MODELS:
                streamer = HuggingchatStreamer(model=item.model)
                stream_response = streamer.chat_response(
                    messages=item.messages,
                )
            else:
                streamer = HuggingfaceStreamer(model=item.model)
                composer = MessageComposer(model=item.model)
                composer.merge(messages=item.messages)
                stream_response = streamer.chat_response(
                    prompt=composer.merged_str,
                    temperature=item.temperature,
                    top_p=item.top_p,
                    max_new_tokens=item.max_tokens,
                    api_key=api_key,
                    use_cache=item.use_cache,
                )

            data_response = streamer.chat_return_dict(stream_response)
            print(data_response)
            data_response = {
              "model": data_response.get('model'),
              "created_at": data_response.get('created'),
              "message": {
                "role": "assistant",
                "content": data_response["choices"][0]["message"]["content"],
               },
              # "response": data_response["choices"][0]["message"]["content"],
              "done": True,
            }
            return data_response
        except HfApiException as e:
            raise HTTPException(status_code=e.status_code, detail=e.detail)
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))
            

    class GenerateRequest(BaseModel):
        model: str = Field(
            default="nous-mixtral-8x7b",
            description="(str) `nous-mixtral-8x7b`",
        )
        prompt: str = Field(
            default="Hello, who are you?",
            description="(str) Prompt",
        )
        stream: bool = Field(
            default=False,
            description="(bool) Stream",
        )
        options: dict = Field(
            default={
                "temperature":0.6,
                "top_p":0.9,
                "max_tokens":-1,
                "use_cache":False
            },
            description="(dict) Options"
        )
        
        # temperature: Union[float, None] = Field(
        #     default=0.5,
        #     description="(float) Temperature",
        # )
        # top_p: Union[float, None] = Field(
        #     default=0.95,
        #     description="(float) top p",
        # )
        # max_tokens: Union[int, None] = Field(
        #     default=-1,
        #     description="(int) Max tokens",
        # )
        # use_cache: bool = Field(
        #     default=False,
        #     description="(bool) Use cache",
        # )
        

    def generate_text(
        self, item: GenerateRequest, api_key: str = Depends(extract_api_key)
    ):
        try:
            item.model = "llama3-8b" if item.model == "llama3" else item.model
            api_key = self.auth_api_key(api_key)

            if item.model == "gpt-3.5-turbo":
                streamer = OpenaiStreamer()
                stream_response = streamer.chat_response(messages=[{"user":item.prompt}])
            elif item.model in PRO_MODELS:
                streamer = HuggingchatStreamer(model=item.model)
                stream_response = streamer.chat_response(
                    messages=[{"user":item.prompt}],
                )
            else:
                streamer = HuggingfaceStreamer(model=item.model)
                options = {k:v for k,v in item.options.items() if v is not None}
                stream_response = streamer.chat_response(
                    prompt=item.prompt,
                    **options,
                    api_key=api_key,
                    # temperature=item.temperature,
                    # top_p=item.top_p,
                    # max_new_tokens=item.max_tokens,
                    # api_key=api_key,
                    # use_cache=item.use_cache,
                    # temperature=item.options.get('temperature', 0.6),
                    # top_p=item.options.get('top_p', 0.95),
                    # max_new_tokens=item.options.get('max_new_tokens', -1),
                    # api_key=api_key,
                    # use_cache=item.options.get('use_cache', False),
                )

            if item.stream:
                event_source_response = EventSourceResponse(
                    streamer.ollama_return_generator(stream_response),
                    media_type="text/event-stream",
                    ping=2000,
                    ping_message_factory=lambda: ServerSentEvent(**{"comment": ""}),
                )

                # import json
                # print(event_source_response, "EVENT RESPONSE FIRST")
                # event_source_response = json.loads(str(event_source_response).split('data: ')[-1])
                # print(event_source_response, "EVENT RESPONSE SECOND")
                # event_source_response = {
                #   "model": event_source_response.get('model'),
                #   "created_at": event_source_response.get('created_at'),
                #   "response": event_source_response.get('choices')[-1].get('delta').get('content'),
                #   "done": True if event_source_response.get('choices')[-1].get('finish_reason') != None else False,
                # }
                # print(event_source_response, "EVENT RESPONSE THIRD")
                
                return event_source_response
            else:
                data_response = streamer.chat_return_dict(stream_response)
                print(data_response)
                data_response = {
                  "model": data_response.get('model'),
                  "created_at": data_response.get('created'),
                  "response": data_response["choices"][0]["message"]["content"],
                  "done": True,
                }
                return data_response
        except HfApiException as e:
            raise HTTPException(status_code=e.status_code, detail=e.detail)
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))

    


    class EmbeddingRequest(BaseModel):
        model: str
        input: list
        options: Optional[dict] = None

    class OllamaEmbeddingRequest(BaseModel):
        model: str
        prompt: str
        options: Optional[dict] = None

    def get_embeddings(self, request: EmbeddingRequest, api_key: str = Depends(extract_api_key)):
        try:
            model = request.model
            model_kwargs = request.options
            encoding = tiktoken.get_encoding("cl100k_base")
            embeddings = self.embeddings[model].encode([encoding.decode(inp) for inp in request.input], api_key=api_key)#, **model_kwargs)
            return {
                "object":"list", 
                "data":[
                    {"object": "embedding", "index": i, "embedding": emb} for i,emb in enumerate(embeddings)#.tolist())
                ], 
                "model": model, 
                "usage":{},
            }
        except ValueError as e:
            raise HTTPException(status_code=400, detail=str(e))

    def get_embeddings_ollama(self, request: OllamaEmbeddingRequest, api_key: str = Depends(extract_api_key)):
        try:
            model = request.model
            model_kwargs = request.options
            embeddings = self.embeddings[model].encode(request.prompt, api_key=api_key)#, **model_kwargs)
            return {"embedding": embeddings}#.tolist()}
        except ValueError as e:
            raise HTTPException(status_code=400, detail=str(e))


    class RerankRequest(BaseModel):
        model: str
        input: str
        documents: list
        return_documents: bool
        top_k: Optional[int]=None

    def _score_to_list(self, x):
        x['score'] = x['score'].tolist()
        return x
    def get_rerank(self, request: RerankRequest, api_key: str = Depends(extract_api_key)):
        ranks = self.rerank[request.model].rank(
            request.input, 
            request.documents, 
            top_k=request.top_k, 
            return_documents=request.return_documents
        )
        return [self._score_to_list(x) for x in ranks]
        

    def get_readme(self):
        readme_path = Path(__file__).parents[1] / "README.md"
        with open(readme_path, "r", encoding="utf-8") as rf:
            readme_str = rf.read()
        readme_html = markdown2.markdown(
            readme_str, extras=["table", "fenced-code-blocks", "highlightjs-lang"]
        )
        return readme_html

    def setup_routes(self):
        for prefix in ["", "/v1", "/api", "/api/v1"]:
            if prefix in ["/api/v1"]:
                include_in_schema = True
            else:
                include_in_schema = False

            self.app.get(
                prefix + "/models",
                summary="Get available models",
                include_in_schema=include_in_schema,
            )(self.get_available_models)

            self.app.post(
                prefix+"/rerank",
                summary="Rerank documents",
                include_in_schema=include_in_schema,
            )(self.get_rerank)

            self.app.post(
                prefix + "/chat/completions",
                summary="OpenAI Chat completions in conversation session",
                include_in_schema=include_in_schema,
            )(self.chat_completions)

            self.app.post(
                prefix + "/generate",
                summary="Ollama text generation",
                include_in_schema=include_in_schema,
            )(self.generate_text)

            self.app.post(
                prefix + "/chat",
                summary="Ollama Chat completions in conversation session",
                include_in_schema=include_in_schema,
            )(self.chat_completions_ollama)

            if prefix in ["/api"]:
                self.app.post(
                    prefix + "/embeddings",
                    summary="Ollama Get Embeddings with prompt",
                    include_in_schema=True,
                )(self.get_embeddings_ollama)
            else:
                self.app.post(
                    prefix + "/embeddings",
                    summary="Get Embeddings with prompt",
                    include_in_schema=include_in_schema,
                )(self.get_embeddings)

        self.app.get(
            "/api/tags",
            summary="Get Available Models Ollama",
            include_in_schema=True,
        )(self.get_available_models_ollama)
        
        self.app.get(
            "/readme",
            summary="README of HF LLM API",
            response_class=HTMLResponse,
            include_in_schema=False,
        )(self.get_readme)


class ArgParser(argparse.ArgumentParser):
    def __init__(self, *args, **kwargs):
        super(ArgParser, self).__init__(*args, **kwargs)

        self.add_argument(
            "-s",
            "--host",
            type=str,
            default=CONFIG["host"],
            help=f"Host for {CONFIG['app_name']}",
        )
        self.add_argument(
            "-p",
            "--port",
            type=int,
            default=CONFIG["port"],
            help=f"Port for {CONFIG['app_name']}",
        )

        self.add_argument(
            "-d",
            "--dev",
            default=False,
            action="store_true",
            help="Run in dev mode",
        )

        self.args = self.parse_args(sys.argv[1:])


app = ChatAPIApp().app

if __name__ == "__main__":
    args = ArgParser().args
    if args.dev:
        uvicorn.run("__main__:app", host=args.host, port=args.port, reload=True)
    else:
        uvicorn.run("__main__:app", host=args.host, port=args.port, reload=False)

    # python -m apis.chat_api      # [Docker] on product mode
    # python -m apis.chat_api -d   # [Dev]    on develop mode