refac: remove nesting

This commit is contained in:
Michael Poluektov 2024-07-09 12:15:09 +01:00
parent e3e02e04e8
commit d7dd901f01

View File

@ -465,51 +465,53 @@ async def chat_completion_functions_handler(
**(valves if valves else {}) **(valves if valves else {})
) )
if not hasattr(function_module, "inlet"):
continue
try: try:
if hasattr(function_module, "inlet"): inlet = function_module.inlet
inlet = function_module.inlet
# Get the signature of the function # Get the signature of the function
sig = inspect.signature(inlet) sig = inspect.signature(inlet)
params = {"body": body} params = {"body": body}
# Extra parameters to be passed to the function # Extra parameters to be passed to the function
extra_params = { extra_params = {
"__model__": model, "__model__": model,
"__id__": filter_id, "__id__": filter_id,
"__event_emitter__": __event_emitter__, "__event_emitter__": __event_emitter__,
"__event_call__": __event_call__, "__event_call__": __event_call__,
}
# Add extra params in contained in function signature
for key, value in extra_params.items():
if key in sig.parameters:
params[key] = value
if "__user__" in sig.parameters:
__user__ = {
"id": user.id,
"email": user.email,
"name": user.name,
"role": user.role,
} }
# Add extra params in contained in function signature try:
for key, value in extra_params.items(): if hasattr(function_module, "UserValves"):
if key in sig.parameters: __user__["valves"] = function_module.UserValves(
params[key] = value **Functions.get_user_valves_by_id_and_user_id(
filter_id, user.id
if "__user__" in sig.parameters:
__user__ = {
"id": user.id,
"email": user.email,
"name": user.name,
"role": user.role,
}
try:
if hasattr(function_module, "UserValves"):
__user__["valves"] = function_module.UserValves(
**Functions.get_user_valves_by_id_and_user_id(
filter_id, user.id
)
) )
except Exception as e: )
print(e) except Exception as e:
print(e)
params = {**params, "__user__": __user__} params = {**params, "__user__": __user__}
if inspect.iscoroutinefunction(inlet): if inspect.iscoroutinefunction(inlet):
body = await inlet(**params) body = await inlet(**params)
else: else:
body = inlet(**params) body = inlet(**params)
except Exception as e: except Exception as e:
print(f"Error: {e}") print(f"Error: {e}")
@ -1184,51 +1186,52 @@ async def chat_completed(form_data: dict, user=Depends(get_verified_user)):
**(valves if valves else {}) **(valves if valves else {})
) )
if not hasattr(function_module, "outlet"):
continue
try: try:
if hasattr(function_module, "outlet"): outlet = function_module.outlet
outlet = function_module.outlet
# Get the signature of the function # Get the signature of the function
sig = inspect.signature(outlet) sig = inspect.signature(outlet)
params = {"body": data} params = {"body": data}
# Extra parameters to be passed to the function # Extra parameters to be passed to the function
extra_params = { extra_params = {
"__model__": model, "__model__": model,
"__id__": filter_id, "__id__": filter_id,
"__event_emitter__": __event_emitter__, "__event_emitter__": __event_emitter__,
"__event_call__": __event_call__, "__event_call__": __event_call__,
}
# Add extra params in contained in function signature
for key, value in extra_params.items():
if key in sig.parameters:
params[key] = value
if "__user__" in sig.parameters:
__user__ = {
"id": user.id,
"email": user.email,
"name": user.name,
"role": user.role,
} }
# Add extra params in contained in function signature try:
for key, value in extra_params.items(): if hasattr(function_module, "UserValves"):
if key in sig.parameters: __user__["valves"] = function_module.UserValves(
params[key] = value **Functions.get_user_valves_by_id_and_user_id(
filter_id, user.id
if "__user__" in sig.parameters:
__user__ = {
"id": user.id,
"email": user.email,
"name": user.name,
"role": user.role,
}
try:
if hasattr(function_module, "UserValves"):
__user__["valves"] = function_module.UserValves(
**Functions.get_user_valves_by_id_and_user_id(
filter_id, user.id
)
) )
except Exception as e: )
print(e) except Exception as e:
print(e)
params = {**params, "__user__": __user__} params = {**params, "__user__": __user__}
if inspect.iscoroutinefunction(outlet): if inspect.iscoroutinefunction(outlet):
data = await outlet(**params) data = await outlet(**params)
else: else:
data = outlet(**params) data = outlet(**params)
except Exception as e: except Exception as e:
print(f"Error: {e}") print(f"Error: {e}")