-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
90 lines (69 loc) · 3.37 KB
/
model.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
from datetime import datetime
from sqlalchemy import BOOLEAN, JSON, TEXT, TIMESTAMP, Column, Index, String
from sqlalchemy.ext.declarative import declarative_base
"""
A base class is required to define models:
https://docs.sqlalchemy.org/en/20/orm/declarative_tables.html
We build three models in the module:
------ --------- ------------
| app | --- has many ---> | version | --- has many ---> | iteraction |
------ --------- ------------
App is the container of versions. But only one version can be active.
Each version (except the first one of every app) contains a parent version, which
means the versions construct a tree instead of a list. The version has a configuration
which stores the DAG. The DAG defines how to interact with LLM and how to deal with
input data. Each time the DAG is executed, a new iteraction is produced. The iteraction
stores all data the DAG nodes produced.
"""
Base = declarative_base()
class Application(Base):
"""
Application models app. An app has many versions but only one of them are active.
Each time the app is called, the active_version's configration is used to build
the DAG, and then produce the interaction record.
"""
__tablename__ = "applications"
id = Column(String(36), primary_key=True, nullable=False)
name = Column(String(256), nullable=False)
user = Column(String(256), nullable=False)
langfuse_public_key = Column(String(64), nullable=True)
langfuse_secret_key = Column(String(64), nullable=True)
active_version = Column(String(36), nullable=True)
created_at = Column(TIMESTAMP, nullable=False)
updated_at = Column(TIMESTAMP, nullable=False)
deleted_at = Column(TIMESTAMP, nullable=True)
deleted_at_index = Index("ix_application_deleted_at", deleted_at)
class ApplicationVersion(Base):
"""
ApplicationVersion models version of app. It is mainly used to store DAG configurations.
"""
__tablename__ = "versions"
id = Column(String(36), primary_key=True, nullable=False)
name = Column(String(36), nullable=False)
user = Column(String(256), nullable=False)
parent_id = Column(String(36), nullable=True)
app_id = Column(String(36), nullable=False)
meta = Column(JSON, nullable=True)
configuration = Column(JSON, nullable=False)
created_at = Column(TIMESTAMP, nullable=False)
updated_at = Column(TIMESTAMP, nullable=False)
deleted_at = Column(TIMESTAMP, nullable=True)
application_index = Index("ix_version_app_id", app_id)
created_at_index = Index("ix_version_created_at", created_at)
deleted_at_index = Index("ix_version_deleted_at", deleted_at)
class Interaction(Base):
"""
Interaction records every interaction with a specific app version.
"""
__tablename__ = "interactions"
id = Column(String(36), primary_key=True, nullable=False)
user = Column(String(256), nullable=False)
app_id = Column(String(36), nullable=False)
version_id = Column(String(36), nullable=False)
created_at = Column(TIMESTAMP, nullable=False)
updated_at = Column(TIMESTAMP, nullable=False)
output = Column(TEXT, nullable=True)
data = Column(JSON, nullable=True)
error = Column(JSON, nullable=True)
version_index = Index("ix_interactions_version_id", version_id)
created_at_index = Index("ix_interactions_created_at", created_at)