refactor(api): Switch to dify_config (#6750)

Signed-off-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
-LAN- 2024-07-30 11:15:26 +08:00 committed by GitHub
parent daa31b2cb3
commit a98284b1ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 21 additions and 19 deletions

View File

@ -249,8 +249,7 @@ def migrate_knowledge_vector_database():
create_count = 0 create_count = 0
skipped_count = 0 skipped_count = 0
total_count = 0 total_count = 0
config = current_app.config vector_type = dify_config.VECTOR_STORE
vector_type = config.get('VECTOR_STORE')
page = 1 page = 1
while True: while True:
try: try:
@ -484,8 +483,7 @@ def convert_to_agent_apps():
@click.option('--field', default='metadata.doc_id', prompt=False, help='index field , default is metadata.doc_id.') @click.option('--field', default='metadata.doc_id', prompt=False, help='index field , default is metadata.doc_id.')
def add_qdrant_doc_id_index(field: str): def add_qdrant_doc_id_index(field: str):
click.echo(click.style('Start add qdrant doc_id index.', fg='green')) click.echo(click.style('Start add qdrant doc_id index.', fg='green'))
config = current_app.config vector_type = dify_config.VECTOR_STORE
vector_type = config.get('VECTOR_STORE')
if vector_type != "qdrant": if vector_type != "qdrant":
click.echo(click.style('Sorry, only support qdrant vector store.', fg='red')) click.echo(click.style('Sorry, only support qdrant vector store.', fg='red'))
return return
@ -502,13 +500,15 @@ def add_qdrant_doc_id_index(field: str):
from core.rag.datasource.vdb.qdrant.qdrant_vector import QdrantConfig from core.rag.datasource.vdb.qdrant.qdrant_vector import QdrantConfig
for binding in bindings: for binding in bindings:
if dify_config.QDRANT_URL is None:
raise ValueError('Qdrant url is required.')
qdrant_config = QdrantConfig( qdrant_config = QdrantConfig(
endpoint=config.get('QDRANT_URL'), endpoint=dify_config.QDRANT_URL,
api_key=config.get('QDRANT_API_KEY'), api_key=dify_config.QDRANT_API_KEY,
root_path=current_app.root_path, root_path=current_app.root_path,
timeout=config.get('QDRANT_CLIENT_TIMEOUT'), timeout=dify_config.QDRANT_CLIENT_TIMEOUT,
grpc_port=config.get('QDRANT_GRPC_PORT'), grpc_port=dify_config.QDRANT_GRPC_PORT,
prefer_grpc=config.get('QDRANT_GRPC_ENABLED') prefer_grpc=dify_config.QDRANT_GRPC_ENABLED
) )
try: try:
client = qdrant_client.QdrantClient(**qdrant_config.to_qdrant_params()) client = qdrant_client.QdrantClient(**qdrant_config.to_qdrant_params())

View File

@ -71,7 +71,7 @@ class ResetPasswordApi(Resource):
# AccountService.update_password(account, new_password) # AccountService.update_password(account, new_password)
# todo: Send email # todo: Send email
# MAILCHIMP_API_KEY = current_app.config['MAILCHIMP_TRANSACTIONAL_API_KEY'] # MAILCHIMP_API_KEY = dify_config.MAILCHIMP_TRANSACTIONAL_API_KEY
# mailchimp = MailchimpTransactional(MAILCHIMP_API_KEY) # mailchimp = MailchimpTransactional(MAILCHIMP_API_KEY)
# message = { # message = {
@ -92,7 +92,7 @@ class ResetPasswordApi(Resource):
# 'message': message, # 'message': message,
# # required for transactional email # # required for transactional email
# ' settings': { # ' settings': {
# 'sandbox_mode': current_app.config['MAILCHIMP_SANDBOX_MODE'], # 'sandbox_mode': dify_config.MAILCHIMP_SANDBOX_MODE,
# }, # },
# }) # })

View File

@ -12,6 +12,7 @@ from flask import Flask, current_app
from flask_login import current_user from flask_login import current_user
from sqlalchemy.orm.exc import ObjectDeletedError from sqlalchemy.orm.exc import ObjectDeletedError
from configs import dify_config
from core.errors.error import ProviderTokenNotInitError from core.errors.error import ProviderTokenNotInitError
from core.llm_generator.llm_generator import LLMGenerator from core.llm_generator.llm_generator import LLMGenerator
from core.model_manager import ModelInstance, ModelManager from core.model_manager import ModelInstance, ModelManager
@ -224,7 +225,7 @@ class IndexingRunner:
features = FeatureService.get_features(tenant_id) features = FeatureService.get_features(tenant_id)
if features.billing.enabled: if features.billing.enabled:
count = len(extract_settings) count = len(extract_settings)
batch_upload_limit = int(current_app.config['BATCH_UPLOAD_LIMIT']) batch_upload_limit = dify_config.BATCH_UPLOAD_LIMIT
if count > batch_upload_limit: if count > batch_upload_limit:
raise ValueError(f"You have reached the batch upload limit of {batch_upload_limit}.") raise ValueError(f"You have reached the batch upload limit of {batch_upload_limit}.")
@ -427,7 +428,7 @@ class IndexingRunner:
# The user-defined segmentation rule # The user-defined segmentation rule
rules = json.loads(processing_rule.rules) rules = json.loads(processing_rule.rules)
segmentation = rules["segmentation"] segmentation = rules["segmentation"]
max_segmentation_tokens_length = int(current_app.config['INDEXING_MAX_SEGMENTATION_TOKENS_LENGTH']) max_segmentation_tokens_length = dify_config.INDEXING_MAX_SEGMENTATION_TOKENS_LENGTH
if segmentation["max_tokens"] < 50 or segmentation["max_tokens"] > max_segmentation_tokens_length: if segmentation["max_tokens"] < 50 or segmentation["max_tokens"] > max_segmentation_tokens_length:
raise ValueError(f"Custom segment length should be between 50 and {max_segmentation_tokens_length}.") raise ValueError(f"Custom segment length should be between 50 and {max_segmentation_tokens_length}.")

View File

@ -2,9 +2,9 @@ import json
from collections import defaultdict from collections import defaultdict
from typing import Any, Optional from typing import Any, Optional
from flask import current_app
from pydantic import BaseModel from pydantic import BaseModel
from configs import dify_config
from core.rag.datasource.keyword.jieba.jieba_keyword_table_handler import JiebaKeywordTableHandler from core.rag.datasource.keyword.jieba.jieba_keyword_table_handler import JiebaKeywordTableHandler
from core.rag.datasource.keyword.keyword_base import BaseKeyword from core.rag.datasource.keyword.keyword_base import BaseKeyword
from core.rag.models.document import Document from core.rag.models.document import Document
@ -139,7 +139,7 @@ class Jieba(BaseKeyword):
if keyword_table_dict: if keyword_table_dict:
return keyword_table_dict['__data__']['table'] return keyword_table_dict['__data__']['table']
else: else:
keyword_data_source_type = current_app.config['KEYWORD_DATA_SOURCE_TYPE'] keyword_data_source_type = dify_config.KEYWORD_DATA_SOURCE_TYPE
dataset_keyword_table = DatasetKeywordTable( dataset_keyword_table = DatasetKeywordTable(
dataset_id=self.dataset.id, dataset_id=self.dataset.id,
keyword_table='', keyword_table='',

View File

@ -1,15 +1,16 @@
import jwt import jwt
from flask import current_app
from werkzeug.exceptions import Unauthorized from werkzeug.exceptions import Unauthorized
from configs import dify_config
class PassportService: class PassportService:
def __init__(self): def __init__(self):
self.sk = current_app.config.get('SECRET_KEY') self.sk = dify_config.SECRET_KEY
def issue(self, payload): def issue(self, payload):
return jwt.encode(payload, self.sk, algorithm='HS256') return jwt.encode(payload, self.sk, algorithm='HS256')
def verify(self, token): def verify(self, token):
try: try:
return jwt.decode(token, self.sk, algorithms=['HS256']) return jwt.decode(token, self.sk, algorithms=['HS256'])