langchain 0.0.197-rc.0 → 0.0.197-rc.1

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 (202) hide show
  1. package/dist/agents/executor.cjs +351 -1
  2. package/dist/agents/executor.d.ts +61 -3
  3. package/dist/agents/executor.js +350 -1
  4. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  5. package/dist/base_language/count_tokens.cjs +1 -1
  6. package/dist/base_language/count_tokens.d.ts +1 -1
  7. package/dist/base_language/count_tokens.js +1 -1
  8. package/dist/base_language/index.cjs +1 -1
  9. package/dist/base_language/index.d.ts +1 -1
  10. package/dist/base_language/index.js +1 -1
  11. package/dist/cache/base.cjs +1 -1
  12. package/dist/cache/base.d.ts +1 -1
  13. package/dist/cache/base.js +1 -1
  14. package/dist/cache/index.cjs +1 -1
  15. package/dist/cache/index.d.ts +1 -1
  16. package/dist/cache/index.js +1 -1
  17. package/dist/callbacks/base.cjs +1 -1
  18. package/dist/callbacks/base.d.ts +1 -1
  19. package/dist/callbacks/base.js +1 -1
  20. package/dist/callbacks/handlers/console.cjs +1 -1
  21. package/dist/callbacks/handlers/console.d.ts +1 -1
  22. package/dist/callbacks/handlers/console.js +1 -1
  23. package/dist/callbacks/handlers/initialize.cjs +1 -1
  24. package/dist/callbacks/handlers/initialize.d.ts +1 -1
  25. package/dist/callbacks/handlers/initialize.js +1 -1
  26. package/dist/callbacks/handlers/log_stream.cjs +1 -1
  27. package/dist/callbacks/handlers/log_stream.d.ts +1 -1
  28. package/dist/callbacks/handlers/log_stream.js +1 -1
  29. package/dist/callbacks/handlers/run_collector.cjs +1 -1
  30. package/dist/callbacks/handlers/run_collector.d.ts +1 -1
  31. package/dist/callbacks/handlers/run_collector.js +1 -1
  32. package/dist/callbacks/handlers/tracer.cjs +1 -1
  33. package/dist/callbacks/handlers/tracer.d.ts +1 -1
  34. package/dist/callbacks/handlers/tracer.js +1 -1
  35. package/dist/callbacks/handlers/tracer_langchain.cjs +1 -1
  36. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -1
  37. package/dist/callbacks/handlers/tracer_langchain.js +1 -1
  38. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +1 -1
  39. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -1
  40. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -1
  41. package/dist/callbacks/manager.cjs +1 -1
  42. package/dist/callbacks/manager.d.ts +1 -1
  43. package/dist/callbacks/manager.js +1 -1
  44. package/dist/callbacks/promises.cjs +1 -1
  45. package/dist/callbacks/promises.d.ts +1 -1
  46. package/dist/callbacks/promises.js +1 -1
  47. package/dist/chains/base.cjs +16 -0
  48. package/dist/chains/base.d.ts +2 -0
  49. package/dist/chains/base.js +16 -0
  50. package/dist/chains/openai_functions/structured_output.cjs +1 -1
  51. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  52. package/dist/chains/openai_functions/structured_output.js +1 -1
  53. package/dist/chains/openai_moderation.cjs +2 -2
  54. package/dist/chains/openai_moderation.d.ts +1 -1
  55. package/dist/chains/openai_moderation.js +1 -1
  56. package/dist/chat_models/base.cjs +1 -1
  57. package/dist/chat_models/base.d.ts +1 -1
  58. package/dist/chat_models/base.js +1 -1
  59. package/dist/chat_models/fireworks.d.ts +2 -2
  60. package/dist/chat_models/iflytek_xinghuo/common.d.ts +1 -1
  61. package/dist/chat_models/minimax.d.ts +2 -2
  62. package/dist/chat_models/openai.cjs +4 -698
  63. package/dist/chat_models/openai.d.ts +4 -137
  64. package/dist/chat_models/openai.js +2 -695
  65. package/dist/document.cjs +1 -1
  66. package/dist/document.d.ts +1 -1
  67. package/dist/document.js +1 -1
  68. package/dist/document_loaders/fs/openai_whisper_audio.cjs +2 -2
  69. package/dist/document_loaders/fs/openai_whisper_audio.d.ts +1 -1
  70. package/dist/document_loaders/fs/openai_whisper_audio.js +1 -1
  71. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  72. package/dist/document_loaders/web/s3.d.ts +1 -1
  73. package/dist/embeddings/base.cjs +1 -1
  74. package/dist/embeddings/base.d.ts +1 -1
  75. package/dist/embeddings/base.js +1 -1
  76. package/dist/embeddings/cache_backed.cjs +1 -1
  77. package/dist/embeddings/cache_backed.js +1 -1
  78. package/dist/embeddings/openai.cjs +2 -240
  79. package/dist/embeddings/openai.d.ts +1 -82
  80. package/dist/embeddings/openai.js +1 -239
  81. package/dist/experimental/openai_assistant/index.cjs +3 -3
  82. package/dist/experimental/openai_assistant/index.d.ts +1 -1
  83. package/dist/experimental/openai_assistant/index.js +1 -1
  84. package/dist/experimental/openai_assistant/schema.d.ts +1 -1
  85. package/dist/experimental/openai_files/index.cjs +2 -2
  86. package/dist/experimental/openai_files/index.d.ts +1 -1
  87. package/dist/experimental/openai_files/index.js +1 -1
  88. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  89. package/dist/llms/base.cjs +1 -1
  90. package/dist/llms/base.d.ts +1 -1
  91. package/dist/llms/base.js +1 -1
  92. package/dist/llms/fireworks.d.ts +2 -2
  93. package/dist/llms/openai-chat.cjs +3 -445
  94. package/dist/llms/openai-chat.d.ts +4 -123
  95. package/dist/llms/openai-chat.js +2 -443
  96. package/dist/llms/openai.cjs +6 -530
  97. package/dist/llms/openai.d.ts +4 -123
  98. package/dist/llms/openai.js +2 -525
  99. package/dist/load/index.cjs +4 -2
  100. package/dist/load/index.d.ts +2 -2
  101. package/dist/load/index.js +4 -2
  102. package/dist/load/serializable.cjs +1 -1
  103. package/dist/load/serializable.d.ts +1 -1
  104. package/dist/load/serializable.js +1 -1
  105. package/dist/memory/base.cjs +2 -2
  106. package/dist/memory/base.d.ts +2 -2
  107. package/dist/memory/base.js +2 -2
  108. package/dist/output_parsers/list.cjs +1 -1
  109. package/dist/output_parsers/list.d.ts +1 -1
  110. package/dist/output_parsers/list.js +1 -1
  111. package/dist/output_parsers/openai_functions.cjs +1 -1
  112. package/dist/output_parsers/openai_functions.d.ts +1 -1
  113. package/dist/output_parsers/openai_functions.js +1 -1
  114. package/dist/output_parsers/structured.d.ts +1 -1
  115. package/dist/prompts/base.cjs +3 -3
  116. package/dist/prompts/base.d.ts +3 -3
  117. package/dist/prompts/base.js +3 -3
  118. package/dist/prompts/chat.cjs +2 -2
  119. package/dist/prompts/chat.d.ts +2 -2
  120. package/dist/prompts/chat.js +2 -2
  121. package/dist/prompts/few_shot.cjs +1 -1
  122. package/dist/prompts/few_shot.d.ts +1 -1
  123. package/dist/prompts/few_shot.js +1 -1
  124. package/dist/prompts/index.cjs +1 -1
  125. package/dist/prompts/index.d.ts +1 -1
  126. package/dist/prompts/index.js +1 -1
  127. package/dist/prompts/pipeline.cjs +1 -1
  128. package/dist/prompts/pipeline.d.ts +1 -1
  129. package/dist/prompts/pipeline.js +1 -1
  130. package/dist/prompts/prompt.cjs +1 -1
  131. package/dist/prompts/prompt.d.ts +1 -1
  132. package/dist/prompts/prompt.js +1 -1
  133. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +1 -1
  134. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -1
  135. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -1
  136. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +1 -1
  137. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -1
  138. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -1
  139. package/dist/prompts/selectors/conditional.cjs +1 -1
  140. package/dist/prompts/selectors/conditional.d.ts +1 -1
  141. package/dist/prompts/selectors/conditional.js +1 -1
  142. package/dist/prompts/serde.d.ts +1 -1
  143. package/dist/prompts/template.cjs +1 -1
  144. package/dist/prompts/template.d.ts +1 -1
  145. package/dist/prompts/template.js +1 -1
  146. package/dist/runnables/index.cjs +1 -1
  147. package/dist/runnables/index.d.ts +1 -1
  148. package/dist/runnables/index.js +1 -1
  149. package/dist/schema/document.cjs +1 -1
  150. package/dist/schema/document.d.ts +1 -1
  151. package/dist/schema/document.js +1 -1
  152. package/dist/schema/index.cjs +7 -7
  153. package/dist/schema/index.d.ts +11 -11
  154. package/dist/schema/index.js +7 -7
  155. package/dist/schema/output_parser.cjs +1 -1
  156. package/dist/schema/output_parser.d.ts +1 -1
  157. package/dist/schema/output_parser.js +1 -1
  158. package/dist/schema/retriever.cjs +1 -1
  159. package/dist/schema/retriever.d.ts +1 -1
  160. package/dist/schema/retriever.js +1 -1
  161. package/dist/schema/runnable/base.cjs +1 -1
  162. package/dist/schema/runnable/base.d.ts +1 -1
  163. package/dist/schema/runnable/base.js +1 -1
  164. package/dist/schema/runnable/branch.cjs +1 -1
  165. package/dist/schema/runnable/branch.d.ts +1 -1
  166. package/dist/schema/runnable/branch.js +1 -1
  167. package/dist/schema/runnable/config.d.ts +1 -1
  168. package/dist/schema/runnable/index.cjs +1 -1
  169. package/dist/schema/runnable/index.d.ts +1 -1
  170. package/dist/schema/runnable/index.js +1 -1
  171. package/dist/schema/runnable/passthrough.cjs +1 -1
  172. package/dist/schema/runnable/passthrough.d.ts +1 -1
  173. package/dist/schema/runnable/passthrough.js +1 -1
  174. package/dist/schema/runnable/router.cjs +1 -1
  175. package/dist/schema/runnable/router.d.ts +1 -1
  176. package/dist/schema/runnable/router.js +1 -1
  177. package/dist/schema/storage.cjs +1 -1
  178. package/dist/schema/storage.d.ts +1 -1
  179. package/dist/schema/storage.js +1 -1
  180. package/dist/tools/base.cjs +4 -108
  181. package/dist/tools/base.d.ts +1 -72
  182. package/dist/tools/base.js +1 -105
  183. package/dist/tools/bingserpapi.d.ts +1 -1
  184. package/dist/tools/convert_to_openai.cjs +4 -38
  185. package/dist/tools/convert_to_openai.d.ts +1 -11
  186. package/dist/tools/convert_to_openai.js +1 -35
  187. package/dist/tools/searchapi.d.ts +1 -1
  188. package/dist/tools/serpapi.d.ts +1 -1
  189. package/dist/tools/serper.d.ts +1 -1
  190. package/dist/types/openai-types.d.ts +1 -133
  191. package/dist/util/async_caller.cjs +1 -1
  192. package/dist/util/async_caller.d.ts +1 -1
  193. package/dist/util/async_caller.js +1 -1
  194. package/dist/util/openai.cjs +2 -18
  195. package/dist/util/openai.d.ts +1 -1
  196. package/dist/util/openai.js +1 -17
  197. package/dist/util/openapi.d.ts +2 -2
  198. package/dist/util/prompt-layer.d.ts +1 -1
  199. package/package.json +6 -5
  200. package/dist/util/openai-format-fndef.cjs +0 -81
  201. package/dist/util/openai-format-fndef.d.ts +0 -44
  202. package/dist/util/openai-format-fndef.js +0 -77
@@ -1,127 +1,8 @@
1
- import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
1
+ import { OpenAIChat } from "@langchain/openai";
2
2
  import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
3
- import { GenerationChunk, LLMResult } from "../schema/index.js";
4
- import { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput, OpenAICoreRequestOptions, LegacyOpenAIInput } from "../types/openai-types.js";
5
- import { BaseLLMParams, LLM } from "./base.js";
6
- export { type AzureOpenAIInput, type OpenAIChatInput };
7
- /**
8
- * Interface that extends the OpenAICallOptions interface and includes an
9
- * optional promptIndex property. It represents the options that can be
10
- * passed when making a call to the OpenAI Chat API.
11
- */
12
- export interface OpenAIChatCallOptions extends OpenAICallOptions {
13
- promptIndex?: number;
14
- }
15
- /**
16
- * Wrapper around OpenAI large language models that use the Chat endpoint.
17
- *
18
- * To use you should have the `openai` package installed, with the
19
- * `OPENAI_API_KEY` environment variable set.
20
- *
21
- * To use with Azure you should have the `openai` package installed, with the
22
- * `AZURE_OPENAI_API_KEY`,
23
- * `AZURE_OPENAI_API_INSTANCE_NAME`,
24
- * `AZURE_OPENAI_API_DEPLOYMENT_NAME`
25
- * and `AZURE_OPENAI_API_VERSION` environment variable set.
26
- *
27
- * @remarks
28
- * Any parameters that are valid to be passed to {@link
29
- * https://platform.openai.com/docs/api-reference/chat/create |
30
- * `openai.createCompletion`} can be passed through {@link modelKwargs}, even
31
- * if not explicitly available on this class.
32
- *
33
- * @augments BaseLLM
34
- * @augments OpenAIInput
35
- * @augments AzureOpenAIChatInput
36
- * @example
37
- * ```typescript
38
- * const model = new OpenAIChat({
39
- * prefixMessages: [
40
- * {
41
- * role: "system",
42
- * content: "You are a helpful assistant that answers in pirate language",
43
- * },
44
- * ],
45
- * maxTokens: 50,
46
- * });
47
- *
48
- * const res = await model.call(
49
- * "What would be a good company name for a company that makes colorful socks?"
50
- * );
51
- * console.log({ res });
52
- * ```
53
- */
54
- export declare class OpenAIChat extends LLM<OpenAIChatCallOptions> implements OpenAIChatInput, AzureOpenAIInput {
55
- static lc_name(): string;
56
- get callKeys(): string[];
57
- lc_serializable: boolean;
58
- get lc_secrets(): {
59
- [key: string]: string;
60
- } | undefined;
61
- get lc_aliases(): Record<string, string>;
62
- temperature: number;
63
- topP: number;
64
- frequencyPenalty: number;
65
- presencePenalty: number;
66
- n: number;
67
- logitBias?: Record<string, number>;
68
- maxTokens?: number;
69
- modelName: string;
70
- prefixMessages?: OpenAIClient.Chat.ChatCompletionMessageParam[];
71
- modelKwargs?: OpenAIChatInput["modelKwargs"];
72
- timeout?: number;
73
- stop?: string[];
74
- user?: string;
75
- streaming: boolean;
76
- openAIApiKey?: string;
77
- azureOpenAIApiVersion?: string;
78
- azureOpenAIApiKey?: string;
79
- azureOpenAIApiInstanceName?: string;
80
- azureOpenAIApiDeploymentName?: string;
81
- azureOpenAIBasePath?: string;
82
- organization?: string;
83
- private client;
84
- private clientConfig;
85
- constructor(fields?: Partial<OpenAIChatInput> & Partial<AzureOpenAIInput> & BaseLLMParams & {
86
- configuration?: ClientOptions & LegacyOpenAIInput;
87
- },
88
- /** @deprecated */
89
- configuration?: ClientOptions & LegacyOpenAIInput);
90
- /**
91
- * Get the parameters used to invoke the model
92
- */
93
- invocationParams(options?: this["ParsedCallOptions"]): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages">;
94
- /** @ignore */
95
- _identifyingParams(): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages"> & {
96
- model_name: string;
97
- } & ClientOptions;
98
- /**
99
- * Get the identifying parameters for the model
100
- */
101
- identifyingParams(): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages"> & {
102
- model_name: string;
103
- } & ClientOptions;
104
- /**
105
- * Formats the messages for the OpenAI API.
106
- * @param prompt The prompt to be formatted.
107
- * @returns Array of formatted messages.
108
- */
109
- private formatMessages;
110
- _streamResponseChunks(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<GenerationChunk>;
111
- /** @ignore */
112
- _call(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
113
- /**
114
- * Calls the OpenAI API with retry logic in case of failures.
115
- * @param request The request to send to the OpenAI API.
116
- * @param options Optional configuration for the API call.
117
- * @returns The response from the OpenAI API.
118
- */
119
- completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming, options?: OpenAICoreRequestOptions): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;
120
- completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming, options?: OpenAICoreRequestOptions): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;
121
- /** @ignore */
122
- private _getClientOptions;
123
- _llmType(): string;
124
- }
3
+ import type { LLMResult } from "../schema/index.js";
4
+ export { type AzureOpenAIInput, type OpenAICallOptions, type OpenAIInput, type OpenAIChatCallOptions, } from "@langchain/openai";
5
+ export { OpenAIChat };
125
6
  /**
126
7
  * PromptLayer wrapper to OpenAIChat
127
8
  */
@@ -1,448 +1,7 @@
1
- import { OpenAI as OpenAIClient } from "openai";
2
- import { GenerationChunk } from "../schema/index.js";
3
- import { getEndpoint } from "../util/azure.js";
1
+ import { OpenAIChat } from "@langchain/openai";
4
2
  import { getEnvironmentVariable } from "../util/env.js";
5
3
  import { promptLayerTrackRequest } from "../util/prompt-layer.js";
6
- import { LLM } from "./base.js";
7
- import { wrapOpenAIClientError } from "../util/openai.js";
8
- /**
9
- * Wrapper around OpenAI large language models that use the Chat endpoint.
10
- *
11
- * To use you should have the `openai` package installed, with the
12
- * `OPENAI_API_KEY` environment variable set.
13
- *
14
- * To use with Azure you should have the `openai` package installed, with the
15
- * `AZURE_OPENAI_API_KEY`,
16
- * `AZURE_OPENAI_API_INSTANCE_NAME`,
17
- * `AZURE_OPENAI_API_DEPLOYMENT_NAME`
18
- * and `AZURE_OPENAI_API_VERSION` environment variable set.
19
- *
20
- * @remarks
21
- * Any parameters that are valid to be passed to {@link
22
- * https://platform.openai.com/docs/api-reference/chat/create |
23
- * `openai.createCompletion`} can be passed through {@link modelKwargs}, even
24
- * if not explicitly available on this class.
25
- *
26
- * @augments BaseLLM
27
- * @augments OpenAIInput
28
- * @augments AzureOpenAIChatInput
29
- * @example
30
- * ```typescript
31
- * const model = new OpenAIChat({
32
- * prefixMessages: [
33
- * {
34
- * role: "system",
35
- * content: "You are a helpful assistant that answers in pirate language",
36
- * },
37
- * ],
38
- * maxTokens: 50,
39
- * });
40
- *
41
- * const res = await model.call(
42
- * "What would be a good company name for a company that makes colorful socks?"
43
- * );
44
- * console.log({ res });
45
- * ```
46
- */
47
- export class OpenAIChat extends LLM {
48
- static lc_name() {
49
- return "OpenAIChat";
50
- }
51
- get callKeys() {
52
- return [...super.callKeys, "options", "promptIndex"];
53
- }
54
- get lc_secrets() {
55
- return {
56
- openAIApiKey: "OPENAI_API_KEY",
57
- azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
58
- organization: "OPENAI_ORGANIZATION",
59
- };
60
- }
61
- get lc_aliases() {
62
- return {
63
- modelName: "model",
64
- openAIApiKey: "openai_api_key",
65
- azureOpenAIApiVersion: "azure_openai_api_version",
66
- azureOpenAIApiKey: "azure_openai_api_key",
67
- azureOpenAIApiInstanceName: "azure_openai_api_instance_name",
68
- azureOpenAIApiDeploymentName: "azure_openai_api_deployment_name",
69
- };
70
- }
71
- constructor(fields,
72
- /** @deprecated */
73
- configuration) {
74
- super(fields ?? {});
75
- Object.defineProperty(this, "lc_serializable", {
76
- enumerable: true,
77
- configurable: true,
78
- writable: true,
79
- value: true
80
- });
81
- Object.defineProperty(this, "temperature", {
82
- enumerable: true,
83
- configurable: true,
84
- writable: true,
85
- value: 1
86
- });
87
- Object.defineProperty(this, "topP", {
88
- enumerable: true,
89
- configurable: true,
90
- writable: true,
91
- value: 1
92
- });
93
- Object.defineProperty(this, "frequencyPenalty", {
94
- enumerable: true,
95
- configurable: true,
96
- writable: true,
97
- value: 0
98
- });
99
- Object.defineProperty(this, "presencePenalty", {
100
- enumerable: true,
101
- configurable: true,
102
- writable: true,
103
- value: 0
104
- });
105
- Object.defineProperty(this, "n", {
106
- enumerable: true,
107
- configurable: true,
108
- writable: true,
109
- value: 1
110
- });
111
- Object.defineProperty(this, "logitBias", {
112
- enumerable: true,
113
- configurable: true,
114
- writable: true,
115
- value: void 0
116
- });
117
- Object.defineProperty(this, "maxTokens", {
118
- enumerable: true,
119
- configurable: true,
120
- writable: true,
121
- value: void 0
122
- });
123
- Object.defineProperty(this, "modelName", {
124
- enumerable: true,
125
- configurable: true,
126
- writable: true,
127
- value: "gpt-3.5-turbo"
128
- });
129
- Object.defineProperty(this, "prefixMessages", {
130
- enumerable: true,
131
- configurable: true,
132
- writable: true,
133
- value: void 0
134
- });
135
- Object.defineProperty(this, "modelKwargs", {
136
- enumerable: true,
137
- configurable: true,
138
- writable: true,
139
- value: void 0
140
- });
141
- Object.defineProperty(this, "timeout", {
142
- enumerable: true,
143
- configurable: true,
144
- writable: true,
145
- value: void 0
146
- });
147
- Object.defineProperty(this, "stop", {
148
- enumerable: true,
149
- configurable: true,
150
- writable: true,
151
- value: void 0
152
- });
153
- Object.defineProperty(this, "user", {
154
- enumerable: true,
155
- configurable: true,
156
- writable: true,
157
- value: void 0
158
- });
159
- Object.defineProperty(this, "streaming", {
160
- enumerable: true,
161
- configurable: true,
162
- writable: true,
163
- value: false
164
- });
165
- Object.defineProperty(this, "openAIApiKey", {
166
- enumerable: true,
167
- configurable: true,
168
- writable: true,
169
- value: void 0
170
- });
171
- Object.defineProperty(this, "azureOpenAIApiVersion", {
172
- enumerable: true,
173
- configurable: true,
174
- writable: true,
175
- value: void 0
176
- });
177
- Object.defineProperty(this, "azureOpenAIApiKey", {
178
- enumerable: true,
179
- configurable: true,
180
- writable: true,
181
- value: void 0
182
- });
183
- Object.defineProperty(this, "azureOpenAIApiInstanceName", {
184
- enumerable: true,
185
- configurable: true,
186
- writable: true,
187
- value: void 0
188
- });
189
- Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
190
- enumerable: true,
191
- configurable: true,
192
- writable: true,
193
- value: void 0
194
- });
195
- Object.defineProperty(this, "azureOpenAIBasePath", {
196
- enumerable: true,
197
- configurable: true,
198
- writable: true,
199
- value: void 0
200
- });
201
- Object.defineProperty(this, "organization", {
202
- enumerable: true,
203
- configurable: true,
204
- writable: true,
205
- value: void 0
206
- });
207
- Object.defineProperty(this, "client", {
208
- enumerable: true,
209
- configurable: true,
210
- writable: true,
211
- value: void 0
212
- });
213
- Object.defineProperty(this, "clientConfig", {
214
- enumerable: true,
215
- configurable: true,
216
- writable: true,
217
- value: void 0
218
- });
219
- this.openAIApiKey =
220
- fields?.openAIApiKey ?? getEnvironmentVariable("OPENAI_API_KEY");
221
- this.azureOpenAIApiKey =
222
- fields?.azureOpenAIApiKey ??
223
- getEnvironmentVariable("AZURE_OPENAI_API_KEY");
224
- if (!this.azureOpenAIApiKey && !this.openAIApiKey) {
225
- throw new Error("OpenAI or Azure OpenAI API key not found");
226
- }
227
- this.azureOpenAIApiInstanceName =
228
- fields?.azureOpenAIApiInstanceName ??
229
- getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
230
- this.azureOpenAIApiDeploymentName =
231
- (fields?.azureOpenAIApiCompletionsDeploymentName ||
232
- fields?.azureOpenAIApiDeploymentName) ??
233
- (getEnvironmentVariable("AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME") ||
234
- getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
235
- this.azureOpenAIApiVersion =
236
- fields?.azureOpenAIApiVersion ??
237
- getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
238
- this.azureOpenAIBasePath =
239
- fields?.azureOpenAIBasePath ??
240
- getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
241
- this.organization =
242
- fields?.configuration?.organization ??
243
- getEnvironmentVariable("OPENAI_ORGANIZATION");
244
- this.modelName = fields?.modelName ?? this.modelName;
245
- this.prefixMessages = fields?.prefixMessages ?? this.prefixMessages;
246
- this.modelKwargs = fields?.modelKwargs ?? {};
247
- this.timeout = fields?.timeout;
248
- this.temperature = fields?.temperature ?? this.temperature;
249
- this.topP = fields?.topP ?? this.topP;
250
- this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
251
- this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
252
- this.n = fields?.n ?? this.n;
253
- this.logitBias = fields?.logitBias;
254
- this.maxTokens = fields?.maxTokens;
255
- this.stop = fields?.stop;
256
- this.user = fields?.user;
257
- this.streaming = fields?.streaming ?? false;
258
- if (this.n > 1) {
259
- throw new Error("Cannot use n > 1 in OpenAIChat LLM. Use ChatOpenAI Chat Model instead.");
260
- }
261
- if (this.azureOpenAIApiKey) {
262
- if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
263
- throw new Error("Azure OpenAI API instance name not found");
264
- }
265
- if (!this.azureOpenAIApiDeploymentName) {
266
- throw new Error("Azure OpenAI API deployment name not found");
267
- }
268
- if (!this.azureOpenAIApiVersion) {
269
- throw new Error("Azure OpenAI API version not found");
270
- }
271
- this.openAIApiKey = this.openAIApiKey ?? "";
272
- }
273
- this.clientConfig = {
274
- apiKey: this.openAIApiKey,
275
- organization: this.organization,
276
- baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
277
- dangerouslyAllowBrowser: true,
278
- defaultHeaders: configuration?.baseOptions?.headers ??
279
- fields?.configuration?.baseOptions?.headers,
280
- defaultQuery: configuration?.baseOptions?.params ??
281
- fields?.configuration?.baseOptions?.params,
282
- ...configuration,
283
- ...fields?.configuration,
284
- };
285
- }
286
- /**
287
- * Get the parameters used to invoke the model
288
- */
289
- invocationParams(options) {
290
- return {
291
- model: this.modelName,
292
- temperature: this.temperature,
293
- top_p: this.topP,
294
- frequency_penalty: this.frequencyPenalty,
295
- presence_penalty: this.presencePenalty,
296
- n: this.n,
297
- logit_bias: this.logitBias,
298
- max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
299
- stop: options?.stop ?? this.stop,
300
- user: this.user,
301
- stream: this.streaming,
302
- ...this.modelKwargs,
303
- };
304
- }
305
- /** @ignore */
306
- _identifyingParams() {
307
- return {
308
- model_name: this.modelName,
309
- ...this.invocationParams(),
310
- ...this.clientConfig,
311
- };
312
- }
313
- /**
314
- * Get the identifying parameters for the model
315
- */
316
- identifyingParams() {
317
- return {
318
- model_name: this.modelName,
319
- ...this.invocationParams(),
320
- ...this.clientConfig,
321
- };
322
- }
323
- /**
324
- * Formats the messages for the OpenAI API.
325
- * @param prompt The prompt to be formatted.
326
- * @returns Array of formatted messages.
327
- */
328
- formatMessages(prompt) {
329
- const message = {
330
- role: "user",
331
- content: prompt,
332
- };
333
- return this.prefixMessages ? [...this.prefixMessages, message] : [message];
334
- }
335
- async *_streamResponseChunks(prompt, options, runManager) {
336
- const params = {
337
- ...this.invocationParams(options),
338
- messages: this.formatMessages(prompt),
339
- stream: true,
340
- };
341
- const stream = await this.completionWithRetry(params, options);
342
- for await (const data of stream) {
343
- const choice = data?.choices[0];
344
- if (!choice) {
345
- continue;
346
- }
347
- const { delta } = choice;
348
- const generationChunk = new GenerationChunk({
349
- text: delta.content ?? "",
350
- });
351
- yield generationChunk;
352
- const newTokenIndices = {
353
- prompt: options.promptIndex ?? 0,
354
- completion: choice.index ?? 0,
355
- };
356
- // eslint-disable-next-line no-void
357
- void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices);
358
- }
359
- if (options.signal?.aborted) {
360
- throw new Error("AbortError");
361
- }
362
- }
363
- /** @ignore */
364
- async _call(prompt, options, runManager) {
365
- const params = this.invocationParams(options);
366
- if (params.stream) {
367
- const stream = await this._streamResponseChunks(prompt, options, runManager);
368
- let finalChunk;
369
- for await (const chunk of stream) {
370
- if (finalChunk === undefined) {
371
- finalChunk = chunk;
372
- }
373
- else {
374
- finalChunk = finalChunk.concat(chunk);
375
- }
376
- }
377
- return finalChunk?.text ?? "";
378
- }
379
- else {
380
- const response = await this.completionWithRetry({
381
- ...params,
382
- stream: false,
383
- messages: this.formatMessages(prompt),
384
- }, {
385
- signal: options.signal,
386
- ...options.options,
387
- });
388
- return response?.choices[0]?.message?.content ?? "";
389
- }
390
- }
391
- async completionWithRetry(request, options) {
392
- const requestOptions = this._getClientOptions(options);
393
- return this.caller.call(async () => {
394
- try {
395
- const res = await this.client.chat.completions.create(request, requestOptions);
396
- return res;
397
- }
398
- catch (e) {
399
- const error = wrapOpenAIClientError(e);
400
- throw error;
401
- }
402
- });
403
- }
404
- /** @ignore */
405
- _getClientOptions(options) {
406
- if (!this.client) {
407
- const openAIEndpointConfig = {
408
- azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
409
- azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
410
- azureOpenAIApiKey: this.azureOpenAIApiKey,
411
- azureOpenAIBasePath: this.azureOpenAIBasePath,
412
- baseURL: this.clientConfig.baseURL,
413
- };
414
- const endpoint = getEndpoint(openAIEndpointConfig);
415
- const params = {
416
- ...this.clientConfig,
417
- baseURL: endpoint,
418
- timeout: this.timeout,
419
- maxRetries: 0,
420
- };
421
- if (!params.baseURL) {
422
- delete params.baseURL;
423
- }
424
- this.client = new OpenAIClient(params);
425
- }
426
- const requestOptions = {
427
- ...this.clientConfig,
428
- ...options,
429
- };
430
- if (this.azureOpenAIApiKey) {
431
- requestOptions.headers = {
432
- "api-key": this.azureOpenAIApiKey,
433
- ...requestOptions.headers,
434
- };
435
- requestOptions.query = {
436
- "api-version": this.azureOpenAIApiVersion,
437
- ...requestOptions.query,
438
- };
439
- }
440
- return requestOptions;
441
- }
442
- _llmType() {
443
- return "openai";
444
- }
445
- }
4
+ export { OpenAIChat };
446
5
  /**
447
6
  * PromptLayer wrapper to OpenAIChat
448
7
  */