mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +08:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: zhanluxianshen <zhanluxianshen@163.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: Davide Delbianco <davide.delbianco@outlook.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: Yongtao Huang <99629139+hyongtao-db@users.noreply.github.com> Co-authored-by: Qiang Lee <18018968632@163.com> Co-authored-by: 李强04 <liqiang04@gaotu.cn> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Matri Qi <matrixdom@126.com> Co-authored-by: huayaoyue6 <huayaoyue@163.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: znn <jubinkumarsoni@gmail.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Muke Wang <shaodwaaron@gmail.com> Co-authored-by: wangmuke <wangmuke@kingsware.cn> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Eric Guo <eric.guocz@gmail.com> Co-authored-by: Zhedong Cen <cenzhedong2@126.com> Co-authored-by: jiangbo721 <jiangbo721@163.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: hjlarry <25834719+hjlarry@users.noreply.github.com> Co-authored-by: lxsummer <35754229+lxjustdoit@users.noreply.github.com> Co-authored-by: 湛露先生 <zhanluxianshen@163.com> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Yessenia-d <yessenia.contact@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: 17hz <0x149527@gmail.com> Co-authored-by: Amy <1530140574@qq.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Nite Knite <nkCoding@gmail.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: Petrus Han <petrus.hanks@gmail.com> Co-authored-by: iamjoel <2120155+iamjoel@users.noreply.github.com> Co-authored-by: Kalo Chin <frog.beepers.0n@icloud.com> Co-authored-by: Ujjwal Maurya <ujjwalsbx@gmail.com> Co-authored-by: Maries <xh001x@hotmail.com>
237 lines
7.9 KiB
Python
237 lines
7.9 KiB
Python
from flask_restx import Api, Namespace, fields
|
|
|
|
from fields.member_fields import simple_account_fields
|
|
from libs.helper import TimestampField
|
|
|
|
from .raws import FilesContainedField
|
|
|
|
|
|
class MessageTextField(fields.Raw):
|
|
def format(self, value):
|
|
return value[0]["text"] if value else ""
|
|
|
|
|
|
feedback_fields = {
|
|
"rating": fields.String,
|
|
"content": fields.String,
|
|
"from_source": fields.String,
|
|
"from_end_user_id": fields.String,
|
|
"from_account": fields.Nested(simple_account_fields, allow_null=True),
|
|
}
|
|
|
|
annotation_fields = {
|
|
"id": fields.String,
|
|
"question": fields.String,
|
|
"content": fields.String,
|
|
"account": fields.Nested(simple_account_fields, allow_null=True),
|
|
"created_at": TimestampField,
|
|
}
|
|
|
|
annotation_hit_history_fields = {
|
|
"annotation_id": fields.String(attribute="id"),
|
|
"annotation_create_account": fields.Nested(simple_account_fields, allow_null=True),
|
|
"created_at": TimestampField,
|
|
}
|
|
|
|
message_file_fields = {
|
|
"id": fields.String,
|
|
"filename": fields.String,
|
|
"type": fields.String,
|
|
"url": fields.String,
|
|
"mime_type": fields.String,
|
|
"size": fields.Integer,
|
|
"transfer_method": fields.String,
|
|
"belongs_to": fields.String(default="user"),
|
|
"upload_file_id": fields.String(default=None),
|
|
}
|
|
|
|
|
|
def build_message_file_model(api_or_ns: Api | Namespace):
|
|
"""Build the message file fields for the API or Namespace."""
|
|
return api_or_ns.model("MessageFile", message_file_fields)
|
|
|
|
|
|
agent_thought_fields = {
|
|
"id": fields.String,
|
|
"chain_id": fields.String,
|
|
"message_id": fields.String,
|
|
"position": fields.Integer,
|
|
"thought": fields.String,
|
|
"tool": fields.String,
|
|
"tool_labels": fields.Raw,
|
|
"tool_input": fields.String,
|
|
"created_at": TimestampField,
|
|
"observation": fields.String,
|
|
"files": fields.List(fields.String),
|
|
}
|
|
|
|
message_detail_fields = {
|
|
"id": fields.String,
|
|
"conversation_id": fields.String,
|
|
"inputs": FilesContainedField,
|
|
"query": fields.String,
|
|
"message": fields.Raw,
|
|
"message_tokens": fields.Integer,
|
|
"answer": fields.String(attribute="re_sign_file_url_answer"),
|
|
"answer_tokens": fields.Integer,
|
|
"provider_response_latency": fields.Float,
|
|
"from_source": fields.String,
|
|
"from_end_user_id": fields.String,
|
|
"from_account_id": fields.String,
|
|
"feedbacks": fields.List(fields.Nested(feedback_fields)),
|
|
"workflow_run_id": fields.String,
|
|
"annotation": fields.Nested(annotation_fields, allow_null=True),
|
|
"annotation_hit_history": fields.Nested(annotation_hit_history_fields, allow_null=True),
|
|
"created_at": TimestampField,
|
|
"agent_thoughts": fields.List(fields.Nested(agent_thought_fields)),
|
|
"message_files": fields.List(fields.Nested(message_file_fields)),
|
|
"metadata": fields.Raw(attribute="message_metadata_dict"),
|
|
"status": fields.String,
|
|
"error": fields.String,
|
|
"parent_message_id": fields.String,
|
|
}
|
|
|
|
feedback_stat_fields = {"like": fields.Integer, "dislike": fields.Integer}
|
|
status_count_fields = {"success": fields.Integer, "failed": fields.Integer, "partial_success": fields.Integer}
|
|
model_config_fields = {
|
|
"opening_statement": fields.String,
|
|
"suggested_questions": fields.Raw,
|
|
"model": fields.Raw,
|
|
"user_input_form": fields.Raw,
|
|
"pre_prompt": fields.String,
|
|
"agent_mode": fields.Raw,
|
|
}
|
|
|
|
simple_model_config_fields = {
|
|
"model": fields.Raw(attribute="model_dict"),
|
|
"pre_prompt": fields.String,
|
|
}
|
|
|
|
simple_message_detail_fields = {
|
|
"inputs": FilesContainedField,
|
|
"query": fields.String,
|
|
"message": MessageTextField,
|
|
"answer": fields.String,
|
|
}
|
|
|
|
conversation_fields = {
|
|
"id": fields.String,
|
|
"status": fields.String,
|
|
"from_source": fields.String,
|
|
"from_end_user_id": fields.String,
|
|
"from_end_user_session_id": fields.String(),
|
|
"from_account_id": fields.String,
|
|
"from_account_name": fields.String,
|
|
"read_at": TimestampField,
|
|
"created_at": TimestampField,
|
|
"updated_at": TimestampField,
|
|
"annotation": fields.Nested(annotation_fields, allow_null=True),
|
|
"model_config": fields.Nested(simple_model_config_fields),
|
|
"user_feedback_stats": fields.Nested(feedback_stat_fields),
|
|
"admin_feedback_stats": fields.Nested(feedback_stat_fields),
|
|
"message": fields.Nested(simple_message_detail_fields, attribute="first_message"),
|
|
}
|
|
|
|
conversation_pagination_fields = {
|
|
"page": fields.Integer,
|
|
"limit": fields.Integer(attribute="per_page"),
|
|
"total": fields.Integer,
|
|
"has_more": fields.Boolean(attribute="has_next"),
|
|
"data": fields.List(fields.Nested(conversation_fields), attribute="items"),
|
|
}
|
|
|
|
conversation_message_detail_fields = {
|
|
"id": fields.String,
|
|
"status": fields.String,
|
|
"from_source": fields.String,
|
|
"from_end_user_id": fields.String,
|
|
"from_account_id": fields.String,
|
|
"created_at": TimestampField,
|
|
"model_config": fields.Nested(model_config_fields),
|
|
"message": fields.Nested(message_detail_fields, attribute="first_message"),
|
|
}
|
|
|
|
conversation_with_summary_fields = {
|
|
"id": fields.String,
|
|
"status": fields.String,
|
|
"from_source": fields.String,
|
|
"from_end_user_id": fields.String,
|
|
"from_end_user_session_id": fields.String,
|
|
"from_account_id": fields.String,
|
|
"from_account_name": fields.String,
|
|
"name": fields.String,
|
|
"summary": fields.String(attribute="summary_or_query"),
|
|
"read_at": TimestampField,
|
|
"created_at": TimestampField,
|
|
"updated_at": TimestampField,
|
|
"annotated": fields.Boolean,
|
|
"model_config": fields.Nested(simple_model_config_fields),
|
|
"message_count": fields.Integer,
|
|
"user_feedback_stats": fields.Nested(feedback_stat_fields),
|
|
"admin_feedback_stats": fields.Nested(feedback_stat_fields),
|
|
"status_count": fields.Nested(status_count_fields),
|
|
}
|
|
|
|
conversation_with_summary_pagination_fields = {
|
|
"page": fields.Integer,
|
|
"limit": fields.Integer(attribute="per_page"),
|
|
"total": fields.Integer,
|
|
"has_more": fields.Boolean(attribute="has_next"),
|
|
"data": fields.List(fields.Nested(conversation_with_summary_fields), attribute="items"),
|
|
}
|
|
|
|
conversation_detail_fields = {
|
|
"id": fields.String,
|
|
"status": fields.String,
|
|
"from_source": fields.String,
|
|
"from_end_user_id": fields.String,
|
|
"from_account_id": fields.String,
|
|
"created_at": TimestampField,
|
|
"updated_at": TimestampField,
|
|
"annotated": fields.Boolean,
|
|
"introduction": fields.String,
|
|
"model_config": fields.Nested(model_config_fields),
|
|
"message_count": fields.Integer,
|
|
"user_feedback_stats": fields.Nested(feedback_stat_fields),
|
|
"admin_feedback_stats": fields.Nested(feedback_stat_fields),
|
|
}
|
|
|
|
simple_conversation_fields = {
|
|
"id": fields.String,
|
|
"name": fields.String,
|
|
"inputs": FilesContainedField,
|
|
"status": fields.String,
|
|
"introduction": fields.String,
|
|
"created_at": TimestampField,
|
|
"updated_at": TimestampField,
|
|
}
|
|
|
|
conversation_delete_fields = {
|
|
"result": fields.String,
|
|
}
|
|
|
|
conversation_infinite_scroll_pagination_fields = {
|
|
"limit": fields.Integer,
|
|
"has_more": fields.Boolean,
|
|
"data": fields.List(fields.Nested(simple_conversation_fields)),
|
|
}
|
|
|
|
|
|
def build_conversation_infinite_scroll_pagination_model(api_or_ns: Api | Namespace):
|
|
"""Build the conversation infinite scroll pagination model for the API or Namespace."""
|
|
simple_conversation_model = build_simple_conversation_model(api_or_ns)
|
|
|
|
copied_fields = conversation_infinite_scroll_pagination_fields.copy()
|
|
copied_fields["data"] = fields.List(fields.Nested(simple_conversation_model))
|
|
return api_or_ns.model("ConversationInfiniteScrollPagination", copied_fields)
|
|
|
|
|
|
def build_conversation_delete_model(api_or_ns: Api | Namespace):
|
|
"""Build the conversation delete model for the API or Namespace."""
|
|
return api_or_ns.model("ConversationDelete", conversation_delete_fields)
|
|
|
|
|
|
def build_simple_conversation_model(api_or_ns: Api | Namespace):
|
|
"""Build the simple conversation model for the API or Namespace."""
|
|
return api_or_ns.model("SimpleConversation", simple_conversation_fields)
|