File size: 8,071 Bytes
cbc00ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | # -*- coding: utf-8 -*-
# Copyright (c) Alibaba, Inc. and its affiliates.
"""
@File : application_response.py
@Date : 2024-02-24
@Desc : application call response
"""
from dataclasses import dataclass
from http import HTTPStatus
from typing import Dict, List
from dashscope.api_entities.dashscope_response import (
DashScopeAPIResponse,
DictMixin,
)
@dataclass(init=False)
class ApplicationThought(DictMixin):
thought: str
action_type: str
response: str
action_name: str
action: str
action_input_stream: str
action_input: Dict
observation: str
def __init__(
self,
thought: str = None,
action_type: str = None,
response: str = None,
action_name: str = None,
action: str = None,
action_input_stream: str = None,
action_input: Dict = None,
observation: str = None,
**kwargs,
):
"""Thought of app completion call result which describe model planning and doc retrieval # noqa: E501 # pylint: disable=line-too-long
or plugin calls details.
Args:
thought (str, optional): Model's inference thought for doc retrieval or plugin process.
action_type (str, optional): Action type. response : final response; api: to run api calls. # pylint: disable=line-too-long
response (str, optional): Model's results.
action_name (str, optional): Action name, e.g. searchDocument、api.
action (str, optional): Code of action, means which plugin or action to be run.
action_input_stream (str, optional): Input param with stream.
action_input (dict, optional): Api or plugin input parameters.
observation (str, optional): Result of api call or doc retrieval.
"""
super().__init__(
thought=thought,
action_type=action_type,
response=response,
action_name=action_name,
action=action,
action_input_stream=action_input_stream,
action_input=action_input,
observation=observation,
**kwargs,
)
@dataclass(init=False)
class ApplicationDocReference(DictMixin):
index_id: str
title: str
doc_id: str
doc_name: str
doc_url: str
text: str
biz_id: str
images: List[str]
page_number: List[int]
def __init__(
self,
index_id: str = None,
title: str = None,
doc_id: str = None,
doc_name: str = None,
doc_url: str = None,
text: str = None,
biz_id: str = None,
images: List[str] = None,
page_number: List[int] = None,
**kwargs,
):
"""Doc references for retrieval result.
Args:
index_id (str, optional): Index id of doc retrival result reference. # noqa: E501
title (str, optional): Title of original doc that retrieved.
doc_id (str, optional): Id of original doc that retrieved.
doc_name (str, optional): Name of original doc that retrieved.
doc_url (str, optional): Url of original doc that retrieved.
text (str, optional): Text in original doc that retrieved.
biz_id (str, optional): Biz id that caller is able to associated for biz logic. # noqa: E501 # pylint: disable=line-too-long
images (list, optional): List of referenced image URLs
"""
super().__init__(
index_id=index_id,
title=title,
doc_id=doc_id,
doc_name=doc_name,
doc_url=doc_url,
text=text,
biz_id=biz_id,
images=images,
page_number=page_number,
**kwargs,
)
@dataclass(init=False)
class WorkflowMessage(DictMixin):
node_id: str
node_name: str
node_type: str
node_status: str
node_is_completed: str
node_msg_seq_id: int
message: str
class Message(DictMixin):
role: str
content: str
def __init__(
self,
node_id: str = None,
node_name: str = None,
node_type: str = None,
node_status: str = None,
node_is_completed: str = None,
node_msg_seq_id: int = None,
message: Message = None,
**kwargs,
):
"""Workflow message.
Args:
node_id (str, optional): .
node_name (str, optional): .
node_type (str, optional): .
node_status (str, optional): .
node_is_completed (str, optional): .
node_msg_seq_id (int, optional): .
message (Message, optional): .
"""
super().__init__(
node_id=node_id,
node_name=node_name,
node_type=node_type,
node_status=node_status,
node_is_completed=node_is_completed,
node_msg_seq_id=node_msg_seq_id,
message=message,
**kwargs,
)
@dataclass(init=False)
class ApplicationOutput(DictMixin):
text: str
finish_reason: str
session_id: str
thoughts: List[ApplicationThought]
doc_references: List[ApplicationDocReference]
workflow_message: WorkflowMessage
def __init__(
self,
text: str = None,
finish_reason: str = None,
session_id: str = None,
thoughts: List[ApplicationThought] = None,
doc_references: List[ApplicationDocReference] = None,
workflow_message: WorkflowMessage = None,
**kwargs,
):
ths = None
if thoughts is not None:
ths = []
for thought in thoughts:
ths.append(ApplicationThought(**thought))
refs = None
if doc_references is not None:
refs = []
for ref in doc_references:
refs.append(ApplicationDocReference(**ref))
super().__init__(
text=text,
finish_reason=finish_reason,
session_id=session_id,
thoughts=ths,
doc_references=refs,
workflow_message=workflow_message,
**kwargs,
)
@dataclass(init=False)
class ApplicationModelUsage(DictMixin):
model_id: str
input_tokens: int
output_tokens: int
def __init__(
self,
model_id: str = None,
input_tokens: int = 0,
output_tokens: int = 0,
**kwargs,
):
super().__init__(
model_id=model_id,
input_tokens=input_tokens,
output_tokens=output_tokens,
**kwargs,
)
@dataclass(init=False)
class ApplicationUsage(DictMixin):
models: List[ApplicationModelUsage]
def __init__(self, models: List[ApplicationModelUsage] = None, **kwargs):
model_usages = None
if models is not None:
model_usages = []
for model_usage in models:
model_usages.append(ApplicationModelUsage(**model_usage))
super().__init__(models=model_usages, **kwargs)
@dataclass(init=False)
class ApplicationResponse(DashScopeAPIResponse):
output: ApplicationOutput
usage: ApplicationUsage
@staticmethod
def from_api_response(api_response: DashScopeAPIResponse):
if api_response.status_code == HTTPStatus.OK:
usage = {}
if api_response.usage:
usage = api_response.usage
return ApplicationResponse(
status_code=api_response.status_code,
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
output=ApplicationOutput(**api_response.output),
usage=ApplicationUsage(**usage),
)
else:
return ApplicationResponse(
status_code=api_response.status_code,
request_id=api_response.request_id,
code=api_response.code,
message=api_response.message,
)
|