@livekit/agents-plugin-openai 0.0.0-next-20260624041820

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +18 -0
  3. package/dist/index.cjs +74 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +9 -0
  6. package/dist/index.d.ts +9 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +30 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/llm.cjs +382 -0
  11. package/dist/llm.cjs.map +1 -0
  12. package/dist/llm.d.cts +230 -0
  13. package/dist/llm.d.ts +230 -0
  14. package/dist/llm.d.ts.map +1 -0
  15. package/dist/llm.js +357 -0
  16. package/dist/llm.js.map +1 -0
  17. package/dist/llm.test.cjs +36 -0
  18. package/dist/llm.test.cjs.map +1 -0
  19. package/dist/llm.test.d.cts +2 -0
  20. package/dist/llm.test.d.ts +2 -0
  21. package/dist/llm.test.d.ts.map +1 -0
  22. package/dist/llm.test.js +35 -0
  23. package/dist/llm.test.js.map +1 -0
  24. package/dist/models.cjs +50 -0
  25. package/dist/models.cjs.map +1 -0
  26. package/dist/models.d.cts +26 -0
  27. package/dist/models.d.ts +26 -0
  28. package/dist/models.d.ts.map +1 -0
  29. package/dist/models.js +25 -0
  30. package/dist/models.js.map +1 -0
  31. package/dist/realtime/api_proto.cjs +41 -0
  32. package/dist/realtime/api_proto.cjs.map +1 -0
  33. package/dist/realtime/api_proto.d.cts +501 -0
  34. package/dist/realtime/api_proto.d.ts +501 -0
  35. package/dist/realtime/api_proto.d.ts.map +1 -0
  36. package/dist/realtime/api_proto.js +13 -0
  37. package/dist/realtime/api_proto.js.map +1 -0
  38. package/dist/realtime/index.cjs +25 -0
  39. package/dist/realtime/index.cjs.map +1 -0
  40. package/dist/realtime/index.d.cts +3 -0
  41. package/dist/realtime/index.d.ts +3 -0
  42. package/dist/realtime/index.d.ts.map +1 -0
  43. package/dist/realtime/index.js +3 -0
  44. package/dist/realtime/index.js.map +1 -0
  45. package/dist/realtime/realtime_model.cjs +1658 -0
  46. package/dist/realtime/realtime_model.cjs.map +1 -0
  47. package/dist/realtime/realtime_model.d.cts +202 -0
  48. package/dist/realtime/realtime_model.d.ts +202 -0
  49. package/dist/realtime/realtime_model.d.ts.map +1 -0
  50. package/dist/realtime/realtime_model.js +1637 -0
  51. package/dist/realtime/realtime_model.js.map +1 -0
  52. package/dist/realtime/realtime_model.test.cjs +596 -0
  53. package/dist/realtime/realtime_model.test.cjs.map +1 -0
  54. package/dist/realtime/realtime_model.test.d.cts +2 -0
  55. package/dist/realtime/realtime_model.test.d.ts +2 -0
  56. package/dist/realtime/realtime_model.test.d.ts.map +1 -0
  57. package/dist/realtime/realtime_model.test.js +600 -0
  58. package/dist/realtime/realtime_model.test.js.map +1 -0
  59. package/dist/responses/index.cjs +23 -0
  60. package/dist/responses/index.cjs.map +1 -0
  61. package/dist/responses/index.d.cts +2 -0
  62. package/dist/responses/index.d.ts +2 -0
  63. package/dist/responses/index.d.ts.map +1 -0
  64. package/dist/responses/index.js +2 -0
  65. package/dist/responses/index.js.map +1 -0
  66. package/dist/responses/llm.cjs +353 -0
  67. package/dist/responses/llm.cjs.map +1 -0
  68. package/dist/responses/llm.d.cts +52 -0
  69. package/dist/responses/llm.d.ts +52 -0
  70. package/dist/responses/llm.d.ts.map +1 -0
  71. package/dist/responses/llm.js +327 -0
  72. package/dist/responses/llm.js.map +1 -0
  73. package/dist/responses/llm.test.cjs +37 -0
  74. package/dist/responses/llm.test.cjs.map +1 -0
  75. package/dist/responses/llm.test.d.cts +2 -0
  76. package/dist/responses/llm.test.d.ts +2 -0
  77. package/dist/responses/llm.test.d.ts.map +1 -0
  78. package/dist/responses/llm.test.js +36 -0
  79. package/dist/responses/llm.test.js.map +1 -0
  80. package/dist/stt.cjs +627 -0
  81. package/dist/stt.cjs.map +1 -0
  82. package/dist/stt.d.cts +105 -0
  83. package/dist/stt.d.ts +105 -0
  84. package/dist/stt.d.ts.map +1 -0
  85. package/dist/stt.js +605 -0
  86. package/dist/stt.js.map +1 -0
  87. package/dist/stt.test.cjs +132 -0
  88. package/dist/stt.test.cjs.map +1 -0
  89. package/dist/stt.test.d.cts +2 -0
  90. package/dist/stt.test.d.ts +2 -0
  91. package/dist/stt.test.d.ts.map +1 -0
  92. package/dist/stt.test.js +131 -0
  93. package/dist/stt.test.js.map +1 -0
  94. package/dist/tool_utils.cjs +53 -0
  95. package/dist/tool_utils.cjs.map +1 -0
  96. package/dist/tool_utils.d.cts +4 -0
  97. package/dist/tool_utils.d.ts +4 -0
  98. package/dist/tool_utils.d.ts.map +1 -0
  99. package/dist/tool_utils.js +29 -0
  100. package/dist/tool_utils.js.map +1 -0
  101. package/dist/tool_utils.test.cjs +77 -0
  102. package/dist/tool_utils.test.cjs.map +1 -0
  103. package/dist/tool_utils.test.d.cts +2 -0
  104. package/dist/tool_utils.test.d.ts +2 -0
  105. package/dist/tool_utils.test.d.ts.map +1 -0
  106. package/dist/tool_utils.test.js +76 -0
  107. package/dist/tool_utils.test.js.map +1 -0
  108. package/dist/tools.cjs +117 -0
  109. package/dist/tools.cjs.map +1 -0
  110. package/dist/tools.d.cts +88 -0
  111. package/dist/tools.d.ts +88 -0
  112. package/dist/tools.d.ts.map +1 -0
  113. package/dist/tools.js +90 -0
  114. package/dist/tools.js.map +1 -0
  115. package/dist/tts.cjs +141 -0
  116. package/dist/tts.cjs.map +1 -0
  117. package/dist/tts.d.cts +42 -0
  118. package/dist/tts.d.ts +42 -0
  119. package/dist/tts.d.ts.map +1 -0
  120. package/dist/tts.js +116 -0
  121. package/dist/tts.js.map +1 -0
  122. package/dist/tts.test.cjs +17 -0
  123. package/dist/tts.test.cjs.map +1 -0
  124. package/dist/tts.test.d.cts +2 -0
  125. package/dist/tts.test.d.ts +2 -0
  126. package/dist/tts.test.d.ts.map +1 -0
  127. package/dist/tts.test.js +16 -0
  128. package/dist/tts.test.js.map +1 -0
  129. package/dist/ws/index.cjs +29 -0
  130. package/dist/ws/index.cjs.map +1 -0
  131. package/dist/ws/index.d.cts +3 -0
  132. package/dist/ws/index.d.ts +3 -0
  133. package/dist/ws/index.d.ts.map +1 -0
  134. package/dist/ws/index.js +5 -0
  135. package/dist/ws/index.js.map +1 -0
  136. package/dist/ws/llm.cjs +523 -0
  137. package/dist/ws/llm.cjs.map +1 -0
  138. package/dist/ws/llm.d.cts +96 -0
  139. package/dist/ws/llm.d.ts +96 -0
  140. package/dist/ws/llm.d.ts.map +1 -0
  141. package/dist/ws/llm.js +505 -0
  142. package/dist/ws/llm.js.map +1 -0
  143. package/dist/ws/llm.test.cjs +69 -0
  144. package/dist/ws/llm.test.cjs.map +1 -0
  145. package/dist/ws/llm.test.d.cts +2 -0
  146. package/dist/ws/llm.test.d.ts +2 -0
  147. package/dist/ws/llm.test.d.ts.map +1 -0
  148. package/dist/ws/llm.test.js +68 -0
  149. package/dist/ws/llm.test.js.map +1 -0
  150. package/dist/ws/types.cjs +129 -0
  151. package/dist/ws/types.cjs.map +1 -0
  152. package/dist/ws/types.d.cts +172 -0
  153. package/dist/ws/types.d.ts +172 -0
  154. package/dist/ws/types.d.ts.map +1 -0
  155. package/dist/ws/types.js +96 -0
  156. package/dist/ws/types.js.map +1 -0
  157. package/package.json +56 -0
  158. package/src/index.ts +25 -0
  159. package/src/llm.test.ts +38 -0
  160. package/src/llm.ts +553 -0
  161. package/src/models.ts +235 -0
  162. package/src/realtime/api_proto.ts +714 -0
  163. package/src/realtime/index.ts +5 -0
  164. package/src/realtime/realtime_model.test.ts +793 -0
  165. package/src/realtime/realtime_model.ts +2116 -0
  166. package/src/responses/index.ts +4 -0
  167. package/src/responses/llm.test.ts +39 -0
  168. package/src/responses/llm.ts +452 -0
  169. package/src/stt.test.ts +167 -0
  170. package/src/stt.ts +793 -0
  171. package/src/tool_utils.test.ts +84 -0
  172. package/src/tool_utils.ts +46 -0
  173. package/src/tools.ts +166 -0
  174. package/src/tts.test.ts +19 -0
  175. package/src/tts.ts +158 -0
  176. package/src/ws/index.ts +17 -0
  177. package/src/ws/llm.test.ts +83 -0
  178. package/src/ws/llm.ts +710 -0
  179. package/src/ws/types.ts +132 -0
package/src/llm.ts ADDED
@@ -0,0 +1,553 @@
1
+ // SPDX-FileCopyrightText: 2025 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ import type { APIConnectOptions } from '@livekit/agents';
5
+ import { DEFAULT_API_CONNECT_OPTIONS, inference, llm } from '@livekit/agents';
6
+ import { AzureOpenAI, OpenAI } from 'openai';
7
+ import type {
8
+ CerebrasChatModels,
9
+ ChatModels,
10
+ DeepSeekChatModels,
11
+ GroqChatModels,
12
+ MetaChatModels,
13
+ OctoChatModels,
14
+ PerplexityChatModels,
15
+ ReasoningEffort,
16
+ TelnyxChatModels,
17
+ TogetherChatModels,
18
+ XAIChatModels,
19
+ } from './models.js';
20
+ import { defaultReasoningEffort } from './models.js';
21
+
22
+ export interface LLMOptions {
23
+ model: string | ChatModels;
24
+ apiKey?: string;
25
+ baseURL?: string;
26
+ user?: string;
27
+ temperature?: number;
28
+ topP?: number;
29
+ client?: OpenAI;
30
+ toolChoice?: llm.ToolChoice;
31
+ parallelToolCalls?: boolean;
32
+ metadata?: Record<string, string>;
33
+ maxCompletionTokens?: number;
34
+ serviceTier?: string;
35
+ store?: boolean;
36
+ reasoningEffort?: ReasoningEffort;
37
+ strictToolSchema?: boolean;
38
+ }
39
+
40
+ const defaultLLMOptions: LLMOptions = {
41
+ model: 'gpt-4.1',
42
+ apiKey: process.env.OPENAI_API_KEY,
43
+ parallelToolCalls: true,
44
+ strictToolSchema: false,
45
+ };
46
+
47
+ const defaultAzureLLMOptions: LLMOptions = {
48
+ model: 'gpt-4.1',
49
+ apiKey: process.env.AZURE_API_KEY,
50
+ strictToolSchema: false,
51
+ };
52
+
53
+ export class LLM extends llm.LLM {
54
+ #opts: LLMOptions;
55
+ #client: OpenAI;
56
+ #providerFmt: llm.ProviderFormat;
57
+
58
+ /**
59
+ * Create a new instance of OpenAI LLM.
60
+ *
61
+ * @remarks
62
+ * `apiKey` must be set to your OpenAI API key, either using the argument or by setting the
63
+ * `OPENAI_API_KEY` environment variable.
64
+ */
65
+ constructor(
66
+ opts: Partial<LLMOptions> = defaultLLMOptions,
67
+ providerFmt: llm.ProviderFormat = 'openai',
68
+ ) {
69
+ super();
70
+
71
+ this.#opts = { ...defaultLLMOptions, ...opts };
72
+ this.#opts.reasoningEffort ??= defaultReasoningEffort(this.#opts.model);
73
+ this.#providerFmt = providerFmt;
74
+ if (this.#opts.apiKey === undefined && !this.#opts.client) {
75
+ throw new Error('OpenAI API key is required, whether as an argument or as $OPENAI_API_KEY');
76
+ }
77
+
78
+ this.#client =
79
+ this.#opts.client ||
80
+ new OpenAI({
81
+ baseURL: this.#opts.baseURL,
82
+ apiKey: this.#opts.apiKey,
83
+ });
84
+ }
85
+
86
+ label(): string {
87
+ return 'openai.LLM';
88
+ }
89
+
90
+ get model(): string {
91
+ return this.#opts.model;
92
+ }
93
+
94
+ get provider(): string {
95
+ try {
96
+ const url = new URL(this.#client.baseURL);
97
+ return url.host;
98
+ } catch {
99
+ return 'api.openai.com';
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Create a new instance of OpenAI LLM with Azure.
105
+ *
106
+ * @remarks
107
+ * This automatically infers the following arguments from their corresponding environment variables if they are not provided:
108
+ * - `apiKey` from `AZURE_OPENAI_API_KEY`
109
+ * - `organization` from `OPENAI_ORG_ID`
110
+ * - `project` from `OPENAI_PROJECT_ID`
111
+ * - `azureAdToken` from `AZURE_OPENAI_AD_TOKEN`
112
+ * - `apiVersion` from `OPENAI_API_VERSION`
113
+ * - `azureEndpoint` from `AZURE_OPENAI_ENDPOINT`
114
+ */
115
+ static withAzure(
116
+ opts: {
117
+ model: string | ChatModels;
118
+ azureEndpoint?: string;
119
+ azureDeployment?: string;
120
+ apiVersion?: string;
121
+ apiKey?: string;
122
+ azureAdToken?: string;
123
+ azureAdTokenProvider?: () => Promise<string>;
124
+ organization?: string;
125
+ project?: string;
126
+ baseURL?: string;
127
+ user?: string;
128
+ temperature?: number;
129
+ } = defaultAzureLLMOptions,
130
+ ): LLM {
131
+ opts = { ...defaultAzureLLMOptions, ...opts };
132
+ if (opts.apiKey === undefined) {
133
+ throw new Error('Azure API key is required, whether as an argument or as $AZURE_API_KEY');
134
+ }
135
+
136
+ return new LLM({
137
+ temperature: opts.temperature,
138
+ user: opts.user,
139
+ client: new AzureOpenAI(opts),
140
+ });
141
+ }
142
+
143
+ /**
144
+ * Create a new instance of Cerebras LLM.
145
+ *
146
+ * @remarks
147
+ * `apiKey` must be set to your Cerebras API key, either using the argument or by setting the
148
+ * `CEREBRAS_API_KEY` environment variable.
149
+ */
150
+ static withCerebras(
151
+ opts: Partial<{
152
+ model: string | CerebrasChatModels;
153
+ apiKey?: string;
154
+ baseURL?: string;
155
+ user?: string;
156
+ temperature?: number;
157
+ client: OpenAI;
158
+ }> = {},
159
+ ): LLM {
160
+ opts.apiKey = opts.apiKey || process.env.CEREBRAS_API_KEY;
161
+ if (opts.apiKey === undefined) {
162
+ throw new Error(
163
+ 'Cerebras API key is required, whether as an argument or as $CEREBRAS_API_KEY',
164
+ );
165
+ }
166
+
167
+ return new LLM({
168
+ model: 'llama3.1-8b',
169
+ baseURL: 'https://api.cerebras.ai/v1',
170
+ ...opts,
171
+ });
172
+ }
173
+
174
+ /**
175
+ * Create a new instance of Fireworks LLM.
176
+ *
177
+ * @remarks
178
+ * `apiKey` must be set to your Fireworks API key, either using the argument or by setting the
179
+ * `FIREWORKS_API_KEY` environment variable.
180
+ */
181
+ static withFireworks(opts: Partial<LLMOptions> = {}): LLM {
182
+ opts.apiKey = opts.apiKey || process.env.FIREWORKS_API_KEY;
183
+ if (opts.apiKey === undefined) {
184
+ throw new Error(
185
+ 'Fireworks API key is required, whether as an argument or as $FIREWORKS_API_KEY',
186
+ );
187
+ }
188
+
189
+ return new LLM({
190
+ model: 'accounts/fireworks/models/llama-v3p1-70b-instruct',
191
+ baseURL: 'https://api.fireworks.ai/inference/v1',
192
+ ...opts,
193
+ });
194
+ }
195
+
196
+ /**
197
+ * Create a new instance of xAI LLM.
198
+ *
199
+ * @remarks
200
+ * `apiKey` must be set to your xAI API key, either using the argument or by setting the
201
+ * `XAI_API_KEY` environment variable.
202
+ */
203
+ static withXAI(
204
+ opts: Partial<{
205
+ model: string | XAIChatModels;
206
+ apiKey?: string;
207
+ baseURL?: string;
208
+ user?: string;
209
+ temperature?: number;
210
+ client: OpenAI;
211
+ }> = {},
212
+ ): LLM {
213
+ opts.apiKey = opts.apiKey || process.env.XAI_API_KEY;
214
+ if (opts.apiKey === undefined) {
215
+ throw new Error('xAI API key is required, whether as an argument or as $XAI_API_KEY');
216
+ }
217
+
218
+ return new LLM({
219
+ model: 'grok-2-public',
220
+ baseURL: 'https://api.x.ai/v1',
221
+ ...opts,
222
+ });
223
+ }
224
+
225
+ /**
226
+ * Create a new instance of Groq LLM.
227
+ *
228
+ * @remarks
229
+ * `apiKey` must be set to your Groq API key, either using the argument or by setting the
230
+ * `GROQ_API_KEY` environment variable.
231
+ */
232
+ static withGroq(
233
+ opts: Partial<{
234
+ model: string | GroqChatModels;
235
+ apiKey?: string;
236
+ baseURL?: string;
237
+ user?: string;
238
+ temperature?: number;
239
+ client: OpenAI;
240
+ }> = {},
241
+ ): LLM {
242
+ opts.apiKey = opts.apiKey || process.env.GROQ_API_KEY;
243
+ if (opts.apiKey === undefined) {
244
+ throw new Error('Groq API key is required, whether as an argument or as $GROQ_API_KEY');
245
+ }
246
+
247
+ return new LLM({
248
+ model: 'openai/gpt-oss-120b',
249
+ baseURL: 'https://api.groq.com/openai/v1',
250
+ ...opts,
251
+ });
252
+ }
253
+
254
+ /**
255
+ * Create a new instance of DeepSeek LLM.
256
+ *
257
+ * @remarks
258
+ * `apiKey` must be set to your DeepSeek API key, either using the argument or by setting the
259
+ * `DEEPSEEK_API_KEY` environment variable.
260
+ */
261
+ static withDeepSeek(
262
+ opts: Partial<{
263
+ model: string | DeepSeekChatModels;
264
+ apiKey?: string;
265
+ baseURL?: string;
266
+ user?: string;
267
+ temperature?: number;
268
+ client: OpenAI;
269
+ }> = {},
270
+ ): LLM {
271
+ opts.apiKey = opts.apiKey || process.env.DEEPSEEK_API_KEY;
272
+ if (opts.apiKey === undefined) {
273
+ throw new Error(
274
+ 'DeepSeek API key is required, whether as an argument or as $DEEPSEEK_API_KEY',
275
+ );
276
+ }
277
+
278
+ return new LLM({
279
+ model: 'deepseek-chat',
280
+ baseURL: 'https://api.deepseek.com/v1',
281
+ ...opts,
282
+ });
283
+ }
284
+
285
+ /**
286
+ * Create a new instance of OctoAI LLM.
287
+ *
288
+ * @remarks
289
+ * `apiKey` must be set to your OctoAI API key, either using the argument or by setting the
290
+ * `OCTOAI_TOKEN` environment variable.
291
+ */
292
+ static withOcto(
293
+ opts: Partial<{
294
+ model: string | OctoChatModels;
295
+ apiKey?: string;
296
+ baseURL?: string;
297
+ user?: string;
298
+ temperature?: number;
299
+ client: OpenAI;
300
+ }> = {},
301
+ ): LLM {
302
+ opts.apiKey = opts.apiKey || process.env.OCTOAI_TOKEN;
303
+ if (opts.apiKey === undefined) {
304
+ throw new Error('OctoAI API key is required, whether as an argument or as $OCTOAI_TOKEN');
305
+ }
306
+
307
+ return new LLM({
308
+ model: 'llama-2-13b-chat',
309
+ baseURL: 'https://text.octoai.run/v1',
310
+ ...opts,
311
+ });
312
+ }
313
+
314
+ /** Create a new instance of Ollama LLM. */
315
+ static withOllama(
316
+ opts: Partial<{
317
+ model: string;
318
+ baseURL?: string;
319
+ temperature?: number;
320
+ client: OpenAI;
321
+ }> = {},
322
+ ): LLM {
323
+ return new LLM({
324
+ model: 'llama3.1',
325
+ baseURL: 'http://localhost:11434/v1',
326
+ apiKey: 'ollama',
327
+ ...opts,
328
+ });
329
+ }
330
+
331
+ /**
332
+ * Create a new instance of PerplexityAI LLM.
333
+ *
334
+ * @remarks
335
+ * `apiKey` must be set to your PerplexityAI API key, either using the argument or by setting the
336
+ * `PERPLEXITY_API_KEY` environment variable.
337
+ */
338
+ static withPerplexity(
339
+ opts: Partial<{
340
+ model: string | PerplexityChatModels;
341
+ apiKey?: string;
342
+ baseURL?: string;
343
+ user?: string;
344
+ temperature?: number;
345
+ client: OpenAI;
346
+ }> = {},
347
+ ): LLM {
348
+ opts.apiKey = opts.apiKey || process.env.PERPLEXITY_API_KEY;
349
+ if (opts.apiKey === undefined) {
350
+ throw new Error(
351
+ 'PerplexityAI API key is required, whether as an argument or as $PERPLEXITY_API_KEY',
352
+ );
353
+ }
354
+
355
+ return new LLM({
356
+ model: 'llama-3.1-sonar-small-128k-chat',
357
+ baseURL: 'https://api.perplexity.ai',
358
+ ...opts,
359
+ });
360
+ }
361
+
362
+ /**
363
+ * Create a new instance of TogetherAI LLM.
364
+ *
365
+ * @remarks
366
+ * `apiKey` must be set to your TogetherAI API key, either using the argument or by setting the
367
+ * `TOGETHER_API_KEY` environment variable.
368
+ */
369
+ static withTogether(
370
+ opts: Partial<{
371
+ model: string | TogetherChatModels;
372
+ apiKey?: string;
373
+ baseURL?: string;
374
+ user?: string;
375
+ temperature?: number;
376
+ client: OpenAI;
377
+ }> = {},
378
+ ): LLM {
379
+ opts.apiKey = opts.apiKey || process.env.TOGETHER_API_KEY;
380
+ if (opts.apiKey === undefined) {
381
+ throw new Error(
382
+ 'TogetherAI API key is required, whether as an argument or as $TOGETHER_API_KEY',
383
+ );
384
+ }
385
+
386
+ return new LLM({
387
+ model: 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo',
388
+ baseURL: 'https://api.together.xyz/v1',
389
+ ...opts,
390
+ });
391
+ }
392
+
393
+ /**
394
+ * Create a new instance of Telnyx LLM.
395
+ *
396
+ * @remarks
397
+ * `apiKey` must be set to your Telnyx API key, either using the argument or by setting the
398
+ * `TELNYX_API_KEY` environment variable.
399
+ */
400
+ static withTelnyx(
401
+ opts: Partial<{
402
+ model: string | TelnyxChatModels;
403
+ apiKey?: string;
404
+ baseURL?: string;
405
+ user?: string;
406
+ temperature?: number;
407
+ client: OpenAI;
408
+ }> = {},
409
+ ): LLM {
410
+ opts.apiKey = opts.apiKey || process.env.TELNYX_API_KEY;
411
+ if (opts.apiKey === undefined) {
412
+ throw new Error('Telnyx API key is required, whether as an argument or as $TELNYX_API_KEY');
413
+ }
414
+
415
+ return new LLM({
416
+ model: 'meta-llama/Meta-Llama-3.1-70B-Instruct',
417
+ baseURL: 'https://api.telnyx.com/v2/ai',
418
+ ...opts,
419
+ });
420
+ }
421
+
422
+ /**
423
+ * Create a new instance of Meta Llama LLM.
424
+ *
425
+ * @remarks
426
+ * `apiKey` must be set to your Meta Llama API key, either using the argument or by setting the
427
+ * `LLAMA_API_KEY` environment variable.
428
+ */
429
+ static withMeta(
430
+ opts: Partial<{
431
+ apiKey?: string;
432
+ baseURL?: string;
433
+ client?: OpenAI;
434
+ model?: string | MetaChatModels;
435
+ temperature?: number;
436
+ user?: string;
437
+ }> = {},
438
+ ): LLM {
439
+ opts.apiKey = opts.apiKey || process.env.LLAMA_API_KEY;
440
+ opts.baseURL = opts.baseURL || 'https://api.llama.com/compat/v1/';
441
+ opts.model = opts.model || 'Llama-4-Maverick-17B-128E-Instruct-FP8';
442
+
443
+ if (opts.apiKey === undefined) {
444
+ throw new Error(
445
+ 'Meta Llama API key is required, either as argument or set LLAMA_API_KEY environment variable',
446
+ );
447
+ }
448
+
449
+ return new LLM(opts);
450
+ }
451
+
452
+ /**
453
+ * Create a new instance of OVHcloud AI Endpoints LLM.
454
+ *
455
+ * @remarks
456
+ * `apiKey` must be set to your OVHcloud AI Endpoints API key, either using the argument or by setting the
457
+ * `OVHCLOUD_API_KEY` environment variable.
458
+ */
459
+ static withOVHcloud(opts: Partial<LLMOptions> = {}): LLM {
460
+ opts.apiKey = opts.apiKey || process.env.OVHCLOUD_API_KEY;
461
+ if (opts.apiKey === undefined) {
462
+ throw new Error(
463
+ 'OVHcloud AI Endpoints API key is required, whether as an argument or as $OVHCLOUD_API_KEY',
464
+ );
465
+ }
466
+
467
+ return new LLM({
468
+ model: 'gpt-oss-120b',
469
+ baseURL: 'https://oai.endpoints.kepler.ai.cloud.ovh.net/v1',
470
+ ...opts,
471
+ });
472
+ }
473
+
474
+ chat({
475
+ chatCtx,
476
+ toolCtx: toolCtxInput,
477
+ connOptions = DEFAULT_API_CONNECT_OPTIONS,
478
+ parallelToolCalls,
479
+ toolChoice,
480
+ extraKwargs,
481
+ }: {
482
+ chatCtx: llm.ChatContext;
483
+ toolCtx?: llm.ToolContextLike;
484
+ connOptions?: APIConnectOptions;
485
+ parallelToolCalls?: boolean;
486
+ toolChoice?: llm.ToolChoice;
487
+ extraKwargs?: Record<string, unknown>;
488
+ }): LLMStream {
489
+ const toolCtx = llm.toToolContext(toolCtxInput);
490
+ const extras: Record<string, unknown> = { ...extraKwargs };
491
+
492
+ if (this.#opts.metadata) {
493
+ extras.metadata = this.#opts.metadata;
494
+ }
495
+
496
+ if (this.#opts.user) {
497
+ extras.user = this.#opts.user;
498
+ }
499
+
500
+ if (this.#opts.maxCompletionTokens) {
501
+ extras.max_completion_tokens = this.#opts.maxCompletionTokens;
502
+ }
503
+
504
+ if (this.#opts.temperature) {
505
+ extras.temperature = this.#opts.temperature;
506
+ }
507
+
508
+ if (this.#opts.topP !== undefined) {
509
+ extras.top_p = this.#opts.topP;
510
+ }
511
+
512
+ if (this.#opts.serviceTier) {
513
+ extras.service_tier = this.#opts.serviceTier;
514
+ }
515
+
516
+ if (this.#opts.store !== undefined) {
517
+ extras.store = this.#opts.store;
518
+ }
519
+
520
+ if (this.#opts.reasoningEffort !== undefined) {
521
+ extras.reasoning_effort = this.#opts.reasoningEffort;
522
+ }
523
+
524
+ parallelToolCalls =
525
+ parallelToolCalls !== undefined ? parallelToolCalls : this.#opts.parallelToolCalls;
526
+ if (
527
+ toolCtx &&
528
+ Object.keys(toolCtx.functionTools).length > 0 &&
529
+ parallelToolCalls !== undefined
530
+ ) {
531
+ extras.parallel_tool_calls = parallelToolCalls;
532
+ }
533
+
534
+ toolChoice = toolChoice !== undefined ? toolChoice : this.#opts.toolChoice;
535
+ if (toolChoice) {
536
+ extras.tool_choice = toolChoice;
537
+ }
538
+
539
+ return new LLMStream(this as unknown as inference.LLM, {
540
+ model: this.#opts.model,
541
+ providerFmt: this.#providerFmt,
542
+ client: this.#client as any,
543
+ chatCtx,
544
+ toolCtx,
545
+ connOptions,
546
+ modelOptions: extras,
547
+ strictToolSchema: this.#opts.strictToolSchema || false,
548
+ gatewayOptions: undefined, // OpenAI plugin doesn't use gateway authentication
549
+ });
550
+ }
551
+ }
552
+
553
+ export class LLMStream extends inference.LLMStream {}