@langchain/xai 0.0.1 → 0.0.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.
@@ -16,9 +16,9 @@ const openai_1 = require("@langchain/openai");
16
16
  * export XAI_API_KEY="your-api-key"
17
17
  * ```
18
18
  *
19
- * ## [Constructor args](https://api.js.langchain.com/classes/langchain_xai.ChatXAI.html#constructor)
19
+ * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_xai.ChatXAI.html#constructor)
20
20
  *
21
- * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_xai.ChatXAICallOptions.html)
21
+ * ## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_xai.ChatXAICallOptions.html)
22
22
  *
23
23
  * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
24
24
  * They can also be passed via `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
@@ -334,7 +334,7 @@ class ChatXAI extends openai_1.ChatOpenAI {
334
334
  return "ChatXAI";
335
335
  }
336
336
  _llmType() {
337
- return "xAI";
337
+ return "xai";
338
338
  }
339
339
  get lc_secrets() {
340
340
  return {
@@ -411,5 +411,33 @@ class ChatXAI extends openai_1.ChatOpenAI {
411
411
  }
412
412
  return super.completionWithRetry(newRequest, options);
413
413
  }
414
+ _convertOpenAIDeltaToBaseMessageChunk(
415
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
416
+ delta, rawResponse, defaultRole) {
417
+ const messageChunk = super._convertOpenAIDeltaToBaseMessageChunk(delta, rawResponse, defaultRole);
418
+ // Make concatenating chunks work without merge warning
419
+ if (!rawResponse.choices[0]?.finish_reason) {
420
+ delete messageChunk.response_metadata.usage;
421
+ delete messageChunk.usage_metadata;
422
+ }
423
+ else {
424
+ messageChunk.usage_metadata = messageChunk.response_metadata.usage;
425
+ }
426
+ return messageChunk;
427
+ }
428
+ _convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse) {
429
+ const langChainMessage = super._convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse);
430
+ langChainMessage.additional_kwargs.reasoning_content =
431
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
432
+ message.reasoning_content;
433
+ return langChainMessage;
434
+ }
435
+ withStructuredOutput(outputSchema, config) {
436
+ const ensuredConfig = { ...config };
437
+ if (ensuredConfig?.method === undefined) {
438
+ ensuredConfig.method = "functionCalling";
439
+ }
440
+ return super.withStructuredOutput(outputSchema, ensuredConfig);
441
+ }
414
442
  }
415
443
  exports.ChatXAI = ChatXAI;
@@ -1,6 +1,10 @@
1
+ import { BaseLanguageModelInput } from "@langchain/core/language_models/base";
1
2
  import { BaseChatModelCallOptions, BindToolsInput, LangSmithParams, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
2
3
  import { Serialized } from "@langchain/core/load/serializable";
3
- import { type OpenAICoreRequestOptions, type OpenAIClient, ChatOpenAI, OpenAIToolChoice } from "@langchain/openai";
4
+ import { AIMessageChunk, BaseMessage } from "@langchain/core/messages";
5
+ import { Runnable } from "@langchain/core/runnables";
6
+ import { type OpenAICoreRequestOptions, type OpenAIClient, ChatOpenAI, OpenAIToolChoice, ChatOpenAIStructuredOutputMethodOptions } from "@langchain/openai";
7
+ import { z } from "zod";
4
8
  type ChatXAIToolType = BindToolsInput | OpenAIClient.ChatCompletionTool;
5
9
  export interface ChatXAICallOptions extends BaseChatModelCallOptions {
6
10
  headers?: Record<string, string>;
@@ -57,9 +61,9 @@ export interface ChatXAIInput extends BaseChatModelParams {
57
61
  * export XAI_API_KEY="your-api-key"
58
62
  * ```
59
63
  *
60
- * ## [Constructor args](https://api.js.langchain.com/classes/langchain_xai.ChatXAI.html#constructor)
64
+ * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_xai.ChatXAI.html#constructor)
61
65
  *
62
- * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_xai.ChatXAICallOptions.html)
66
+ * ## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_xai.ChatXAICallOptions.html)
63
67
  *
64
68
  * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
65
69
  * They can also be passed via `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
@@ -383,5 +387,16 @@ export declare class ChatXAI extends ChatOpenAI<ChatXAICallOptions> {
383
387
  getLsParams(options: this["ParsedCallOptions"]): LangSmithParams;
384
388
  completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming, options?: OpenAICoreRequestOptions): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;
385
389
  completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming, options?: OpenAICoreRequestOptions): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;
390
+ protected _convertOpenAIDeltaToBaseMessageChunk(delta: Record<string, any>, rawResponse: OpenAIClient.ChatCompletionChunk, defaultRole?: "function" | "user" | "system" | "developer" | "assistant" | "tool"): AIMessageChunk;
391
+ protected _convertOpenAIChatCompletionMessageToBaseMessage(message: OpenAIClient.ChatCompletionMessage, rawResponse: OpenAIClient.ChatCompletion): BaseMessage;
392
+ withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: z.ZodType<RunOutput> | Record<string, any>, config?: ChatOpenAIStructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
393
+ withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: z.ZodType<RunOutput> | Record<string, any>, config?: ChatOpenAIStructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
394
+ raw: BaseMessage;
395
+ parsed: RunOutput;
396
+ }>;
397
+ withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: z.ZodType<RunOutput> | Record<string, any>, config?: ChatOpenAIStructuredOutputMethodOptions<boolean>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {
398
+ raw: BaseMessage;
399
+ parsed: RunOutput;
400
+ }>;
386
401
  }
387
402
  export {};
@@ -13,9 +13,9 @@ import { ChatOpenAI, } from "@langchain/openai";
13
13
  * export XAI_API_KEY="your-api-key"
14
14
  * ```
15
15
  *
16
- * ## [Constructor args](https://api.js.langchain.com/classes/langchain_xai.ChatXAI.html#constructor)
16
+ * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_xai.ChatXAI.html#constructor)
17
17
  *
18
- * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_xai.ChatXAICallOptions.html)
18
+ * ## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_xai.ChatXAICallOptions.html)
19
19
  *
20
20
  * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
21
21
  * They can also be passed via `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
@@ -331,7 +331,7 @@ export class ChatXAI extends ChatOpenAI {
331
331
  return "ChatXAI";
332
332
  }
333
333
  _llmType() {
334
- return "xAI";
334
+ return "xai";
335
335
  }
336
336
  get lc_secrets() {
337
337
  return {
@@ -408,4 +408,32 @@ export class ChatXAI extends ChatOpenAI {
408
408
  }
409
409
  return super.completionWithRetry(newRequest, options);
410
410
  }
411
+ _convertOpenAIDeltaToBaseMessageChunk(
412
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
413
+ delta, rawResponse, defaultRole) {
414
+ const messageChunk = super._convertOpenAIDeltaToBaseMessageChunk(delta, rawResponse, defaultRole);
415
+ // Make concatenating chunks work without merge warning
416
+ if (!rawResponse.choices[0]?.finish_reason) {
417
+ delete messageChunk.response_metadata.usage;
418
+ delete messageChunk.usage_metadata;
419
+ }
420
+ else {
421
+ messageChunk.usage_metadata = messageChunk.response_metadata.usage;
422
+ }
423
+ return messageChunk;
424
+ }
425
+ _convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse) {
426
+ const langChainMessage = super._convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse);
427
+ langChainMessage.additional_kwargs.reasoning_content =
428
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
429
+ message.reasoning_content;
430
+ return langChainMessage;
431
+ }
432
+ withStructuredOutput(outputSchema, config) {
433
+ const ensuredConfig = { ...config };
434
+ if (ensuredConfig?.method === undefined) {
435
+ ensuredConfig.method = "functionCalling";
436
+ }
437
+ return super.withStructuredOutput(outputSchema, ensuredConfig);
438
+ }
411
439
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/xai",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "xAI integration for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -35,7 +35,8 @@
35
35
  "author": "LangChain",
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
- "@langchain/openai": "~0.3.0"
38
+ "@langchain/openai": "~0.4.4",
39
+ "zod": "^3.24.2"
39
40
  },
40
41
  "peerDependencies": {
41
42
  "@langchain/core": ">=0.2.21 <0.4.0"
@@ -67,7 +68,7 @@
67
68
  "rollup": "^4.5.2",
68
69
  "ts-jest": "^29.1.0",
69
70
  "typescript": "<5.2.0",
70
- "zod": "^3.22.4"
71
+ "zod-to-json-schema": "^3.23.1"
71
72
  },
72
73
  "publishConfig": {
73
74
  "access": "public"