openai/chatkit-python

Public

mirrored from https://github.com/openai/chatkit-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.1.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

chatkit/store.py

141lines · modeblame

f688d870victor-openai8 months ago1import uuid
2from abc import ABC, abstractmethod
3from typing import Any, Generic, Literal
4
5from typing_extensions import TypeVar
6
7from .types import (
8Attachment,
9AttachmentCreateParams,
10Page,
11ThreadItem,
12ThreadMetadata,
13)
14
15TContext = TypeVar("TContext", default=Any)
16
17StoreItemType = Literal[
18"thread", "message", "tool_call", "task", "workflow", "attachment"
19]
20
21
22_ID_PREFIXES: dict[StoreItemType, str] = {
23"thread": "thr",
24"message": "msg",
25"tool_call": "tc",
26"workflow": "wf",
27"task": "tsk",
28"attachment": "atc",
29}
30
31
32def default_generate_id(item_type: StoreItemType) -> str:
33prefix = _ID_PREFIXES[item_type]
34return f"{prefix}_{uuid.uuid4().hex[:8]}"
35
36
37class NotFoundError(Exception):
38pass
39
40
41class AttachmentStore(ABC, Generic[TContext]):
42@abstractmethod
43async def delete_attachment(self, attachment_id: str, context: TContext) -> None:
44pass
45
46async def create_attachment(
47self, input: AttachmentCreateParams, context: TContext
48) -> Attachment:
49raise NotImplementedError(
50f"{type(self).__name__} must override create_attachment() to support two-phase file upload"
51)
52
53def generate_attachment_id(self, mime_type: str, context: TContext) -> str:
54"""Return a new identifier for a file. Override this method to customize file ID generation."""
55
56return default_generate_id("attachment")
57
58
59class Store(ABC, Generic[TContext]):
60def generate_thread_id(self, context: TContext) -> str:
61"""Return a new identifier for a thread. Override this method to customize thread ID generation."""
62
63return default_generate_id("thread")
64
65def generate_item_id(
66self, item_type: StoreItemType, thread: ThreadMetadata, context: TContext
67) -> str:
68"""Return a new identifier for a thread item. Override this method to customize item ID generation."""
69
70return default_generate_id(item_type)
71
72@abstractmethod
73async def load_thread(self, thread_id: str, context: TContext) -> ThreadMetadata:
74pass
75
76@abstractmethod
77async def save_thread(self, thread: ThreadMetadata, context: TContext) -> None:
78pass
79
80@abstractmethod
81async def load_thread_items(
82self,
83thread_id: str,
84after: str | None,
85limit: int,
86order: str,
87context: TContext,
88) -> Page[ThreadItem]:
89pass
90
91@abstractmethod
92async def save_attachment(self, attachment: Attachment, context: TContext) -> None:
93pass
94
95@abstractmethod
96async def load_attachment(
97self, attachment_id: str, context: TContext
98) -> Attachment:
99pass
100
101@abstractmethod
102async def delete_attachment(self, attachment_id: str, context: TContext) -> None:
103pass
104
105@abstractmethod
106async def load_threads(
107self,
108limit: int,
109after: str | None,
110order: str,
111context: TContext,
112) -> Page[ThreadMetadata]:
113pass
114
115@abstractmethod
116async def add_thread_item(
117self, thread_id: str, item: ThreadItem, context: TContext
118) -> None:
119pass
120
121@abstractmethod
122async def save_item(
123self, thread_id: str, item: ThreadItem, context: TContext
124) -> None:
125pass
126
127@abstractmethod
128async def load_item(
129self, thread_id: str, item_id: str, context: TContext
130) -> ThreadItem:
131pass
132
133@abstractmethod
134async def delete_thread(self, thread_id: str, context: TContext) -> None:
135pass
136
137@abstractmethod
138async def delete_thread_item(
139self, thread_id: str, item_id: str, context: TContext
140) -> None:
141pass