KevinHuSh commited on
Commit
6be3dd5
Β·
1 Parent(s): c372afe

rename web_server to api (#29)

Browse files

* add front end code

* change licence

* rename web_server to API

* change name to web_server

Files changed (41) hide show
  1. {web_server β†’ api}/__init__.py +0 -0
  2. {web_server β†’ api}/apps/__init__.py +1 -1
  3. api/apps/chunk_app.py +150 -0
  4. {web_server β†’ api}/apps/document_app.py +3 -4
  5. {web_server β†’ api}/apps/kb_app.py +1 -1
  6. {web_server β†’ api}/apps/llm_app.py +3 -5
  7. {web_server β†’ api}/apps/user_app.py +15 -1
  8. {web_server β†’ api}/db/__init__.py +1 -1
  9. {web_server β†’ api}/db/db_models.py +1 -1
  10. {web_server β†’ api}/db/db_services.py +6 -6
  11. {web_server β†’ api}/db/db_utils.py +1 -1
  12. {web_server β†’ api}/db/init_data.py +1 -1
  13. {web_server β†’ api}/db/operatioins.py +1 -1
  14. {web_server β†’ api}/db/reload_config_base.py +1 -1
  15. {web_server β†’ api}/db/runtime_config.py +1 -1
  16. {web_server β†’ api}/db/services/__init__.py +1 -1
  17. {web_server β†’ api}/db/services/common_service.py +1 -1
  18. {web_server β†’ api}/db/services/dialog_service.py +1 -1
  19. {web_server β†’ api}/db/services/document_service.py +8 -1
  20. {web_server β†’ api}/db/services/kb_service.py +1 -1
  21. {web_server β†’ api}/db/services/knowledgebase_service.py +1 -1
  22. {web_server β†’ api}/db/services/llm_service.py +25 -2
  23. {web_server β†’ api}/db/services/user_service.py +1 -1
  24. {web_server β†’ api}/errors/__init__.py +2 -2
  25. {web_server β†’ api}/errors/error_services.py +2 -2
  26. {web_server β†’ api}/errors/general_error.py +1 -1
  27. {web_server β†’ api}/flask_session/2029240f6d1128be89ddc32729463129 +0 -0
  28. {web_server β†’ api}/hook/__init__.py +0 -0
  29. {web_server β†’ api}/hook/api/client_authentication.py +0 -0
  30. {web_server β†’ api}/hook/api/permission.py +0 -0
  31. {web_server β†’ api}/hook/api/site_authentication.py +0 -0
  32. {web_server β†’ api}/hook/common/parameters.py +0 -0
  33. {web_server β†’ api}/ragflow_server.py +4 -4
  34. {web_server β†’ api}/settings.py +14 -14
  35. {web_server β†’ api}/utils/__init__.py +8 -8
  36. {web_server β†’ api}/utils/api_utils.py +5 -5
  37. {web_server β†’ api}/utils/file_utils.py +10 -10
  38. {web_server β†’ api}/utils/log_utils.py +11 -16
  39. {web_server β†’ api}/utils/t_crypt.py +0 -0
  40. {web_server β†’ api}/versions.py +2 -2
  41. web_server/db/service_registry.py +0 -164
{web_server β†’ api}/__init__.py RENAMED
File without changes
{web_server β†’ api}/apps/__init__.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
api/apps/chunk_app.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ import base64
17
+ import hashlib
18
+ import pathlib
19
+ import re
20
+
21
+ from elasticsearch_dsl import Q
22
+ from flask import request
23
+ from flask_login import login_required, current_user
24
+
25
+ from rag.nlp import search, huqie
26
+ from rag.utils import ELASTICSEARCH, rmSpace
27
+ from web_server.db import LLMType
28
+ from web_server.db.services import duplicate_name
29
+ from web_server.db.services.kb_service import KnowledgebaseService
30
+ from web_server.db.services.llm_service import TenantLLMService
31
+ from web_server.db.services.user_service import UserTenantService
32
+ from web_server.utils.api_utils import server_error_response, get_data_error_result, validate_request
33
+ from web_server.utils import get_uuid
34
+ from web_server.db.services.document_service import DocumentService
35
+ from web_server.settings import RetCode
36
+ from web_server.utils.api_utils import get_json_result
37
+ from rag.utils.minio_conn import MINIO
38
+ from web_server.utils.file_utils import filename_type
39
+
40
+ retrival = search.Dealer(ELASTICSEARCH, None)
41
+
42
+ @manager.route('/list', methods=['POST'])
43
+ @login_required
44
+ @validate_request("doc_id")
45
+ def list():
46
+ req = request.json
47
+ doc_id = req["doc_id"]
48
+ page = req.get("page", 1)
49
+ size = req.get("size", 30)
50
+ question = req.get("keywords", "")
51
+ try:
52
+ tenants = UserTenantService.query(user_id=current_user.id)
53
+ if not tenants:
54
+ return get_data_error_result(retmsg="Tenant not found!")
55
+ res = retrival.search({
56
+ "doc_ids": [doc_id], "page": page, "size": size, "question": question
57
+ }, search.index_name(tenants[0].tenant_id))
58
+ return get_json_result(data=res)
59
+ except Exception as e:
60
+ if str(e).find("not_found") > 0:
61
+ return get_json_result(data=False, retmsg=f'Index not found!',
62
+ retcode=RetCode.DATA_ERROR)
63
+ return server_error_response(e)
64
+
65
+
66
+ @manager.route('/get', methods=['GET'])
67
+ @login_required
68
+ def get():
69
+ chunk_id = request.args["chunk_id"]
70
+ try:
71
+ tenants = UserTenantService.query(user_id=current_user.id)
72
+ if not tenants:
73
+ return get_data_error_result(retmsg="Tenant not found!")
74
+ res = ELASTICSEARCH.get(chunk_id, search.index_name(tenants[0].tenant_id))
75
+ if not res.get("found"):return server_error_response("Chunk not found")
76
+ id = res["_id"]
77
+ res = res["_source"]
78
+ res["chunk_id"] = id
79
+ k = []
80
+ for n in res.keys():
81
+ if re.search(r"(_vec$|_sm_)", n):
82
+ k.append(n)
83
+ if re.search(r"(_tks|_ltks)", n):
84
+ res[n] = rmSpace(res[n])
85
+ for n in k: del res[n]
86
+
87
+ return get_json_result(data=res)
88
+ except Exception as e:
89
+ if str(e).find("NotFoundError") >= 0:
90
+ return get_json_result(data=False, retmsg=f'Chunk not found!',
91
+ retcode=RetCode.DATA_ERROR)
92
+ return server_error_response(e)
93
+
94
+
95
+ @manager.route('/set', methods=['POST'])
96
+ @login_required
97
+ @validate_request("doc_id", "chunk_id", "content_ltks", "important_kwd", "docnm_kwd")
98
+ def set():
99
+ req = request.json
100
+ d = {"id": req["chunk_id"]}
101
+ d["content_ltks"] = huqie.qie(req["content_ltks"])
102
+ d["content_sm_ltks"] = huqie.qieqie(d["content_ltks"])
103
+ d["important_kwd"] = req["important_kwd"]
104
+ d["important_tks"] = huqie.qie(" ".join(req["important_kwd"]))
105
+
106
+ try:
107
+ tenant_id = DocumentService.get_tenant_id(req["doc_id"])
108
+ if not tenant_id: return get_data_error_result(retmsg="Tenant not found!")
109
+ embd_mdl = TenantLLMService.model_instance(tenant_id, LLMType.EMBEDDING.value)
110
+ v, c = embd_mdl.encode([req["docnm_kwd"], req["content_ltks"]])
111
+ v = 0.1 * v[0] + 0.9 * v[1]
112
+ d["q_%d_vec"%len(v)] = v.tolist()
113
+ ELASTICSEARCH.upsert([d], search.index_name(tenant_id))
114
+ return get_json_result(data=True)
115
+ except Exception as e:
116
+ return server_error_response(e)
117
+
118
+
119
+ @manager.route('/create', methods=['POST'])
120
+ @login_required
121
+ @validate_request("doc_id", "content_ltks", "important_kwd")
122
+ def set():
123
+ req = request.json
124
+ md5 = hashlib.md5()
125
+ md5.update((req["content_ltks"] + req["doc_id"]).encode("utf-8"))
126
+ chunck_id = md5.hexdigest()
127
+ d = {"id": chunck_id, "content_ltks": huqie.qie(req["content_ltks"])}
128
+ d["content_sm_ltks"] = huqie.qieqie(d["content_ltks"])
129
+ d["important_kwd"] = req["important_kwd"]
130
+ d["important_tks"] = huqie.qie(" ".join(req["important_kwd"]))
131
+
132
+ try:
133
+ e, doc = DocumentService.get_by_id(req["doc_id"])
134
+ if not e: return get_data_error_result(retmsg="Document not found!")
135
+ d["kb_id"] = [doc.kb_id]
136
+ d["docnm_kwd"] = doc.name
137
+ d["doc_id"] = doc.id
138
+
139
+ tenant_id = DocumentService.get_tenant_id(req["doc_id"])
140
+ if not tenant_id: return get_data_error_result(retmsg="Tenant not found!")
141
+
142
+ embd_mdl = TenantLLMService.model_instance(tenant_id, LLMType.EMBEDDING.value)
143
+ v, c = embd_mdl.encode([doc.name, req["content_ltks"]])
144
+ DocumentService.increment_chunk_num(req["doc_id"], doc.kb_id, c, 1, 0)
145
+ v = 0.1 * v[0] + 0.9 * v[1]
146
+ d["q_%d_vec"%len(v)] = v.tolist()
147
+ ELASTICSEARCH.upsert([d], search.index_name(tenant_id))
148
+ return get_json_result(data={"chunk_id": chunck_id})
149
+ except Exception as e:
150
+ return server_error_response(e)
{web_server β†’ api}/apps/document_app.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -24,10 +24,9 @@ from rag.nlp import search
24
  from rag.utils import ELASTICSEARCH
25
  from web_server.db.services import duplicate_name
26
  from web_server.db.services.kb_service import KnowledgebaseService
27
- from web_server.db.services.user_service import TenantService
28
  from web_server.utils.api_utils import server_error_response, get_data_error_result, validate_request
29
- from web_server.utils import get_uuid, get_format_time
30
- from web_server.db import StatusEnum, FileType
31
  from web_server.db.services.document_service import DocumentService
32
  from web_server.settings import RetCode
33
  from web_server.utils.api_utils import get_json_result
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
24
  from rag.utils import ELASTICSEARCH
25
  from web_server.db.services import duplicate_name
26
  from web_server.db.services.kb_service import KnowledgebaseService
 
27
  from web_server.utils.api_utils import server_error_response, get_data_error_result, validate_request
28
+ from web_server.utils import get_uuid
29
+ from web_server.db import FileType
30
  from web_server.db.services.document_service import DocumentService
31
  from web_server.settings import RetCode
32
  from web_server.utils.api_utils import get_json_result
{web_server β†’ api}/apps/kb_app.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/apps/llm_app.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@ from web_server.utils.api_utils import get_json_result
33
  def factories():
34
  try:
35
  fac = LLMFactoriesService.get_all()
36
- return get_json_result(data=fac.to_json())
37
  except Exception as e:
38
  return server_error_response(e)
39
 
@@ -60,9 +60,7 @@ def set_api_key():
60
  @login_required
61
  def my_llms():
62
  try:
63
- objs = TenantLLMService.query(tenant_id=current_user.id)
64
- objs = [o.to_dict() for o in objs]
65
- for o in objs: del o["api_key"]
66
  return get_json_result(data=objs)
67
  except Exception as e:
68
  return server_error_response(e)
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
33
  def factories():
34
  try:
35
  fac = LLMFactoriesService.get_all()
36
+ return get_json_result(data=[f.to_dict() for f in fac])
37
  except Exception as e:
38
  return server_error_response(e)
39
 
 
60
  @login_required
61
  def my_llms():
62
  try:
63
+ objs = TenantLLMService.get_my_llms(current_user.id)
 
 
64
  return get_json_result(data=objs)
65
  except Exception as e:
66
  return server_error_response(e)
{web_server β†’ api}/apps/user_app.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -252,3 +252,17 @@ def tenant_info():
252
  return get_json_result(data=tenants)
253
  except Exception as e:
254
  return server_error_response(e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
252
  return get_json_result(data=tenants)
253
  except Exception as e:
254
  return server_error_response(e)
255
+
256
+
257
+ @manager.route("/set_tenant_info", methods=["POST"])
258
+ @login_required
259
+ @validate_request("tenant_id", "asr_id", "embd_id", "img2txt_id", "llm_id")
260
+ def set_tenant_info():
261
+ req = request.json
262
+ try:
263
+ tid = req["tenant_id"]
264
+ del req["tenant_id"]
265
+ TenantService.update_by_id(tid, req)
266
+ return get_json_result(data=True)
267
+ except Exception as e:
268
+ return server_error_response(e)
{web_server β†’ api}/db/__init__.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/db_models.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/db_services.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2021 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@ import time
19
  from functools import wraps
20
  from shortuuid import ShortUUID
21
 
22
- from web_server.versions import get_fate_version
23
 
24
  from web_server.errors.error_services import *
25
  from web_server.settings import (
@@ -34,7 +34,7 @@ server_instance = (
34
  json.dumps({
35
  'instance_id': instance_id,
36
  'timestamp': round(time.time() * 1000),
37
- 'version': get_fate_version() or '',
38
  'host': HOST,
39
  'grpc_port': GRPC_PORT,
40
  'http_port': HTTP_PORT,
@@ -68,7 +68,7 @@ class ServicesDB(abc.ABC):
68
  @abc.abstractmethod
69
  def supported_services(self):
70
  """The names of supported services.
71
- The returned list SHOULD contain `fateflow` (model download) and `servings` (FATE-Serving).
72
 
73
  :return: The service names.
74
  :rtype: list
@@ -142,8 +142,8 @@ class ServicesDB(abc.ABC):
142
  @check_service_supported
143
  def get_urls(self, service_name, with_values=False):
144
  """Query service urls from database. The urls may belong to other nodes.
145
- Currently, only `fateflow` (model download) urls and `servings` (FATE-Serving) urls are supported.
146
- `fateflow` is a url containing scheme, host, port and path,
147
  while `servings` only contains host and port.
148
 
149
  :param str service_name: The service name.
 
1
  #
2
+ # Copyright 2021 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
19
  from functools import wraps
20
  from shortuuid import ShortUUID
21
 
22
+ from web_server.versions import get_rag_version
23
 
24
  from web_server.errors.error_services import *
25
  from web_server.settings import (
 
34
  json.dumps({
35
  'instance_id': instance_id,
36
  'timestamp': round(time.time() * 1000),
37
+ 'version': get_rag_version() or '',
38
  'host': HOST,
39
  'grpc_port': GRPC_PORT,
40
  'http_port': HTTP_PORT,
 
68
  @abc.abstractmethod
69
  def supported_services(self):
70
  """The names of supported services.
71
+ The returned list SHOULD contain `ragflow` (model download) and `servings` (RAG-Serving).
72
 
73
  :return: The service names.
74
  :rtype: list
 
142
  @check_service_supported
143
  def get_urls(self, service_name, with_values=False):
144
  """Query service urls from database. The urls may belong to other nodes.
145
+ Currently, only `ragflow` (model download) urls and `servings` (RAG-Serving) urls are supported.
146
+ `ragflow` is a url containing scheme, host, port and path,
147
  while `servings` only contains host and port.
148
 
149
  :param str service_name: The service name.
{web_server β†’ api}/db/db_utils.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/init_data.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/operatioins.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/reload_config_base.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/runtime_config.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/services/__init__.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/services/common_service.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/services/dialog_service.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/services/document_service.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -87,3 +87,10 @@ class DocumentService(CommonService):
87
  num = Knowledgebase.update(token_num=Knowledgebase.token_num+token_num, chunk_num=Knowledgebase.chunk_num+chunk_num).where(Knowledgebase.id==kb_id).execute()
88
  return num
89
 
 
 
 
 
 
 
 
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
87
  num = Knowledgebase.update(token_num=Knowledgebase.token_num+token_num, chunk_num=Knowledgebase.chunk_num+chunk_num).where(Knowledgebase.id==kb_id).execute()
88
  return num
89
 
90
+ @classmethod
91
+ @DB.connection_context()
92
+ def get_tenant_id(cls, doc_id):
93
+ docs = cls.model.select(Knowledgebase.tenant_id).join(Knowledgebase, on=(Knowledgebase.id == cls.model.kb_id)).where(cls.model.id == doc_id, Knowledgebase.status==StatusEnum.VALID.value)
94
+ docs = docs.dicts()
95
+ if not docs:return
96
+ return docs[0]["tenant_id"]
{web_server β†’ api}/db/services/kb_service.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/services/knowledgebase_service.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/db/services/llm_service.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -16,10 +16,11 @@
16
  import peewee
17
  from werkzeug.security import generate_password_hash, check_password_hash
18
 
 
 
19
  from web_server.db.db_models import DB, UserTenant
20
  from web_server.db.db_models import LLMFactories, LLM, TenantLLM
21
  from web_server.db.services.common_service import CommonService
22
- from web_server.utils import get_uuid, get_format_time
23
  from web_server.db.db_utils import StatusEnum
24
 
25
 
@@ -51,3 +52,25 @@ class TenantLLMService(CommonService):
51
  if not objs:return
52
  return objs[0]
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
16
  import peewee
17
  from werkzeug.security import generate_password_hash, check_password_hash
18
 
19
+ from rag.llm import EmbeddingModel, CvModel
20
+ from web_server.db import LLMType
21
  from web_server.db.db_models import DB, UserTenant
22
  from web_server.db.db_models import LLMFactories, LLM, TenantLLM
23
  from web_server.db.services.common_service import CommonService
 
24
  from web_server.db.db_utils import StatusEnum
25
 
26
 
 
52
  if not objs:return
53
  return objs[0]
54
 
55
+ @classmethod
56
+ @DB.connection_context()
57
+ def get_my_llms(cls, tenant_id):
58
+ fields = [cls.model.llm_factory, LLMFactories.logo, LLMFactories.tags, cls.model.model_type, cls.model.llm_name]
59
+ objs = cls.model.select(*fields).join(LLMFactories, on=(cls.model.llm_factory==LLMFactories.name)).where(cls.model.tenant_id==tenant_id).dicts()
60
+
61
+ return list(objs)
62
+
63
+ @classmethod
64
+ @DB.connection_context()
65
+ def model_instance(cls, tenant_id, llm_type):
66
+ model_config = cls.get_api_key(tenant_id, model_type=LLMType.EMBEDDING)
67
+ if not model_config:
68
+ model_config = {"llm_factory": "local", "api_key": "", "llm_name": ""}
69
+ else:
70
+ model_config = model_config[0].to_dict()
71
+ if llm_type == LLMType.EMBEDDING:
72
+ if model_config["llm_factory"] not in EmbeddingModel: return
73
+ return EmbeddingModel[model_config["llm_factory"]](model_config["api_key"], model_config["llm_name"])
74
+ if llm_type == LLMType.IMAGE2TEXT:
75
+ if model_config["llm_factory"] not in CvModel: return
76
+ return CvModel[model_config.llm_factory](model_config["api_key"], model_config["llm_name"])
{web_server β†’ api}/db/services/user_service.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/errors/__init__.py RENAMED
@@ -1,8 +1,8 @@
1
  from .general_error import *
2
 
3
 
4
- class FateFlowError(Exception):
5
- message = 'Unknown Fate Flow Error'
6
 
7
  def __init__(self, message=None, *args, **kwargs):
8
  message = str(message) if message is not None else self.message
 
1
  from .general_error import *
2
 
3
 
4
+ class RagFlowError(Exception):
5
+ message = 'Unknown Rag Flow Error'
6
 
7
  def __init__(self, message=None, *args, **kwargs):
8
  message = str(message) if message is not None else self.message
{web_server β†’ api}/errors/error_services.py RENAMED
@@ -1,10 +1,10 @@
1
- from web_server.errors import FateFlowError
2
 
3
  __all__ = ['ServicesError', 'ServiceNotSupported', 'ZooKeeperNotConfigured',
4
  'MissingZooKeeperUsernameOrPassword', 'ZooKeeperBackendError']
5
 
6
 
7
- class ServicesError(FateFlowError):
8
  message = 'Unknown services error'
9
 
10
 
 
1
+ from web_server.errors import RagFlowError
2
 
3
  __all__ = ['ServicesError', 'ServiceNotSupported', 'ZooKeeperNotConfigured',
4
  'MissingZooKeeperUsernameOrPassword', 'ZooKeeperBackendError']
5
 
6
 
7
+ class ServicesError(RagFlowError):
8
  message = 'Unknown services error'
9
 
10
 
{web_server β†’ api}/errors/general_error.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
{web_server β†’ api}/flask_session/2029240f6d1128be89ddc32729463129 RENAMED
File without changes
{web_server β†’ api}/hook/__init__.py RENAMED
File without changes
{web_server β†’ api}/hook/api/client_authentication.py RENAMED
File without changes
{web_server β†’ api}/hook/api/permission.py RENAMED
File without changes
{web_server β†’ api}/hook/api/site_authentication.py RENAMED
File without changes
{web_server β†’ api}/hook/common/parameters.py RENAMED
File without changes
{web_server β†’ api}/ragflow_server.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -46,7 +46,7 @@ if __name__ == '__main__':
46
  # init runtime config
47
  import argparse
48
  parser = argparse.ArgumentParser()
49
- parser.add_argument('--version', default=False, help="fate flow version", action='store_true')
50
  parser.add_argument('--debug', default=False, help="debug mode", action='store_true')
51
  args = parser.parse_args()
52
  if args.version:
@@ -64,13 +64,13 @@ if __name__ == '__main__':
64
 
65
  peewee_logger = logging.getLogger('peewee')
66
  peewee_logger.propagate = False
67
- # fate_arch.common.log.ROpenHandler
68
  peewee_logger.addHandler(database_logger.handlers[0])
69
  peewee_logger.setLevel(database_logger.level)
70
 
71
  # start http server
72
  try:
73
- stat_logger.info("FATE Flow http server start...")
74
  werkzeug_logger = logging.getLogger("werkzeug")
75
  for h in access_logger.handlers:
76
  werkzeug_logger.addHandler(h)
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
46
  # init runtime config
47
  import argparse
48
  parser = argparse.ArgumentParser()
49
+ parser.add_argument('--version', default=False, help="rag flow version", action='store_true')
50
  parser.add_argument('--debug', default=False, help="debug mode", action='store_true')
51
  args = parser.parse_args()
52
  if args.version:
 
64
 
65
  peewee_logger = logging.getLogger('peewee')
66
  peewee_logger.propagate = False
67
+ # rag_arch.common.log.ROpenHandler
68
  peewee_logger.addHandler(database_logger.handlers[0])
69
  peewee_logger.setLevel(database_logger.level)
70
 
71
  # start http server
72
  try:
73
+ stat_logger.info("RAG Flow http server start...")
74
  werkzeug_logger = logging.getLogger("werkzeug")
75
  for h in access_logger.handlers:
76
  werkzeug_logger.addHandler(h)
{web_server β†’ api}/settings.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -24,10 +24,10 @@ from web_server.utils.log_utils import LoggerFactory, getLogger
24
 
25
  # Server
26
  API_VERSION = "v1"
27
- FATE_FLOW_SERVICE_NAME = "ragflow"
28
  SERVER_MODULE = "rag_flow_server.py"
29
  TEMP_DIRECTORY = os.path.join(get_project_base_directory(), "temp")
30
- FATE_FLOW_CONF_PATH = os.path.join(get_project_base_directory(), "conf")
31
 
32
  SUBPROCESS_STD_LOG_NAME = "std.log"
33
 
@@ -52,21 +52,21 @@ IMAGE2TEXT_MDL = LLM.get("image2text_model", "gpt-4-vision-preview")
52
 
53
  # distribution
54
  DEPENDENT_DISTRIBUTION = get_base_config("dependent_distribution", False)
55
- FATE_FLOW_UPDATE_CHECK = False
56
 
57
- HOST = get_base_config(FATE_FLOW_SERVICE_NAME, {}).get("host", "127.0.0.1")
58
- HTTP_PORT = get_base_config(FATE_FLOW_SERVICE_NAME, {}).get("http_port")
59
 
60
- SECRET_KEY = get_base_config(FATE_FLOW_SERVICE_NAME, {}).get("secret_key", "infiniflow")
61
- TOKEN_EXPIRE_IN = get_base_config(FATE_FLOW_SERVICE_NAME, {}).get("token_expires_in", 3600)
62
 
63
- NGINX_HOST = get_base_config(FATE_FLOW_SERVICE_NAME, {}).get("nginx", {}).get("host") or HOST
64
- NGINX_HTTP_PORT = get_base_config(FATE_FLOW_SERVICE_NAME, {}).get("nginx", {}).get("http_port") or HTTP_PORT
65
 
66
- RANDOM_INSTANCE_ID = get_base_config(FATE_FLOW_SERVICE_NAME, {}).get("random_instance_id", False)
67
 
68
- PROXY = get_base_config(FATE_FLOW_SERVICE_NAME, {}).get("proxy")
69
- PROXY_PROTOCOL = get_base_config(FATE_FLOW_SERVICE_NAME, {}).get("protocol")
70
 
71
  DATABASE = decrypt_database_config()
72
 
@@ -133,7 +133,7 @@ class CustomEnum(Enum):
133
 
134
 
135
  class PythonDependenceName(CustomEnum):
136
- Fate_Source_Code = "python"
137
  Python_Env = "miniconda"
138
 
139
 
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
24
 
25
  # Server
26
  API_VERSION = "v1"
27
+ RAG_FLOW_SERVICE_NAME = "ragflow"
28
  SERVER_MODULE = "rag_flow_server.py"
29
  TEMP_DIRECTORY = os.path.join(get_project_base_directory(), "temp")
30
+ RAG_FLOW_CONF_PATH = os.path.join(get_project_base_directory(), "conf")
31
 
32
  SUBPROCESS_STD_LOG_NAME = "std.log"
33
 
 
52
 
53
  # distribution
54
  DEPENDENT_DISTRIBUTION = get_base_config("dependent_distribution", False)
55
+ RAG_FLOW_UPDATE_CHECK = False
56
 
57
+ HOST = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("host", "127.0.0.1")
58
+ HTTP_PORT = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("http_port")
59
 
60
+ SECRET_KEY = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("secret_key", "infiniflow")
61
+ TOKEN_EXPIRE_IN = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("token_expires_in", 3600)
62
 
63
+ NGINX_HOST = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("nginx", {}).get("host") or HOST
64
+ NGINX_HTTP_PORT = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("nginx", {}).get("http_port") or HTTP_PORT
65
 
66
+ RANDOM_INSTANCE_ID = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("random_instance_id", False)
67
 
68
+ PROXY = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("proxy")
69
+ PROXY_PROTOCOL = get_base_config(RAG_FLOW_SERVICE_NAME, {}).get("protocol")
70
 
71
  DATABASE = decrypt_database_config()
72
 
 
133
 
134
 
135
  class PythonDependenceName(CustomEnum):
136
+ Rag_Source_Code = "python"
137
  Python_Env = "miniconda"
138
 
139
 
{web_server β†’ api}/utils/__init__.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
14
  # limitations under the License.
15
  #
16
  import base64
17
- from datetime import datetime
18
  import io
19
  import json
20
  import os
@@ -185,7 +185,7 @@ def deserialize_b64(src):
185
 
186
  safe_module = {
187
  'numpy',
188
- 'fate_flow'
189
  }
190
 
191
 
@@ -287,16 +287,16 @@ def get_uuid():
287
  return uuid.uuid1().hex
288
 
289
 
290
- def datetime_format(date_time: datetime) -> datetime:
291
- return datetime(date_time.year, date_time.month, date_time.day, date_time.hour, date_time.minute, date_time.second)
292
 
293
 
294
- def get_format_time() -> datetime:
295
- return datetime_format(datetime.now())
296
 
297
 
298
  def str2date(date_time: str):
299
- return datetime.strptime(date_time, '%Y-%m-%d')
300
 
301
 
302
  def elapsed2time(elapsed):
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
14
  # limitations under the License.
15
  #
16
  import base64
17
+ import datetime
18
  import io
19
  import json
20
  import os
 
185
 
186
  safe_module = {
187
  'numpy',
188
+ 'rag_flow'
189
  }
190
 
191
 
 
287
  return uuid.uuid1().hex
288
 
289
 
290
+ def datetime_format(date_time: datetime.datetime) -> datetime.datetime:
291
+ return datetime.datetime(date_time.year, date_time.month, date_time.day, date_time.hour, date_time.minute, date_time.second)
292
 
293
 
294
+ def get_format_time() -> datetime.datetime:
295
+ return datetime_format(datetime.datetime.now())
296
 
297
 
298
  def str2date(date_time: str):
299
+ return datetime.datetime.strptime(date_time, '%Y-%m-%d')
300
 
301
 
302
  def elapsed2time(elapsed):
{web_server β†’ api}/utils/api_utils.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ from flask import (
25
  from werkzeug.http import HTTP_STATUS_CODES
26
 
27
  from web_server.utils import json_dumps
28
- from web_server.versions import get_fate_version
29
  from web_server.settings import RetCode
30
  from web_server.settings import (
31
  REQUEST_MAX_WAIT_SEC, REQUEST_WAIT_SEC,
@@ -73,7 +73,7 @@ def request(**kwargs):
73
  return sess.send(prepped, stream=stream, timeout=timeout)
74
 
75
 
76
- fate_version = get_fate_version() or ''
77
 
78
 
79
  def get_exponential_backoff_interval(retries, full_jitter=False):
@@ -93,7 +93,7 @@ def get_json_result(retcode=RetCode.SUCCESS, retmsg='success', data=None, job_id
93
  result_dict = {
94
  "retcode": retcode,
95
  "retmsg":retmsg,
96
- # "retmsg": re.sub(r"fate", "seceum", retmsg, flags=re.IGNORECASE),
97
  "data": data,
98
  "jobId": job_id,
99
  "meta": meta,
@@ -109,7 +109,7 @@ def get_json_result(retcode=RetCode.SUCCESS, retmsg='success', data=None, job_id
109
 
110
  def get_data_error_result(retcode=RetCode.DATA_ERROR, retmsg='Sorry! Data missing!'):
111
  import re
112
- result_dict = {"retcode": retcode, "retmsg": re.sub(r"fate", "seceum", retmsg, flags=re.IGNORECASE)}
113
  response = {}
114
  for key, value in result_dict.items():
115
  if value is None and key != "retcode":
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
25
  from werkzeug.http import HTTP_STATUS_CODES
26
 
27
  from web_server.utils import json_dumps
28
+ from web_server.versions import get_rag_version
29
  from web_server.settings import RetCode
30
  from web_server.settings import (
31
  REQUEST_MAX_WAIT_SEC, REQUEST_WAIT_SEC,
 
73
  return sess.send(prepped, stream=stream, timeout=timeout)
74
 
75
 
76
+ rag_version = get_rag_version() or ''
77
 
78
 
79
  def get_exponential_backoff_interval(retries, full_jitter=False):
 
93
  result_dict = {
94
  "retcode": retcode,
95
  "retmsg":retmsg,
96
+ # "retmsg": re.sub(r"rag", "seceum", retmsg, flags=re.IGNORECASE),
97
  "data": data,
98
  "jobId": job_id,
99
  "meta": meta,
 
109
 
110
  def get_data_error_result(retcode=RetCode.DATA_ERROR, retmsg='Sorry! Data missing!'):
111
  import re
112
+ result_dict = {"retcode": retcode, "retmsg": re.sub(r"rag", "seceum", retmsg, flags=re.IGNORECASE)}
113
  response = {}
114
  for key, value in result_dict.items():
115
  if value is None and key != "retcode":
{web_server β†’ api}/utils/file_utils.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@ from ruamel.yaml import YAML
24
  from web_server.db import FileType
25
 
26
  PROJECT_BASE = os.getenv("RAG_PROJECT_BASE") or os.getenv("RAG_DEPLOY_BASE")
27
- FATE_BASE = os.getenv("RAG_BASE")
28
 
29
  def get_project_base_directory(*args):
30
  global PROJECT_BASE
@@ -42,10 +42,10 @@ def get_project_base_directory(*args):
42
  return PROJECT_BASE
43
 
44
 
45
- def get_fate_directory(*args):
46
- global FATE_BASE
47
- if FATE_BASE is None:
48
- FATE_BASE = os.path.abspath(
49
  os.path.join(
50
  os.path.dirname(os.path.realpath(__file__)),
51
  os.pardir,
@@ -54,12 +54,12 @@ def get_fate_directory(*args):
54
  )
55
  )
56
  if args:
57
- return os.path.join(FATE_BASE, *args)
58
- return FATE_BASE
59
 
60
 
61
- def get_fate_python_directory(*args):
62
- return get_fate_directory("python", *args)
63
 
64
 
65
 
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
24
  from web_server.db import FileType
25
 
26
  PROJECT_BASE = os.getenv("RAG_PROJECT_BASE") or os.getenv("RAG_DEPLOY_BASE")
27
+ RAG_BASE = os.getenv("RAG_BASE")
28
 
29
  def get_project_base_directory(*args):
30
  global PROJECT_BASE
 
42
  return PROJECT_BASE
43
 
44
 
45
+ def get_rag_directory(*args):
46
+ global RAG_BASE
47
+ if RAG_BASE is None:
48
+ RAG_BASE = os.path.abspath(
49
  os.path.join(
50
  os.path.dirname(os.path.realpath(__file__)),
51
  os.pardir,
 
54
  )
55
  )
56
  if args:
57
+ return os.path.join(RAG_BASE, *args)
58
+ return RAG_BASE
59
 
60
 
61
+ def get_rag_python_directory(*args):
62
+ return get_rag_directory("python", *args)
63
 
64
 
65
 
{web_server β†’ api}/utils/log_utils.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -127,13 +127,9 @@ class LoggerFactory(object):
127
  else:
128
  log_file = os.path.join(log_dir, "{}.log".format(class_name))
129
  else:
130
- log_file = os.path.join(log_dir, "fate_flow_{}.log".format(
131
- log_type) if level == LoggerFactory.LEVEL else 'fate_flow_{}_error.log'.format(log_type))
132
- job_id = job_id or os.getenv("FATE_JOB_ID")
133
- if job_id:
134
- formatter = logging.Formatter(LoggerFactory.LOG_FORMAT.replace("jobId", job_id))
135
- else:
136
- formatter = logging.Formatter(LoggerFactory.LOG_FORMAT.replace("jobId", "Server"))
137
  os.makedirs(os.path.dirname(log_file), exist_ok=True)
138
  if LoggerFactory.log_share:
139
  handler = ROpenHandler(log_file,
@@ -150,7 +146,6 @@ class LoggerFactory(object):
150
  if level:
151
  handler.level = level
152
 
153
- handler.setFormatter(formatter)
154
  return handler
155
 
156
  @staticmethod
@@ -264,28 +259,28 @@ def exception_to_trace_string(ex):
264
 
265
 
266
  def get_logger_base_dir():
267
- job_log_dir = file_utils.get_fate_flow_directory('logs')
268
  return job_log_dir
269
 
270
 
271
  def get_job_logger(job_id, log_type):
272
- fate_flow_log_dir = file_utils.get_fate_flow_directory('logs', 'fate_flow')
273
- job_log_dir = file_utils.get_fate_flow_directory('logs', job_id)
274
  if not job_id:
275
- log_dirs = [fate_flow_log_dir]
276
  else:
277
  if log_type == 'audit':
278
- log_dirs = [job_log_dir, fate_flow_log_dir]
279
  else:
280
  log_dirs = [job_log_dir]
281
  if LoggerFactory.log_share:
282
  oldmask = os.umask(000)
283
  os.makedirs(job_log_dir, exist_ok=True)
284
- os.makedirs(fate_flow_log_dir, exist_ok=True)
285
  os.umask(oldmask)
286
  else:
287
  os.makedirs(job_log_dir, exist_ok=True)
288
- os.makedirs(fate_flow_log_dir, exist_ok=True)
289
  logger = LoggerFactory.new_logger(f"{job_id}_{log_type}")
290
  for job_log_dir in log_dirs:
291
  handler = LoggerFactory.get_handler(class_name=None, level=LoggerFactory.LEVEL,
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
127
  else:
128
  log_file = os.path.join(log_dir, "{}.log".format(class_name))
129
  else:
130
+ log_file = os.path.join(log_dir, "rag_flow_{}.log".format(
131
+ log_type) if level == LoggerFactory.LEVEL else 'rag_flow_{}_error.log'.format(log_type))
132
+
 
 
 
 
133
  os.makedirs(os.path.dirname(log_file), exist_ok=True)
134
  if LoggerFactory.log_share:
135
  handler = ROpenHandler(log_file,
 
146
  if level:
147
  handler.level = level
148
 
 
149
  return handler
150
 
151
  @staticmethod
 
259
 
260
 
261
  def get_logger_base_dir():
262
+ job_log_dir = file_utils.get_rag_flow_directory('logs')
263
  return job_log_dir
264
 
265
 
266
  def get_job_logger(job_id, log_type):
267
+ rag_flow_log_dir = file_utils.get_rag_flow_directory('logs', 'rag_flow')
268
+ job_log_dir = file_utils.get_rag_flow_directory('logs', job_id)
269
  if not job_id:
270
+ log_dirs = [rag_flow_log_dir]
271
  else:
272
  if log_type == 'audit':
273
+ log_dirs = [job_log_dir, rag_flow_log_dir]
274
  else:
275
  log_dirs = [job_log_dir]
276
  if LoggerFactory.log_share:
277
  oldmask = os.umask(000)
278
  os.makedirs(job_log_dir, exist_ok=True)
279
+ os.makedirs(rag_flow_log_dir, exist_ok=True)
280
  os.umask(oldmask)
281
  else:
282
  os.makedirs(job_log_dir, exist_ok=True)
283
+ os.makedirs(rag_flow_log_dir, exist_ok=True)
284
  logger = LoggerFactory.new_logger(f"{job_id}_{log_type}")
285
  for job_log_dir in log_dirs:
286
  handler = LoggerFactory.get_handler(class_name=None, level=LoggerFactory.LEVEL,
{web_server β†’ api}/utils/t_crypt.py RENAMED
File without changes
{web_server β†’ api}/versions.py RENAMED
@@ -1,5 +1,5 @@
1
  #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
@@ -26,5 +26,5 @@ def get_versions() -> typing.Mapping[str, typing.Any]:
26
  dotenv_path=os.path.join(get_project_base_directory(), "rag.env")
27
  )
28
 
29
- def get_fate_version() -> typing.Optional[str]:
30
  return get_versions().get("RAG")
 
1
  #
2
+ # Copyright 2019 The RAG Flow Authors. All Rights Reserved.
3
  #
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
  # you may not use this file except in compliance with the License.
 
26
  dotenv_path=os.path.join(get_project_base_directory(), "rag.env")
27
  )
28
 
29
+ def get_rag_version() -> typing.Optional[str]:
30
  return get_versions().get("RAG")
web_server/db/service_registry.py DELETED
@@ -1,164 +0,0 @@
1
- #
2
- # Copyright 2019 The FATE Authors. All Rights Reserved.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- #
16
- import socket
17
- from pathlib import Path
18
- from web_server import utils
19
- from .db_models import DB, ServiceRegistryInfo, ServerRegistryInfo
20
- from .reload_config_base import ReloadConfigBase
21
-
22
-
23
- class ServiceRegistry(ReloadConfigBase):
24
- @classmethod
25
- @DB.connection_context()
26
- def load_service(cls, **kwargs) -> [ServiceRegistryInfo]:
27
- service_registry_list = ServiceRegistryInfo.query(**kwargs)
28
- return [service for service in service_registry_list]
29
-
30
- @classmethod
31
- @DB.connection_context()
32
- def save_service_info(cls, server_name, service_name, uri, method="POST", server_info=None, params=None, data=None, headers=None, protocol="http"):
33
- if not server_info:
34
- server_list = ServerRegistry.query_server_info_from_db(server_name=server_name)
35
- if not server_list:
36
- raise Exception(f"no found server {server_name}")
37
- server_info = server_list[0]
38
- url = f"{server_info.f_protocol}://{server_info.f_host}:{server_info.f_port}{uri}"
39
- else:
40
- url = f"{server_info.get('protocol', protocol)}://{server_info.get('host')}:{server_info.get('port')}{uri}"
41
- service_info = {
42
- "f_server_name": server_name,
43
- "f_service_name": service_name,
44
- "f_url": url,
45
- "f_method": method,
46
- "f_params": params if params else {},
47
- "f_data": data if data else {},
48
- "f_headers": headers if headers else {}
49
- }
50
- entity_model, status = ServiceRegistryInfo.get_or_create(
51
- f_server_name=server_name,
52
- f_service_name=service_name,
53
- defaults=service_info)
54
- if status is False:
55
- for key in service_info:
56
- setattr(entity_model, key, service_info[key])
57
- entity_model.save(force_insert=False)
58
-
59
-
60
- class ServerRegistry(ReloadConfigBase):
61
- FATEBOARD = None
62
- FATE_ON_STANDALONE = None
63
- FATE_ON_EGGROLL = None
64
- FATE_ON_SPARK = None
65
- MODEL_STORE_ADDRESS = None
66
- SERVINGS = None
67
- FATEMANAGER = None
68
- STUDIO = None
69
-
70
- @classmethod
71
- def load(cls):
72
- cls.load_server_info_from_conf()
73
- cls.load_server_info_from_db()
74
-
75
- @classmethod
76
- def load_server_info_from_conf(cls):
77
- path = Path(utils.file_utils.get_project_base_directory()) / 'conf' / utils.SERVICE_CONF
78
- conf = utils.file_utils.load_yaml_conf(path)
79
- if not isinstance(conf, dict):
80
- raise ValueError('invalid config file')
81
-
82
- local_path = path.with_name(f'local.{utils.SERVICE_CONF}')
83
- if local_path.exists():
84
- local_conf = utils.file_utils.load_yaml_conf(local_path)
85
- if not isinstance(local_conf, dict):
86
- raise ValueError('invalid local config file')
87
- conf.update(local_conf)
88
- for k, v in conf.items():
89
- if isinstance(v, dict):
90
- setattr(cls, k.upper(), v)
91
-
92
- @classmethod
93
- def register(cls, server_name, server_info):
94
- cls.save_server_info_to_db(server_name, server_info.get("host"), server_info.get("port"), protocol=server_info.get("protocol", "http"))
95
- setattr(cls, server_name, server_info)
96
-
97
- @classmethod
98
- def save(cls, service_config):
99
- update_server = {}
100
- for server_name, server_info in service_config.items():
101
- cls.parameter_check(server_info)
102
- api_info = server_info.pop("api", {})
103
- for service_name, info in api_info.items():
104
- ServiceRegistry.save_service_info(server_name, service_name, uri=info.get('uri'), method=info.get('method', 'POST'), server_info=server_info)
105
- cls.save_server_info_to_db(server_name, server_info.get("host"), server_info.get("port"), protocol="http")
106
- setattr(cls, server_name.upper(), server_info)
107
- return update_server
108
-
109
- @classmethod
110
- def parameter_check(cls, service_info):
111
- if "host" in service_info and "port" in service_info:
112
- cls.connection_test(service_info.get("host"), service_info.get("port"))
113
-
114
- @classmethod
115
- def connection_test(cls, ip, port):
116
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
117
- result = s.connect_ex((ip, port))
118
- if result != 0:
119
- raise ConnectionRefusedError(f"connection refused: host {ip}, port {port}")
120
-
121
- @classmethod
122
- def query(cls, service_name, default=None):
123
- service_info = getattr(cls, service_name, default)
124
- if not service_info:
125
- service_info = utils.get_base_config(service_name, default)
126
- return service_info
127
-
128
- @classmethod
129
- @DB.connection_context()
130
- def query_server_info_from_db(cls, server_name=None) -> [ServerRegistryInfo]:
131
- if server_name:
132
- server_list = ServerRegistryInfo.select().where(ServerRegistryInfo.f_server_name==server_name.upper())
133
- else:
134
- server_list = ServerRegistryInfo.select()
135
- return [server for server in server_list]
136
-
137
- @classmethod
138
- @DB.connection_context()
139
- def load_server_info_from_db(cls):
140
- for server in cls.query_server_info_from_db():
141
- server_info = {
142
- "host": server.f_host,
143
- "port": server.f_port,
144
- "protocol": server.f_protocol
145
- }
146
- setattr(cls, server.f_server_name.upper(), server_info)
147
-
148
-
149
- @classmethod
150
- @DB.connection_context()
151
- def save_server_info_to_db(cls, server_name, host, port, protocol="http"):
152
- server_info = {
153
- "f_server_name": server_name,
154
- "f_host": host,
155
- "f_port": port,
156
- "f_protocol": protocol
157
- }
158
- entity_model, status = ServerRegistryInfo.get_or_create(
159
- f_server_name=server_name,
160
- defaults=server_info)
161
- if status is False:
162
- for key in server_info:
163
- setattr(entity_model, key, server_info[key])
164
- entity_model.save(force_insert=False)