|
1 | | -from dataclasses import dataclass |
2 | | -from typing import Any, Dict, List, Literal, Optional, Tuple |
| 1 | +from typing import Any, Dict, List, Optional, Tuple |
3 | 2 |
|
4 | 3 | import chevron |
5 | 4 | from ldclient import Context |
6 | 5 | from ldclient.client import LDClient |
7 | 6 |
|
| 7 | +from ldai.models import ( |
| 8 | + AIConfig, |
| 9 | + LDAIAgent, |
| 10 | + LDAIAgentConfig, |
| 11 | + LDAIAgentDefaults, |
| 12 | + LDAIAgents, |
| 13 | + LDMessage, |
| 14 | + ModelConfig, |
| 15 | + ProviderConfig, |
| 16 | +) |
8 | 17 | from ldai.tracker import LDAIConfigTracker |
9 | 18 |
|
10 | 19 |
|
11 | | -@dataclass |
12 | | -class LDMessage: |
13 | | - role: Literal['system', 'user', 'assistant'] |
14 | | - content: str |
15 | | - |
16 | | - def to_dict(self) -> dict: |
17 | | - """ |
18 | | - Render the given message as a dictionary object. |
19 | | - """ |
20 | | - return { |
21 | | - 'role': self.role, |
22 | | - 'content': self.content, |
23 | | - } |
24 | | - |
25 | | - |
26 | | -class ModelConfig: |
27 | | - """ |
28 | | - Configuration related to the model. |
29 | | - """ |
30 | | - |
31 | | - def __init__(self, name: str, parameters: Optional[Dict[str, Any]] = None, custom: Optional[Dict[str, Any]] = None): |
32 | | - """ |
33 | | - :param name: The name of the model. |
34 | | - :param parameters: Additional model-specific parameters. |
35 | | - :param custom: Additional customer provided data. |
36 | | - """ |
37 | | - self._name = name |
38 | | - self._parameters = parameters |
39 | | - self._custom = custom |
40 | | - |
41 | | - @property |
42 | | - def name(self) -> str: |
43 | | - """ |
44 | | - The name of the model. |
45 | | - """ |
46 | | - return self._name |
47 | | - |
48 | | - def get_parameter(self, key: str) -> Any: |
49 | | - """ |
50 | | - Retrieve model-specific parameters. |
51 | | -
|
52 | | - Accessing a named, typed attribute (e.g. name) will result in the call |
53 | | - being delegated to the appropriate property. |
54 | | - """ |
55 | | - if key == 'name': |
56 | | - return self.name |
57 | | - |
58 | | - if self._parameters is None: |
59 | | - return None |
60 | | - |
61 | | - return self._parameters.get(key) |
62 | | - |
63 | | - def get_custom(self, key: str) -> Any: |
64 | | - """ |
65 | | - Retrieve customer provided data. |
66 | | - """ |
67 | | - if self._custom is None: |
68 | | - return None |
69 | | - |
70 | | - return self._custom.get(key) |
71 | | - |
72 | | - def to_dict(self) -> dict: |
73 | | - """ |
74 | | - Render the given model config as a dictionary object. |
75 | | - """ |
76 | | - return { |
77 | | - 'name': self._name, |
78 | | - 'parameters': self._parameters, |
79 | | - 'custom': self._custom, |
80 | | - } |
81 | | - |
82 | | - |
83 | | -class ProviderConfig: |
84 | | - """ |
85 | | - Configuration related to the provider. |
86 | | - """ |
87 | | - |
88 | | - def __init__(self, name: str): |
89 | | - self._name = name |
90 | | - |
91 | | - @property |
92 | | - def name(self) -> str: |
93 | | - """ |
94 | | - The name of the provider. |
95 | | - """ |
96 | | - return self._name |
97 | | - |
98 | | - def to_dict(self) -> dict: |
99 | | - """ |
100 | | - Render the given provider config as a dictionary object. |
101 | | - """ |
102 | | - return { |
103 | | - 'name': self._name, |
104 | | - } |
105 | | - |
106 | | - |
107 | | -@dataclass(frozen=True) |
108 | | -class AIConfig: |
109 | | - enabled: Optional[bool] = None |
110 | | - model: Optional[ModelConfig] = None |
111 | | - messages: Optional[List[LDMessage]] = None |
112 | | - provider: Optional[ProviderConfig] = None |
113 | | - |
114 | | - def to_dict(self) -> dict: |
115 | | - """ |
116 | | - Render the given default values as an AIConfig-compatible dictionary object. |
117 | | - """ |
118 | | - return { |
119 | | - '_ldMeta': { |
120 | | - 'enabled': self.enabled or False, |
121 | | - }, |
122 | | - 'model': self.model.to_dict() if self.model else None, |
123 | | - 'messages': [message.to_dict() for message in self.messages] if self.messages else None, |
124 | | - 'provider': self.provider.to_dict() if self.provider else None, |
125 | | - } |
126 | | - |
127 | | - |
128 | | -@dataclass(frozen=True) |
129 | | -class LDAIAgent: |
130 | | - """ |
131 | | - Represents an AI agent configuration with instructions and model settings. |
132 | | -
|
133 | | - An agent is similar to an AIConfig but focuses on instructions rather than messages, |
134 | | - making it suitable for AI assistant/agent use cases. |
135 | | - """ |
136 | | - enabled: Optional[bool] = None |
137 | | - model: Optional[ModelConfig] = None |
138 | | - provider: Optional[ProviderConfig] = None |
139 | | - instructions: Optional[str] = None |
140 | | - tracker: Optional[LDAIConfigTracker] = None |
141 | | - |
142 | | - def to_dict(self) -> Dict[str, Any]: |
143 | | - """ |
144 | | - Render the given agent as a dictionary object. |
145 | | - """ |
146 | | - result: Dict[str, Any] = { |
147 | | - '_ldMeta': { |
148 | | - 'enabled': self.enabled or False, |
149 | | - }, |
150 | | - 'model': self.model.to_dict() if self.model else None, |
151 | | - 'provider': self.provider.to_dict() if self.provider else None, |
152 | | - } |
153 | | - if self.instructions is not None: |
154 | | - result['instructions'] = self.instructions |
155 | | - return result |
156 | | - |
157 | | - |
158 | | -@dataclass(frozen=True) |
159 | | -class LDAIAgentDefaults: |
160 | | - """ |
161 | | - Default values for AI agent configurations. |
162 | | -
|
163 | | - Similar to LDAIAgent but without tracker and with optional enabled field, |
164 | | - used as fallback values when agent configurations are not available. |
165 | | - """ |
166 | | - enabled: Optional[bool] = None |
167 | | - model: Optional[ModelConfig] = None |
168 | | - provider: Optional[ProviderConfig] = None |
169 | | - instructions: Optional[str] = None |
170 | | - |
171 | | - def to_dict(self) -> Dict[str, Any]: |
172 | | - """ |
173 | | - Render the given agent defaults as a dictionary object. |
174 | | - """ |
175 | | - result: Dict[str, Any] = { |
176 | | - '_ldMeta': { |
177 | | - 'enabled': self.enabled or False, |
178 | | - }, |
179 | | - 'model': self.model.to_dict() if self.model else None, |
180 | | - 'provider': self.provider.to_dict() if self.provider else None, |
181 | | - } |
182 | | - if self.instructions is not None: |
183 | | - result['instructions'] = self.instructions |
184 | | - return result |
185 | | - |
186 | | - |
187 | | -@dataclass |
188 | | -class LDAIAgentConfig: |
189 | | - """ |
190 | | - Configuration for individual agent in batch requests. |
191 | | -
|
192 | | - Combines agent key with its specific default configuration and variables. |
193 | | - """ |
194 | | - key: str |
195 | | - default_value: LDAIAgentDefaults |
196 | | - variables: Optional[Dict[str, Any]] = None |
197 | | - |
198 | | - |
199 | | -# Type alias for multiple agents |
200 | | -LDAIAgents = Dict[str, LDAIAgent] |
201 | | - |
202 | | - |
203 | 20 | class LDAIClient: |
204 | 21 | """The LaunchDarkly AI SDK client object.""" |
205 | 22 |
|
|
0 commit comments