2024-01-12 12:34:01 +08:00
|
|
|
import base64
|
2023-08-24 21:27:31 +08:00
|
|
|
import json
|
2024-06-18 13:26:01 +08:00
|
|
|
import logging
|
2024-01-12 12:34:01 +08:00
|
|
|
import secrets
|
2024-06-06 00:42:00 +08:00
|
|
|
from typing import Optional
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
import click
|
2024-02-06 13:21:13 +08:00
|
|
|
from flask import current_app
|
|
|
|
from werkzeug.exceptions import NotFound
|
|
|
|
|
2024-07-03 21:09:23 +08:00
|
|
|
from configs import dify_config
|
2024-06-06 00:42:00 +08:00
|
|
|
from constants.languages import languages
|
2024-02-26 19:47:29 +08:00
|
|
|
from core.rag.datasource.vdb.vector_factory import Vector
|
2024-06-08 22:29:24 +08:00
|
|
|
from core.rag.datasource.vdb.vector_type import VectorType
|
2024-02-26 19:47:29 +08:00
|
|
|
from core.rag.models.document import Document
|
2024-06-28 20:20:23 +08:00
|
|
|
from events.app_event import app_was_created
|
2023-05-15 08:51:32 +08:00
|
|
|
from extensions.ext_database import db
|
2024-06-18 13:26:01 +08:00
|
|
|
from extensions.ext_redis import redis_client
|
2024-01-12 12:34:01 +08:00
|
|
|
from libs.helper import email as email_validate
|
|
|
|
from libs.password import hash_password, password_pattern, valid_password
|
2023-06-09 11:36:38 +08:00
|
|
|
from libs.rsa import generate_key_pair
|
2024-01-30 14:33:48 +08:00
|
|
|
from models.account import Tenant
|
2024-02-26 19:47:29 +08:00
|
|
|
from models.dataset import Dataset, DatasetCollectionBinding, DocumentSegment
|
|
|
|
from models.dataset import Document as DatasetDocument
|
2024-04-08 18:51:46 +08:00
|
|
|
from models.model import Account, App, AppAnnotationSetting, AppMode, Conversation, MessageAnnotation
|
2024-01-30 14:33:48 +08:00
|
|
|
from models.provider import Provider, ProviderModel
|
2024-06-06 00:42:00 +08:00
|
|
|
from services.account_service import RegisterService, TenantService
|
2023-06-09 11:36:38 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
@click.command("reset-password", help="Reset the account password.")
|
|
|
|
@click.option("--email", prompt=True, help="The email address of the account whose password you need to reset")
|
|
|
|
@click.option("--new-password", prompt=True, help="the new password.")
|
|
|
|
@click.option("--password-confirm", prompt=True, help="the new password confirm.")
|
2023-05-15 08:51:32 +08:00
|
|
|
def reset_password(email, new_password, password_confirm):
|
2024-01-30 14:33:48 +08:00
|
|
|
"""
|
|
|
|
Reset password of owner account
|
|
|
|
Only available in SELF_HOSTED mode
|
|
|
|
"""
|
2023-05-15 08:51:32 +08:00
|
|
|
if str(new_password).strip() != str(password_confirm).strip():
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("sorry. The two passwords do not match.", fg="red"))
|
2023-05-15 08:51:32 +08:00
|
|
|
return
|
2024-01-30 14:33:48 +08:00
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
account = db.session.query(Account).filter(Account.email == email).one_or_none()
|
2024-01-30 14:33:48 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
if not account:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("sorry. the account: [{}] not exist .".format(email), fg="red"))
|
2023-05-15 08:51:32 +08:00
|
|
|
return
|
2024-01-30 14:33:48 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
try:
|
|
|
|
valid_password(new_password)
|
|
|
|
except:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("sorry. The passwords must match {} ".format(password_pattern), fg="red"))
|
2023-05-15 08:51:32 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
# generate password salt
|
|
|
|
salt = secrets.token_bytes(16)
|
|
|
|
base64_salt = base64.b64encode(salt).decode()
|
|
|
|
|
|
|
|
# encrypt password with salt
|
|
|
|
password_hashed = hash_password(new_password, salt)
|
|
|
|
base64_password_hashed = base64.b64encode(password_hashed).decode()
|
|
|
|
account.password = base64_password_hashed
|
|
|
|
account.password_salt = base64_salt
|
|
|
|
db.session.commit()
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Congratulations! Password has been reset.", fg="green"))
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
@click.command("reset-email", help="Reset the account email.")
|
|
|
|
@click.option("--email", prompt=True, help="The old email address of the account whose email you need to reset")
|
|
|
|
@click.option("--new-email", prompt=True, help="the new email.")
|
|
|
|
@click.option("--email-confirm", prompt=True, help="the new email confirm.")
|
2023-05-15 08:51:32 +08:00
|
|
|
def reset_email(email, new_email, email_confirm):
|
2024-01-30 14:33:48 +08:00
|
|
|
"""
|
|
|
|
Replace account email
|
|
|
|
:return:
|
|
|
|
"""
|
2023-05-15 08:51:32 +08:00
|
|
|
if str(new_email).strip() != str(email_confirm).strip():
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Sorry, new email and confirm email do not match.", fg="red"))
|
2023-05-15 08:51:32 +08:00
|
|
|
return
|
2024-01-30 14:33:48 +08:00
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
account = db.session.query(Account).filter(Account.email == email).one_or_none()
|
2024-01-30 14:33:48 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
if not account:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("sorry. the account: [{}] not exist .".format(email), fg="red"))
|
2023-05-15 08:51:32 +08:00
|
|
|
return
|
2024-01-30 14:33:48 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
try:
|
|
|
|
email_validate(new_email)
|
|
|
|
except:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("sorry. {} is not a valid email. ".format(email), fg="red"))
|
2023-05-15 08:51:32 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
account.email = new_email
|
|
|
|
db.session.commit()
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Congratulations!, email has been reset.", fg="green"))
|
|
|
|
|
|
|
|
|
|
|
|
@click.command(
|
|
|
|
"reset-encrypt-key-pair",
|
|
|
|
help="Reset the asymmetric key pair of workspace for encrypt LLM credentials. "
|
|
|
|
"After the reset, all LLM credentials will become invalid, "
|
|
|
|
"requiring re-entry."
|
|
|
|
"Only support SELF_HOSTED mode.",
|
|
|
|
)
|
|
|
|
@click.confirmation_option(
|
|
|
|
prompt=click.style(
|
2024-09-13 14:24:49 +08:00
|
|
|
"Are you sure you want to reset encrypt key pair? this operation cannot be rolled back!", fg="red"
|
2024-08-15 12:54:05 +08:00
|
|
|
)
|
|
|
|
)
|
2023-06-09 11:36:38 +08:00
|
|
|
def reset_encrypt_key_pair():
|
2024-01-30 14:33:48 +08:00
|
|
|
"""
|
|
|
|
Reset the encrypted key pair of workspace for encrypt LLM credentials.
|
|
|
|
After the reset, all LLM credentials will become invalid, requiring re-entry.
|
|
|
|
Only support SELF_HOSTED mode.
|
|
|
|
"""
|
2024-08-15 12:54:05 +08:00
|
|
|
if dify_config.EDITION != "SELF_HOSTED":
|
|
|
|
click.echo(click.style("Sorry, only support SELF_HOSTED mode.", fg="red"))
|
2023-06-09 11:36:38 +08:00
|
|
|
return
|
|
|
|
|
2024-03-28 17:02:52 +08:00
|
|
|
tenants = db.session.query(Tenant).all()
|
|
|
|
for tenant in tenants:
|
|
|
|
if not tenant:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Sorry, no workspace found. Please enter /install to initialize.", fg="red"))
|
2024-03-28 17:02:52 +08:00
|
|
|
return
|
2023-06-09 11:36:38 +08:00
|
|
|
|
2024-03-28 17:02:52 +08:00
|
|
|
tenant.encrypt_public_key = generate_key_pair(tenant.id)
|
2023-06-09 11:36:38 +08:00
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
db.session.query(Provider).filter(Provider.provider_type == "custom", Provider.tenant_id == tenant.id).delete()
|
2024-03-28 17:02:52 +08:00
|
|
|
db.session.query(ProviderModel).filter(ProviderModel.tenant_id == tenant.id).delete()
|
|
|
|
db.session.commit()
|
2023-06-09 11:36:38 +08:00
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(
|
|
|
|
click.style(
|
2024-09-13 14:24:49 +08:00
|
|
|
"Congratulations! The asymmetric key pair of workspace {} has been reset.".format(tenant.id),
|
2024-08-15 12:54:05 +08:00
|
|
|
fg="green",
|
|
|
|
)
|
|
|
|
)
|
2023-06-09 11:36:38 +08:00
|
|
|
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
@click.command("vdb-migrate", help="migrate vector db.")
|
|
|
|
@click.option("--scope", default="all", prompt=False, help="The scope of vector database to migrate, Default is All.")
|
2024-03-04 17:22:06 +08:00
|
|
|
def vdb_migrate(scope: str):
|
2024-08-15 12:54:05 +08:00
|
|
|
if scope in ["knowledge", "all"]:
|
2024-03-04 17:22:06 +08:00
|
|
|
migrate_knowledge_vector_database()
|
2024-08-15 12:54:05 +08:00
|
|
|
if scope in ["annotation", "all"]:
|
2024-03-04 17:22:06 +08:00
|
|
|
migrate_annotation_vector_database()
|
|
|
|
|
|
|
|
|
|
|
|
def migrate_annotation_vector_database():
|
|
|
|
"""
|
|
|
|
Migrate annotation datas to target vector database .
|
|
|
|
"""
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Start migrate annotation data.", fg="green"))
|
2024-03-04 17:22:06 +08:00
|
|
|
create_count = 0
|
|
|
|
skipped_count = 0
|
|
|
|
total_count = 0
|
|
|
|
page = 1
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
# get apps info
|
2024-08-15 12:54:05 +08:00
|
|
|
apps = (
|
|
|
|
db.session.query(App)
|
|
|
|
.filter(App.status == "normal")
|
|
|
|
.order_by(App.created_at.desc())
|
|
|
|
.paginate(page=page, per_page=50)
|
|
|
|
)
|
2024-03-04 17:22:06 +08:00
|
|
|
except NotFound:
|
|
|
|
break
|
|
|
|
|
|
|
|
page += 1
|
|
|
|
for app in apps:
|
|
|
|
total_count = total_count + 1
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(
|
|
|
|
f"Processing the {total_count} app {app.id}. " + f"{create_count} created, {skipped_count} skipped."
|
|
|
|
)
|
2024-03-04 17:22:06 +08:00
|
|
|
try:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo("Create app annotation index: {}".format(app.id))
|
|
|
|
app_annotation_setting = (
|
|
|
|
db.session.query(AppAnnotationSetting).filter(AppAnnotationSetting.app_id == app.id).first()
|
|
|
|
)
|
2024-03-04 17:22:06 +08:00
|
|
|
|
|
|
|
if not app_annotation_setting:
|
|
|
|
skipped_count = skipped_count + 1
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo("App annotation setting is disabled: {}".format(app.id))
|
2024-03-04 17:22:06 +08:00
|
|
|
continue
|
|
|
|
# get dataset_collection_binding info
|
2024-08-15 12:54:05 +08:00
|
|
|
dataset_collection_binding = (
|
|
|
|
db.session.query(DatasetCollectionBinding)
|
|
|
|
.filter(DatasetCollectionBinding.id == app_annotation_setting.collection_binding_id)
|
|
|
|
.first()
|
|
|
|
)
|
2024-03-04 17:22:06 +08:00
|
|
|
if not dataset_collection_binding:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo("App annotation collection binding is not exist: {}".format(app.id))
|
2024-03-04 17:22:06 +08:00
|
|
|
continue
|
|
|
|
annotations = db.session.query(MessageAnnotation).filter(MessageAnnotation.app_id == app.id).all()
|
|
|
|
dataset = Dataset(
|
|
|
|
id=app.id,
|
|
|
|
tenant_id=app.tenant_id,
|
2024-08-15 12:54:05 +08:00
|
|
|
indexing_technique="high_quality",
|
2024-03-04 17:22:06 +08:00
|
|
|
embedding_model_provider=dataset_collection_binding.provider_name,
|
|
|
|
embedding_model=dataset_collection_binding.model_name,
|
2024-08-15 12:54:05 +08:00
|
|
|
collection_binding_id=dataset_collection_binding.id,
|
2024-03-04 17:22:06 +08:00
|
|
|
)
|
|
|
|
documents = []
|
|
|
|
if annotations:
|
|
|
|
for annotation in annotations:
|
|
|
|
document = Document(
|
|
|
|
page_content=annotation.question,
|
2024-08-15 12:54:05 +08:00
|
|
|
metadata={"annotation_id": annotation.id, "app_id": app.id, "doc_id": annotation.id},
|
2024-03-04 17:22:06 +08:00
|
|
|
)
|
|
|
|
documents.append(document)
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
vector = Vector(dataset, attributes=["doc_id", "annotation_id", "app_id"])
|
2024-03-04 17:22:06 +08:00
|
|
|
click.echo(f"Start to migrate annotation, app_id: {app.id}.")
|
|
|
|
|
|
|
|
try:
|
|
|
|
vector.delete()
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style(f"Successfully delete vector index for app: {app.id}.", fg="green"))
|
2024-03-04 17:22:06 +08:00
|
|
|
except Exception as e:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style(f"Failed to delete vector index for app {app.id}.", fg="red"))
|
2024-03-04 17:22:06 +08:00
|
|
|
raise e
|
|
|
|
if documents:
|
|
|
|
try:
|
|
|
|
click.echo(
|
2024-08-15 12:54:05 +08:00
|
|
|
click.style(
|
|
|
|
f"Start to created vector index with {len(documents)} annotations for app {app.id}.",
|
|
|
|
fg="green",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
vector.create(documents)
|
|
|
|
click.echo(click.style(f"Successfully created vector index for app {app.id}.", fg="green"))
|
2024-03-04 17:22:06 +08:00
|
|
|
except Exception as e:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style(f"Failed to created vector index for app {app.id}.", fg="red"))
|
2024-03-04 17:22:06 +08:00
|
|
|
raise e
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(f"Successfully migrated app annotation {app.id}.")
|
2024-03-04 17:22:06 +08:00
|
|
|
create_count += 1
|
|
|
|
except Exception as e:
|
|
|
|
click.echo(
|
2024-08-15 12:54:05 +08:00
|
|
|
click.style(
|
|
|
|
"Create app annotation index error: {} {}".format(e.__class__.__name__, str(e)), fg="red"
|
|
|
|
)
|
|
|
|
)
|
2024-03-04 17:22:06 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
click.echo(
|
2024-08-15 12:54:05 +08:00
|
|
|
click.style(
|
|
|
|
f"Congratulations! Create {create_count} app annotation indexes, and skipped {skipped_count} apps.",
|
|
|
|
fg="green",
|
|
|
|
)
|
|
|
|
)
|
2024-03-04 17:22:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
def migrate_knowledge_vector_database():
|
2024-01-30 14:33:48 +08:00
|
|
|
"""
|
2024-02-26 19:47:29 +08:00
|
|
|
Migrate vector database datas to target vector database .
|
2024-01-30 14:33:48 +08:00
|
|
|
"""
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Start migrate vector db.", fg="green"))
|
2023-08-24 21:27:31 +08:00
|
|
|
create_count = 0
|
2024-02-29 16:24:51 +08:00
|
|
|
skipped_count = 0
|
|
|
|
total_count = 0
|
2024-07-30 11:15:26 +08:00
|
|
|
vector_type = dify_config.VECTOR_STORE
|
2023-08-24 21:27:31 +08:00
|
|
|
page = 1
|
|
|
|
while True:
|
|
|
|
try:
|
2024-08-15 12:54:05 +08:00
|
|
|
datasets = (
|
|
|
|
db.session.query(Dataset)
|
|
|
|
.filter(Dataset.indexing_technique == "high_quality")
|
|
|
|
.order_by(Dataset.created_at.desc())
|
|
|
|
.paginate(page=page, per_page=50)
|
|
|
|
)
|
2023-08-24 21:27:31 +08:00
|
|
|
except NotFound:
|
|
|
|
break
|
|
|
|
|
|
|
|
page += 1
|
|
|
|
for dataset in datasets:
|
2024-02-29 16:24:51 +08:00
|
|
|
total_count = total_count + 1
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(
|
2024-09-13 14:24:49 +08:00
|
|
|
f"Processing the {total_count} dataset {dataset.id}. {create_count} created, {skipped_count} skipped."
|
2024-08-15 12:54:05 +08:00
|
|
|
)
|
2024-02-26 19:47:29 +08:00
|
|
|
try:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo("Create dataset vdb index: {}".format(dataset.id))
|
2024-02-26 19:47:29 +08:00
|
|
|
if dataset.index_struct_dict:
|
2024-08-15 12:54:05 +08:00
|
|
|
if dataset.index_struct_dict["type"] == vector_type:
|
2024-02-29 16:24:51 +08:00
|
|
|
skipped_count = skipped_count + 1
|
2024-02-26 19:47:29 +08:00
|
|
|
continue
|
2024-08-15 12:54:05 +08:00
|
|
|
collection_name = ""
|
2024-06-08 22:29:24 +08:00
|
|
|
if vector_type == VectorType.WEAVIATE:
|
2024-02-26 19:47:29 +08:00
|
|
|
dataset_id = dataset.id
|
2024-02-29 12:47:10 +08:00
|
|
|
collection_name = Dataset.gen_collection_name_by_id(dataset_id)
|
2024-08-15 12:54:05 +08:00
|
|
|
index_struct_dict = {"type": VectorType.WEAVIATE, "vector_store": {"class_prefix": collection_name}}
|
2024-02-26 19:47:29 +08:00
|
|
|
dataset.index_struct = json.dumps(index_struct_dict)
|
2024-06-08 22:29:24 +08:00
|
|
|
elif vector_type == VectorType.QDRANT:
|
2024-02-26 19:47:29 +08:00
|
|
|
if dataset.collection_binding_id:
|
2024-08-15 12:54:05 +08:00
|
|
|
dataset_collection_binding = (
|
|
|
|
db.session.query(DatasetCollectionBinding)
|
|
|
|
.filter(DatasetCollectionBinding.id == dataset.collection_binding_id)
|
|
|
|
.one_or_none()
|
|
|
|
)
|
2024-02-26 19:47:29 +08:00
|
|
|
if dataset_collection_binding:
|
|
|
|
collection_name = dataset_collection_binding.collection_name
|
2023-08-29 15:00:36 +08:00
|
|
|
else:
|
2024-08-15 12:54:05 +08:00
|
|
|
raise ValueError("Dataset Collection Bindings is not exist!")
|
2024-02-26 19:47:29 +08:00
|
|
|
else:
|
|
|
|
dataset_id = dataset.id
|
2024-02-29 12:47:10 +08:00
|
|
|
collection_name = Dataset.gen_collection_name_by_id(dataset_id)
|
2024-08-15 12:54:05 +08:00
|
|
|
index_struct_dict = {"type": VectorType.QDRANT, "vector_store": {"class_prefix": collection_name}}
|
2024-02-26 19:47:29 +08:00
|
|
|
dataset.index_struct = json.dumps(index_struct_dict)
|
|
|
|
|
2024-06-08 22:29:24 +08:00
|
|
|
elif vector_type == VectorType.MILVUS:
|
2024-02-26 19:47:29 +08:00
|
|
|
dataset_id = dataset.id
|
2024-02-29 12:47:10 +08:00
|
|
|
collection_name = Dataset.gen_collection_name_by_id(dataset_id)
|
2024-08-15 12:54:05 +08:00
|
|
|
index_struct_dict = {"type": VectorType.MILVUS, "vector_store": {"class_prefix": collection_name}}
|
2024-02-26 19:47:29 +08:00
|
|
|
dataset.index_struct = json.dumps(index_struct_dict)
|
2024-06-08 22:29:24 +08:00
|
|
|
elif vector_type == VectorType.RELYT:
|
2024-04-15 11:52:34 +08:00
|
|
|
dataset_id = dataset.id
|
|
|
|
collection_name = Dataset.gen_collection_name_by_id(dataset_id)
|
2024-08-15 12:54:05 +08:00
|
|
|
index_struct_dict = {"type": "relyt", "vector_store": {"class_prefix": collection_name}}
|
2024-06-14 19:25:17 +08:00
|
|
|
dataset.index_struct = json.dumps(index_struct_dict)
|
|
|
|
elif vector_type == VectorType.TENCENT:
|
|
|
|
dataset_id = dataset.id
|
|
|
|
collection_name = Dataset.gen_collection_name_by_id(dataset_id)
|
2024-08-15 12:54:05 +08:00
|
|
|
index_struct_dict = {"type": VectorType.TENCENT, "vector_store": {"class_prefix": collection_name}}
|
2024-04-15 11:52:34 +08:00
|
|
|
dataset.index_struct = json.dumps(index_struct_dict)
|
2024-06-08 22:29:24 +08:00
|
|
|
elif vector_type == VectorType.PGVECTOR:
|
2024-05-10 17:20:30 +08:00
|
|
|
dataset_id = dataset.id
|
|
|
|
collection_name = Dataset.gen_collection_name_by_id(dataset_id)
|
2024-08-15 12:54:05 +08:00
|
|
|
index_struct_dict = {"type": VectorType.PGVECTOR, "vector_store": {"class_prefix": collection_name}}
|
2024-05-10 17:20:30 +08:00
|
|
|
dataset.index_struct = json.dumps(index_struct_dict)
|
2024-06-19 12:44:33 +08:00
|
|
|
elif vector_type == VectorType.OPENSEARCH:
|
|
|
|
dataset_id = dataset.id
|
|
|
|
collection_name = Dataset.gen_collection_name_by_id(dataset_id)
|
|
|
|
index_struct_dict = {
|
|
|
|
"type": VectorType.OPENSEARCH,
|
2024-08-15 12:54:05 +08:00
|
|
|
"vector_store": {"class_prefix": collection_name},
|
2024-06-19 12:44:33 +08:00
|
|
|
}
|
|
|
|
dataset.index_struct = json.dumps(index_struct_dict)
|
2024-07-09 13:32:04 +08:00
|
|
|
elif vector_type == VectorType.ANALYTICDB:
|
|
|
|
dataset_id = dataset.id
|
|
|
|
collection_name = Dataset.gen_collection_name_by_id(dataset_id)
|
|
|
|
index_struct_dict = {
|
|
|
|
"type": VectorType.ANALYTICDB,
|
2024-08-15 12:54:05 +08:00
|
|
|
"vector_store": {"class_prefix": collection_name},
|
2024-07-09 13:32:04 +08:00
|
|
|
}
|
|
|
|
dataset.index_struct = json.dumps(index_struct_dict)
|
2024-08-13 17:36:20 +08:00
|
|
|
elif vector_type == VectorType.ELASTICSEARCH:
|
|
|
|
dataset_id = dataset.id
|
|
|
|
index_name = Dataset.gen_collection_name_by_id(dataset_id)
|
2024-08-15 12:54:05 +08:00
|
|
|
index_struct_dict = {"type": "elasticsearch", "vector_store": {"class_prefix": index_name}}
|
2024-08-13 17:36:20 +08:00
|
|
|
dataset.index_struct = json.dumps(index_struct_dict)
|
2024-02-26 19:47:29 +08:00
|
|
|
else:
|
2024-06-08 22:29:24 +08:00
|
|
|
raise ValueError(f"Vector store {vector_type} is not supported.")
|
2024-02-26 19:47:29 +08:00
|
|
|
|
|
|
|
vector = Vector(dataset)
|
2024-02-29 16:24:51 +08:00
|
|
|
click.echo(f"Start to migrate dataset {dataset.id}.")
|
2024-02-26 19:47:29 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
vector.delete()
|
2024-02-29 16:24:51 +08:00
|
|
|
click.echo(
|
2024-08-15 12:54:05 +08:00
|
|
|
click.style(
|
|
|
|
f"Successfully delete vector index {collection_name} for dataset {dataset.id}.", fg="green"
|
|
|
|
)
|
|
|
|
)
|
2024-02-26 19:47:29 +08:00
|
|
|
except Exception as e:
|
2024-02-29 16:24:51 +08:00
|
|
|
click.echo(
|
2024-08-15 12:54:05 +08:00
|
|
|
click.style(
|
|
|
|
f"Failed to delete vector index {collection_name} for dataset {dataset.id}.", fg="red"
|
|
|
|
)
|
|
|
|
)
|
2024-02-26 19:47:29 +08:00
|
|
|
raise e
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
dataset_documents = (
|
|
|
|
db.session.query(DatasetDocument)
|
|
|
|
.filter(
|
|
|
|
DatasetDocument.dataset_id == dataset.id,
|
|
|
|
DatasetDocument.indexing_status == "completed",
|
|
|
|
DatasetDocument.enabled == True,
|
|
|
|
DatasetDocument.archived == False,
|
|
|
|
)
|
|
|
|
.all()
|
|
|
|
)
|
2024-02-26 19:47:29 +08:00
|
|
|
|
|
|
|
documents = []
|
2024-02-29 16:24:51 +08:00
|
|
|
segments_count = 0
|
2024-02-26 19:47:29 +08:00
|
|
|
for dataset_document in dataset_documents:
|
2024-08-15 12:54:05 +08:00
|
|
|
segments = (
|
|
|
|
db.session.query(DocumentSegment)
|
|
|
|
.filter(
|
|
|
|
DocumentSegment.document_id == dataset_document.id,
|
|
|
|
DocumentSegment.status == "completed",
|
|
|
|
DocumentSegment.enabled == True,
|
|
|
|
)
|
|
|
|
.all()
|
|
|
|
)
|
2024-02-26 19:47:29 +08:00
|
|
|
|
|
|
|
for segment in segments:
|
|
|
|
document = Document(
|
|
|
|
page_content=segment.content,
|
|
|
|
metadata={
|
|
|
|
"doc_id": segment.index_node_id,
|
|
|
|
"doc_hash": segment.index_node_hash,
|
|
|
|
"document_id": segment.document_id,
|
|
|
|
"dataset_id": segment.dataset_id,
|
2024-08-15 12:54:05 +08:00
|
|
|
},
|
2024-02-26 19:47:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
documents.append(document)
|
2024-02-29 16:24:51 +08:00
|
|
|
segments_count = segments_count + 1
|
2024-02-26 19:47:29 +08:00
|
|
|
|
|
|
|
if documents:
|
|
|
|
try:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(
|
|
|
|
click.style(
|
2024-09-12 14:00:36 +08:00
|
|
|
f"Start to created vector index with {len(documents)} documents of {segments_count}"
|
|
|
|
f" segments for dataset {dataset.id}.",
|
2024-08-15 12:54:05 +08:00
|
|
|
fg="green",
|
|
|
|
)
|
|
|
|
)
|
2024-02-26 19:47:29 +08:00
|
|
|
vector.create(documents)
|
2024-02-29 16:24:51 +08:00
|
|
|
click.echo(
|
2024-08-15 12:54:05 +08:00
|
|
|
click.style(f"Successfully created vector index for dataset {dataset.id}.", fg="green")
|
|
|
|
)
|
2023-08-29 15:00:36 +08:00
|
|
|
except Exception as e:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style(f"Failed to created vector index for dataset {dataset.id}.", fg="red"))
|
2024-02-26 19:47:29 +08:00
|
|
|
raise e
|
|
|
|
db.session.add(dataset)
|
|
|
|
db.session.commit()
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(f"Successfully migrated dataset {dataset.id}.")
|
2024-02-26 19:47:29 +08:00
|
|
|
create_count += 1
|
|
|
|
except Exception as e:
|
|
|
|
db.session.rollback()
|
|
|
|
click.echo(
|
2024-08-15 12:54:05 +08:00
|
|
|
click.style("Create dataset index error: {} {}".format(e.__class__.__name__, str(e)), fg="red")
|
|
|
|
)
|
2024-02-26 19:47:29 +08:00
|
|
|
continue
|
2023-08-24 21:27:31 +08:00
|
|
|
|
2024-02-29 16:24:51 +08:00
|
|
|
click.echo(
|
2024-08-15 12:54:05 +08:00
|
|
|
click.style(
|
|
|
|
f"Congratulations! Create {create_count} dataset indexes, and skipped {skipped_count} datasets.", fg="green"
|
|
|
|
)
|
|
|
|
)
|
2023-08-24 21:27:31 +08:00
|
|
|
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
@click.command("convert-to-agent-apps", help="Convert Agent Assistant to Agent App.")
|
2024-04-08 18:51:46 +08:00
|
|
|
def convert_to_agent_apps():
|
|
|
|
"""
|
|
|
|
Convert Agent Assistant to Agent App.
|
|
|
|
"""
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Start convert to agent apps.", fg="green"))
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
proceeded_app_ids = []
|
|
|
|
|
|
|
|
while True:
|
|
|
|
# fetch first 1000 apps
|
|
|
|
sql_query = """SELECT a.id AS id FROM apps a
|
|
|
|
INNER JOIN app_model_configs am ON a.app_model_config_id=am.id
|
|
|
|
WHERE a.mode = 'chat'
|
|
|
|
AND am.agent_mode is not null
|
|
|
|
AND (
|
|
|
|
am.agent_mode like '%"strategy": "function_call"%'
|
|
|
|
OR am.agent_mode like '%"strategy": "react"%'
|
|
|
|
)
|
|
|
|
AND (
|
|
|
|
am.agent_mode like '{"enabled": true%'
|
|
|
|
OR am.agent_mode like '{"max_iteration": %'
|
|
|
|
) ORDER BY a.created_at DESC LIMIT 1000
|
|
|
|
"""
|
|
|
|
|
|
|
|
with db.engine.begin() as conn:
|
|
|
|
rs = conn.execute(db.text(sql_query))
|
|
|
|
|
|
|
|
apps = []
|
|
|
|
for i in rs:
|
|
|
|
app_id = str(i.id)
|
|
|
|
if app_id not in proceeded_app_ids:
|
|
|
|
proceeded_app_ids.append(app_id)
|
|
|
|
app = db.session.query(App).filter(App.id == app_id).first()
|
|
|
|
apps.append(app)
|
|
|
|
|
|
|
|
if len(apps) == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
for app in apps:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo("Converting app: {}".format(app.id))
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
app.mode = AppMode.AGENT_CHAT.value
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# update conversation mode to agent
|
|
|
|
db.session.query(Conversation).filter(Conversation.app_id == app.id).update(
|
|
|
|
{Conversation.mode: AppMode.AGENT_CHAT.value}
|
|
|
|
)
|
|
|
|
|
|
|
|
db.session.commit()
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Converted app: {}".format(app.id), fg="green"))
|
2024-04-08 18:51:46 +08:00
|
|
|
except Exception as e:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Convert app error: {} {}".format(e.__class__.__name__, str(e)), fg="red"))
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Congratulations! Converted {} agent apps.".format(len(proceeded_app_ids)), fg="green"))
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
@click.command("add-qdrant-doc-id-index", help="add qdrant doc_id index.")
|
|
|
|
@click.option("--field", default="metadata.doc_id", prompt=False, help="index field , default is metadata.doc_id.")
|
2024-05-22 01:42:08 +08:00
|
|
|
def add_qdrant_doc_id_index(field: str):
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Start add qdrant doc_id index.", fg="green"))
|
2024-07-30 11:15:26 +08:00
|
|
|
vector_type = dify_config.VECTOR_STORE
|
2024-05-22 01:42:08 +08:00
|
|
|
if vector_type != "qdrant":
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Sorry, only support qdrant vector store.", fg="red"))
|
2024-05-22 01:42:08 +08:00
|
|
|
return
|
|
|
|
create_count = 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
bindings = db.session.query(DatasetCollectionBinding).all()
|
|
|
|
if not bindings:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Sorry, no dataset collection bindings found.", fg="red"))
|
2024-05-22 01:42:08 +08:00
|
|
|
return
|
|
|
|
import qdrant_client
|
|
|
|
from qdrant_client.http.exceptions import UnexpectedResponse
|
|
|
|
from qdrant_client.http.models import PayloadSchemaType
|
|
|
|
|
|
|
|
from core.rag.datasource.vdb.qdrant.qdrant_vector import QdrantConfig
|
2024-08-15 12:54:05 +08:00
|
|
|
|
2024-05-22 01:42:08 +08:00
|
|
|
for binding in bindings:
|
2024-07-30 11:15:26 +08:00
|
|
|
if dify_config.QDRANT_URL is None:
|
2024-08-15 12:54:05 +08:00
|
|
|
raise ValueError("Qdrant url is required.")
|
2024-05-22 01:42:08 +08:00
|
|
|
qdrant_config = QdrantConfig(
|
2024-07-30 11:15:26 +08:00
|
|
|
endpoint=dify_config.QDRANT_URL,
|
|
|
|
api_key=dify_config.QDRANT_API_KEY,
|
2024-05-22 01:42:08 +08:00
|
|
|
root_path=current_app.root_path,
|
2024-07-30 11:15:26 +08:00
|
|
|
timeout=dify_config.QDRANT_CLIENT_TIMEOUT,
|
|
|
|
grpc_port=dify_config.QDRANT_GRPC_PORT,
|
2024-08-15 12:54:05 +08:00
|
|
|
prefer_grpc=dify_config.QDRANT_GRPC_ENABLED,
|
2024-05-22 01:42:08 +08:00
|
|
|
)
|
|
|
|
try:
|
|
|
|
client = qdrant_client.QdrantClient(**qdrant_config.to_qdrant_params())
|
|
|
|
# create payload index
|
2024-08-15 12:54:05 +08:00
|
|
|
client.create_payload_index(binding.collection_name, field, field_schema=PayloadSchemaType.KEYWORD)
|
2024-05-22 01:42:08 +08:00
|
|
|
create_count += 1
|
|
|
|
except UnexpectedResponse as e:
|
|
|
|
# Collection does not exist, so return
|
|
|
|
if e.status_code == 404:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(
|
|
|
|
click.style(f"Collection not found, collection_name:{binding.collection_name}.", fg="red")
|
|
|
|
)
|
2024-05-22 01:42:08 +08:00
|
|
|
continue
|
|
|
|
# Some other error occurred, so re-raise the exception
|
|
|
|
else:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(
|
|
|
|
click.style(
|
|
|
|
f"Failed to create qdrant index, collection_name:{binding.collection_name}.", fg="red"
|
|
|
|
)
|
|
|
|
)
|
2024-05-22 01:42:08 +08:00
|
|
|
|
|
|
|
except Exception as e:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Failed to create qdrant client.", fg="red"))
|
2024-05-22 01:42:08 +08:00
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style(f"Congratulations! Create {create_count} collection indexes.", fg="green"))
|
2024-05-22 01:42:08 +08:00
|
|
|
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
@click.command("create-tenant", help="Create account and tenant.")
|
|
|
|
@click.option("--email", prompt=True, help="The email address of the tenant account.")
|
2024-08-30 15:53:50 +08:00
|
|
|
@click.option("--name", prompt=True, help="The workspace name of the tenant account.")
|
2024-08-15 12:54:05 +08:00
|
|
|
@click.option("--language", prompt=True, help="Account language, default: en-US.")
|
2024-08-30 15:53:50 +08:00
|
|
|
def create_tenant(email: str, language: Optional[str] = None, name: Optional[str] = None):
|
2024-06-06 00:42:00 +08:00
|
|
|
"""
|
|
|
|
Create tenant account
|
|
|
|
"""
|
|
|
|
if not email:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Sorry, email is required.", fg="red"))
|
2024-06-06 00:42:00 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
# Create account
|
|
|
|
email = email.strip()
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
if "@" not in email:
|
|
|
|
click.echo(click.style("Sorry, invalid email address.", fg="red"))
|
2024-06-06 00:42:00 +08:00
|
|
|
return
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
account_name = email.split("@")[0]
|
2024-06-06 00:42:00 +08:00
|
|
|
|
|
|
|
if language not in languages:
|
2024-08-15 12:54:05 +08:00
|
|
|
language = "en-US"
|
2024-06-06 00:42:00 +08:00
|
|
|
|
2024-08-30 15:53:50 +08:00
|
|
|
name = name.strip()
|
|
|
|
|
2024-06-06 00:42:00 +08:00
|
|
|
# generate random password
|
|
|
|
new_password = secrets.token_urlsafe(16)
|
|
|
|
|
|
|
|
# register account
|
2024-08-15 12:54:05 +08:00
|
|
|
account = RegisterService.register(email=email, name=account_name, password=new_password, language=language)
|
2024-06-06 00:42:00 +08:00
|
|
|
|
2024-08-30 15:53:50 +08:00
|
|
|
TenantService.create_owner_tenant_if_not_exist(account, name)
|
2024-06-06 00:42:00 +08:00
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(
|
|
|
|
click.style(
|
2024-09-13 14:24:49 +08:00
|
|
|
"Congratulations! Account and tenant created.\nAccount: {}\nPassword: {}".format(email, new_password),
|
2024-08-15 12:54:05 +08:00
|
|
|
fg="green",
|
|
|
|
)
|
|
|
|
)
|
2024-06-06 00:42:00 +08:00
|
|
|
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
@click.command("upgrade-db", help="upgrade the database")
|
2024-06-18 13:26:01 +08:00
|
|
|
def upgrade_db():
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo("Preparing database migration...")
|
|
|
|
lock = redis_client.lock(name="db_upgrade_lock", timeout=60)
|
2024-06-18 13:26:01 +08:00
|
|
|
if lock.acquire(blocking=False):
|
|
|
|
try:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Start database migration.", fg="green"))
|
2024-06-18 13:26:01 +08:00
|
|
|
|
|
|
|
# run db migration
|
|
|
|
import flask_migrate
|
2024-08-15 12:54:05 +08:00
|
|
|
|
2024-06-18 13:26:01 +08:00
|
|
|
flask_migrate.upgrade()
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Database migration successful!", fg="green"))
|
2024-06-18 13:26:01 +08:00
|
|
|
|
|
|
|
except Exception as e:
|
2024-08-15 12:54:05 +08:00
|
|
|
logging.exception(f"Database migration failed, error: {e}")
|
2024-06-18 13:26:01 +08:00
|
|
|
finally:
|
|
|
|
lock.release()
|
|
|
|
else:
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo("Database migration skipped")
|
2024-06-18 13:26:01 +08:00
|
|
|
|
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
@click.command("fix-app-site-missing", help="Fix app related site missing issue.")
|
2024-06-28 20:20:23 +08:00
|
|
|
def fix_app_site_missing():
|
|
|
|
"""
|
|
|
|
Fix app related site missing issue.
|
|
|
|
"""
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Start fix app related site missing issue.", fg="green"))
|
2024-06-28 20:20:23 +08:00
|
|
|
|
2024-06-28 20:33:53 +08:00
|
|
|
failed_app_ids = []
|
2024-06-28 20:20:23 +08:00
|
|
|
while True:
|
2024-06-28 20:33:53 +08:00
|
|
|
sql = """select apps.id as id from apps left join sites on sites.app_id=apps.id
|
2024-06-28 20:20:23 +08:00
|
|
|
where sites.id is null limit 1000"""
|
2024-06-28 20:33:53 +08:00
|
|
|
with db.engine.begin() as conn:
|
|
|
|
rs = conn.execute(db.text(sql))
|
|
|
|
|
|
|
|
processed_count = 0
|
|
|
|
for i in rs:
|
|
|
|
processed_count += 1
|
|
|
|
app_id = str(i.id)
|
|
|
|
|
|
|
|
if app_id in failed_app_ids:
|
|
|
|
continue
|
2024-06-28 20:20:23 +08:00
|
|
|
|
2024-06-28 20:33:53 +08:00
|
|
|
try:
|
2024-06-28 20:20:23 +08:00
|
|
|
app = db.session.query(App).filter(App.id == app_id).first()
|
|
|
|
tenant = app.tenant
|
|
|
|
if tenant:
|
|
|
|
accounts = tenant.get_accounts()
|
|
|
|
if not accounts:
|
|
|
|
print("Fix app {} failed.".format(app.id))
|
|
|
|
continue
|
|
|
|
|
|
|
|
account = accounts[0]
|
|
|
|
print("Fix app {} related site missing issue.".format(app.id))
|
|
|
|
app_was_created.send(app, account=account)
|
2024-06-28 20:33:53 +08:00
|
|
|
except Exception as e:
|
|
|
|
failed_app_ids.append(app_id)
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Fix app {} related site missing issue failed!".format(app_id), fg="red"))
|
|
|
|
logging.exception(f"Fix app related site missing issue failed, error: {e}")
|
2024-06-28 20:33:53 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
if not processed_count:
|
|
|
|
break
|
2024-06-28 20:20:23 +08:00
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
click.echo(click.style("Congratulations! Fix app related site missing issue successful!", fg="green"))
|
2024-06-28 20:20:23 +08:00
|
|
|
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
def register_commands(app):
|
|
|
|
app.cli.add_command(reset_password)
|
|
|
|
app.cli.add_command(reset_email)
|
2023-06-09 11:36:38 +08:00
|
|
|
app.cli.add_command(reset_encrypt_key_pair)
|
2024-02-26 19:47:29 +08:00
|
|
|
app.cli.add_command(vdb_migrate)
|
2024-04-08 18:51:46 +08:00
|
|
|
app.cli.add_command(convert_to_agent_apps)
|
2024-05-22 01:42:08 +08:00
|
|
|
app.cli.add_command(add_qdrant_doc_id_index)
|
2024-06-06 00:42:00 +08:00
|
|
|
app.cli.add_command(create_tenant)
|
2024-06-18 13:26:01 +08:00
|
|
|
app.cli.add_command(upgrade_db)
|
2024-06-28 20:20:23 +08:00
|
|
|
app.cli.add_command(fix_app_site_missing)
|