forked from EDEAI/NexusAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
celery_app.py
288 lines (262 loc) · 9.64 KB
/
celery_app.py
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import os, json
os.environ['DATABASE_AUTO_COMMIT'] = 'True'
from datetime import datetime
from typing import List, Dict, Any, Literal, Optional
from celery import Celery
from langchain_core.documents import Document
from config import settings
from core.database.models import Agents, AppNodeExecutions, AppRuns, Apps, CustomTools, Datasets, UploadFiles, Workflows
from core.dataset import DatasetManagement, DatasetRetrieval
from core.workflow import (
ObjectVariable,
VariableTypes,
create_variable_from_dict,
Context,
create_context_from_dict,
create_recursive_task_category_from_dict,
flatten_variable_with_values,
get_first_variable_value
)
from core.workflow.nodes import AgentNode, SkillNode, create_node_from_dict, UPLOAD_FILES_KEY
from core.workflow.nodes.base.import_to_kb_base import ImportToKBBaseNode
from core.llm.prompt import create_prompt_from_dict
from languages import get_language_content
from log import Logger
redis_url = f'redis://:{settings.REDIS_PASSWORD}@{settings.REDIS_HOST}:{settings.REDIS_PORT}/{settings.REDIS_DB}'
# Initialize a Celery application with the given name, broker, and backend
celery_app = Celery('celery_app', broker=redis_url, backend=redis_url)
# Define a Celery task to run a workflow node
# This task takes two dictionaries and additional keyword arguments:
# - node_dict: A dictionary representing a node
# - context_dict: A dictionary representing the context
# It creates the node and the context from these dictionaries, then runs the node with the context and any additional keyword arguments
@celery_app.task
def run_workflow_node(
node_dict: Dict[str, Any],
**kwargs
) -> Dict[str, Any]:
context_dict = kwargs.pop('context_dict', None)
if context_dict:
kwargs['context'] = create_context_from_dict(context_dict)
task = kwargs['task']
if task:
task['current'] = create_recursive_task_category_from_dict(task['current']) if task['current'] else None
task['parent'] = create_recursive_task_category_from_dict(task['parent']) if task['parent'] else None
user_id = kwargs.get('user_id', 0)
os.environ['ACTUAL_USER_ID'] = str(user_id)
return create_node_from_dict(node_dict).run(**kwargs)
@celery_app.task
def run_app(
app_type: Literal['agent', 'skill'],
id_: int,
user_id: int,
input_dict: Dict[str, Any],
**kwargs
) -> Dict[str, Any]:
os.environ['ACTUAL_USER_ID'] = str(user_id)
input_: ObjectVariable = create_variable_from_dict(input_dict)
match app_type:
case 'agent':
agent = Agents().get_agent_by_id(id_)
app_id = agent['app_id']
run_type = 1 if agent['publish_status'] == 0 else 2
app = Apps().get_app_by_id(app_id)
node = AgentNode(
title=app['name'],
desc=app['description'],
input=input_,
agent_id=id_,
ability_id=kwargs['ability_id'],
prompt=create_prompt_from_dict(kwargs['prompt']),
)
case 'skill':
skill = CustomTools().get_skill_by_id(id_)
app_id = skill['app_id']
run_type = 1 if skill['publish_status'] == 0 else 2
app = Apps().get_app_by_id(app_id)
node = SkillNode(
title=app['name'],
desc=app['description'],
input=input_,
skill_id=id_
)
case _:
raise ValueError(f'Invalid app type: {app_type}')
# App validation
# try:
# node.validate()
# except Exception as e:
# return {
# 'status': 'failed',
# 'message': str(e)
# }
# Run app
result = node.run(
Context(),
app_id=app_id,
user_id=user_id,
type=run_type,
**kwargs
)
outputs = result['data'].get('outputs')
result['data']['outputs'] = flatten_variable_with_values(create_variable_from_dict(outputs))
if app_type == 'agent':
result['data']['outputs_md'] = get_first_variable_value(create_variable_from_dict(outputs))
else:
result['data']['outputs_md'] = None
return result
@celery_app.task
def run_dataset(
dataset_id: int,
user_id: int,
user_input: str,
**kwargs
) -> Dict[str, Any]:
logger = Logger.get_logger('dataset')
try:
os.environ['ACTUAL_USER_ID'] = str(user_id)
retrieval, retrieval_result, _ = DatasetRetrieval.single_retrieve(
dataset_id, 0, 0, user_id, 1
)
retrieval.invoke(user_input)
return {
'status': 'success',
'message': 'Dataset executed successfully.',
'data': [
{
'index_id': str(segment.metadata['index_id']),
'score': segment.metadata.get('score', 0.0),
'reranking_score': segment.metadata.get('relevance_score', 0.0)
}
for segment in retrieval_result
]
}
except Exception as e:
logger.exception('ERROR!!')
return {
'status': 'failed',
'message': str(e)
}
@celery_app.task
def run_node(
node_dict: Dict[str, Any],
node_input: Optional[Dict[str, Any]],
user_id: int,
workflow_id: int,
context: Optional[List[Dict[str, Any]]],
**kwargs
) -> Dict[str, Any]:
os.environ['ACTUAL_USER_ID'] = str(user_id)
app_node_exec = AppNodeExecutions()
node = create_node_from_dict(node_dict)
if node_input:
node.data['input'] = create_variable_from_dict(node_input)
workflow = Workflows().select_one(
columns=['app_id', 'publish_status'],
conditions=[{'column': 'id', 'value': workflow_id}]
)
assert workflow, 'Invalid workflow ID!'
run_type = 1 if workflow['publish_status'] == 0 else 2
exec_id = app_node_exec.insert(
{
'workflow_id': workflow_id,
'user_id': user_id,
'app_run_id': 0,
'type': run_type,
'node_id': node.id,
'node_type': node.data['type'],
'node_name': node.data['title'],
'node_graph': node.to_dict(),
'status': 2, # Status indicating the node execution has started
}
)
result = node.run(
create_context_from_dict(context) if context else Context(),
app_id=workflow['app_id'],
workflow_id=workflow_id,
user_id=user_id,
type=run_type,
**kwargs
)
if result['status'] == 'success':
now = datetime.now()
app_node_exec.update(
{'column': 'id', 'value': exec_id},
{
'status': 3,
'finished_time': now,
**result['data']
}
)
result['data']['finished_time'] = str(now.replace(microsecond=0))
if inputs := result['data'].get('inputs'):
inputs = create_variable_from_dict(inputs)
inputs = flatten_variable_with_values(inputs)
if upload_files := inputs.pop(UPLOAD_FILES_KEY, None):
upload_files_ids = upload_files.values()
upload_files_names = []
for file_id in upload_files_ids:
file_data = UploadFiles().get_file_by_id(file_id)
upload_files_names.append(file_data['name'] + file_data['extension'])
inputs[get_language_content('upload_files')] = upload_files_names
result['data']['inputs'] = inputs
if outputs := result['data'].get('outputs'):
result['data']['outputs'] = flatten_variable_with_values(create_variable_from_dict(outputs))
if node.data['type'] in ['llm', 'agent']:
result['data']['outputs_md'] = get_first_variable_value(create_variable_from_dict(outputs))
elif node.data['type'] in ['recursive_task_generation', 'recursive_task_execution']:
task_dict = json.loads(get_first_variable_value(create_variable_from_dict(outputs)))
result['data']['outputs_md'] = create_recursive_task_category_from_dict(task_dict).to_markdown()
else:
result['data']['outputs_md'] = None
else:
app_node_exec.update(
{'column': 'id', 'value': exec_id},
{
'status': 4,
'error': result['message']
}
)
return result
@celery_app.task
def reindex_dataset(dataset_id: int, new_embeddings_config_id: int):
new_collection_name = DatasetManagement.reindex_dataset(dataset_id, new_embeddings_config_id)
Datasets().update(
{'column': 'id', 'value': dataset_id},
{'collection_name': new_collection_name}
)
@celery_app.task
def import_output_variable_to_knowledge_base(
node: Dict[str, Any],
new_variable: Dict[str, Any],
knowledge_base_mapping: Dict[str, int],
app_run_id: int,
new_node_exec_id: int,
source_string: Optional[str] = None
):
node: ImportToKBBaseNode = create_node_from_dict(node)
node.import_variables_to_knowledge_base(
create_variable_from_dict(new_variable),
knowledge_base_mapping,
app_run_id,
new_node_exec_id,
False,
source_string
)
AppRuns().update(
{'column': 'id', 'value': app_run_id},
{'need_human_confirm': 0}
)
# Update Celery application configuration
# Set the expiration time of task results to 3600 seconds (1 hour)
celery_app.conf.update(
result_expires=3600,
)
if __name__ == '__main__':
celery_app.worker_main(
argv=[
'worker',
'--loglevel=info',
f'--concurrency={settings.CELERY_WORKERS}'
]
)