langchain 0.0.152 → 0.0.154

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 (72) hide show
  1. package/chat_models/fireworks.cjs +1 -0
  2. package/chat_models/fireworks.d.ts +1 -0
  3. package/chat_models/fireworks.js +1 -0
  4. package/dist/agents/executor.cjs +9 -2
  5. package/dist/agents/executor.js +9 -2
  6. package/dist/base_language/count_tokens.cjs +1 -1
  7. package/dist/base_language/count_tokens.js +1 -1
  8. package/dist/base_language/index.cjs +36 -0
  9. package/dist/base_language/index.d.ts +9 -1
  10. package/dist/base_language/index.js +36 -0
  11. package/dist/cache/base.cjs +24 -1
  12. package/dist/cache/base.d.ts +9 -0
  13. package/dist/cache/base.js +21 -0
  14. package/dist/cache/cloudflare_kv.cjs +2 -5
  15. package/dist/cache/cloudflare_kv.js +3 -6
  16. package/dist/cache/ioredis.cjs +16 -6
  17. package/dist/cache/ioredis.d.ts +5 -2
  18. package/dist/cache/ioredis.js +17 -7
  19. package/dist/cache/momento.cjs +6 -2
  20. package/dist/cache/momento.js +7 -3
  21. package/dist/cache/redis.cjs +3 -5
  22. package/dist/cache/redis.js +4 -6
  23. package/dist/cache/upstash_redis.cjs +2 -5
  24. package/dist/cache/upstash_redis.js +3 -6
  25. package/dist/chains/openai_functions/structured_output.d.ts +2 -2
  26. package/dist/chat_models/base.cjs +64 -20
  27. package/dist/chat_models/base.d.ts +8 -1
  28. package/dist/chat_models/base.js +64 -20
  29. package/dist/chat_models/fireworks.cjs +81 -0
  30. package/dist/chat_models/fireworks.d.ts +33 -0
  31. package/dist/chat_models/fireworks.js +77 -0
  32. package/dist/chat_models/ollama.cjs +22 -5
  33. package/dist/chat_models/ollama.d.ts +1 -2
  34. package/dist/chat_models/ollama.js +22 -5
  35. package/dist/chat_models/openai.d.ts +2 -2
  36. package/dist/llms/base.cjs +10 -26
  37. package/dist/llms/base.d.ts +4 -4
  38. package/dist/llms/base.js +4 -20
  39. package/dist/llms/fireworks.cjs +92 -0
  40. package/dist/llms/fireworks.d.ts +33 -0
  41. package/dist/llms/fireworks.js +88 -0
  42. package/dist/llms/ollama.cjs +24 -8
  43. package/dist/llms/ollama.d.ts +1 -2
  44. package/dist/llms/ollama.js +24 -8
  45. package/dist/llms/openai-chat.cjs +1 -5
  46. package/dist/llms/openai-chat.d.ts +1 -1
  47. package/dist/llms/openai-chat.js +1 -5
  48. package/dist/llms/openai.cjs +1 -1
  49. package/dist/llms/openai.d.ts +2 -2
  50. package/dist/llms/openai.js +1 -1
  51. package/dist/load/import_map.cjs +4 -2
  52. package/dist/load/import_map.d.ts +2 -0
  53. package/dist/load/import_map.js +2 -0
  54. package/dist/schema/index.cjs +50 -1
  55. package/dist/schema/index.d.ts +5 -0
  56. package/dist/schema/index.js +48 -0
  57. package/dist/schema/output_parser.cjs +38 -6
  58. package/dist/schema/output_parser.d.ts +20 -5
  59. package/dist/schema/output_parser.js +38 -6
  60. package/dist/schema/runnable/base.cjs +65 -10
  61. package/dist/schema/runnable/base.d.ts +17 -3
  62. package/dist/schema/runnable/base.js +65 -10
  63. package/dist/stores/message/utils.cjs +2 -50
  64. package/dist/stores/message/utils.d.ts +0 -14
  65. package/dist/stores/message/utils.js +2 -49
  66. package/dist/util/ollama.cjs +2 -2
  67. package/dist/util/ollama.d.ts +6 -0
  68. package/dist/util/ollama.js +2 -2
  69. package/llms/fireworks.cjs +1 -0
  70. package/llms/fireworks.d.ts +1 -0
  71. package/llms/fireworks.js +1 -0
  72. package/package.json +17 -1
@@ -46,5 +46,5 @@ export declare class FunctionCallStructuredOutputParser<T extends z.AnyZodObject
46
46
  * as well as an additional required "outputSchema" JSON Schema object.
47
47
  * @returns OpenAPIChain
48
48
  */
49
- export declare function createStructuredOutputChain<T extends z.AnyZodObject = z.AnyZodObject>(input: StructuredOutputChainInput): LLMChain<any, ChatOpenAI | BaseChatModel<BaseFunctionCallOptions>>;
50
- export declare function createStructuredOutputChainFromZod<T extends z.AnyZodObject>(zodSchema: T, input: Omit<StructuredOutputChainInput, "outputSchema">): LLMChain<any, ChatOpenAI | BaseChatModel<BaseFunctionCallOptions>>;
49
+ export declare function createStructuredOutputChain<T extends z.AnyZodObject = z.AnyZodObject>(input: StructuredOutputChainInput): LLMChain<any, BaseChatModel<BaseFunctionCallOptions> | ChatOpenAI<BaseFunctionCallOptions>>;
50
+ export declare function createStructuredOutputChainFromZod<T extends z.AnyZodObject>(zodSchema: T, input: Omit<StructuredOutputChainInput, "outputSchema">): LLMChain<any, BaseChatModel<BaseFunctionCallOptions> | ChatOpenAI<BaseFunctionCallOptions>>;
@@ -94,33 +94,18 @@ class BaseChatModel extends index_js_2.BaseLanguageModel {
94
94
  })));
95
95
  }
96
96
  }
97
- /**
98
- * Generates chat based on the input messages.
99
- * @param messages An array of arrays of BaseMessage instances.
100
- * @param options The call options or an array of stop sequences.
101
- * @param callbacks The callbacks for the language model.
102
- * @returns A Promise that resolves to an LLMResult.
103
- */
104
- async generate(messages, options, callbacks) {
105
- // parse call options
106
- let parsedOptions;
107
- if (Array.isArray(options)) {
108
- parsedOptions = { stop: options };
109
- }
110
- else {
111
- parsedOptions = options;
112
- }
97
+ /** @ignore */
98
+ async _generateUncached(messages, parsedOptions, handledOptions) {
113
99
  const baseMessages = messages.map((messageList) => messageList.map(index_js_1.coerceMessageLikeToMessage));
114
- const [runnableConfig, callOptions] = this._separateRunnableConfigFromCallOptions(parsedOptions);
115
100
  // create callback manager and start run
116
- const callbackManager_ = await manager_js_1.CallbackManager.configure(runnableConfig.callbacks ?? callbacks, this.callbacks, runnableConfig.tags, this.tags, runnableConfig.metadata, this.metadata, { verbose: this.verbose });
101
+ const callbackManager_ = await manager_js_1.CallbackManager.configure(handledOptions.callbacks, this.callbacks, handledOptions.tags, this.tags, handledOptions.metadata, this.metadata, { verbose: this.verbose });
117
102
  const extra = {
118
- options: callOptions,
103
+ options: parsedOptions,
119
104
  invocation_params: this?.invocationParams(parsedOptions),
120
105
  };
121
106
  const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), baseMessages, undefined, undefined, extra);
122
107
  // generate results
123
- const results = await Promise.allSettled(baseMessages.map((messageList, i) => this._generate(messageList, { ...callOptions, promptIndex: i }, runManagers?.[i])));
108
+ const results = await Promise.allSettled(baseMessages.map((messageList, i) => this._generate(messageList, { ...parsedOptions, promptIndex: i }, runManagers?.[i])));
124
109
  // handle results
125
110
  const generations = [];
126
111
  const llmOutputs = [];
@@ -155,6 +140,54 @@ class BaseChatModel extends index_js_2.BaseLanguageModel {
155
140
  });
156
141
  return output;
157
142
  }
143
+ /**
144
+ * Generates chat based on the input messages.
145
+ * @param messages An array of arrays of BaseMessage instances.
146
+ * @param options The call options or an array of stop sequences.
147
+ * @param callbacks The callbacks for the language model.
148
+ * @returns A Promise that resolves to an LLMResult.
149
+ */
150
+ async generate(messages, options, callbacks) {
151
+ // parse call options
152
+ let parsedOptions;
153
+ if (Array.isArray(options)) {
154
+ parsedOptions = { stop: options };
155
+ }
156
+ else {
157
+ parsedOptions = options;
158
+ }
159
+ const baseMessages = messages.map((messageList) => messageList.map(index_js_1.coerceMessageLikeToMessage));
160
+ const [runnableConfig, callOptions] = this._separateRunnableConfigFromCallOptions(parsedOptions);
161
+ runnableConfig.callbacks = runnableConfig.callbacks ?? callbacks;
162
+ if (!this.cache) {
163
+ return this._generateUncached(baseMessages, callOptions, runnableConfig);
164
+ }
165
+ const { cache } = this;
166
+ const llmStringKey = this._getSerializedCacheKeyParametersForCall(callOptions);
167
+ const missingPromptIndices = [];
168
+ const generations = await Promise.all(baseMessages.map(async (baseMessage, index) => {
169
+ // Join all content into one string for the prompt index
170
+ const prompt = BaseChatModel._convertInputToPromptValue(baseMessage).toString();
171
+ const result = await cache.lookup(prompt, llmStringKey);
172
+ if (!result) {
173
+ missingPromptIndices.push(index);
174
+ }
175
+ return result;
176
+ }));
177
+ let llmOutput = {};
178
+ if (missingPromptIndices.length > 0) {
179
+ const results = await this._generateUncached(missingPromptIndices.map((i) => baseMessages[i]), callOptions, runnableConfig);
180
+ await Promise.all(results.generations.map(async (generation, index) => {
181
+ const promptIndex = missingPromptIndices[index];
182
+ generations[promptIndex] = generation;
183
+ // Join all content into one string for the prompt index
184
+ const prompt = BaseChatModel._convertInputToPromptValue(baseMessages[promptIndex]).toString();
185
+ return cache.update(prompt, llmStringKey, generation);
186
+ }));
187
+ llmOutput = results.llmOutput ?? {};
188
+ }
189
+ return { generations, llmOutput };
190
+ }
158
191
  /**
159
192
  * Get the parameters used to invoke the model
160
193
  */
@@ -165,6 +198,17 @@ class BaseChatModel extends index_js_2.BaseLanguageModel {
165
198
  _modelType() {
166
199
  return "base_chat_model";
167
200
  }
201
+ /**
202
+ * @deprecated
203
+ * Return a json-like object representing this LLM.
204
+ */
205
+ serialize() {
206
+ return {
207
+ ...this.invocationParams(),
208
+ _type: this._llmType(),
209
+ _model: this._modelType(),
210
+ };
211
+ }
168
212
  /**
169
213
  * Generates a prompt based on the input prompt values.
170
214
  * @param promptValues An array of BasePromptValue instances.
@@ -49,6 +49,8 @@ export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCal
49
49
  invoke(input: BaseLanguageModelInput, options?: CallOptions): Promise<BaseMessageChunk>;
50
50
  _streamResponseChunks(_messages: BaseMessage[], _options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
51
51
  _streamIterator(input: BaseLanguageModelInput, options?: CallOptions): AsyncGenerator<BaseMessageChunk>;
52
+ /** @ignore */
53
+ _generateUncached(messages: BaseMessageLike[][], parsedOptions: this["ParsedCallOptions"], handledOptions: RunnableConfig): Promise<LLMResult>;
52
54
  /**
53
55
  * Generates chat based on the input messages.
54
56
  * @param messages An array of arrays of BaseMessage instances.
@@ -63,6 +65,11 @@ export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCal
63
65
  invocationParams(_options?: this["ParsedCallOptions"]): any;
64
66
  _modelType(): string;
65
67
  abstract _llmType(): string;
68
+ /**
69
+ * @deprecated
70
+ * Return a json-like object representing this LLM.
71
+ */
72
+ serialize(): SerializedLLM;
66
73
  /**
67
74
  * Generates a prompt based on the input prompt values.
68
75
  * @param promptValues An array of BasePromptValue instances.
@@ -109,7 +116,7 @@ export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCal
109
116
  * An abstract class that extends BaseChatModel and provides a simple
110
117
  * implementation of _generate.
111
118
  */
112
- export declare abstract class SimpleChatModel extends BaseChatModel {
119
+ export declare abstract class SimpleChatModel<CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions> extends BaseChatModel<CallOptions> {
113
120
  abstract _call(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
114
121
  _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
115
122
  }
@@ -90,33 +90,18 @@ export class BaseChatModel extends BaseLanguageModel {
90
90
  })));
91
91
  }
92
92
  }
93
- /**
94
- * Generates chat based on the input messages.
95
- * @param messages An array of arrays of BaseMessage instances.
96
- * @param options The call options or an array of stop sequences.
97
- * @param callbacks The callbacks for the language model.
98
- * @returns A Promise that resolves to an LLMResult.
99
- */
100
- async generate(messages, options, callbacks) {
101
- // parse call options
102
- let parsedOptions;
103
- if (Array.isArray(options)) {
104
- parsedOptions = { stop: options };
105
- }
106
- else {
107
- parsedOptions = options;
108
- }
93
+ /** @ignore */
94
+ async _generateUncached(messages, parsedOptions, handledOptions) {
109
95
  const baseMessages = messages.map((messageList) => messageList.map(coerceMessageLikeToMessage));
110
- const [runnableConfig, callOptions] = this._separateRunnableConfigFromCallOptions(parsedOptions);
111
96
  // create callback manager and start run
112
- const callbackManager_ = await CallbackManager.configure(runnableConfig.callbacks ?? callbacks, this.callbacks, runnableConfig.tags, this.tags, runnableConfig.metadata, this.metadata, { verbose: this.verbose });
97
+ const callbackManager_ = await CallbackManager.configure(handledOptions.callbacks, this.callbacks, handledOptions.tags, this.tags, handledOptions.metadata, this.metadata, { verbose: this.verbose });
113
98
  const extra = {
114
- options: callOptions,
99
+ options: parsedOptions,
115
100
  invocation_params: this?.invocationParams(parsedOptions),
116
101
  };
117
102
  const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), baseMessages, undefined, undefined, extra);
118
103
  // generate results
119
- const results = await Promise.allSettled(baseMessages.map((messageList, i) => this._generate(messageList, { ...callOptions, promptIndex: i }, runManagers?.[i])));
104
+ const results = await Promise.allSettled(baseMessages.map((messageList, i) => this._generate(messageList, { ...parsedOptions, promptIndex: i }, runManagers?.[i])));
120
105
  // handle results
121
106
  const generations = [];
122
107
  const llmOutputs = [];
@@ -151,6 +136,54 @@ export class BaseChatModel extends BaseLanguageModel {
151
136
  });
152
137
  return output;
153
138
  }
139
+ /**
140
+ * Generates chat based on the input messages.
141
+ * @param messages An array of arrays of BaseMessage instances.
142
+ * @param options The call options or an array of stop sequences.
143
+ * @param callbacks The callbacks for the language model.
144
+ * @returns A Promise that resolves to an LLMResult.
145
+ */
146
+ async generate(messages, options, callbacks) {
147
+ // parse call options
148
+ let parsedOptions;
149
+ if (Array.isArray(options)) {
150
+ parsedOptions = { stop: options };
151
+ }
152
+ else {
153
+ parsedOptions = options;
154
+ }
155
+ const baseMessages = messages.map((messageList) => messageList.map(coerceMessageLikeToMessage));
156
+ const [runnableConfig, callOptions] = this._separateRunnableConfigFromCallOptions(parsedOptions);
157
+ runnableConfig.callbacks = runnableConfig.callbacks ?? callbacks;
158
+ if (!this.cache) {
159
+ return this._generateUncached(baseMessages, callOptions, runnableConfig);
160
+ }
161
+ const { cache } = this;
162
+ const llmStringKey = this._getSerializedCacheKeyParametersForCall(callOptions);
163
+ const missingPromptIndices = [];
164
+ const generations = await Promise.all(baseMessages.map(async (baseMessage, index) => {
165
+ // Join all content into one string for the prompt index
166
+ const prompt = BaseChatModel._convertInputToPromptValue(baseMessage).toString();
167
+ const result = await cache.lookup(prompt, llmStringKey);
168
+ if (!result) {
169
+ missingPromptIndices.push(index);
170
+ }
171
+ return result;
172
+ }));
173
+ let llmOutput = {};
174
+ if (missingPromptIndices.length > 0) {
175
+ const results = await this._generateUncached(missingPromptIndices.map((i) => baseMessages[i]), callOptions, runnableConfig);
176
+ await Promise.all(results.generations.map(async (generation, index) => {
177
+ const promptIndex = missingPromptIndices[index];
178
+ generations[promptIndex] = generation;
179
+ // Join all content into one string for the prompt index
180
+ const prompt = BaseChatModel._convertInputToPromptValue(baseMessages[promptIndex]).toString();
181
+ return cache.update(prompt, llmStringKey, generation);
182
+ }));
183
+ llmOutput = results.llmOutput ?? {};
184
+ }
185
+ return { generations, llmOutput };
186
+ }
154
187
  /**
155
188
  * Get the parameters used to invoke the model
156
189
  */
@@ -161,6 +194,17 @@ export class BaseChatModel extends BaseLanguageModel {
161
194
  _modelType() {
162
195
  return "base_chat_model";
163
196
  }
197
+ /**
198
+ * @deprecated
199
+ * Return a json-like object representing this LLM.
200
+ */
201
+ serialize() {
202
+ return {
203
+ ...this.invocationParams(),
204
+ _type: this._llmType(),
205
+ _model: this._modelType(),
206
+ };
207
+ }
164
208
  /**
165
209
  * Generates a prompt based on the input prompt values.
166
210
  * @param promptValues An array of BasePromptValue instances.
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChatFireworks = void 0;
4
+ const openai_js_1 = require("./openai.cjs");
5
+ const env_js_1 = require("../util/env.cjs");
6
+ /**
7
+ * Wrapper around Fireworks API for large language models fine-tuned for chat
8
+ *
9
+ * Fireworks API is compatible to the OpenAI API with some limitations described in
10
+ * https://readme.fireworks.ai/docs/openai-compatibility.
11
+ *
12
+ * To use, you should have the `openai` package installed and
13
+ * the `FIREWORKS_API_KEY` environment variable set.
14
+ */
15
+ class ChatFireworks extends openai_js_1.ChatOpenAI {
16
+ static lc_name() {
17
+ return "ChatFireworks";
18
+ }
19
+ _llmType() {
20
+ return "fireworks";
21
+ }
22
+ get lc_secrets() {
23
+ return {
24
+ fireworksApiKey: "FIREWORKS_API_KEY",
25
+ };
26
+ }
27
+ constructor(fields) {
28
+ const fireworksApiKey = fields?.fireworksApiKey || (0, env_js_1.getEnvironmentVariable)("FIREWORKS_API_KEY");
29
+ if (!fireworksApiKey) {
30
+ throw new Error(`Fireworks API key not found. Please set the FIREWORKS_API_KEY environment variable or provide the key into "fireworksApiKey"`);
31
+ }
32
+ super({
33
+ ...fields,
34
+ modelName: fields?.modelName || "accounts/fireworks/models/llama-v2-13b-chat",
35
+ openAIApiKey: fireworksApiKey,
36
+ configuration: {
37
+ baseURL: "https://api.fireworks.ai/inference/v1",
38
+ },
39
+ });
40
+ Object.defineProperty(this, "lc_serializable", {
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true,
44
+ value: true
45
+ });
46
+ Object.defineProperty(this, "fireworksApiKey", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: void 0
51
+ });
52
+ this.fireworksApiKey = fireworksApiKey;
53
+ }
54
+ toJSON() {
55
+ const result = super.toJSON();
56
+ if ("kwargs" in result &&
57
+ typeof result.kwargs === "object" &&
58
+ result.kwargs != null) {
59
+ delete result.kwargs.openai_api_key;
60
+ delete result.kwargs.configuration;
61
+ }
62
+ return result;
63
+ }
64
+ /**
65
+ * Calls the Fireworks API with retry logic in case of failures.
66
+ * @param request The request to send to the Fireworks API.
67
+ * @param options Optional configuration for the API call.
68
+ * @returns The response from the Fireworks API.
69
+ */
70
+ async completionWithRetry(request, options) {
71
+ delete request.frequency_penalty;
72
+ delete request.presence_penalty;
73
+ delete request.logit_bias;
74
+ delete request.functions;
75
+ if (request.stream === true) {
76
+ return super.completionWithRetry(request, options);
77
+ }
78
+ return super.completionWithRetry(request, options);
79
+ }
80
+ }
81
+ exports.ChatFireworks = ChatFireworks;
@@ -0,0 +1,33 @@
1
+ import type { OpenAI as OpenAIClient } from "openai";
2
+ import type { ChatOpenAICallOptions, OpenAIChatInput } from "./openai.js";
3
+ import type { OpenAICoreRequestOptions } from "../types/openai-types.js";
4
+ import type { BaseChatModelParams } from "./base.js";
5
+ import { ChatOpenAI } from "./openai.js";
6
+ type FireworksUnsupportedArgs = "frequencyPenalty" | "presencePenalty" | "logitBias" | "functions";
7
+ type FireworksUnsupportedCallOptions = "functions" | "function_call" | "tools";
8
+ export type ChatFireworksCallOptions = Partial<Omit<ChatOpenAICallOptions, FireworksUnsupportedCallOptions>>;
9
+ /**
10
+ * Wrapper around Fireworks API for large language models fine-tuned for chat
11
+ *
12
+ * Fireworks API is compatible to the OpenAI API with some limitations described in
13
+ * https://readme.fireworks.ai/docs/openai-compatibility.
14
+ *
15
+ * To use, you should have the `openai` package installed and
16
+ * the `FIREWORKS_API_KEY` environment variable set.
17
+ */
18
+ export declare class ChatFireworks extends ChatOpenAI<ChatFireworksCallOptions> {
19
+ static lc_name(): string;
20
+ _llmType(): string;
21
+ get lc_secrets(): {
22
+ [key: string]: string;
23
+ } | undefined;
24
+ lc_serializable: boolean;
25
+ fireworksApiKey?: string;
26
+ constructor(fields?: Partial<Omit<OpenAIChatInput, "openAIApiKey" | FireworksUnsupportedArgs>> & BaseChatModelParams & {
27
+ fireworksApiKey?: string;
28
+ });
29
+ toJSON(): import("../load/serializable.js").Serialized;
30
+ completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming, options?: OpenAICoreRequestOptions): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;
31
+ completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming, options?: OpenAICoreRequestOptions): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;
32
+ }
33
+ export {};
@@ -0,0 +1,77 @@
1
+ import { ChatOpenAI } from "./openai.js";
2
+ import { getEnvironmentVariable } from "../util/env.js";
3
+ /**
4
+ * Wrapper around Fireworks API for large language models fine-tuned for chat
5
+ *
6
+ * Fireworks API is compatible to the OpenAI API with some limitations described in
7
+ * https://readme.fireworks.ai/docs/openai-compatibility.
8
+ *
9
+ * To use, you should have the `openai` package installed and
10
+ * the `FIREWORKS_API_KEY` environment variable set.
11
+ */
12
+ export class ChatFireworks extends ChatOpenAI {
13
+ static lc_name() {
14
+ return "ChatFireworks";
15
+ }
16
+ _llmType() {
17
+ return "fireworks";
18
+ }
19
+ get lc_secrets() {
20
+ return {
21
+ fireworksApiKey: "FIREWORKS_API_KEY",
22
+ };
23
+ }
24
+ constructor(fields) {
25
+ const fireworksApiKey = fields?.fireworksApiKey || getEnvironmentVariable("FIREWORKS_API_KEY");
26
+ if (!fireworksApiKey) {
27
+ throw new Error(`Fireworks API key not found. Please set the FIREWORKS_API_KEY environment variable or provide the key into "fireworksApiKey"`);
28
+ }
29
+ super({
30
+ ...fields,
31
+ modelName: fields?.modelName || "accounts/fireworks/models/llama-v2-13b-chat",
32
+ openAIApiKey: fireworksApiKey,
33
+ configuration: {
34
+ baseURL: "https://api.fireworks.ai/inference/v1",
35
+ },
36
+ });
37
+ Object.defineProperty(this, "lc_serializable", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: true
42
+ });
43
+ Object.defineProperty(this, "fireworksApiKey", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: void 0
48
+ });
49
+ this.fireworksApiKey = fireworksApiKey;
50
+ }
51
+ toJSON() {
52
+ const result = super.toJSON();
53
+ if ("kwargs" in result &&
54
+ typeof result.kwargs === "object" &&
55
+ result.kwargs != null) {
56
+ delete result.kwargs.openai_api_key;
57
+ delete result.kwargs.configuration;
58
+ }
59
+ return result;
60
+ }
61
+ /**
62
+ * Calls the Fireworks API with retry logic in case of failures.
63
+ * @param request The request to send to the Fireworks API.
64
+ * @param options Optional configuration for the API call.
65
+ * @returns The response from the Fireworks API.
66
+ */
67
+ async completionWithRetry(request, options) {
68
+ delete request.frequency_penalty;
69
+ delete request.presence_penalty;
70
+ delete request.logit_bias;
71
+ delete request.functions;
72
+ if (request.stream === true) {
73
+ return super.completionWithRetry(request, options);
74
+ }
75
+ return super.completionWithRetry(request, options);
76
+ }
77
+ }
@@ -303,11 +303,28 @@ class ChatOllama extends base_js_1.SimpleChatModel {
303
303
  prompt: this._formatMessagesAsPrompt(input),
304
304
  }, options));
305
305
  for await (const chunk of stream) {
306
- yield new index_js_1.ChatGenerationChunk({
307
- text: chunk.response,
308
- message: new index_js_1.AIMessageChunk({ content: chunk.response }),
309
- });
310
- await runManager?.handleLLMNewToken(chunk.response ?? "");
306
+ if (!chunk.done) {
307
+ yield new index_js_1.ChatGenerationChunk({
308
+ text: chunk.response,
309
+ message: new index_js_1.AIMessageChunk({ content: chunk.response }),
310
+ });
311
+ await runManager?.handleLLMNewToken(chunk.response ?? "");
312
+ }
313
+ else {
314
+ yield new index_js_1.ChatGenerationChunk({
315
+ text: "",
316
+ message: new index_js_1.AIMessageChunk({ content: "" }),
317
+ generationInfo: {
318
+ model: chunk.model,
319
+ total_duration: chunk.total_duration,
320
+ load_duration: chunk.load_duration,
321
+ prompt_eval_count: chunk.prompt_eval_count,
322
+ prompt_eval_duration: chunk.prompt_eval_duration,
323
+ eval_count: chunk.eval_count,
324
+ eval_duration: chunk.eval_duration,
325
+ },
326
+ });
327
+ }
311
328
  }
312
329
  }
313
330
  _formatMessagesAsPrompt(messages) {
@@ -14,8 +14,7 @@ export interface OllamaCallOptions extends BaseLanguageModelCallOptions {
14
14
  * models in a chat-like fashion. It extends the SimpleChatModel class and
15
15
  * implements the OllamaInput interface.
16
16
  */
17
- export declare class ChatOllama extends SimpleChatModel implements OllamaInput {
18
- CallOptions: OllamaCallOptions;
17
+ export declare class ChatOllama extends SimpleChatModel<OllamaCallOptions> implements OllamaInput {
19
18
  static lc_name(): string;
20
19
  lc_serializable: boolean;
21
20
  model: string;
@@ -300,11 +300,28 @@ export class ChatOllama extends SimpleChatModel {
300
300
  prompt: this._formatMessagesAsPrompt(input),
301
301
  }, options));
302
302
  for await (const chunk of stream) {
303
- yield new ChatGenerationChunk({
304
- text: chunk.response,
305
- message: new AIMessageChunk({ content: chunk.response }),
306
- });
307
- await runManager?.handleLLMNewToken(chunk.response ?? "");
303
+ if (!chunk.done) {
304
+ yield new ChatGenerationChunk({
305
+ text: chunk.response,
306
+ message: new AIMessageChunk({ content: chunk.response }),
307
+ });
308
+ await runManager?.handleLLMNewToken(chunk.response ?? "");
309
+ }
310
+ else {
311
+ yield new ChatGenerationChunk({
312
+ text: "",
313
+ message: new AIMessageChunk({ content: "" }),
314
+ generationInfo: {
315
+ model: chunk.model,
316
+ total_duration: chunk.total_duration,
317
+ load_duration: chunk.load_duration,
318
+ prompt_eval_count: chunk.prompt_eval_count,
319
+ prompt_eval_duration: chunk.prompt_eval_duration,
320
+ eval_count: chunk.eval_count,
321
+ eval_duration: chunk.eval_duration,
322
+ },
323
+ });
324
+ }
308
325
  }
309
326
  }
310
327
  _formatMessagesAsPrompt(messages) {
@@ -37,9 +37,9 @@ export interface ChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCa
37
37
  * `openai.createChatCompletion`} can be passed through {@link modelKwargs}, even
38
38
  * if not explicitly available on this class.
39
39
  */
40
- export declare class ChatOpenAI extends BaseChatModel<ChatOpenAICallOptions> implements OpenAIChatInput, AzureOpenAIInput {
40
+ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends BaseChatModel<CallOptions> implements OpenAIChatInput, AzureOpenAIInput {
41
41
  static lc_name(): string;
42
- get callKeys(): (keyof ChatOpenAICallOptions)[];
42
+ get callKeys(): string[];
43
43
  lc_serializable: boolean;
44
44
  get lc_secrets(): {
45
45
  [key: string]: string;
@@ -1,16 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LLM = exports.BaseLLM = void 0;
4
- const index_js_1 = require("../cache/index.cjs");
5
- const index_js_2 = require("../schema/index.cjs");
6
- const index_js_3 = require("../base_language/index.cjs");
4
+ const index_js_1 = require("../schema/index.cjs");
5
+ const index_js_2 = require("../base_language/index.cjs");
7
6
  const manager_js_1 = require("../callbacks/manager.cjs");
8
7
  const base_js_1 = require("../memory/base.cjs");
9
8
  /**
10
9
  * LLM Wrapper. Provides an {@link call} (an {@link generate}) function that takes in a prompt (or prompts) and returns a string.
11
10
  */
12
- class BaseLLM extends index_js_3.BaseLanguageModel {
13
- constructor({ cache, concurrency, ...rest }) {
11
+ class BaseLLM extends index_js_2.BaseLanguageModel {
12
+ constructor({ concurrency, ...rest }) {
14
13
  super(concurrency ? { maxConcurrency: concurrency, ...rest } : rest);
15
14
  Object.defineProperty(this, "lc_namespace", {
16
15
  enumerable: true,
@@ -18,21 +17,6 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
18
17
  writable: true,
19
18
  value: ["langchain", "llms", this._llmType()]
20
19
  });
21
- Object.defineProperty(this, "cache", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: void 0
26
- });
27
- if (typeof cache === "object") {
28
- this.cache = cache;
29
- }
30
- else if (cache) {
31
- this.cache = index_js_1.InMemoryCache.global();
32
- }
33
- else {
34
- this.cache = undefined;
35
- }
36
20
  }
37
21
  /**
38
22
  * This method takes an input and options, and returns a string. It
@@ -72,7 +56,7 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
72
56
  invocation_params: this?.invocationParams(callOptions),
73
57
  };
74
58
  const runManagers = await callbackManager_?.handleLLMStart(this.toJSON(), [prompt.toString()], undefined, undefined, extra);
75
- let generation = new index_js_2.GenerationChunk({
59
+ let generation = new index_js_1.GenerationChunk({
76
60
  text: "",
77
61
  });
78
62
  try {
@@ -160,7 +144,7 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
160
144
  // This defines RUN_KEY as a non-enumerable property on the output object
161
145
  // so that it is not serialized when the output is stringified, and so that
162
146
  // it isnt included when listing the keys of the output object.
163
- Object.defineProperty(output, index_js_2.RUN_KEY, {
147
+ Object.defineProperty(output, index_js_1.RUN_KEY, {
164
148
  value: runIds ? { runIds } : undefined,
165
149
  configurable: true,
166
150
  });
@@ -186,9 +170,7 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
186
170
  return this._generateUncached(prompts, callOptions, runnableConfig);
187
171
  }
188
172
  const { cache } = this;
189
- const params = this.serialize();
190
- params.stop = callOptions.stop ?? params.stop;
191
- const llmStringKey = `${Object.entries(params).sort()}`;
173
+ const llmStringKey = this._getSerializedCacheKeyParametersForCall(callOptions);
192
174
  const missingPromptIndices = [];
193
175
  const generations = await Promise.all(prompts.map(async (prompt, index) => {
194
176
  const result = await cache.lookup(prompt, llmStringKey);
@@ -238,7 +220,7 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
238
220
  async predictMessages(messages, options, callbacks) {
239
221
  const text = (0, base_js_1.getBufferString)(messages);
240
222
  const prediction = await this.call(text, options, callbacks);
241
- return new index_js_2.AIMessage(prediction);
223
+ return new index_js_1.AIMessage(prediction);
242
224
  }
243
225
  /**
244
226
  * Get the identifying parameters of the LLM.
@@ -248,6 +230,7 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
248
230
  return {};
249
231
  }
250
232
  /**
233
+ * @deprecated
251
234
  * Return a json-like object representing this LLM.
252
235
  */
253
236
  serialize() {
@@ -261,6 +244,7 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
261
244
  return "base_llm";
262
245
  }
263
246
  /**
247
+ * @deprecated
264
248
  * Load an LLM from a json-like object describing it.
265
249
  */
266
250
  static async deserialize(data) {