Skip to content

Commit

Permalink
add interpreter to graph (infiniflow#1347)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

infiniflow#918 

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
  • Loading branch information
KevinHuSh authored Jul 3, 2024
1 parent 760c558 commit dd9925c
Show file tree
Hide file tree
Showing 9 changed files with 870 additions and 8 deletions.
12 changes: 6 additions & 6 deletions api/apps/canvas_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from flask import request, Response
from flask_login import login_required, current_user

from api.db.db_models import UserCanvas
from api.db.services.canvas_service import CanvasTemplateService, UserCanvasService
from api.utils import get_uuid
from api.utils.api_utils import get_json_result, server_error_response, validate_request
Expand All @@ -34,8 +35,9 @@ def templates():
@manager.route('/list', methods=['GET'])
@login_required
def canvas_list():

return get_json_result(data=[c.to_dict() for c in UserCanvasService.query(user_id=current_user.id)])
return get_json_result(data=sorted([c.to_dict() for c in \
UserCanvasService.query(user_id=current_user.id)], key=lambda x: x["update_time"])
)


@manager.route('/rm', methods=['POST'])
Expand All @@ -53,7 +55,7 @@ def rm():
def save():
req = request.json
req["user_id"] = current_user.id
if not isinstance(req["dsl"], str):req["dsl"] = json.dumps(req["dsl"], ensure_ascii=False)
if not isinstance(req["dsl"], str): req["dsl"] = json.dumps(req["dsl"], ensure_ascii=False)

req["dsl"] = json.loads(req["dsl"])
if "id" not in req:
Expand Down Expand Up @@ -111,7 +113,7 @@ def sse():
for k in ans.keys():
final_ans[k] = ans[k]
ans = {"answer": ans["content"], "reference": ans.get("reference", [])}
yield "data:" + json.dumps({"retcode": 0, "retmsg": "", "data": ans}, ensure_ascii=False) +"\n\n"
yield "data:" + json.dumps({"retcode": 0, "retmsg": "", "data": ans}, ensure_ascii=False) + "\n\n"

canvas.messages.append({"role": "assistant", "content": final_ans["content"]})
if "reference" in final_ans:
Expand Down Expand Up @@ -153,5 +155,3 @@ def reset():
return get_json_result(data=req["dsl"])
except Exception as e:
return server_error_response(e)


5 changes: 5 additions & 0 deletions api/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ class FileSource(StrEnum):
KNOWLEDGEBASE = "knowledgebase"
S3 = "s3"


class CanvasType(StrEnum):
ChatBot = "chatbot"
DocBot = "docbot"

KNOWLEDGEBASE_FOLDER_NAME=".knowledgebase"
18 changes: 18 additions & 0 deletions api/db/init_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import json
import os
import time
import uuid
Expand All @@ -21,11 +22,13 @@
from api.db import LLMType, UserTenantRole
from api.db.db_models import init_database_tables as init_web_db, LLMFactories, LLM, TenantLLM
from api.db.services import UserService
from api.db.services.canvas_service import CanvasTemplateService
from api.db.services.document_service import DocumentService
from api.db.services.knowledgebase_service import KnowledgebaseService
from api.db.services.llm_service import LLMFactoriesService, LLMService, TenantLLMService, LLMBundle
from api.db.services.user_service import TenantService, UserTenantService
from api.settings import CHAT_MDL, EMBEDDING_MDL, ASR_MDL, IMAGE2TEXT_MDL, PARSERS, LLM_FACTORY, API_KEY, LLM_BASE_URL
from api.utils.file_utils import get_project_base_directory


def init_superuser():
Expand Down Expand Up @@ -694,13 +697,28 @@ def init_llm_factory():
"""


def add_graph_templates():
dir = os.path.join(get_project_base_directory(), "graph", "templates")
for fnm in os.listdir(dir):
try:
cnvs = json.load(open(os.path.join(dir, fnm), "r"))
try:
CanvasTemplateService.save(**cnvs)
except:
CanvasTemplateService.update_by_id(cnvs["id"], cnvs)
except Exception as e:
print("Add graph templates error: ", e)
print("------------", flush=True)


def init_web_data():
start_time = time.time()

init_llm_factory()
if not UserService.get_all().count():
init_superuser()

add_graph_templates()
print("init web data success:{}".format(time.time() - start_time))


Expand Down
Loading

0 comments on commit dd9925c

Please sign in to comment.