Skip to content

Commit

Permalink
fixup! feat: Add task processing API
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin committed Jun 7, 2024
1 parent e285cf0 commit e611990
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,32 @@ async def lifespan(_app: FastAPI):
class BackgroundProcessTask(threading.Thread):
def run(self, *args, **kwargs): # pylint: disable=unused-argument
nc = NextcloudApp()
# Hack to workaround auth problems
nc.set_user("admin")

task_type_ids = set()
for chain_name, _ in chains.items():
(model, task) = chain_name.split(":", 2)
task_type_ids.add("core:text2text:" + task)

while True:
response = nc.providers.task_processing.next_task("core:text2text")
# Reset user
nc.set_user("")
response = nc.providers.task_processing.next_task(list(task_type_ids))
if not isinstance(response, dict):
time.sleep(5)
continue

task = response["task"]
provider = response["provider"]

nc.set_user(task["userId"])

try:
chain_name = task["name"][4:]
chain_name = provider["name"][5:]
print(f"chain: {chain_name}", flush=True)
chain_load = chains.get(chain_name)
if chain_load is None:
NextcloudApp().providers.task_processing.report_result(
task["id"], error="Requested model is not available"
task["id"], error_message="Requested model is not available"
)
continue
chain = chain_load()
Expand All @@ -62,13 +71,13 @@ def run(self, *args, **kwargs): # pylint: disable=unused-argument
print(result, flush=True)
NextcloudApp().providers.task_processing.report_result(
task["id"],
str(result).split(sep="<|assistant|>", maxsplit=1)[-1].strip(),
{"output": str(result).split(sep="<|assistant|>", maxsplit=1)[-1].strip()},
)
except Exception as e: # noqa
print(str(e), flush=True)
nc = NextcloudApp()
nc.log(LogLvl.ERROR, str(e))
nc.providers.task_processing.report_result(task["id"], error=str(e))
nc.providers.task_processing.report_result(task["id"], error_message=str(e))


async def enabled_handler(enabled: bool, nc: AsyncNextcloudApp) -> str:
Expand Down

0 comments on commit e611990

Please sign in to comment.