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/actions.py

53lines · modeblame

f688d870victor-openai8 months ago1from __future__ import annotations
2
3from typing import Any, Generic, Literal, TypeVar, get_args, get_origin
4
5from pydantic import BaseModel, Field
6
7Handler = Literal["client", "server"]
8LoadingBehavior = Literal["auto", "none", "self", "container"]
9
10DEFAULT_HANDLER: Handler = "server"
11DEFAULT_LOADING_BEHAVIOR: LoadingBehavior = "auto"
12
13
14class ActionConfig(BaseModel):
15type: str
16payload: Any = None
17handler: Handler = DEFAULT_HANDLER
18loadingBehavior: LoadingBehavior = DEFAULT_LOADING_BEHAVIOR
19
20
21TType = TypeVar("TType", bound=str)
22TPayload = TypeVar("TPayload")
23
24
25class Action(BaseModel, Generic[TType, TPayload]):
26type: TType = Field(default=TType, frozen=True) # pyright: ignore
ee4063b8David Weedon8 months ago27payload: TPayload = None # pyright: ignore - default to None to allow no-payload actions
f688d870victor-openai8 months ago28
29@classmethod
30def create(
31cls,
32payload: TPayload,
33handler: Handler = DEFAULT_HANDLER,
34loading_behavior: LoadingBehavior = DEFAULT_LOADING_BEHAVIOR,
35) -> ActionConfig:
36actionType: Any = None
37anno = cls.model_fields["type"].annotation
38if get_origin(anno) is Literal:
39lits = get_args(anno)
40if len(lits) == 1 and isinstance(lits[0], str):
41actionType = lits[0]
42
43if actionType is None:
44raise TypeError(
45"Cannot infer 'type' for this Action[...]. Do not call create() on generic Action."
46)
47
48return ActionConfig(
49type=actionType,
50payload=payload,
51handler=handler,
52loadingBehavior=loading_behavior,
53)