openai
OpenAI integration for CLTK.
Internal; no stability guarantees
This module provides a small wrapper class (:class:OpenAIConnection) around the
OpenAI client and high‑level helpers to generate linguistic annotations from
LLMs for a given language (resolved by Glottolog ID).
OpenAIConnection
OpenAIConnection(
model: AVAILABLE_OPENAI_MODELS,
api_key: Optional[str] = None,
temperature: float = 1.0,
)
Thin wrapper around the OpenAI client for CLTK use cases.
Parameters:
-
api_key(Optional[str], default:None) –OpenAI API key.
-
model(AVAILABLE_OPENAI_MODELS) –Small set of supported model aliases.
-
temperature(float, default:1.0) –Sampling temperature (default 1.0).
Attributes:
-
client–OpenAI client instance.
Initialize the client and resolve language/dialect metadata.
Source code in cltk/genai/openai.py
generate
Call the OpenAI responses API synchronously with retries and code-block parsing.
Source code in cltk/genai/openai.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
AsyncOpenAIConnection
AsyncOpenAIConnection(
model: AVAILABLE_OPENAI_MODELS,
api_key: Optional[str] = None,
temperature: float = 1.0,
)
Asynchronous variant of :class:OpenAIConnection.
Provides an async generate_async() method and uses the
AsyncOpenAI client under the hood. Mirrors the behavior and logging of
the synchronous client while enabling concurrent requests.
Parameters:
-
model(AVAILABLE_OPENAI_MODELS) –Model alias to use (see
AVAILABLE_OPENAI_MODELS). -
api_key(Optional[str], default:None) –Optional OpenAI API key. Falls back to
OPENAI_API_KEY. -
temperature(float, default:1.0) –Sampling temperature for generation.
Source code in cltk/genai/openai.py
generate_async
async
Call the OpenAI responses API asynchronously with retries.
Source code in cltk/genai/openai.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |