llm-exe 2.1.0 → 2.1.2

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.
package/dist/index.d.mts CHANGED
@@ -954,6 +954,9 @@ type AllLlm = {
954
954
  "amazon:meta.chat.v1": {
955
955
  input: AmazonBedrockRequest;
956
956
  };
957
+ "xai.chat.v1": {
958
+ input: OpenAiRequest;
959
+ };
957
960
  };
958
961
  type AllUseLlmOptions = AllLlm & {
959
962
  "openai.gpt-4": {
@@ -977,6 +980,9 @@ type AllUseLlmOptions = AllLlm & {
977
980
  "anthropic.claude-3-haiku": {
978
981
  input: Omit<AnthropicRequest, "model">;
979
982
  };
983
+ "xai.grok-2": {
984
+ input: OpenAiRequest;
985
+ };
980
986
  };
981
987
  type LlmProviderKey = keyof AllLlm;
982
988
  type EmbeddingProviderKey = keyof AllEmbedding;
@@ -994,7 +1000,7 @@ interface BaseRequest<_T extends Record<string, any>> {
994
1000
  interface BaseLlm<_T extends BaseLlCall = BaseLlCall> extends BaseRequest<_T> {
995
1001
  }
996
1002
 
997
- type LlmProvider = "openai.chat" | "openai.embedding" | "openai.chat-mock" | "anthropic.chat" | "amazon:anthropic.chat" | "amazon:meta.chat" | "amazon:nova.chat" | "amazon.embedding";
1003
+ type LlmProvider = "openai.chat" | "openai.embedding" | "openai.chat-mock" | "anthropic.chat" | "amazon:anthropic.chat" | "amazon:meta.chat" | "amazon:nova.chat" | "amazon.embedding" | "xai.chat";
998
1004
  interface Config<Pk = LlmProviderKey> {
999
1005
  key: Pk;
1000
1006
  provider: LlmProvider;
@@ -1168,6 +1174,8 @@ declare namespace index {
1168
1174
  }
1169
1175
 
1170
1176
  declare const configs: {
1177
+ "xai.chat.v1": Config<keyof AllLlm>;
1178
+ "xai.grok-2": Config<keyof AllLlm>;
1171
1179
  "amazon:anthropic.chat.v1": Config<keyof AllLlm>;
1172
1180
  "amazon:meta.chat.v1": Config<keyof AllLlm>;
1173
1181
  "anthropic.chat.v1": Config<keyof AllLlm>;
package/dist/index.d.ts CHANGED
@@ -954,6 +954,9 @@ type AllLlm = {
954
954
  "amazon:meta.chat.v1": {
955
955
  input: AmazonBedrockRequest;
956
956
  };
957
+ "xai.chat.v1": {
958
+ input: OpenAiRequest;
959
+ };
957
960
  };
958
961
  type AllUseLlmOptions = AllLlm & {
959
962
  "openai.gpt-4": {
@@ -977,6 +980,9 @@ type AllUseLlmOptions = AllLlm & {
977
980
  "anthropic.claude-3-haiku": {
978
981
  input: Omit<AnthropicRequest, "model">;
979
982
  };
983
+ "xai.grok-2": {
984
+ input: OpenAiRequest;
985
+ };
980
986
  };
981
987
  type LlmProviderKey = keyof AllLlm;
982
988
  type EmbeddingProviderKey = keyof AllEmbedding;
@@ -994,7 +1000,7 @@ interface BaseRequest<_T extends Record<string, any>> {
994
1000
  interface BaseLlm<_T extends BaseLlCall = BaseLlCall> extends BaseRequest<_T> {
995
1001
  }
996
1002
 
997
- type LlmProvider = "openai.chat" | "openai.embedding" | "openai.chat-mock" | "anthropic.chat" | "amazon:anthropic.chat" | "amazon:meta.chat" | "amazon:nova.chat" | "amazon.embedding";
1003
+ type LlmProvider = "openai.chat" | "openai.embedding" | "openai.chat-mock" | "anthropic.chat" | "amazon:anthropic.chat" | "amazon:meta.chat" | "amazon:nova.chat" | "amazon.embedding" | "xai.chat";
998
1004
  interface Config<Pk = LlmProviderKey> {
999
1005
  key: Pk;
1000
1006
  provider: LlmProvider;
@@ -1168,6 +1174,8 @@ declare namespace index {
1168
1174
  }
1169
1175
 
1170
1176
  declare const configs: {
1177
+ "xai.chat.v1": Config<keyof AllLlm>;
1178
+ "xai.grok-2": Config<keyof AllLlm>;
1171
1179
  "amazon:anthropic.chat.v1": Config<keyof AllLlm>;
1172
1180
  "amazon:meta.chat.v1": Config<keyof AllLlm>;
1173
1181
  "anthropic.chat.v1": Config<keyof AllLlm>;
package/dist/index.js CHANGED
@@ -2145,7 +2145,16 @@ function anthropicPromptSanitize(_messages, _inputBodyObj, _outputObj) {
2145
2145
  ...a
2146
2146
  }))
2147
2147
  ];
2148
- if (first.role === "system") {
2148
+ if (first.role === "system" && messages.length === 0) {
2149
+ return [
2150
+ {
2151
+ role: "user",
2152
+ content: first.content
2153
+ },
2154
+ ...messages
2155
+ ];
2156
+ }
2157
+ if (first.role === "system" && messages.length > 0) {
2149
2158
  _outputObj.system = first.content;
2150
2159
  return messages;
2151
2160
  }
@@ -2286,11 +2295,59 @@ var bedrock = {
2286
2295
  "amazon:meta.chat.v1": amazonMetaChatV1
2287
2296
  };
2288
2297
 
2298
+ // src/llm/config/x/index.ts
2299
+ var xaiChatV1 = {
2300
+ key: "xai.chat.v1",
2301
+ provider: "xai.chat",
2302
+ endpoint: `https://api.x.ai/v1/chat/completions`,
2303
+ options: {
2304
+ prompt: {},
2305
+ topP: {},
2306
+ useJson: {},
2307
+ xAiApiKey: {
2308
+ default: getEnvironmentVariable("XAI_API_KEY")
2309
+ }
2310
+ },
2311
+ method: "POST",
2312
+ headers: `{"Authorization":"Bearer {{xAiApiKey}}", "Content-Type": "application/json" }`,
2313
+ mapBody: {
2314
+ prompt: {
2315
+ key: "messages",
2316
+ sanitize: /* @__PURE__ */ __name((v) => {
2317
+ if (typeof v === "string") {
2318
+ return [
2319
+ {
2320
+ role: "user",
2321
+ content: v
2322
+ }
2323
+ ];
2324
+ }
2325
+ return v;
2326
+ }, "sanitize")
2327
+ },
2328
+ model: {
2329
+ key: "model"
2330
+ },
2331
+ topP: {
2332
+ key: "top_p"
2333
+ },
2334
+ useJson: {
2335
+ key: "response_format.type",
2336
+ sanitize: /* @__PURE__ */ __name((v) => v ? "json_object" : "text", "sanitize")
2337
+ }
2338
+ }
2339
+ };
2340
+ var xai = {
2341
+ "xai.chat.v1": xaiChatV1,
2342
+ "xai.grok-2": withDefaultModel(xaiChatV1, "grok-2-latest")
2343
+ };
2344
+
2289
2345
  // src/llm/config.ts
2290
2346
  var configs = {
2291
2347
  ...openai,
2292
2348
  ...anthropic,
2293
- ...bedrock
2349
+ ...bedrock,
2350
+ ...xai
2294
2351
  };
2295
2352
  function getLlmConfig(provider) {
2296
2353
  const pick2 = configs[provider];
@@ -2521,6 +2578,54 @@ function OutputDefault(result, _config) {
2521
2578
  }
2522
2579
  __name(OutputDefault, "OutputDefault");
2523
2580
 
2581
+ // src/llm/output/xai.ts
2582
+ function formatResult3(result) {
2583
+ if (typeof result?.message?.content === "string") {
2584
+ return {
2585
+ type: "text",
2586
+ text: result.message.content
2587
+ };
2588
+ } else if (result?.message && "tool_calls" in result.message) {
2589
+ const tool_calls = result.message.tool_calls;
2590
+ for (const call of tool_calls) {
2591
+ return {
2592
+ type: "function_use",
2593
+ name: call.function.name,
2594
+ input: JSON.parse(call.function.arguments)
2595
+ };
2596
+ }
2597
+ }
2598
+ return {
2599
+ type: "text",
2600
+ text: ""
2601
+ };
2602
+ }
2603
+ __name(formatResult3, "formatResult");
2604
+ function OutputXAIChat(result, _config) {
2605
+ const id = result.id;
2606
+ const name = result?.model;
2607
+ const created = result.created;
2608
+ const [_content, ..._options] = result?.choices || [];
2609
+ const stopReason = _content?.finish_reason;
2610
+ const content = formatContent(_content, formatResult3);
2611
+ const options = formatOptions(_options, formatResult3);
2612
+ const usage = {
2613
+ output_tokens: result?.usage?.completion_tokens,
2614
+ input_tokens: result?.usage?.prompt_tokens,
2615
+ total_tokens: result?.usage?.total_tokens
2616
+ };
2617
+ return BaseLlmOutput2({
2618
+ id,
2619
+ name,
2620
+ created,
2621
+ usage,
2622
+ stopReason,
2623
+ content,
2624
+ options
2625
+ });
2626
+ }
2627
+ __name(OutputXAIChat, "OutputXAIChat");
2628
+
2524
2629
  // src/llm/output/index.ts
2525
2630
  function getOutputParser(config, response) {
2526
2631
  switch (config?.key) {
@@ -2534,6 +2639,8 @@ function getOutputParser(config, response) {
2534
2639
  return OutputMetaLlama3Chat(response, config);
2535
2640
  // case "amazon:nova.chat.v1":
2536
2641
  // return OutputDefault(response, config);
2642
+ case "xai.chat.v1":
2643
+ return OutputXAIChat(response, config);
2537
2644
  default: {
2538
2645
  if (config?.key?.startsWith("custom:")) {
2539
2646
  return OutputDefault(response, config);
package/dist/index.mjs CHANGED
@@ -2097,7 +2097,16 @@ function anthropicPromptSanitize(_messages, _inputBodyObj, _outputObj) {
2097
2097
  ...a
2098
2098
  }))
2099
2099
  ];
2100
- if (first.role === "system") {
2100
+ if (first.role === "system" && messages.length === 0) {
2101
+ return [
2102
+ {
2103
+ role: "user",
2104
+ content: first.content
2105
+ },
2106
+ ...messages
2107
+ ];
2108
+ }
2109
+ if (first.role === "system" && messages.length > 0) {
2101
2110
  _outputObj.system = first.content;
2102
2111
  return messages;
2103
2112
  }
@@ -2238,11 +2247,59 @@ var bedrock = {
2238
2247
  "amazon:meta.chat.v1": amazonMetaChatV1
2239
2248
  };
2240
2249
 
2250
+ // src/llm/config/x/index.ts
2251
+ var xaiChatV1 = {
2252
+ key: "xai.chat.v1",
2253
+ provider: "xai.chat",
2254
+ endpoint: `https://api.x.ai/v1/chat/completions`,
2255
+ options: {
2256
+ prompt: {},
2257
+ topP: {},
2258
+ useJson: {},
2259
+ xAiApiKey: {
2260
+ default: getEnvironmentVariable("XAI_API_KEY")
2261
+ }
2262
+ },
2263
+ method: "POST",
2264
+ headers: `{"Authorization":"Bearer {{xAiApiKey}}", "Content-Type": "application/json" }`,
2265
+ mapBody: {
2266
+ prompt: {
2267
+ key: "messages",
2268
+ sanitize: /* @__PURE__ */ __name((v) => {
2269
+ if (typeof v === "string") {
2270
+ return [
2271
+ {
2272
+ role: "user",
2273
+ content: v
2274
+ }
2275
+ ];
2276
+ }
2277
+ return v;
2278
+ }, "sanitize")
2279
+ },
2280
+ model: {
2281
+ key: "model"
2282
+ },
2283
+ topP: {
2284
+ key: "top_p"
2285
+ },
2286
+ useJson: {
2287
+ key: "response_format.type",
2288
+ sanitize: /* @__PURE__ */ __name((v) => v ? "json_object" : "text", "sanitize")
2289
+ }
2290
+ }
2291
+ };
2292
+ var xai = {
2293
+ "xai.chat.v1": xaiChatV1,
2294
+ "xai.grok-2": withDefaultModel(xaiChatV1, "grok-2-latest")
2295
+ };
2296
+
2241
2297
  // src/llm/config.ts
2242
2298
  var configs = {
2243
2299
  ...openai,
2244
2300
  ...anthropic,
2245
- ...bedrock
2301
+ ...bedrock,
2302
+ ...xai
2246
2303
  };
2247
2304
  function getLlmConfig(provider) {
2248
2305
  const pick2 = configs[provider];
@@ -2473,6 +2530,54 @@ function OutputDefault(result, _config) {
2473
2530
  }
2474
2531
  __name(OutputDefault, "OutputDefault");
2475
2532
 
2533
+ // src/llm/output/xai.ts
2534
+ function formatResult3(result) {
2535
+ if (typeof result?.message?.content === "string") {
2536
+ return {
2537
+ type: "text",
2538
+ text: result.message.content
2539
+ };
2540
+ } else if (result?.message && "tool_calls" in result.message) {
2541
+ const tool_calls = result.message.tool_calls;
2542
+ for (const call of tool_calls) {
2543
+ return {
2544
+ type: "function_use",
2545
+ name: call.function.name,
2546
+ input: JSON.parse(call.function.arguments)
2547
+ };
2548
+ }
2549
+ }
2550
+ return {
2551
+ type: "text",
2552
+ text: ""
2553
+ };
2554
+ }
2555
+ __name(formatResult3, "formatResult");
2556
+ function OutputXAIChat(result, _config) {
2557
+ const id = result.id;
2558
+ const name = result?.model;
2559
+ const created = result.created;
2560
+ const [_content, ..._options] = result?.choices || [];
2561
+ const stopReason = _content?.finish_reason;
2562
+ const content = formatContent(_content, formatResult3);
2563
+ const options = formatOptions(_options, formatResult3);
2564
+ const usage = {
2565
+ output_tokens: result?.usage?.completion_tokens,
2566
+ input_tokens: result?.usage?.prompt_tokens,
2567
+ total_tokens: result?.usage?.total_tokens
2568
+ };
2569
+ return BaseLlmOutput2({
2570
+ id,
2571
+ name,
2572
+ created,
2573
+ usage,
2574
+ stopReason,
2575
+ content,
2576
+ options
2577
+ });
2578
+ }
2579
+ __name(OutputXAIChat, "OutputXAIChat");
2580
+
2476
2581
  // src/llm/output/index.ts
2477
2582
  function getOutputParser(config, response) {
2478
2583
  switch (config?.key) {
@@ -2486,6 +2591,8 @@ function getOutputParser(config, response) {
2486
2591
  return OutputMetaLlama3Chat(response, config);
2487
2592
  // case "amazon:nova.chat.v1":
2488
2593
  // return OutputDefault(response, config);
2594
+ case "xai.chat.v1":
2595
+ return OutputXAIChat(response, config);
2489
2596
  default: {
2490
2597
  if (config?.key?.startsWith("custom:")) {
2491
2598
  return OutputDefault(response, config);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "llm-exe",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Simplify building LLM-powered apps with easy-to-use base components, supporting text and chat-based prompts with handlebars template engine, output parsers, and flexible function calling capabilities.",
5
5
  "keywords": [
6
6
  "ai",
@@ -9,11 +9,13 @@
9
9
  "anthropic",
10
10
  "bedrock",
11
11
  "llama",
12
+ "gpt-3.5-turbo",
12
13
  "gpt-4",
13
14
  "claude",
15
+ "grok",
14
16
  "chain",
15
17
  "prompt",
16
- "agents"
18
+ "agent"
17
19
  ],
18
20
  "source": "./src/index.ts",
19
21
  "main": "./dist/index.js",
@@ -22,8 +24,7 @@
22
24
  "exports": {
23
25
  ".": {
24
26
  "require": "./dist/index.js",
25
- "import": "./dist/index.mjs",
26
- "types": "./dist/index.d.ts"
27
+ "import": "./dist/index.mjs"
27
28
  }
28
29
  },
29
30
  "files": [
package/readme.md CHANGED
@@ -6,7 +6,8 @@ A package that provides simplified base components to make building and maintain
6
6
 
7
7
  - Write functions powered by LLM's with easy to use building blocks.
8
8
  - Pure Javascript and Typescript. Allows you to pass and infer types.
9
- - Support for text-based (llama-3) and chat-based prompts (gpt-4o, claude-3.5).
9
+ - Support for text-based (llama-3) and chat-based prompts (gpt-4o, claude-3.5, grok-3).
10
+ - Call LLM's from different providers without changing your code. (OpenAi/Anthropic/xAI/AWS Bedrock)
10
11
  - Supercharge your prompts by using handlebars within prompt template.
11
12
  - Allow LLM's to call functions (or call other LLM executors).
12
13
  - Not very opinionated. You have control on how you use it.
@@ -14,7 +15,7 @@ A package that provides simplified base components to make building and maintain
14
15
  ![llm-exe](https://assets.llm-exe.com/llm-exe-featured.jpg)
15
16
 
16
17
 
17
- See full docs [here](https://llm-exe.com)
18
+ See full docs here: [https://llm-exe.com](https://llm-exe.com)
18
19
 
19
20
 
20
21
  ---