@langchain/openai 0.0.10 → 0.0.12

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.
@@ -6,6 +6,7 @@ const messages_1 = require("@langchain/core/messages");
6
6
  const outputs_1 = require("@langchain/core/outputs");
7
7
  const env_1 = require("@langchain/core/utils/env");
8
8
  const chat_models_1 = require("@langchain/core/language_models/chat_models");
9
+ const function_calling_1 = require("@langchain/core/utils/function_calling");
9
10
  const azure_js_1 = require("./utils/azure.cjs");
10
11
  const openai_js_1 = require("./utils/openai.cjs");
11
12
  const openai_format_fndef_js_1 = require("./utils/openai-format-fndef.cjs");
@@ -266,6 +267,18 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
266
267
  writable: true,
267
268
  value: void 0
268
269
  });
270
+ Object.defineProperty(this, "logprobs", {
271
+ enumerable: true,
272
+ configurable: true,
273
+ writable: true,
274
+ value: void 0
275
+ });
276
+ Object.defineProperty(this, "topLogprobs", {
277
+ enumerable: true,
278
+ configurable: true,
279
+ writable: true,
280
+ value: void 0
281
+ });
269
282
  Object.defineProperty(this, "openAIApiKey", {
270
283
  enumerable: true,
271
284
  configurable: true,
@@ -351,6 +364,8 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
351
364
  this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
352
365
  this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
353
366
  this.maxTokens = fields?.maxTokens;
367
+ this.logprobs = fields?.logprobs;
368
+ this.topLogprobs = fields?.topLogprobs;
354
369
  this.n = fields?.n ?? this.n;
355
370
  this.logitBias = fields?.logitBias;
356
371
  this.stop = fields?.stop;
@@ -396,6 +411,8 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
396
411
  frequency_penalty: this.frequencyPenalty,
397
412
  presence_penalty: this.presencePenalty,
398
413
  max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
414
+ logprobs: this.logprobs,
415
+ top_logprobs: this.topLogprobs,
399
416
  n: this.n,
400
417
  logit_bias: this.logitBias,
401
418
  stop: options?.stop ?? this.stop,
@@ -404,7 +421,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
404
421
  functions: options?.functions,
405
422
  function_call: options?.function_call,
406
423
  tools: isStructuredToolArray(options?.tools)
407
- ? options?.tools.map(openai_js_1.formatToOpenAITool)
424
+ ? options?.tools.map(function_calling_1.convertToOpenAITool)
408
425
  : options?.tools,
409
426
  tool_choice: options?.tool_choice,
410
427
  response_format: options?.response_format,
@@ -526,9 +543,10 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
526
543
  text,
527
544
  message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }),
528
545
  };
529
- if (part.finish_reason) {
530
- generation.generationInfo = { finish_reason: part.finish_reason };
531
- }
546
+ generation.generationInfo = {
547
+ ...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
548
+ ...(part.logprobs ? { logprobs: part.logprobs } : {}),
549
+ };
532
550
  generations.push(generation);
533
551
  }
534
552
  return {
@@ -81,6 +81,8 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
81
81
  timeout?: number;
82
82
  streaming: boolean;
83
83
  maxTokens?: number;
84
+ logprobs?: boolean;
85
+ topLogprobs?: number;
84
86
  openAIApiKey?: string;
85
87
  azureOpenAIApiVersion?: string;
86
88
  azureOpenAIApiKey?: string;
@@ -3,8 +3,9 @@ import { AIMessage, AIMessageChunk, ChatMessage, ChatMessageChunk, FunctionMessa
3
3
  import { ChatGenerationChunk, } from "@langchain/core/outputs";
4
4
  import { getEnvironmentVariable } from "@langchain/core/utils/env";
5
5
  import { BaseChatModel, } from "@langchain/core/language_models/chat_models";
6
+ import { convertToOpenAITool } from "@langchain/core/utils/function_calling";
6
7
  import { getEndpoint } from "./utils/azure.js";
7
- import { wrapOpenAIClientError, formatToOpenAITool } from "./utils/openai.js";
8
+ import { wrapOpenAIClientError } from "./utils/openai.js";
8
9
  import { formatFunctionDefinitions, } from "./utils/openai-format-fndef.js";
9
10
  function extractGenericMessageCustomRole(message) {
10
11
  if (message.role !== "system" &&
@@ -262,6 +263,18 @@ export class ChatOpenAI extends BaseChatModel {
262
263
  writable: true,
263
264
  value: void 0
264
265
  });
266
+ Object.defineProperty(this, "logprobs", {
267
+ enumerable: true,
268
+ configurable: true,
269
+ writable: true,
270
+ value: void 0
271
+ });
272
+ Object.defineProperty(this, "topLogprobs", {
273
+ enumerable: true,
274
+ configurable: true,
275
+ writable: true,
276
+ value: void 0
277
+ });
265
278
  Object.defineProperty(this, "openAIApiKey", {
266
279
  enumerable: true,
267
280
  configurable: true,
@@ -347,6 +360,8 @@ export class ChatOpenAI extends BaseChatModel {
347
360
  this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
348
361
  this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
349
362
  this.maxTokens = fields?.maxTokens;
363
+ this.logprobs = fields?.logprobs;
364
+ this.topLogprobs = fields?.topLogprobs;
350
365
  this.n = fields?.n ?? this.n;
351
366
  this.logitBias = fields?.logitBias;
352
367
  this.stop = fields?.stop;
@@ -392,6 +407,8 @@ export class ChatOpenAI extends BaseChatModel {
392
407
  frequency_penalty: this.frequencyPenalty,
393
408
  presence_penalty: this.presencePenalty,
394
409
  max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
410
+ logprobs: this.logprobs,
411
+ top_logprobs: this.topLogprobs,
395
412
  n: this.n,
396
413
  logit_bias: this.logitBias,
397
414
  stop: options?.stop ?? this.stop,
@@ -400,7 +417,7 @@ export class ChatOpenAI extends BaseChatModel {
400
417
  functions: options?.functions,
401
418
  function_call: options?.function_call,
402
419
  tools: isStructuredToolArray(options?.tools)
403
- ? options?.tools.map(formatToOpenAITool)
420
+ ? options?.tools.map(convertToOpenAITool)
404
421
  : options?.tools,
405
422
  tool_choice: options?.tool_choice,
406
423
  response_format: options?.response_format,
@@ -522,9 +539,10 @@ export class ChatOpenAI extends BaseChatModel {
522
539
  text,
523
540
  message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }),
524
541
  };
525
- if (part.finish_reason) {
526
- generation.generationInfo = { finish_reason: part.finish_reason };
527
- }
542
+ generation.generationInfo = {
543
+ ...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
544
+ ...(part.logprobs ? { logprobs: part.logprobs } : {}),
545
+ };
528
546
  generations.push(generation);
529
547
  }
530
548
  return {
package/dist/types.d.ts CHANGED
@@ -83,6 +83,16 @@ export interface LegacyOpenAIInput {
83
83
  };
84
84
  }
85
85
  export interface OpenAIChatInput extends OpenAIBaseInput {
86
+ /**
87
+ * Whether to return log probabilities of the output tokens or not.
88
+ * If true, returns the log probabilities of each output token returned in the content of message.
89
+ */
90
+ logprobs?: boolean;
91
+ /**
92
+ * An integer between 0 and 5 specifying the number of most likely tokens to return at each token position,
93
+ * each with an associated log probability. logprobs must be set to true if this parameter is used.
94
+ */
95
+ topLogprobs?: number;
86
96
  /** ChatGPT messages to pass as a prefix to the prompt */
87
97
  prefixMessages?: OpenAIClient.Chat.CreateChatCompletionRequestMessage[];
88
98
  }
@@ -3,7 +3,7 @@ export interface OpenAIEndpointConfig {
3
3
  azureOpenAIApiInstanceName?: string;
4
4
  azureOpenAIApiKey?: string;
5
5
  azureOpenAIBasePath?: string;
6
- baseURL?: string;
6
+ baseURL?: string | null;
7
7
  }
8
8
  /**
9
9
  * This function generates an endpoint URL for (Azure) OpenAI
@@ -27,4 +27,4 @@ export interface OpenAIEndpointConfig {
27
27
  *
28
28
  * @returns {string | undefined} The generated (Azure) OpenAI endpoint URL.
29
29
  */
30
- export declare function getEndpoint(config: OpenAIEndpointConfig): string | undefined;
30
+ export declare function getEndpoint(config: OpenAIEndpointConfig): string | null | undefined;
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.formatToOpenAIAssistantTool = exports.formatToOpenAITool = exports.formatToOpenAIFunction = exports.wrapOpenAIClientError = void 0;
4
4
  const openai_1 = require("openai");
5
5
  const zod_to_json_schema_1 = require("zod-to-json-schema");
6
+ const function_calling_1 = require("@langchain/core/utils/function_calling");
7
+ Object.defineProperty(exports, "formatToOpenAIFunction", { enumerable: true, get: function () { return function_calling_1.convertToOpenAIFunction; } });
8
+ Object.defineProperty(exports, "formatToOpenAITool", { enumerable: true, get: function () { return function_calling_1.convertToOpenAITool; } });
6
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
10
  function wrapOpenAIClientError(e) {
8
11
  let error;
@@ -20,32 +23,6 @@ function wrapOpenAIClientError(e) {
20
23
  return error;
21
24
  }
22
25
  exports.wrapOpenAIClientError = wrapOpenAIClientError;
23
- /**
24
- * Formats a `StructuredTool` instance into a format that is compatible
25
- * with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
26
- * function to convert the schema of the `StructuredTool` into a JSON
27
- * schema, which is then used as the parameters for the OpenAI function.
28
- */
29
- function formatToOpenAIFunction(tool) {
30
- return {
31
- name: tool.name,
32
- description: tool.description,
33
- parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema),
34
- };
35
- }
36
- exports.formatToOpenAIFunction = formatToOpenAIFunction;
37
- function formatToOpenAITool(tool) {
38
- const schema = (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema);
39
- return {
40
- type: "function",
41
- function: {
42
- name: tool.name,
43
- description: tool.description,
44
- parameters: schema,
45
- },
46
- };
47
- }
48
- exports.formatToOpenAITool = formatToOpenAITool;
49
26
  function formatToOpenAIAssistantTool(tool) {
50
27
  return {
51
28
  type: "function",
@@ -1,12 +1,6 @@
1
1
  import { type OpenAI as OpenAIClient } from "openai";
2
2
  import type { StructuredToolInterface } from "@langchain/core/tools";
3
+ import { convertToOpenAIFunction, convertToOpenAITool } from "@langchain/core/utils/function_calling";
3
4
  export declare function wrapOpenAIClientError(e: any): any;
4
- /**
5
- * Formats a `StructuredTool` instance into a format that is compatible
6
- * with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
7
- * function to convert the schema of the `StructuredTool` into a JSON
8
- * schema, which is then used as the parameters for the OpenAI function.
9
- */
10
- export declare function formatToOpenAIFunction(tool: StructuredToolInterface): OpenAIClient.Chat.ChatCompletionCreateParams.Function;
11
- export declare function formatToOpenAITool(tool: StructuredToolInterface): OpenAIClient.Chat.ChatCompletionTool;
5
+ export { convertToOpenAIFunction as formatToOpenAIFunction, convertToOpenAITool as formatToOpenAITool, };
12
6
  export declare function formatToOpenAIAssistantTool(tool: StructuredToolInterface): OpenAIClient.Beta.AssistantCreateParams.AssistantToolsFunction;
@@ -1,5 +1,6 @@
1
1
  import { APIConnectionTimeoutError, APIUserAbortError, } from "openai";
2
2
  import { zodToJsonSchema } from "zod-to-json-schema";
3
+ import { convertToOpenAIFunction, convertToOpenAITool, } from "@langchain/core/utils/function_calling";
3
4
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
5
  export function wrapOpenAIClientError(e) {
5
6
  let error;
@@ -16,30 +17,7 @@ export function wrapOpenAIClientError(e) {
16
17
  }
17
18
  return error;
18
19
  }
19
- /**
20
- * Formats a `StructuredTool` instance into a format that is compatible
21
- * with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
22
- * function to convert the schema of the `StructuredTool` into a JSON
23
- * schema, which is then used as the parameters for the OpenAI function.
24
- */
25
- export function formatToOpenAIFunction(tool) {
26
- return {
27
- name: tool.name,
28
- description: tool.description,
29
- parameters: zodToJsonSchema(tool.schema),
30
- };
31
- }
32
- export function formatToOpenAITool(tool) {
33
- const schema = zodToJsonSchema(tool.schema);
34
- return {
35
- type: "function",
36
- function: {
37
- name: tool.name,
38
- description: tool.description,
39
- parameters: schema,
40
- },
41
- };
42
- }
20
+ export { convertToOpenAIFunction as formatToOpenAIFunction, convertToOpenAITool as formatToOpenAITool, };
43
21
  export function formatToOpenAIAssistantTool(tool) {
44
22
  return {
45
23
  type: "function",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -35,9 +35,9 @@
35
35
  "author": "LangChain",
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
- "@langchain/core": "~0.1.9",
38
+ "@langchain/core": "~0.1.13",
39
39
  "js-tiktoken": "^1.0.7",
40
- "openai": "^4.19.0",
40
+ "openai": "^4.24.2",
41
41
  "zod": "^3.22.3",
42
42
  "zod-to-json-schema": "3.20.3"
43
43
  },