Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
afc35be35a
|
|||
28eee676c4
|
|||
cff39d61de
|
|||
d36ebf9694
|
|||
b889b581f2
|
|||
4ea424e0d1
|
|||
9d0cd7e89b
|
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
__pycache__
|
||||||
|
|
||||||
|
/data/*
|
||||||
|
|
||||||
|
/bin/
|
||||||
|
/lib/
|
||||||
|
/include/
|
||||||
|
/pyvenv.cfg
|
24
Readme.md
24
Readme.md
@ -1,2 +1,26 @@
|
|||||||
# Agenten-Plattform
|
# Agenten-Plattform
|
||||||
|
|
||||||
|
## Management
|
||||||
|
|
||||||
|
- `./docker-mgmt/`
|
||||||
|
- `./ums/management/`
|
||||||
|
- `./web/`
|
||||||
|
- `./build-mgmt.sh`
|
||||||
|
|
||||||
|
|
||||||
|
## Basic Agent
|
||||||
|
|
||||||
|
- `./docker-agent/`
|
||||||
|
- `./ums/agent/`
|
||||||
|
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
### Run via Docker
|
||||||
|
- `docker compose up`
|
||||||
|
|
||||||
|
### VS Code Autocomplete ...
|
||||||
|
- `python3 -m venv .` (only once)
|
||||||
|
- `source ./bin/activate`
|
||||||
|
- `pip install requests fastapi pdoc` (only once)
|
||||||
|
- Select Python from `./bin/python` in VS Code
|
||||||
|
@ -1,5 +1,15 @@
|
|||||||
#/bin/bash
|
#/bin/bash
|
||||||
|
|
||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
# https://stackoverflow.com/a/4774063
|
# https://stackoverflow.com/a/4774063
|
||||||
SCRIPTPATH="$(cd -- "$(dirname "$0")" >/dev/null 2>&1; pwd -P)"
|
SCRIPTPATH="$(cd -- "$(dirname "$0")" >/dev/null 2>&1; pwd -P)"
|
||||||
|
|
||||||
|
@ -1,3 +1,13 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
ARG FROM_IMAGE=
|
ARG FROM_IMAGE=
|
||||||
|
|
||||||
FROM $FROM_IMAGE
|
FROM $FROM_IMAGE
|
||||||
|
33
docker-compose.yml
Normal file
33
docker-compose.yml
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
|
# This file is for development!!
|
||||||
|
# docker compose up
|
||||||
|
# See https://git.chai.uni-hamburg.de/UMS-Agenten/Agent-Template for production usage!
|
||||||
|
|
||||||
|
services:
|
||||||
|
management:
|
||||||
|
image: git.chai.uni-hamburg.de/ums-agenten/management:arm64
|
||||||
|
#image: git.chai.uni-hamburg.de/ums-agenten/management:amd64
|
||||||
|
ports:
|
||||||
|
- 8000:80
|
||||||
|
environment:
|
||||||
|
- AGENTS_PROCESS=http://agent_process_1:3001,http://agent_process_2:3001
|
||||||
|
- AGENTS_SOLVE=http://agent_solve_1:3001
|
||||||
|
- AGENTS_GATEKEEPER=http://agent_gatekeeper_1:3001
|
||||||
|
volumes:
|
||||||
|
- ./data/share/:/ums-agenten/share/
|
||||||
|
- ./data/persist-management/:/ums-agenten/persist/
|
||||||
|
# bind code from host to container (for development)
|
||||||
|
- ./ums/:/ums-agenten/plattform/ums/:ro
|
||||||
|
- ./web/:/ums-agenten/plattform/web/
|
||||||
|
# enable auto reloading (for development)
|
||||||
|
entrypoint: bash -c "nginx; SERVE=true uvicorn ums.management.main:app --uds /tmp/uvicorn.sock --proxy-headers --reload"
|
||||||
|
|
@ -1,3 +1,13 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
FROM ubuntu:24.04
|
FROM ubuntu:24.04
|
||||||
|
|
||||||
ARG H_GID
|
ARG H_GID
|
||||||
|
@ -1,3 +1,13 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
server {
|
server {
|
||||||
|
|
||||||
listen 80 default_server;
|
listen 80 default_server;
|
||||||
|
@ -1,3 +1,13 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
user user;
|
user user;
|
||||||
worker_processes auto;
|
worker_processes auto;
|
||||||
pid /run/nginx.pid;
|
pid /run/nginx.pid;
|
||||||
|
@ -1,3 +1,13 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
# non frozen dependecies, to use latest
|
# non frozen dependecies, to use latest
|
||||||
requests
|
requests
|
||||||
tqdm
|
tqdm
|
||||||
|
@ -1,3 +1,13 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
from setuptools import find_packages, setup
|
from setuptools import find_packages, setup
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
|
@ -1,3 +1,13 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
[supervisord]
|
[supervisord]
|
||||||
nodaemon=true
|
nodaemon=true
|
||||||
user=root
|
user=root
|
||||||
@ -10,6 +20,7 @@ autorestart=false
|
|||||||
|
|
||||||
[fcgi-program:uvicorn]
|
[fcgi-program:uvicorn]
|
||||||
socket=unix:///tmp/uvicorn.sock
|
socket=unix:///tmp/uvicorn.sock
|
||||||
|
environment=SERVE=true
|
||||||
command=/usr/local/bin/uvicorn ums.management.main:app --uds /tmp/uvicorn.sock --proxy-headers
|
command=/usr/local/bin/uvicorn ums.management.main:app --uds /tmp/uvicorn.sock --proxy-headers
|
||||||
numprocs=4
|
numprocs=4
|
||||||
process_name=uvicorn-%(process_num)d
|
process_name=uvicorn-%(process_num)d
|
||||||
|
17
docs.sh
Executable file
17
docs.sh
Executable file
@ -0,0 +1,17 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
|
pdoc ./ums/ \
|
||||||
|
--output-directory ./web/public/docs/ \
|
||||||
|
--no-browser \
|
||||||
|
--docformat google \
|
||||||
|
--template-directory ./utils/doc-template
|
@ -1,5 +1,15 @@
|
|||||||
#/bin/bash
|
#/bin/bash
|
||||||
|
|
||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
# https://stackoverflow.com/a/4774063
|
# https://stackoverflow.com/a/4774063
|
||||||
SCRIPTPATH="$(cd -- "$(dirname "$0")" >/dev/null 2>&1; pwd -P)"
|
SCRIPTPATH="$(cd -- "$(dirname "$0")" >/dev/null 2>&1; pwd -P)"
|
||||||
|
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
@ -0,0 +1,9 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
@ -0,0 +1,9 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
@ -0,0 +1,9 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
180
ums/management/db.py
Normal file
180
ums/management/db.py
Normal file
@ -0,0 +1,180 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sqlite3, atexit
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from threading import Lock
|
||||||
|
from typing import Generator
|
||||||
|
|
||||||
|
from pydantic import validate_call, BaseModel
|
||||||
|
|
||||||
|
from ums.utils import PERSIST_PATH, AgentMessage
|
||||||
|
|
||||||
|
class RowObject(BaseModel):
|
||||||
|
"""
|
||||||
|
Object representing a database row.
|
||||||
|
"""
|
||||||
|
|
||||||
|
count : int
|
||||||
|
"""
|
||||||
|
The count (primary key) of the item.
|
||||||
|
"""
|
||||||
|
|
||||||
|
sender : str
|
||||||
|
"""
|
||||||
|
The sender of the message.
|
||||||
|
"""
|
||||||
|
|
||||||
|
recipient : str
|
||||||
|
"""
|
||||||
|
The recipient of the message
|
||||||
|
"""
|
||||||
|
|
||||||
|
time : int
|
||||||
|
"""
|
||||||
|
The time (unix timestamp) the message was received/ sent.
|
||||||
|
"""
|
||||||
|
|
||||||
|
message : AgentMessage
|
||||||
|
"""
|
||||||
|
The message received/ sent.
|
||||||
|
"""
|
||||||
|
|
||||||
|
processed : bool
|
||||||
|
"""
|
||||||
|
Did the management process the message, i.e., did the tasks necessary for this message (mostly only relevant for received messages).
|
||||||
|
"""
|
||||||
|
|
||||||
|
class DB():
|
||||||
|
|
||||||
|
_DB_TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.db = sqlite3.connect(
|
||||||
|
os.path.join(PERSIST_PATH, 'messages.db'),
|
||||||
|
check_same_thread=False
|
||||||
|
)
|
||||||
|
self.db.row_factory = sqlite3.Row
|
||||||
|
|
||||||
|
self.dblock = Lock()
|
||||||
|
atexit.register(lambda db : db.close(), self.db)
|
||||||
|
|
||||||
|
self._assure_tables()
|
||||||
|
|
||||||
|
def _assure_tables(self):
|
||||||
|
self.dblock.acquire()
|
||||||
|
with self.db:
|
||||||
|
self.db.execute("""CREATE TABLE IF NOT EXISTS Messages (
|
||||||
|
count INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
id TEXT,
|
||||||
|
sender TEXT,
|
||||||
|
recipient TEXT,
|
||||||
|
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
json BLOB,
|
||||||
|
processed BOOL DEFAULT FALSE
|
||||||
|
)""")
|
||||||
|
self.dblock.release()
|
||||||
|
|
||||||
|
@validate_call
|
||||||
|
def add_message(self, sender:str, recipient:str, message:AgentMessage, processed:bool=False) -> int:
|
||||||
|
self.dblock.acquire()
|
||||||
|
with self.db:
|
||||||
|
self.db.execute(
|
||||||
|
"""INSERT INTO Messages (
|
||||||
|
id, sender, recipient, json, processed
|
||||||
|
) VALUES (
|
||||||
|
:id, :sender, :recipient, :json, :processed
|
||||||
|
)""", {
|
||||||
|
"id" : message.id,
|
||||||
|
"sender" : sender,
|
||||||
|
"recipient" : recipient,
|
||||||
|
"json" : message.model_dump_json(),
|
||||||
|
"processed" : processed
|
||||||
|
})
|
||||||
|
new_count = self.db.execute("SELECT LAST_INSERT_ROWID() as last").fetchone()
|
||||||
|
self.dblock.release()
|
||||||
|
|
||||||
|
return new_count['last']
|
||||||
|
|
||||||
|
@validate_call
|
||||||
|
def set_processed(self, count:int, processed:bool=True) -> bool:
|
||||||
|
self.dblock.acquire()
|
||||||
|
with self.db:
|
||||||
|
try:
|
||||||
|
self.db.execute("UPDATE Messages SET processed = ? WHERE count = ?", (processed, count))
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
self.dblock.release()
|
||||||
|
|
||||||
|
def __iter__(self) -> Generator[RowObject, None, None]:
|
||||||
|
yield from self.iterate()
|
||||||
|
|
||||||
|
@validate_call
|
||||||
|
def iterate(self,
|
||||||
|
id:str|None=None, sender:str|None=None, recipient:str|None=None,
|
||||||
|
processed:bool|None=None,
|
||||||
|
time_after:int|None=None, time_before:int|None=None,
|
||||||
|
limit:int=20, offset:int=0
|
||||||
|
) -> Generator[RowObject, None, None]:
|
||||||
|
|
||||||
|
where = []
|
||||||
|
params = {
|
||||||
|
"lim": limit,
|
||||||
|
"off": offset
|
||||||
|
}
|
||||||
|
|
||||||
|
for v,n in ((id,'id'), (sender,'sender'), (recipient,'recipient'), (processed,'processed')):
|
||||||
|
if not v is None:
|
||||||
|
where.append('{} = :{}'.format(n,n))
|
||||||
|
params[n] = v
|
||||||
|
|
||||||
|
if time_after:
|
||||||
|
where.append("time > :t_after")
|
||||||
|
params['t_after'] = datetime.fromtimestamp(time_after).strftime(self._DB_TIME_FORMAT)
|
||||||
|
|
||||||
|
if time_before:
|
||||||
|
where.append("time < :t_before")
|
||||||
|
params['t_before'] = datetime.fromtimestamp(time_before).strftime(self._DB_TIME_FORMAT)
|
||||||
|
|
||||||
|
if len(where) > 0:
|
||||||
|
where_clause = "WHERE " + (' AND '.join(where))
|
||||||
|
else:
|
||||||
|
where_clause = ""
|
||||||
|
|
||||||
|
with self.db:
|
||||||
|
for row in self.db.execute(
|
||||||
|
"SELECT * FROM Messages {} LIMIT :lim OFFSET :off".format(where_clause),
|
||||||
|
params
|
||||||
|
):
|
||||||
|
yield self._create_row_object(row)
|
||||||
|
|
||||||
|
def _create_row_object(self, row:sqlite3.Row) -> RowObject:
|
||||||
|
return RowObject(
|
||||||
|
count=row['count'],
|
||||||
|
sender=row['sender'],
|
||||||
|
recipient=row['recipient'],
|
||||||
|
time=int(datetime.strptime(row['time'], self._DB_TIME_FORMAT).timestamp()),
|
||||||
|
message=AgentMessage.model_construct(row['json']),
|
||||||
|
processed=row['processed']
|
||||||
|
)
|
||||||
|
|
||||||
|
def by_count(self, count:int) -> RowObject|None:
|
||||||
|
with self.db:
|
||||||
|
try:
|
||||||
|
return self._create_row_object(
|
||||||
|
self.db.execute("SELECT * FROM Messages WHERE count = ?", (count,)).fetchone()
|
||||||
|
)
|
||||||
|
except:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
35
ums/management/interface.py
Normal file
35
ums/management/interface.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Request
|
||||||
|
from fastapi.templating import Jinja2Templates
|
||||||
|
|
||||||
|
from ums.management.db import DB
|
||||||
|
|
||||||
|
class Interface():
|
||||||
|
|
||||||
|
def __init__(self, template:Jinja2Templates, db:DB):
|
||||||
|
self.template = template
|
||||||
|
self.db = db
|
||||||
|
|
||||||
|
self.router = APIRouter(
|
||||||
|
prefix="/app",
|
||||||
|
tags=["app, gui"]
|
||||||
|
)
|
||||||
|
|
||||||
|
self._add_routes()
|
||||||
|
|
||||||
|
def _add_routes(self):
|
||||||
|
@self.router.get("/", summary="Test")
|
||||||
|
def corpus(request: Request):
|
||||||
|
|
||||||
|
print(self.db.by_count(3))
|
||||||
|
|
||||||
|
return {}
|
@ -1,17 +1,84 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
# TEST ONLY
|
import os
|
||||||
|
|
||||||
from typing import Union
|
from fastapi import FastAPI, Request
|
||||||
|
from fastapi.templating import Jinja2Templates
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from ums.management.interface import Interface
|
||||||
|
from ums.management.db import DB
|
||||||
|
|
||||||
app = FastAPI()
|
from ums.utils import AgentMessage, RiddleData, RiddleDataType, RiddleSolution, TEMPLATE_PATH
|
||||||
|
|
||||||
@app.get("/")
|
class WebMain():
|
||||||
def read_root():
|
|
||||||
return {"Hello": "World"}
|
def __init__(self):
|
||||||
|
self._init_app()
|
||||||
|
self._init_templates()
|
||||||
|
|
||||||
|
self.db = DB()
|
||||||
|
|
||||||
|
self._add_routes()
|
||||||
|
self._add_routers()
|
||||||
|
|
||||||
|
|
||||||
@app.get("/items/{item_id}")
|
def _init_app(self):
|
||||||
def read_item(item_id: int, q: Union[str, None] = None):
|
self.app = FastAPI(
|
||||||
return {"item_id": item_id, "q": q}
|
title="Agenten Plattform",
|
||||||
|
description="Agenten Plattform – Management",
|
||||||
|
openapi_url="/api/schema.json",
|
||||||
|
docs_url='/api',
|
||||||
|
redoc_url=None
|
||||||
|
)
|
||||||
|
|
||||||
|
def _init_templates(self):
|
||||||
|
self.template = Jinja2Templates(
|
||||||
|
directory=TEMPLATE_PATH,
|
||||||
|
auto_reload=True
|
||||||
|
)
|
||||||
|
|
||||||
|
def _add_routers(self):
|
||||||
|
interface_router = Interface(self.template, self.db)
|
||||||
|
self.app.include_router(interface_router.router)
|
||||||
|
|
||||||
|
|
||||||
|
def _add_routes(self):
|
||||||
|
|
||||||
|
@self.app.get("/test", summary="Test")
|
||||||
|
def huhu(request: Request) -> AgentMessage:
|
||||||
|
ex = AgentMessage(
|
||||||
|
id="ex1",
|
||||||
|
riddle={
|
||||||
|
"context":"Example 1",
|
||||||
|
"question":"Get the name of the person."
|
||||||
|
},
|
||||||
|
data=[
|
||||||
|
RiddleData(
|
||||||
|
type=RiddleDataType.TEXT,
|
||||||
|
file_plain="./cv.txt"
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
ex.status.extract.required = False
|
||||||
|
|
||||||
|
ex.solution = RiddleSolution(
|
||||||
|
solution="Otto",
|
||||||
|
explanation="Written in line 6 after 'Name:'"
|
||||||
|
)
|
||||||
|
|
||||||
|
ins_count = self.db.add_message('from', 'to', ex)
|
||||||
|
self.db.set_processed(ins_count)
|
||||||
|
|
||||||
|
return ex
|
||||||
|
|
||||||
|
if __name__ == "ums.management.main" and os.environ.get('SERVE', 'false') == 'true':
|
||||||
|
main = WebMain()
|
||||||
|
app = main.app
|
21
ums/utils/__init__.py
Normal file
21
ums/utils/__init__.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
|
from ums.utils.types import (
|
||||||
|
RiddleInformation,
|
||||||
|
AgentMessage,
|
||||||
|
Riddle,
|
||||||
|
RiddleSolution,
|
||||||
|
RiddleData,
|
||||||
|
RiddleDataType,
|
||||||
|
RiddleStatus
|
||||||
|
)
|
||||||
|
|
||||||
|
from ums.utils.const import *
|
21
ums/utils/const.py
Normal file
21
ums/utils/const.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
|
"""
|
||||||
|
This file contains shared constants.
|
||||||
|
See the content ...
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
BASE_PATH = '/ums-agenten'
|
||||||
|
SHARE_PATH = os.path.join(BASE_PATH, 'share')
|
||||||
|
PERSIST_PATH = os.path.join(BASE_PATH, 'persist')
|
||||||
|
TEMPLATE_PATH = os.path.join(BASE_PATH, 'plattform', 'web', 'templates')
|
292
ums/utils/types.py
Normal file
292
ums/utils/types.py
Normal file
@ -0,0 +1,292 @@
|
|||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
|
"""
|
||||||
|
This represents the basic types used to interact with the management.
|
||||||
|
The types are implemented using [pydantic](https://docs.pydantic.dev/).
|
||||||
|
It provides validation, allow JSON serialization and works well with [FastAPI](https://fastapi.tiangolo.com/) which is used internally for the http request between the agents and the management.
|
||||||
|
|
||||||
|
### Example
|
||||||
|
|
||||||
|
```python
|
||||||
|
ex = AgentMessage(
|
||||||
|
id="ex1",
|
||||||
|
riddle={
|
||||||
|
"context":"Example 1",
|
||||||
|
"question":"Get the name of the person."
|
||||||
|
},
|
||||||
|
data=[
|
||||||
|
RiddleData(
|
||||||
|
type=RiddleDataType.TEXT,
|
||||||
|
file_plain="./cv.txt"
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
ex.status.extract.required = False
|
||||||
|
```
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "ex1",
|
||||||
|
"sub_ids": [],
|
||||||
|
"riddle": {
|
||||||
|
"context": "Example 1",
|
||||||
|
"question": "Get the name of the person.",
|
||||||
|
"solutions_before": []
|
||||||
|
},
|
||||||
|
"solution": null,
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"file_plain": "/ums-agenten/share/cv.txt",
|
||||||
|
"file_extracted": null
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"status": {
|
||||||
|
"extract": {
|
||||||
|
"required": false,
|
||||||
|
"finished": false
|
||||||
|
},
|
||||||
|
"solve": {
|
||||||
|
"required": true,
|
||||||
|
"finished": false
|
||||||
|
},
|
||||||
|
"validate": {
|
||||||
|
"required": true,
|
||||||
|
"finished": false
|
||||||
|
},
|
||||||
|
"trial": 0,
|
||||||
|
"solved": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```python
|
||||||
|
ex.solution = RiddleSolution(
|
||||||
|
solution="Otto",
|
||||||
|
explanation="Written in line 6 after 'Name:'"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
...
|
||||||
|
"solution": {
|
||||||
|
"solution": "Otto",
|
||||||
|
"explanation": "Written in line 6 after 'Name:'",
|
||||||
|
"used_data": [],
|
||||||
|
"accepted": false,
|
||||||
|
"review": null
|
||||||
|
},
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
from typing_extensions import Annotated
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from pydantic.functional_validators import AfterValidator
|
||||||
|
|
||||||
|
from ums.utils.const import SHARE_PATH
|
||||||
|
|
||||||
|
class RiddleInformation(BaseModel):
|
||||||
|
"""
|
||||||
|
This is the basic class used as superclass for all message and infos
|
||||||
|
about a riddle.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class RiddleDataType(Enum):
|
||||||
|
"""
|
||||||
|
Enum for the three types of data used in a riddle.
|
||||||
|
"""
|
||||||
|
|
||||||
|
TEXT = "text"
|
||||||
|
IMAGE = "image"
|
||||||
|
AUDIO = "audio"
|
||||||
|
|
||||||
|
def _check_data_file(file_name:str) -> str:
|
||||||
|
if not file_name.startswith('/'):
|
||||||
|
file_name = os.path.join(SHARE_PATH, file_name)
|
||||||
|
|
||||||
|
assert file_name.startswith(SHARE_PATH), "The data file needs to be in {}!".format(SHARE_PATH)
|
||||||
|
|
||||||
|
file_name = os.path.realpath(file_name, strict=False)
|
||||||
|
|
||||||
|
assert os.path.isfile(file_name), "The data file {} does not exist!".format(file_name)
|
||||||
|
|
||||||
|
return file_name
|
||||||
|
|
||||||
|
class RiddleData(RiddleInformation):
|
||||||
|
"""
|
||||||
|
A data item to be used to solve the riddle
|
||||||
|
"""
|
||||||
|
|
||||||
|
type: RiddleDataType
|
||||||
|
"""
|
||||||
|
The type of the data item.
|
||||||
|
"""
|
||||||
|
|
||||||
|
file_plain: Annotated[str, AfterValidator(_check_data_file)]
|
||||||
|
"""
|
||||||
|
The plain file (as path to file system) without any processing.
|
||||||
|
|
||||||
|
The path will be validated and must start with `SHARE_PATH` (or be relative to `SHARE_PATH`).
|
||||||
|
The file must exist.
|
||||||
|
"""
|
||||||
|
|
||||||
|
file_extracted: Annotated[str, AfterValidator(_check_data_file)] | None = None
|
||||||
|
"""
|
||||||
|
The processed files (as path to file system), i.e., a schematic file containing all extracted informations.
|
||||||
|
|
||||||
|
The path will be validated and must start with `SHARE_PATH` (or be relative to `SHARE_PATH`).
|
||||||
|
The file must exist.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class RiddleSolution(RiddleInformation):
|
||||||
|
"""
|
||||||
|
A solution of a riddle.
|
||||||
|
"""
|
||||||
|
|
||||||
|
solution: str
|
||||||
|
"""
|
||||||
|
The textual value of the solution.
|
||||||
|
"""
|
||||||
|
|
||||||
|
explanation: str
|
||||||
|
"""
|
||||||
|
An explanation of the solution.
|
||||||
|
"""
|
||||||
|
|
||||||
|
used_data: List[RiddleData] = []
|
||||||
|
"""
|
||||||
|
The data items used to create the solution (optional).
|
||||||
|
"""
|
||||||
|
|
||||||
|
accepted : bool = False
|
||||||
|
"""
|
||||||
|
If the solution is accepted by validator/ gatekeeper.
|
||||||
|
"""
|
||||||
|
|
||||||
|
review: str | None = None
|
||||||
|
"""
|
||||||
|
A review of the solution (if None: not tried to validate)
|
||||||
|
"""
|
||||||
|
|
||||||
|
class Riddle(RiddleInformation):
|
||||||
|
"""
|
||||||
|
The riddle (the task description and possibly a solution)
|
||||||
|
"""
|
||||||
|
|
||||||
|
context: str
|
||||||
|
"""
|
||||||
|
The context of the riddle (as textual string).
|
||||||
|
"""
|
||||||
|
|
||||||
|
question: str
|
||||||
|
"""
|
||||||
|
The actual main question of the riddle (as textual string).
|
||||||
|
"""
|
||||||
|
|
||||||
|
solutions_before: List[RiddleSolution] = []
|
||||||
|
"""
|
||||||
|
If already tried to solve this riddle before, the (not accepted) solutions are stored here
|
||||||
|
"""
|
||||||
|
|
||||||
|
class RiddleSubStatus(RiddleInformation):
|
||||||
|
"""
|
||||||
|
The sub status for each possible step a riddle may go though.
|
||||||
|
"""
|
||||||
|
|
||||||
|
required: bool = True
|
||||||
|
"""
|
||||||
|
Is this step required (i.e., requested)
|
||||||
|
"""
|
||||||
|
|
||||||
|
finished: bool = False
|
||||||
|
"""
|
||||||
|
Was this step already executed.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class RiddleStatus(RiddleInformation):
|
||||||
|
"""
|
||||||
|
The status of a riddle, will be mostly changed by Management when the riddle is sent to different agents while solving it.
|
||||||
|
"""
|
||||||
|
|
||||||
|
extract: RiddleSubStatus = RiddleSubStatus()
|
||||||
|
"""
|
||||||
|
The first extract step (image, text, audio -> more sematic data)
|
||||||
|
|
||||||
|
The `RiddleData` items in `AgentMessage.data` shall have `file_extracted` afterwards.
|
||||||
|
"""
|
||||||
|
|
||||||
|
solve: RiddleSubStatus = RiddleSubStatus()
|
||||||
|
"""
|
||||||
|
The *main* solving step.
|
||||||
|
|
||||||
|
`AgentMessage.solution` shall be an `RiddleSolution` afterwards.
|
||||||
|
"""
|
||||||
|
|
||||||
|
validate: RiddleSubStatus = RiddleSubStatus()
|
||||||
|
"""
|
||||||
|
The validation step, i.e., does the gatekeeper accept the solution in `AgentMessage.solution`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
trial: int = 0
|
||||||
|
"""
|
||||||
|
A counter for the number of trials.
|
||||||
|
Each time the gatekeeper does not accept a solution of this riddle, the value is incremented.
|
||||||
|
"""
|
||||||
|
|
||||||
|
solved: bool = False
|
||||||
|
"""
|
||||||
|
True, after the gatekeeper accepts the solution at `AgentMessage.solution`
|
||||||
|
"""
|
||||||
|
|
||||||
|
class AgentMessage(RiddleInformation):
|
||||||
|
"""
|
||||||
|
The basic message, which is sent be the agent and the management.
|
||||||
|
The objects will be JSON en- and decoded.
|
||||||
|
"""
|
||||||
|
|
||||||
|
id: str
|
||||||
|
"""
|
||||||
|
The riddle id, e.g., ``ex1``
|
||||||
|
This is a unique string and identifies the riddle.
|
||||||
|
"""
|
||||||
|
|
||||||
|
sub_ids: List[str] = []
|
||||||
|
"""
|
||||||
|
There might be cases, when an agent decided to split in riddle in multiple *smaller* steps.
|
||||||
|
Each *sub* riddle will then get its own id (i.e., ``ex1-sub1``) while the sub id is added here as reference.
|
||||||
|
"""
|
||||||
|
|
||||||
|
riddle: Riddle
|
||||||
|
"""
|
||||||
|
The riddle to solve.
|
||||||
|
"""
|
||||||
|
|
||||||
|
solution: RiddleSolution | None = None
|
||||||
|
"""
|
||||||
|
The solution of the riddle (or empty if no solution available)
|
||||||
|
"""
|
||||||
|
|
||||||
|
data: List[RiddleData] = []
|
||||||
|
"""
|
||||||
|
The data to get the solution from.
|
||||||
|
"""
|
||||||
|
|
||||||
|
status: RiddleStatus = RiddleStatus()
|
||||||
|
"""
|
||||||
|
The status of the riddle.
|
||||||
|
"""
|
20
utils/doc-template/module.html.jinja2
Normal file
20
utils/doc-template/module.html.jinja2
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
{# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
#}
|
||||||
|
|
||||||
|
|
||||||
|
{% extends "default/module.html.jinja2" %}
|
||||||
|
|
||||||
|
{% macro is_public(doc) %}
|
||||||
|
{% if doc.name in ("model_config", "model_fields", "model_computed_fields") %}
|
||||||
|
{% else %}
|
||||||
|
{{ default_is_public(doc) }}
|
||||||
|
{% endif %}
|
||||||
|
{% endmacro %}
|
10
vars.sh
10
vars.sh
@ -1,5 +1,15 @@
|
|||||||
#/bin/bash
|
#/bin/bash
|
||||||
|
|
||||||
|
# Agenten Plattform
|
||||||
|
#
|
||||||
|
# (c) 2024 Magnus Bender
|
||||||
|
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||||
|
# Universitaet Hamburg
|
||||||
|
# https://www.chai.uni-hamburg.de/~bender
|
||||||
|
#
|
||||||
|
# source code released under the terms of GNU Public License Version 3
|
||||||
|
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
IMAGE_REGISTRY="git.chai.uni-hamburg.de"
|
IMAGE_REGISTRY="git.chai.uni-hamburg.de"
|
||||||
IMAGE_OWNER="ums-agenten"
|
IMAGE_OWNER="ums-agenten"
|
||||||
IMAGE_NAME_AGENT="base-agent"
|
IMAGE_NAME_AGENT="base-agent"
|
||||||
|
7
web/public/docs/index.html
Normal file
7
web/public/docs/index.html
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="refresh" content="0; url=./ums.html"/>
|
||||||
|
</head>
|
||||||
|
</html>
|
46
web/public/docs/search.js
Normal file
46
web/public/docs/search.js
Normal file
File diff suppressed because one or more lines are too long
249
web/public/docs/ums.html
Normal file
249
web/public/docs/ums.html
Normal file
File diff suppressed because one or more lines are too long
248
web/public/docs/ums/__cli__.html
Normal file
248
web/public/docs/ums/__cli__.html
Normal file
File diff suppressed because one or more lines are too long
248
web/public/docs/ums/agent.html
Normal file
248
web/public/docs/ums/agent.html
Normal file
File diff suppressed because one or more lines are too long
254
web/public/docs/ums/management.html
Normal file
254
web/public/docs/ums/management.html
Normal file
File diff suppressed because one or more lines are too long
943
web/public/docs/ums/management/db.html
Normal file
943
web/public/docs/ums/management/db.html
Normal file
File diff suppressed because one or more lines are too long
392
web/public/docs/ums/management/interface.html
Normal file
392
web/public/docs/ums/management/interface.html
Normal file
File diff suppressed because one or more lines are too long
424
web/public/docs/ums/management/main.html
Normal file
424
web/public/docs/ums/management/main.html
Normal file
File diff suppressed because one or more lines are too long
265
web/public/docs/ums/utils.html
Normal file
265
web/public/docs/ums/utils.html
Normal file
File diff suppressed because one or more lines are too long
327
web/public/docs/ums/utils/const.html
Normal file
327
web/public/docs/ums/utils/const.html
Normal file
File diff suppressed because one or more lines are too long
1696
web/public/docs/ums/utils/types.html
Normal file
1696
web/public/docs/ums/utils/types.html
Normal file
File diff suppressed because one or more lines are too long
@ -1,8 +1,14 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>UMS-Agenten Management</title>
|
<title>UMS-Agenten – Management</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Empty</h1>
|
<h1>UMS-Agenten – Management</h1>
|
||||||
|
<ul>
|
||||||
|
<li><a href="/app/" target="_blank">Web App (small GUI)</a></li>
|
||||||
|
<li><a href="/app/new" target="_blank">Add Riddle via GUI</a></li>
|
||||||
|
<li><a href="/api/" target="_blank">API Documentations</a></li>
|
||||||
|
<li><a href="/docs/" target="_blank">Code Documentation</a></li>
|
||||||
|
</ul>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Reference in New Issue
Block a user