Skip to content
Snippets Groups Projects
llm_app.py 4.85 KiB
Newer Older
  • Learn to ignore specific revisions
  • KevinHuSh's avatar
    KevinHuSh committed
    #
    
    #  Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
    
    KevinHuSh's avatar
    KevinHuSh committed
    #
    #  Licensed under the Apache License, Version 2.0 (the "License");
    #  you may not use this file except in compliance with the License.
    #  You may obtain a copy of the License at
    #
    #      http://www.apache.org/licenses/LICENSE-2.0
    #
    #  Unless required by applicable law or agreed to in writing, software
    #  distributed under the License is distributed on an "AS IS" BASIS,
    #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    #  See the License for the specific language governing permissions and
    #  limitations under the License.
    #
    from flask import request
    from flask_login import login_required, current_user
    
    KevinHuSh's avatar
    KevinHuSh committed
    from api.db.services.llm_service import LLMFactoriesService, TenantLLMService, LLMService
    from api.utils.api_utils import server_error_response, get_data_error_result, validate_request
    
    KevinHuSh's avatar
    KevinHuSh committed
    from api.db import StatusEnum, LLMType
    from api.db.db_models import TenantLLM
    
    KevinHuSh's avatar
    KevinHuSh committed
    from api.utils.api_utils import get_json_result
    
    KevinHuSh's avatar
    KevinHuSh committed
    from rag.llm import EmbeddingModel, ChatModel
    
    KevinHuSh's avatar
    KevinHuSh committed
    
    
    @manager.route('/factories', methods=['GET'])
    @login_required
    def factories():
        try:
            fac = LLMFactoriesService.get_all()
    
            return get_json_result(data=[f.to_dict() for f in fac])
    
    KevinHuSh's avatar
    KevinHuSh committed
        except Exception as e:
            return server_error_response(e)
    
    
    @manager.route('/set_api_key', methods=['POST'])
    @login_required
    @validate_request("llm_factory", "api_key")
    def set_api_key():
        req = request.json
    
        chat_passed = False
        factory = req["llm_factory"]
    
        for llm in LLMService.query(fid=factory):
    
            if llm.model_type == LLMType.EMBEDDING.value:
    
                mdl = EmbeddingModel[factory](
    
                    req["api_key"], llm.llm_name)
                try:
                    arr, tc = mdl.encode(["Test if the api key is available"])
    
                    if len(arr[0]) == 0 or tc == 0:
                        raise Exception("Fail")
    
    KevinHuSh's avatar
    KevinHuSh committed
                    msg += f"\nFail to access embedding model({llm.llm_name}) using this api key." + str(e)
    
            elif not chat_passed and llm.model_type == LLMType.CHAT.value:
                mdl = ChatModel[factory](
    
                    req["api_key"], llm.llm_name)
                try:
    
                    m, tc = mdl.chat(None, [{"role": "user", "content": "Hello! How are you doing!"}], {
                                     "temperature": 0.9})
                    if not tc:
                        raise Exception(m)
    
                    chat_passed = True
    
                    msg += f"\nFail to access model({llm.llm_name}) using this api key." + str(
                        e)
    
        if msg:
            return get_data_error_result(retmsg=msg)
    
    KevinHuSh's avatar
    KevinHuSh committed
        llm = {
            "api_key": req["api_key"]
        }
        for n in ["model_type", "llm_name"]:
    
            if n in req:
                llm[n] = req[n]
    
    KevinHuSh's avatar
    KevinHuSh committed
    
    
        if not TenantLLMService.filter_update(
                [TenantLLM.tenant_id == current_user.id, TenantLLM.llm_factory == factory], llm):
    
            for llm in LLMService.query(fid=factory):
    
                TenantLLMService.save(
                    tenant_id=current_user.id,
                    llm_factory=factory,
                    llm_name=llm.llm_name,
                    model_type=llm.model_type,
                    api_key=req["api_key"])
    
    KevinHuSh's avatar
    KevinHuSh committed
        return get_json_result(data=True)
    
    
    @manager.route('/my_llms', methods=['GET'])
    @login_required
    def my_llms():
        try:
    
    KevinHuSh's avatar
    KevinHuSh committed
            res = {}
            for o in TenantLLMService.get_my_llms(current_user.id):
                if o["llm_factory"] not in res:
                    res[o["llm_factory"]] = {
                        "tags": o["tags"],
                        "llm": []
                    }
                res[o["llm_factory"]]["llm"].append({
                    "type": o["model_type"],
    
                    "name": o["llm_name"],
    
    KevinHuSh's avatar
    KevinHuSh committed
                    "used_token": o["used_tokens"]
                })
            return get_json_result(data=res)
    
    KevinHuSh's avatar
    KevinHuSh committed
        except Exception as e:
            return server_error_response(e)
    
    
    @manager.route('/list', methods=['GET'])
    @login_required
    def list():
    
        model_type = request.args.get("model_type")
    
    KevinHuSh's avatar
    KevinHuSh committed
        try:
            objs = TenantLLMService.query(tenant_id=current_user.id)
    
            facts = set([o.to_dict()["llm_factory"] for o in objs if o.api_key])
    
    KevinHuSh's avatar
    KevinHuSh committed
            llms = LLMService.get_all()
    
            llms = [m.to_dict()
                    for m in llms if m.status == StatusEnum.VALID.value]
    
    KevinHuSh's avatar
    KevinHuSh committed
            for m in llms:
    
    KevinHuSh's avatar
    KevinHuSh committed
                m["available"] = m["fid"] in facts or m["llm_name"].lower() == "flag-embedding"
    
    KevinHuSh's avatar
    KevinHuSh committed
            res = {}
            for m in llms:
    
                if model_type and m["model_type"] != model_type:
                    continue
                if m["fid"] not in res:
                    res[m["fid"]] = []
    
    KevinHuSh's avatar
    KevinHuSh committed
                res[m["fid"]].append(m)
    
            return get_json_result(data=res)
        except Exception as e:
    
    KevinHuSh's avatar
    KevinHuSh committed
            return server_error_response(e)