-
Notifications
You must be signed in to change notification settings - Fork 19.2k
feat(improve-api-endpoints): Added Datasets and Annotation APIs #12237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a47b514
feat(improve-api-endpoints): Added Datasets and Annotation APIs
jasonfish568 d7854a5
feat(improve-api-endpoints): Added available model API
jasonfish568 f9c86a1
feat(improve-api-endpoints): Improve annotation API
jasonfish568 a7d85c2
style: fixed ruff errors
jasonfish568 d589f0b
style: Fix ruff error
jasonfish568 b489355
style: Fix lint errors
jasonfish568 f00a65e
Merge branch 'main' into main
jasonfish568 8c8c87e
docs: Added docs in mdx files for the Dataset APIs implemented
jasonfish568 65e96df
Merge branch 'main' of github.com:jasonfish568/dify
jasonfish568 6a8c603
docs: Added annotation API documentation to Chat App documentation.
jasonfish568 ec8290d
Merge pull request #1 from langgenius/main
jasonfish568 55c6476
Merge pull request #2 from langgenius/main
jasonfish568 edd9755
Merge branch 'main' into main
jasonfish568 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| from flask import request | ||
| from flask_restful import Resource, marshal, marshal_with, reqparse # type: ignore | ||
| from werkzeug.exceptions import Forbidden | ||
|
|
||
| from controllers.service_api import api | ||
| from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token | ||
| from extensions.ext_redis import redis_client | ||
| from fields.annotation_fields import ( | ||
| annotation_fields, | ||
| ) | ||
| from libs.login import current_user | ||
| from models.model import App, EndUser | ||
| from services.annotation_service import AppAnnotationService | ||
|
|
||
|
|
||
| class AnnotationReplyActionApi(Resource): | ||
| @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON)) | ||
| def post(self, app_model: App, end_user: EndUser, action): | ||
| parser = reqparse.RequestParser() | ||
| parser.add_argument("score_threshold", required=True, type=float, location="json") | ||
| parser.add_argument("embedding_provider_name", required=True, type=str, location="json") | ||
| parser.add_argument("embedding_model_name", required=True, type=str, location="json") | ||
| args = parser.parse_args() | ||
| if action == "enable": | ||
| result = AppAnnotationService.enable_app_annotation(args, app_model.id) | ||
| elif action == "disable": | ||
| result = AppAnnotationService.disable_app_annotation(app_model.id) | ||
| else: | ||
| raise ValueError("Unsupported annotation reply action") | ||
| return result, 200 | ||
|
|
||
|
|
||
| class AnnotationReplyActionStatusApi(Resource): | ||
| @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY)) | ||
| def get(self, app_model: App, end_user: EndUser, job_id, action): | ||
| job_id = str(job_id) | ||
| app_annotation_job_key = "{}_app_annotation_job_{}".format(action, str(job_id)) | ||
| cache_result = redis_client.get(app_annotation_job_key) | ||
| if cache_result is None: | ||
| raise ValueError("The job is not exist.") | ||
|
|
||
| job_status = cache_result.decode() | ||
| error_msg = "" | ||
| if job_status == "error": | ||
| app_annotation_error_key = "{}_app_annotation_error_{}".format(action, str(job_id)) | ||
| error_msg = redis_client.get(app_annotation_error_key).decode() | ||
|
|
||
| return {"job_id": job_id, "job_status": job_status, "error_msg": error_msg}, 200 | ||
|
|
||
|
|
||
| class AnnotationListApi(Resource): | ||
| @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY)) | ||
| def get(self, app_model: App, end_user: EndUser): | ||
| page = request.args.get("page", default=1, type=int) | ||
| limit = request.args.get("limit", default=20, type=int) | ||
| keyword = request.args.get("keyword", default="", type=str) | ||
|
|
||
| annotation_list, total = AppAnnotationService.get_annotation_list_by_app_id(app_model.id, page, limit, keyword) | ||
| response = { | ||
| "data": marshal(annotation_list, annotation_fields), | ||
| "has_more": len(annotation_list) == limit, | ||
| "limit": limit, | ||
| "total": total, | ||
| "page": page, | ||
| } | ||
| return response, 200 | ||
|
|
||
| @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON)) | ||
| @marshal_with(annotation_fields) | ||
| def post(self, app_model: App, end_user: EndUser): | ||
| parser = reqparse.RequestParser() | ||
| parser.add_argument("question", required=True, type=str, location="json") | ||
| parser.add_argument("answer", required=True, type=str, location="json") | ||
| args = parser.parse_args() | ||
| annotation = AppAnnotationService.insert_app_annotation_directly(args, app_model.id) | ||
| return annotation | ||
|
|
||
|
|
||
| class AnnotationUpdateDeleteApi(Resource): | ||
| @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON)) | ||
| @marshal_with(annotation_fields) | ||
| def post(self, app_model: App, end_user: EndUser, annotation_id): | ||
| if not current_user.is_editor: | ||
| raise Forbidden() | ||
|
|
||
| annotation_id = str(annotation_id) | ||
| parser = reqparse.RequestParser() | ||
| parser.add_argument("question", required=True, type=str, location="json") | ||
| parser.add_argument("answer", required=True, type=str, location="json") | ||
| args = parser.parse_args() | ||
| annotation = AppAnnotationService.update_app_annotation_directly(args, app_model.id, annotation_id) | ||
| return annotation | ||
|
|
||
| @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY)) | ||
| def delete(self, app_model: App, end_user: EndUser, annotation_id): | ||
| if not current_user.is_editor: | ||
| raise Forbidden() | ||
|
|
||
| annotation_id = str(annotation_id) | ||
| AppAnnotationService.delete_app_annotation(app_model.id, annotation_id) | ||
| return {"result": "success"}, 200 | ||
|
|
||
|
|
||
| api.add_resource(AnnotationReplyActionApi, "/apps/annotation-reply/<string:action>") | ||
| api.add_resource(AnnotationReplyActionStatusApi, "/apps/annotation-reply/<string:action>/status/<uuid:job_id>") | ||
| api.add_resource(AnnotationListApi, "/apps/annotations") | ||
| api.add_resource(AnnotationUpdateDeleteApi, "/apps/annotations/<uuid:annotation_id>") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from flask_login import current_user # type: ignore | ||
| from flask_restful import Resource # type: ignore | ||
|
|
||
| from controllers.service_api import api | ||
| from controllers.service_api.wraps import validate_dataset_token | ||
| from core.model_runtime.utils.encoders import jsonable_encoder | ||
| from services.model_provider_service import ModelProviderService | ||
|
|
||
|
|
||
| class ModelProviderAvailableModelApi(Resource): | ||
| @validate_dataset_token | ||
| def get(self, _, model_type): | ||
| tenant_id = current_user.current_tenant_id | ||
|
|
||
| model_provider_service = ModelProviderService() | ||
| models = model_provider_service.get_models_by_model_type(tenant_id=tenant_id, model_type=model_type) | ||
|
|
||
| return jsonable_encoder({"data": models}) | ||
|
|
||
|
|
||
| api.add_resource(ModelProviderAvailableModelApi, "/workspaces/current/models/model-types/<string:model_type>") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.