feat: add explorer completion & chat apis
parent
2439d51231
commit
8b56555437
@ -0,0 +1,180 @@
|
|||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from typing import Generator, Union
|
||||||
|
|
||||||
|
from flask import Response, stream_with_context
|
||||||
|
from flask_login import current_user
|
||||||
|
from flask_restful import reqparse
|
||||||
|
from werkzeug.exceptions import InternalServerError, NotFound
|
||||||
|
|
||||||
|
import services
|
||||||
|
from controllers.console import api
|
||||||
|
from controllers.console.app.error import ConversationCompletedError, AppUnavailableError, ProviderNotInitializeError, \
|
||||||
|
ProviderQuotaExceededError, ProviderModelCurrentlyNotSupportError, CompletionRequestError
|
||||||
|
from controllers.console.explore.error import NotCompletionAppError, NotChatAppError
|
||||||
|
from controllers.console.explore.wraps import InstalledAppResource
|
||||||
|
from core.conversation_message_task import PubHandler
|
||||||
|
from core.llm.error import LLMBadRequestError, LLMAPIUnavailableError, LLMAuthorizationError, LLMAPIConnectionError, \
|
||||||
|
LLMRateLimitError, ProviderTokenNotInitError, QuotaExceededError, ModelCurrentlyNotSupportError
|
||||||
|
from libs.helper import uuid_value
|
||||||
|
from services.completion_service import CompletionService
|
||||||
|
|
||||||
|
|
||||||
|
# define completion api for user
|
||||||
|
class CompletionApi(InstalledAppResource):
|
||||||
|
|
||||||
|
def post(self, installed_app):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'completion':
|
||||||
|
raise NotCompletionAppError()
|
||||||
|
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('inputs', type=dict, required=True, location='json')
|
||||||
|
parser.add_argument('query', type=str, location='json')
|
||||||
|
parser.add_argument('response_mode', type=str, choices=['blocking', 'streaming'], location='json')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
streaming = args['response_mode'] == 'streaming'
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = CompletionService.completion(
|
||||||
|
app_model=app_model,
|
||||||
|
user=current_user,
|
||||||
|
args=args,
|
||||||
|
from_source='api',
|
||||||
|
streaming=streaming
|
||||||
|
)
|
||||||
|
|
||||||
|
return compact_response(response)
|
||||||
|
except services.errors.conversation.ConversationNotExistsError:
|
||||||
|
raise NotFound("Conversation Not Exists.")
|
||||||
|
except services.errors.conversation.ConversationCompletedError:
|
||||||
|
raise ConversationCompletedError()
|
||||||
|
except services.errors.app_model_config.AppModelConfigBrokenError:
|
||||||
|
logging.exception("App model config broken.")
|
||||||
|
raise AppUnavailableError()
|
||||||
|
except ProviderTokenNotInitError:
|
||||||
|
raise ProviderNotInitializeError()
|
||||||
|
except QuotaExceededError:
|
||||||
|
raise ProviderQuotaExceededError()
|
||||||
|
except ModelCurrentlyNotSupportError:
|
||||||
|
raise ProviderModelCurrentlyNotSupportError()
|
||||||
|
except (LLMBadRequestError, LLMAPIConnectionError, LLMAPIUnavailableError,
|
||||||
|
LLMRateLimitError, LLMAuthorizationError) as e:
|
||||||
|
raise CompletionRequestError(str(e))
|
||||||
|
except ValueError as e:
|
||||||
|
raise e
|
||||||
|
except Exception as e:
|
||||||
|
logging.exception("internal server error.")
|
||||||
|
raise InternalServerError()
|
||||||
|
|
||||||
|
|
||||||
|
class CompletionStopApi(InstalledAppResource):
|
||||||
|
def post(self, installed_app, task_id):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'completion':
|
||||||
|
raise NotCompletionAppError()
|
||||||
|
|
||||||
|
PubHandler.stop(current_user, task_id)
|
||||||
|
|
||||||
|
return {'result': 'success'}, 200
|
||||||
|
|
||||||
|
|
||||||
|
class ChatApi(InstalledAppResource):
|
||||||
|
def post(self, installed_app):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'chat':
|
||||||
|
raise NotChatAppError()
|
||||||
|
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('inputs', type=dict, required=True, location='json')
|
||||||
|
parser.add_argument('query', type=str, required=True, location='json')
|
||||||
|
parser.add_argument('response_mode', type=str, choices=['blocking', 'streaming'], location='json')
|
||||||
|
parser.add_argument('conversation_id', type=uuid_value, location='json')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
streaming = args['response_mode'] == 'streaming'
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = CompletionService.completion(
|
||||||
|
app_model=app_model,
|
||||||
|
user=current_user,
|
||||||
|
args=args,
|
||||||
|
from_source='api',
|
||||||
|
streaming=streaming
|
||||||
|
)
|
||||||
|
|
||||||
|
return compact_response(response)
|
||||||
|
except services.errors.conversation.ConversationNotExistsError:
|
||||||
|
raise NotFound("Conversation Not Exists.")
|
||||||
|
except services.errors.conversation.ConversationCompletedError:
|
||||||
|
raise ConversationCompletedError()
|
||||||
|
except services.errors.app_model_config.AppModelConfigBrokenError:
|
||||||
|
logging.exception("App model config broken.")
|
||||||
|
raise AppUnavailableError()
|
||||||
|
except ProviderTokenNotInitError:
|
||||||
|
raise ProviderNotInitializeError()
|
||||||
|
except QuotaExceededError:
|
||||||
|
raise ProviderQuotaExceededError()
|
||||||
|
except ModelCurrentlyNotSupportError:
|
||||||
|
raise ProviderModelCurrentlyNotSupportError()
|
||||||
|
except (LLMBadRequestError, LLMAPIConnectionError, LLMAPIUnavailableError,
|
||||||
|
LLMRateLimitError, LLMAuthorizationError) as e:
|
||||||
|
raise CompletionRequestError(str(e))
|
||||||
|
except ValueError as e:
|
||||||
|
raise e
|
||||||
|
except Exception as e:
|
||||||
|
logging.exception("internal server error.")
|
||||||
|
raise InternalServerError()
|
||||||
|
|
||||||
|
|
||||||
|
class ChatStopApi(InstalledAppResource):
|
||||||
|
def post(self, installed_app, task_id):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'chat':
|
||||||
|
raise NotChatAppError()
|
||||||
|
|
||||||
|
PubHandler.stop(current_user, task_id)
|
||||||
|
|
||||||
|
return {'result': 'success'}, 200
|
||||||
|
|
||||||
|
|
||||||
|
def compact_response(response: Union[dict | Generator]) -> Response:
|
||||||
|
if isinstance(response, dict):
|
||||||
|
return Response(response=json.dumps(response), status=200, mimetype='application/json')
|
||||||
|
else:
|
||||||
|
def generate() -> Generator:
|
||||||
|
try:
|
||||||
|
for chunk in response:
|
||||||
|
yield chunk
|
||||||
|
except services.errors.conversation.ConversationNotExistsError:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(NotFound("Conversation Not Exists.")).get_json()) + "\n\n"
|
||||||
|
except services.errors.conversation.ConversationCompletedError:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(ConversationCompletedError()).get_json()) + "\n\n"
|
||||||
|
except services.errors.app_model_config.AppModelConfigBrokenError:
|
||||||
|
logging.exception("App model config broken.")
|
||||||
|
yield "data: " + json.dumps(api.handle_error(AppUnavailableError()).get_json()) + "\n\n"
|
||||||
|
except ProviderTokenNotInitError:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(ProviderNotInitializeError()).get_json()) + "\n\n"
|
||||||
|
except QuotaExceededError:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(ProviderQuotaExceededError()).get_json()) + "\n\n"
|
||||||
|
except ModelCurrentlyNotSupportError:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(ProviderModelCurrentlyNotSupportError()).get_json()) + "\n\n"
|
||||||
|
except (LLMBadRequestError, LLMAPIConnectionError, LLMAPIUnavailableError,
|
||||||
|
LLMRateLimitError, LLMAuthorizationError) as e:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(CompletionRequestError(str(e))).get_json()) + "\n\n"
|
||||||
|
except ValueError as e:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(e).get_json()) + "\n\n"
|
||||||
|
except Exception:
|
||||||
|
logging.exception("internal server error.")
|
||||||
|
yield "data: " + json.dumps(api.handle_error(InternalServerError()).get_json()) + "\n\n"
|
||||||
|
|
||||||
|
return Response(stream_with_context(generate()), status=200,
|
||||||
|
mimetype='text/event-stream')
|
||||||
|
|
||||||
|
|
||||||
|
api.add_resource(CompletionApi, '/completion-messages')
|
||||||
|
api.add_resource(CompletionStopApi, '/completion-messages/<string:task_id>/stop')
|
||||||
|
api.add_resource(ChatApi, '/chat-messages')
|
||||||
|
api.add_resource(ChatStopApi, '/chat-messages/<string:task_id>/stop')
|
||||||
@ -0,0 +1,127 @@
|
|||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
from flask_login import current_user
|
||||||
|
from flask_restful import fields, reqparse, marshal_with
|
||||||
|
from flask_restful.inputs import int_range
|
||||||
|
from werkzeug.exceptions import NotFound
|
||||||
|
|
||||||
|
from controllers.console import api
|
||||||
|
from controllers.console.explore.error import NotChatAppError
|
||||||
|
from controllers.console.explore.wraps import InstalledAppResource
|
||||||
|
from libs.helper import TimestampField, uuid_value
|
||||||
|
from services.conversation_service import ConversationService
|
||||||
|
from services.errors.conversation import LastConversationNotExistsError, ConversationNotExistsError
|
||||||
|
from services.web_conversation_service import WebConversationService
|
||||||
|
|
||||||
|
conversation_fields = {
|
||||||
|
'id': fields.String,
|
||||||
|
'name': fields.String,
|
||||||
|
'inputs': fields.Raw,
|
||||||
|
'status': fields.String,
|
||||||
|
'introduction': fields.String,
|
||||||
|
'created_at': TimestampField
|
||||||
|
}
|
||||||
|
|
||||||
|
conversation_infinite_scroll_pagination_fields = {
|
||||||
|
'limit': fields.Integer,
|
||||||
|
'has_more': fields.Boolean,
|
||||||
|
'data': fields.List(fields.Nested(conversation_fields))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ConversationListApi(InstalledAppResource):
|
||||||
|
|
||||||
|
@marshal_with(conversation_infinite_scroll_pagination_fields)
|
||||||
|
def get(self, installed_app):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'chat':
|
||||||
|
raise NotChatAppError()
|
||||||
|
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('last_id', type=uuid_value, location='args')
|
||||||
|
parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args')
|
||||||
|
parser.add_argument('pinned', type=str, choices=['true', 'false', None], location='args')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
pinned = None
|
||||||
|
if 'pinned' in args and args['pinned'] is not None:
|
||||||
|
pinned = True if args['pinned'] == 'true' else False
|
||||||
|
|
||||||
|
try:
|
||||||
|
return WebConversationService.pagination_by_last_id(
|
||||||
|
app_model=app_model,
|
||||||
|
end_user=current_user,
|
||||||
|
last_id=args['last_id'],
|
||||||
|
limit=args['limit'],
|
||||||
|
pinned=pinned
|
||||||
|
)
|
||||||
|
except LastConversationNotExistsError:
|
||||||
|
raise NotFound("Last Conversation Not Exists.")
|
||||||
|
|
||||||
|
|
||||||
|
class ConversationApi(InstalledAppResource):
|
||||||
|
def delete(self, installed_app, c_id):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'chat':
|
||||||
|
raise NotChatAppError()
|
||||||
|
|
||||||
|
conversation_id = str(c_id)
|
||||||
|
ConversationService.delete(app_model, conversation_id, current_user)
|
||||||
|
WebConversationService.unpin(app_model, conversation_id, current_user)
|
||||||
|
|
||||||
|
return {"result": "success"}, 204
|
||||||
|
|
||||||
|
|
||||||
|
class ConversationRenameApi(InstalledAppResource):
|
||||||
|
|
||||||
|
@marshal_with(conversation_fields)
|
||||||
|
def post(self, installed_app, c_id):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'chat':
|
||||||
|
raise NotChatAppError()
|
||||||
|
|
||||||
|
conversation_id = str(c_id)
|
||||||
|
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('name', type=str, required=True, location='json')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
try:
|
||||||
|
return ConversationService.rename(app_model, conversation_id, end_user, args['name'])
|
||||||
|
except ConversationNotExistsError:
|
||||||
|
raise NotFound("Conversation Not Exists.")
|
||||||
|
|
||||||
|
|
||||||
|
class ConversationPinApi(InstalledAppResource):
|
||||||
|
|
||||||
|
def patch(self, installed_app, c_id):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'chat':
|
||||||
|
raise NotChatAppError()
|
||||||
|
|
||||||
|
conversation_id = str(c_id)
|
||||||
|
|
||||||
|
try:
|
||||||
|
WebConversationService.pin(app_model, conversation_id, current_user)
|
||||||
|
except ConversationNotExistsError:
|
||||||
|
raise NotFound("Conversation Not Exists.")
|
||||||
|
|
||||||
|
return {"result": "success"}
|
||||||
|
|
||||||
|
|
||||||
|
class ConversationUnPinApi(InstalledAppResource):
|
||||||
|
def patch(self, installed_app, c_id):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'chat':
|
||||||
|
raise NotChatAppError()
|
||||||
|
|
||||||
|
conversation_id = str(c_id)
|
||||||
|
WebConversationService.unpin(app_model, conversation_id, current_user)
|
||||||
|
|
||||||
|
return {"result": "success"}
|
||||||
|
|
||||||
|
|
||||||
|
api.add_resource(ConversationRenameApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/name')
|
||||||
|
api.add_resource(ConversationListApi, '/installed-apps/<uuid:installed_app_id>/conversations')
|
||||||
|
api.add_resource(ConversationApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>')
|
||||||
|
api.add_resource(ConversationPinApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/pin')
|
||||||
|
api.add_resource(ConversationUnPinApi, '/installed-apps/<uuid:installed_app_id>/conversations/<uuid:c_id>/unpin')
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
from libs.exception import BaseHTTPException
|
||||||
|
|
||||||
|
|
||||||
|
class NotCompletionAppError(BaseHTTPException):
|
||||||
|
error_code = 'not_completion_app'
|
||||||
|
description = "Not Completion App"
|
||||||
|
code = 400
|
||||||
|
|
||||||
|
|
||||||
|
class NotChatAppError(BaseHTTPException):
|
||||||
|
error_code = 'not_chat_app'
|
||||||
|
description = "Not Chat App"
|
||||||
|
code = 400
|
||||||
|
|
||||||
|
|
||||||
|
class AppSuggestedQuestionsAfterAnswerDisabledError(BaseHTTPException):
|
||||||
|
error_code = 'app_suggested_questions_after_answer_disabled'
|
||||||
|
description = "Function Suggested questions after answer disabled."
|
||||||
|
code = 403
|
||||||
@ -0,0 +1,132 @@
|
|||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from flask_login import login_required, current_user
|
||||||
|
from flask_restful import Resource, reqparse, fields, marshal_with, abort, inputs
|
||||||
|
from sqlalchemy import and_
|
||||||
|
|
||||||
|
from controllers.console import api
|
||||||
|
from controllers.console.explore.wraps import InstalledAppResource
|
||||||
|
from controllers.console.wraps import account_initialization_required
|
||||||
|
from extensions.ext_database import db
|
||||||
|
from models.model import Tenant, App, InstalledApp, RecommendedApp
|
||||||
|
from services.account_service import TenantService
|
||||||
|
|
||||||
|
app_fields = {
|
||||||
|
'id': fields.String,
|
||||||
|
'name': fields.String,
|
||||||
|
'mode': fields.String,
|
||||||
|
'icon': fields.String,
|
||||||
|
'icon_background': fields.String
|
||||||
|
}
|
||||||
|
|
||||||
|
installed_app_fields = {
|
||||||
|
'id': fields.String,
|
||||||
|
'app': fields.Nested(app_fields, attribute='app'),
|
||||||
|
'app_owner_tenant_id': fields.String,
|
||||||
|
'is_pinned': fields.Boolean,
|
||||||
|
'last_used_at': fields.DateTime,
|
||||||
|
'editable': fields.Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
installed_app_list_fields = {
|
||||||
|
'installed_apps': fields.List(fields.Nested(installed_app_fields))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class InstalledAppsListApi(Resource):
|
||||||
|
@login_required
|
||||||
|
@account_initialization_required
|
||||||
|
@marshal_with(installed_app_list_fields)
|
||||||
|
def get(self):
|
||||||
|
current_tenant_id = Tenant.query.first().id
|
||||||
|
installed_apps = db.session.query(InstalledApp).filter(
|
||||||
|
InstalledApp.tenant_id == current_tenant_id
|
||||||
|
).all()
|
||||||
|
|
||||||
|
current_user.role = TenantService.get_user_role(current_user, current_user.current_tenant)
|
||||||
|
installed_apps = [
|
||||||
|
{
|
||||||
|
'id': installed_app.id,
|
||||||
|
'app': installed_app.app,
|
||||||
|
'app_owner_tenant_id': installed_app.app_owner_tenant_id,
|
||||||
|
'is_pinned': installed_app.is_pinned,
|
||||||
|
'last_used_at': installed_app.last_used_at,
|
||||||
|
"editable": current_user.role in ["owner", "admin"],
|
||||||
|
}
|
||||||
|
for installed_app in installed_apps
|
||||||
|
]
|
||||||
|
installed_apps.sort(key=lambda app: (-app['is_pinned'], app['last_used_at']))
|
||||||
|
|
||||||
|
return {'installed_apps': installed_apps}
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
@account_initialization_required
|
||||||
|
def post(self):
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('app_id', type=str, required=True, help='Invalid app_id')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
current_tenant_id = Tenant.query.first().id
|
||||||
|
app = App.query.get(args['app_id'])
|
||||||
|
if app is None:
|
||||||
|
abort(404, message='App not found')
|
||||||
|
recommended_app = RecommendedApp.query.filter(RecommendedApp.app_id == args['app_id']).first()
|
||||||
|
if recommended_app is None:
|
||||||
|
abort(404, message='App not found')
|
||||||
|
if not app.is_public:
|
||||||
|
abort(403, message="You can't install a non-public app")
|
||||||
|
|
||||||
|
installed_app = InstalledApp.query.filter(and_(
|
||||||
|
InstalledApp.app_id == args['app_id'],
|
||||||
|
InstalledApp.tenant_id == current_tenant_id
|
||||||
|
)).first()
|
||||||
|
|
||||||
|
if installed_app is None:
|
||||||
|
# todo: position
|
||||||
|
recommended_app.install_count += 1
|
||||||
|
|
||||||
|
new_installed_app = InstalledApp(
|
||||||
|
app_id=args['app_id'],
|
||||||
|
tenant_id=current_tenant_id,
|
||||||
|
is_pinned=False,
|
||||||
|
last_used_at=datetime.utcnow()
|
||||||
|
)
|
||||||
|
db.session.add(new_installed_app)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return {'message': 'App installed successfully'}
|
||||||
|
|
||||||
|
|
||||||
|
class InstalledAppApi(InstalledAppResource):
|
||||||
|
"""
|
||||||
|
update and delete an installed app
|
||||||
|
use InstalledAppResource to apply default decorators and get installed_app
|
||||||
|
"""
|
||||||
|
def delete(self, installed_app):
|
||||||
|
if installed_app.app_owner_tenant_id == current_user.current_tenant_id:
|
||||||
|
abort(400, message="You can't uninstall an app owned by the current tenant")
|
||||||
|
|
||||||
|
db.session.delete(installed_app)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return {'result': 'success', 'message': 'App uninstalled successfully'}
|
||||||
|
|
||||||
|
def patch(self, installed_app):
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('is_pinned', type=inputs.boolean)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
commit_args = False
|
||||||
|
if 'is_pinned' in args:
|
||||||
|
installed_app.is_pinned = args['is_pinned']
|
||||||
|
commit_args = True
|
||||||
|
|
||||||
|
if commit_args:
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return {'result': 'success', 'message': 'App info updated successfully'}
|
||||||
|
|
||||||
|
|
||||||
|
api.add_resource(InstalledAppsListApi, '/installed-apps')
|
||||||
|
api.add_resource(InstalledAppApi, '/installed-apps/<uuid:installed_app_id>')
|
||||||
@ -0,0 +1,196 @@
|
|||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from typing import Generator, Union
|
||||||
|
|
||||||
|
from flask import stream_with_context, Response
|
||||||
|
from flask_login import current_user
|
||||||
|
from flask_restful import reqparse, fields, marshal_with
|
||||||
|
from flask_restful.inputs import int_range
|
||||||
|
from werkzeug.exceptions import NotFound, InternalServerError
|
||||||
|
|
||||||
|
import services
|
||||||
|
from controllers.console import api
|
||||||
|
from controllers.console.app.error import AppMoreLikeThisDisabledError, ProviderNotInitializeError, \
|
||||||
|
ProviderQuotaExceededError, ProviderModelCurrentlyNotSupportError, CompletionRequestError
|
||||||
|
from controllers.console.explore.error import NotCompletionAppError, AppSuggestedQuestionsAfterAnswerDisabledError
|
||||||
|
from controllers.console.explore.wraps import InstalledAppResource
|
||||||
|
from core.llm.error import LLMRateLimitError, LLMBadRequestError, LLMAuthorizationError, LLMAPIConnectionError, \
|
||||||
|
ProviderTokenNotInitError, LLMAPIUnavailableError, QuotaExceededError, ModelCurrentlyNotSupportError
|
||||||
|
from libs.helper import uuid_value, TimestampField
|
||||||
|
from services.completion_service import CompletionService
|
||||||
|
from services.errors.app import MoreLikeThisDisabledError
|
||||||
|
from services.errors.conversation import ConversationNotExistsError
|
||||||
|
from services.errors.message import MessageNotExistsError, SuggestedQuestionsAfterAnswerDisabledError
|
||||||
|
from services.message_service import MessageService
|
||||||
|
|
||||||
|
|
||||||
|
class MessageListApi(InstalledAppResource):
|
||||||
|
feedback_fields = {
|
||||||
|
'rating': fields.String
|
||||||
|
}
|
||||||
|
|
||||||
|
message_fields = {
|
||||||
|
'id': fields.String,
|
||||||
|
'conversation_id': fields.String,
|
||||||
|
'inputs': fields.Raw,
|
||||||
|
'query': fields.String,
|
||||||
|
'answer': fields.String,
|
||||||
|
'feedback': fields.Nested(feedback_fields, attribute='user_feedback', allow_null=True),
|
||||||
|
'created_at': TimestampField
|
||||||
|
}
|
||||||
|
|
||||||
|
message_infinite_scroll_pagination_fields = {
|
||||||
|
'limit': fields.Integer,
|
||||||
|
'has_more': fields.Boolean,
|
||||||
|
'data': fields.List(fields.Nested(message_fields))
|
||||||
|
}
|
||||||
|
|
||||||
|
@marshal_with(message_infinite_scroll_pagination_fields)
|
||||||
|
def get(self, installed_app):
|
||||||
|
app_model = installed_app.app
|
||||||
|
|
||||||
|
if app_model.mode != 'chat':
|
||||||
|
raise NotChatAppError()
|
||||||
|
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('conversation_id', required=True, type=uuid_value, location='args')
|
||||||
|
parser.add_argument('first_id', type=uuid_value, location='args')
|
||||||
|
parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
try:
|
||||||
|
return MessageService.pagination_by_first_id(app_model, current_user,
|
||||||
|
args['conversation_id'], args['first_id'], args['limit'])
|
||||||
|
except services.errors.conversation.ConversationNotExistsError:
|
||||||
|
raise NotFound("Conversation Not Exists.")
|
||||||
|
except services.errors.message.FirstMessageNotExistsError:
|
||||||
|
raise NotFound("First Message Not Exists.")
|
||||||
|
|
||||||
|
|
||||||
|
class MessageFeedbackApi(InstalledAppResource):
|
||||||
|
def post(self, installed_app, message_id):
|
||||||
|
app_model = installed_app.app
|
||||||
|
|
||||||
|
message_id = str(message_id)
|
||||||
|
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('rating', type=str, choices=['like', 'dislike', None], location='json')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
try:
|
||||||
|
MessageService.create_feedback(app_model, message_id, current_user, args['rating'])
|
||||||
|
except services.errors.message.MessageNotExistsError:
|
||||||
|
raise NotFound("Message Not Exists.")
|
||||||
|
|
||||||
|
return {'result': 'success'}
|
||||||
|
|
||||||
|
|
||||||
|
class MessageMoreLikeThisApi(InstalledAppResource):
|
||||||
|
def get(self, installed_app, message_id):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'completion':
|
||||||
|
raise NotCompletionAppError()
|
||||||
|
|
||||||
|
message_id = str(message_id)
|
||||||
|
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('response_mode', type=str, required=True, choices=['blocking', 'streaming'], location='args')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
streaming = args['response_mode'] == 'streaming'
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = CompletionService.generate_more_like_this(app_model, current_user, message_id, streaming)
|
||||||
|
return compact_response(response)
|
||||||
|
except MessageNotExistsError:
|
||||||
|
raise NotFound("Message Not Exists.")
|
||||||
|
except MoreLikeThisDisabledError:
|
||||||
|
raise AppMoreLikeThisDisabledError()
|
||||||
|
except ProviderTokenNotInitError:
|
||||||
|
raise ProviderNotInitializeError()
|
||||||
|
except QuotaExceededError:
|
||||||
|
raise ProviderQuotaExceededError()
|
||||||
|
except ModelCurrentlyNotSupportError:
|
||||||
|
raise ProviderModelCurrentlyNotSupportError()
|
||||||
|
except (LLMBadRequestError, LLMAPIConnectionError, LLMAPIUnavailableError,
|
||||||
|
LLMRateLimitError, LLMAuthorizationError) as e:
|
||||||
|
raise CompletionRequestError(str(e))
|
||||||
|
except ValueError as e:
|
||||||
|
raise e
|
||||||
|
except Exception:
|
||||||
|
logging.exception("internal server error.")
|
||||||
|
raise InternalServerError()
|
||||||
|
|
||||||
|
|
||||||
|
def compact_response(response: Union[dict | Generator]) -> Response:
|
||||||
|
if isinstance(response, dict):
|
||||||
|
return Response(response=json.dumps(response), status=200, mimetype='application/json')
|
||||||
|
else:
|
||||||
|
def generate() -> Generator:
|
||||||
|
try:
|
||||||
|
for chunk in response:
|
||||||
|
yield chunk
|
||||||
|
except MessageNotExistsError:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(NotFound("Message Not Exists.")).get_json()) + "\n\n"
|
||||||
|
except MoreLikeThisDisabledError:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(AppMoreLikeThisDisabledError()).get_json()) + "\n\n"
|
||||||
|
except ProviderTokenNotInitError:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(ProviderNotInitializeError()).get_json()) + "\n\n"
|
||||||
|
except QuotaExceededError:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(ProviderQuotaExceededError()).get_json()) + "\n\n"
|
||||||
|
except ModelCurrentlyNotSupportError:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(ProviderModelCurrentlyNotSupportError()).get_json()) + "\n\n"
|
||||||
|
except (LLMBadRequestError, LLMAPIConnectionError, LLMAPIUnavailableError,
|
||||||
|
LLMRateLimitError, LLMAuthorizationError) as e:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(CompletionRequestError(str(e))).get_json()) + "\n\n"
|
||||||
|
except ValueError as e:
|
||||||
|
yield "data: " + json.dumps(api.handle_error(e).get_json()) + "\n\n"
|
||||||
|
except Exception:
|
||||||
|
logging.exception("internal server error.")
|
||||||
|
yield "data: " + json.dumps(api.handle_error(InternalServerError()).get_json()) + "\n\n"
|
||||||
|
|
||||||
|
return Response(stream_with_context(generate()), status=200,
|
||||||
|
mimetype='text/event-stream')
|
||||||
|
|
||||||
|
|
||||||
|
class MessageSuggestedQuestionApi(InstalledAppResource):
|
||||||
|
def get(self, installed_app, message_id):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'chat':
|
||||||
|
raise NotCompletionAppError()
|
||||||
|
|
||||||
|
message_id = str(message_id)
|
||||||
|
|
||||||
|
try:
|
||||||
|
questions = MessageService.get_suggested_questions_after_answer(
|
||||||
|
app_model=app_model,
|
||||||
|
user=current_user,
|
||||||
|
message_id=message_id
|
||||||
|
)
|
||||||
|
except MessageNotExistsError:
|
||||||
|
raise NotFound("Message not found")
|
||||||
|
except ConversationNotExistsError:
|
||||||
|
raise NotFound("Conversation not found")
|
||||||
|
except SuggestedQuestionsAfterAnswerDisabledError:
|
||||||
|
raise AppSuggestedQuestionsAfterAnswerDisabledError()
|
||||||
|
except ProviderTokenNotInitError:
|
||||||
|
raise ProviderNotInitializeError()
|
||||||
|
except QuotaExceededError:
|
||||||
|
raise ProviderQuotaExceededError()
|
||||||
|
except ModelCurrentlyNotSupportError:
|
||||||
|
raise ProviderModelCurrentlyNotSupportError()
|
||||||
|
except (LLMBadRequestError, LLMAPIConnectionError, LLMAPIUnavailableError,
|
||||||
|
LLMRateLimitError, LLMAuthorizationError) as e:
|
||||||
|
raise CompletionRequestError(str(e))
|
||||||
|
except Exception:
|
||||||
|
logging.exception("internal server error.")
|
||||||
|
raise InternalServerError()
|
||||||
|
|
||||||
|
return {'data': questions}
|
||||||
|
|
||||||
|
|
||||||
|
api.add_resource(MessageListApi, '/installed-apps/<uuid:installed_app_id>/messages')
|
||||||
|
api.add_resource(MessageFeedbackApi, '/installed-apps/<uuid:installed_app_id>/messages/<uuid:message_id>/feedbacks')
|
||||||
|
api.add_resource(MessageMoreLikeThisApi, '/installed-apps/<uuid:installed_app_id>/messages/<uuid:message_id>/more-like-this')
|
||||||
|
api.add_resource(MessageSuggestedQuestionApi, '/installed-apps/<uuid:installed_app_id>/messages/<uuid:message_id>/suggested-questions')
|
||||||
@ -0,0 +1,43 @@
|
|||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
from flask_restful import marshal_with, fields
|
||||||
|
|
||||||
|
from controllers.console import api
|
||||||
|
from controllers.console.explore.wraps import InstalledAppResource
|
||||||
|
|
||||||
|
|
||||||
|
class AppParameterApi(InstalledAppResource):
|
||||||
|
"""Resource for app variables."""
|
||||||
|
variable_fields = {
|
||||||
|
'key': fields.String,
|
||||||
|
'name': fields.String,
|
||||||
|
'description': fields.String,
|
||||||
|
'type': fields.String,
|
||||||
|
'default': fields.String,
|
||||||
|
'max_length': fields.Integer,
|
||||||
|
'options': fields.List(fields.String)
|
||||||
|
}
|
||||||
|
|
||||||
|
parameters_fields = {
|
||||||
|
'opening_statement': fields.String,
|
||||||
|
'suggested_questions': fields.Raw,
|
||||||
|
'suggested_questions_after_answer': fields.Raw,
|
||||||
|
'more_like_this': fields.Raw,
|
||||||
|
'user_input_form': fields.Raw,
|
||||||
|
}
|
||||||
|
|
||||||
|
@marshal_with(parameters_fields)
|
||||||
|
def get(self, installed_app):
|
||||||
|
"""Retrieve app parameters."""
|
||||||
|
app_model = installed_app.app
|
||||||
|
app_model_config = app_model.app_model_config
|
||||||
|
|
||||||
|
return {
|
||||||
|
'opening_statement': app_model_config.opening_statement,
|
||||||
|
'suggested_questions': app_model_config.suggested_questions_list,
|
||||||
|
'suggested_questions_after_answer': app_model_config.suggested_questions_after_answer_dict,
|
||||||
|
'more_like_this': app_model_config.more_like_this_dict,
|
||||||
|
'user_input_form': app_model_config.user_input_form_list
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
api.add_resource(AppParameterApi, '/installed-apps/<uuid:installed_app_id>/parameters')
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
from flask_login import current_user
|
||||||
|
from flask_restful import reqparse, marshal_with, fields
|
||||||
|
from flask_restful.inputs import int_range
|
||||||
|
from werkzeug.exceptions import NotFound
|
||||||
|
|
||||||
|
from controllers.console import api
|
||||||
|
from controllers.console.explore.error import NotCompletionAppError
|
||||||
|
from controllers.console.explore.wraps import InstalledAppResource
|
||||||
|
from libs.helper import uuid_value, TimestampField
|
||||||
|
from services.errors.message import MessageNotExistsError
|
||||||
|
from services.saved_message_service import SavedMessageService
|
||||||
|
|
||||||
|
feedback_fields = {
|
||||||
|
'rating': fields.String
|
||||||
|
}
|
||||||
|
|
||||||
|
message_fields = {
|
||||||
|
'id': fields.String,
|
||||||
|
'inputs': fields.Raw,
|
||||||
|
'query': fields.String,
|
||||||
|
'answer': fields.String,
|
||||||
|
'feedback': fields.Nested(feedback_fields, attribute='user_feedback', allow_null=True),
|
||||||
|
'created_at': TimestampField
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class SavedMessageListApi(InstalledAppResource):
|
||||||
|
saved_message_infinite_scroll_pagination_fields = {
|
||||||
|
'limit': fields.Integer,
|
||||||
|
'has_more': fields.Boolean,
|
||||||
|
'data': fields.List(fields.Nested(message_fields))
|
||||||
|
}
|
||||||
|
|
||||||
|
@marshal_with(saved_message_infinite_scroll_pagination_fields)
|
||||||
|
def get(self, installed_app):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'completion':
|
||||||
|
raise NotCompletionAppError()
|
||||||
|
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('last_id', type=uuid_value, location='args')
|
||||||
|
parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
return SavedMessageService.pagination_by_last_id(app_model, current_user, args['last_id'], args['limit'])
|
||||||
|
|
||||||
|
def post(self, installed_app):
|
||||||
|
app_model = installed_app.app
|
||||||
|
if app_model.mode != 'completion':
|
||||||
|
raise NotCompletionAppError()
|
||||||
|
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument('message_id', type=uuid_value, required=True, location='json')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
try:
|
||||||
|
SavedMessageService.save(app_model, current_user, args['message_id'])
|
||||||
|
except MessageNotExistsError:
|
||||||
|
raise NotFound("Message Not Exists.")
|
||||||
|
|
||||||
|
return {'result': 'success'}
|
||||||
|
|
||||||
|
|
||||||
|
class SavedMessageApi(InstalledAppResource):
|
||||||
|
def delete(self, installed_app, message_id):
|
||||||
|
app_model = installed_app.app
|
||||||
|
|
||||||
|
message_id = str(message_id)
|
||||||
|
|
||||||
|
if app_model.mode != 'completion':
|
||||||
|
raise NotCompletionAppError()
|
||||||
|
|
||||||
|
SavedMessageService.delete(app_model, current_user, message_id)
|
||||||
|
|
||||||
|
return {'result': 'success'}
|
||||||
|
|
||||||
|
|
||||||
|
api.add_resource(SavedMessageListApi, '/installed-apps/<uuid:installed_app_id>/saved-messages')
|
||||||
|
api.add_resource(SavedMessageApi, '/installed-apps/<uuid:installed_app_id>/saved-messages/<uuid:message_id>')
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
from flask_login import login_required, current_user
|
||||||
|
from flask_restful import Resource
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
|
from werkzeug.exceptions import NotFound
|
||||||
|
|
||||||
|
from controllers.console.wraps import account_initialization_required
|
||||||
|
from extensions.ext_database import db
|
||||||
|
from models.model import InstalledApp
|
||||||
|
|
||||||
|
|
||||||
|
def installed_app_required(view=None):
|
||||||
|
def decorator(view):
|
||||||
|
@wraps(view)
|
||||||
|
def decorated(*args, **kwargs):
|
||||||
|
if not kwargs.get('installed_app_id'):
|
||||||
|
raise ValueError('missing installed_app_id in path parameters')
|
||||||
|
|
||||||
|
installed_app_id = kwargs.get('installed_app_id')
|
||||||
|
installed_app_id = str(installed_app_id)
|
||||||
|
|
||||||
|
del kwargs['installed_app_id']
|
||||||
|
|
||||||
|
installed_app = db.session.query(InstalledApp).filter(
|
||||||
|
InstalledApp.id == str(installed_app_id),
|
||||||
|
InstalledApp.tenant_id == current_user.current_tenant_id
|
||||||
|
).first()
|
||||||
|
|
||||||
|
if installed_app is None:
|
||||||
|
NotFound('Installed app not found')
|
||||||
|
|
||||||
|
return view(installed_app, *args, **kwargs)
|
||||||
|
return decorated
|
||||||
|
|
||||||
|
if view:
|
||||||
|
return decorator(view)
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
class InstalledAppResource(Resource):
|
||||||
|
# must be reversed if there are multiple decorators
|
||||||
|
method_decorators = [installed_app_required, account_initialization_required, login_required]
|
||||||
Loading…
Reference in New Issue