mirror of
https://github.com/imartinez/privateGPT.git
synced 2026-07-17 01:48:03 +00:00
* fix: avoid to block the loop * fix: blocks in expansion * fix: remove maximum concurrent users ... * fix: multiplexer * fix: readers * fix: more fixes ... * fix: impl * feat: tool scheduler * feat: add adaptative * feat: add chat worker * fix: max * feat: add chat/tools workers * fix: mypy * feat: add generic scheduler * fix: get result * feat: do serializable the tool executor * fix: tools * fix: config * fix: config * fix: args * fix: config * fix: serializer * Revert "fix: blocks in expansion" This reverts commita2110f94a8. * fix: unify all logic * feat: add ingestion scheduler * fix: settings * fix: config * feat: add arq worker to chat * fix: arq worker * fix: add nest * fix: mypy * fix: await * fix: script stress * fix: tokenizer * fix: chat scheduler * fix: mypy * fix: add async tokenizer * fix: improve condense * fix: tool scheduler * feat: add initial real async chat worker * fix: mypy * fix: do resumable local executor ... ... ... fix: revert usleess changes fix: remove parent chat job fix: refactor fix: loop ref: rename models fix: chat engine fix: mypy ... ... ... fix: fix deps * fix: tests * fix: tests * ... * fix: stream * fix: config * fix: scheduler * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Handle PGPT_WORKER_MODE=celery in health check worker status * fix: cancel * fix: arch * fix: test ingestion * fix: deserialization of chat messages * fix: broken results * fix: mypy * fix: test * fix: config * fix: remove arq tool worker * fix: output cls * fix: preserve early resumable tool callbacks * fix: preserve async tool result order * refactor: address worker PR review comments * fix: mypy * test: colocate ARQ chat enqueue coverage * fix: remove redis from tests * test: isolate chat mocks and cancellation timing * fix: tests (cherry picked from commit218b599c66) # Conflicts: # tests/server/chat/anthropic/test_anthropic_client.py # tests/server/chat/anthropic/test_langchain_anthropic.py # tests/server/chat/test_chat_knowledge_revamp.py # tests/server/chat/test_chat_routes.py # tests/server/chat/test_chat_routes_skills_integration.py * fix: tests (cherry picked from commitfc5ec0f72a) * fix: ruff * fix: test * fix: worker config (cherry picked from commit1371c275a1) * fix: principal * test: remove flaky chat cancellation assertion --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
135 lines
4.0 KiB
Python
135 lines
4.0 KiB
Python
# Celery
|
|
import logging
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from celery import states
|
|
from pydantic import BaseModel
|
|
|
|
from private_gpt.celery import states as custom_states
|
|
from private_gpt.celery.error import CeleryError
|
|
from private_gpt.components.broker.broker_component import BrokerComponent
|
|
from private_gpt.di import get_global_injector
|
|
from private_gpt.server.utils.callback import AsyncResponse, BaseCallbackInput, Callback
|
|
|
|
if TYPE_CHECKING:
|
|
from private_gpt.server.utils.callback import AMQP
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _callback_task_name(task: Any) -> str:
|
|
name: str = getattr(task, "callback_task_name", task.name)
|
|
return name
|
|
|
|
|
|
def _publish_callback(
|
|
exchange: str,
|
|
routing_key: str,
|
|
async_response: AsyncResponse,
|
|
final: bool = True,
|
|
) -> None:
|
|
broker_component = get_global_injector().get(BrokerComponent)
|
|
broker_component.publish(
|
|
exchange=exchange,
|
|
routing_key=routing_key,
|
|
body=bytes(async_response.model_dump_json(), "utf-8"),
|
|
)
|
|
if final:
|
|
logger.debug(
|
|
f"Published final callback message to {exchange}/{routing_key}: {async_response}"
|
|
)
|
|
broker_component.join()
|
|
|
|
|
|
def run_callback(
|
|
task: Any,
|
|
state: str,
|
|
result: BaseModel,
|
|
callback: Callback,
|
|
) -> None:
|
|
callback_amqp: AMQP = callback.amqp
|
|
final = False
|
|
|
|
if state == states.SUCCESS:
|
|
task_name = _callback_task_name(task)
|
|
async_response = AsyncResponse(
|
|
data=result,
|
|
type=f"pgpt.{task_name}.done",
|
|
error=None,
|
|
callback_properties=callback.properties,
|
|
)
|
|
routing_key = callback_amqp.routing_key_done or async_response.type
|
|
final = True
|
|
elif state == custom_states.PROGRESS:
|
|
task_name = _callback_task_name(task)
|
|
async_response = AsyncResponse(
|
|
data=result.model_dump(),
|
|
type=f"pgpt.{task_name}.progress",
|
|
error=None,
|
|
callback_properties=callback.properties,
|
|
)
|
|
routing_key = callback_amqp.routing_key_progress or async_response.type
|
|
else:
|
|
# Unify all errors as CeleryError
|
|
error_result = (
|
|
result
|
|
if isinstance(result, CeleryError)
|
|
else CeleryError(errors=[str(result)])
|
|
)
|
|
logger.error(
|
|
f"Task {task.name} failed with state {error_result.details.errors}",
|
|
exc_info=error_result,
|
|
)
|
|
|
|
async_response = AsyncResponse(
|
|
data=None,
|
|
type=f"pgpt.{_callback_task_name(task)}.error",
|
|
error=error_result.dict(),
|
|
callback_properties=callback.properties,
|
|
)
|
|
routing_key = callback_amqp.routing_key_error or async_response.type
|
|
final = True
|
|
|
|
_publish_callback(
|
|
exchange=callback_amqp.exchange,
|
|
routing_key=routing_key,
|
|
async_response=async_response,
|
|
final=final,
|
|
)
|
|
|
|
|
|
def task_after_return(
|
|
task: Any,
|
|
state: str,
|
|
result: BaseModel,
|
|
_task_id: str,
|
|
args: Any,
|
|
_kwargs: dict[str, Any],
|
|
_none: Any,
|
|
) -> None:
|
|
"""Callback After any task is completed.
|
|
|
|
Every task will produce a message back to the broker
|
|
with the result of the task. The payload will have a special
|
|
"type" field with shape `pgpt.{task_name}.done` that will indicate
|
|
The type of the data that is being sent.
|
|
|
|
In case of error a suffix ".error" will be added to the type field.
|
|
"""
|
|
# If the input is not a subclass of BaseCallbackInput, then we don't need to
|
|
# send a callback
|
|
if not args or not issubclass(args[0].__class__, BaseCallbackInput):
|
|
return
|
|
|
|
# If the input is a subclass of BaseCallbackInput, it needs to be the only input
|
|
assert len(args) == 1, "Tasks must have only one argument, which must be the input"
|
|
|
|
# Get callback arguments from the task args
|
|
if args[0].callback is None:
|
|
return
|
|
|
|
# Run the callback defined in the task
|
|
callback: Callback = args[0].callback
|
|
run_callback(task, state, result, callback)
|