langchain 0.0.176 → 0.0.178

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 (84) hide show
  1. package/chat_models/iflytek_xinghuo/web.cjs +1 -0
  2. package/chat_models/iflytek_xinghuo/web.d.ts +1 -0
  3. package/chat_models/iflytek_xinghuo/web.js +1 -0
  4. package/chat_models/iflytek_xinghuo.cjs +1 -0
  5. package/chat_models/iflytek_xinghuo.d.ts +1 -0
  6. package/chat_models/iflytek_xinghuo.js +1 -0
  7. package/dist/chat_models/bedrock.cjs +25 -4
  8. package/dist/chat_models/bedrock.d.ts +2 -1
  9. package/dist/chat_models/bedrock.js +25 -4
  10. package/dist/chat_models/cloudflare_workersai.cjs +70 -24
  11. package/dist/chat_models/cloudflare_workersai.d.ts +6 -2
  12. package/dist/chat_models/cloudflare_workersai.js +71 -25
  13. package/dist/chat_models/iflytek_xinghuo/common.cjs +335 -0
  14. package/dist/chat_models/iflytek_xinghuo/common.d.ts +165 -0
  15. package/dist/chat_models/iflytek_xinghuo/common.js +331 -0
  16. package/dist/chat_models/iflytek_xinghuo/index.cjs +35 -0
  17. package/dist/chat_models/iflytek_xinghuo/index.d.ts +5 -0
  18. package/dist/chat_models/iflytek_xinghuo/index.js +28 -0
  19. package/dist/chat_models/iflytek_xinghuo/web.cjs +30 -0
  20. package/dist/chat_models/iflytek_xinghuo/web.d.ts +5 -0
  21. package/dist/chat_models/iflytek_xinghuo/web.js +26 -0
  22. package/dist/chat_models/llama_cpp.cjs +31 -79
  23. package/dist/chat_models/llama_cpp.d.ts +15 -58
  24. package/dist/chat_models/llama_cpp.js +32 -80
  25. package/dist/chat_models/openai.cjs +91 -6
  26. package/dist/chat_models/openai.d.ts +10 -0
  27. package/dist/chat_models/openai.js +91 -6
  28. package/dist/embeddings/hf.cjs +10 -1
  29. package/dist/embeddings/hf.d.ts +4 -2
  30. package/dist/embeddings/hf.js +10 -1
  31. package/dist/embeddings/llama_cpp.cjs +67 -0
  32. package/dist/embeddings/llama_cpp.d.ts +26 -0
  33. package/dist/embeddings/llama_cpp.js +63 -0
  34. package/dist/embeddings/ollama.cjs +7 -1
  35. package/dist/embeddings/ollama.js +7 -1
  36. package/dist/graphs/neo4j_graph.cjs +36 -5
  37. package/dist/graphs/neo4j_graph.js +14 -3
  38. package/dist/llms/bedrock.cjs +25 -3
  39. package/dist/llms/bedrock.d.ts +2 -1
  40. package/dist/llms/bedrock.js +25 -3
  41. package/dist/llms/cloudflare_workersai.cjs +59 -13
  42. package/dist/llms/cloudflare_workersai.d.ts +9 -3
  43. package/dist/llms/cloudflare_workersai.js +59 -13
  44. package/dist/llms/hf.cjs +10 -1
  45. package/dist/llms/hf.d.ts +3 -0
  46. package/dist/llms/hf.js +10 -1
  47. package/dist/llms/llama_cpp.cjs +25 -65
  48. package/dist/llms/llama_cpp.d.ts +7 -43
  49. package/dist/llms/llama_cpp.js +25 -65
  50. package/dist/load/import_constants.cjs +3 -0
  51. package/dist/load/import_constants.js +3 -0
  52. package/dist/prompts/chat.cjs +8 -0
  53. package/dist/prompts/chat.d.ts +5 -0
  54. package/dist/prompts/chat.js +8 -0
  55. package/dist/prompts/few_shot.cjs +162 -1
  56. package/dist/prompts/few_shot.d.ts +90 -2
  57. package/dist/prompts/few_shot.js +160 -0
  58. package/dist/prompts/index.cjs +2 -1
  59. package/dist/prompts/index.d.ts +1 -1
  60. package/dist/prompts/index.js +1 -1
  61. package/dist/retrievers/zep.cjs +26 -3
  62. package/dist/retrievers/zep.d.ts +11 -2
  63. package/dist/retrievers/zep.js +26 -3
  64. package/dist/util/bedrock.d.ts +2 -0
  65. package/dist/util/event-source-parse.cjs +20 -1
  66. package/dist/util/event-source-parse.d.ts +2 -0
  67. package/dist/util/event-source-parse.js +18 -0
  68. package/dist/util/iflytek_websocket_stream.cjs +81 -0
  69. package/dist/util/iflytek_websocket_stream.d.ts +27 -0
  70. package/dist/util/iflytek_websocket_stream.js +77 -0
  71. package/dist/util/llama_cpp.cjs +34 -0
  72. package/dist/util/llama_cpp.d.ts +46 -0
  73. package/dist/util/llama_cpp.js +28 -0
  74. package/dist/util/openai-format-fndef.cjs +81 -0
  75. package/dist/util/openai-format-fndef.d.ts +44 -0
  76. package/dist/util/openai-format-fndef.js +77 -0
  77. package/dist/util/openapi.d.ts +2 -2
  78. package/dist/vectorstores/pinecone.cjs +5 -5
  79. package/dist/vectorstores/pinecone.d.ts +2 -2
  80. package/dist/vectorstores/pinecone.js +5 -5
  81. package/embeddings/llama_cpp.cjs +1 -0
  82. package/embeddings/llama_cpp.d.ts +1 -0
  83. package/embeddings/llama_cpp.js +1 -0
  84. package/package.json +34 -5
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/chat_models/iflytek_xinghuo/web.cjs');
@@ -0,0 +1 @@
1
+ export * from '../../dist/chat_models/iflytek_xinghuo/web.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/chat_models/iflytek_xinghuo/web.js'
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/chat_models/iflytek_xinghuo/index.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/chat_models/iflytek_xinghuo/index.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/chat_models/iflytek_xinghuo/index.js'
@@ -133,6 +133,12 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
133
133
  writable: true,
134
134
  value: new eventstream_codec_1.EventStreamCodec(util_utf8_1.toUtf8, util_utf8_1.fromUtf8)
135
135
  });
136
+ Object.defineProperty(this, "streaming", {
137
+ enumerable: true,
138
+ configurable: true,
139
+ writable: true,
140
+ value: false
141
+ });
136
142
  this.model = fields?.model ?? this.model;
137
143
  const allowedModels = ["ai21", "anthropic", "amazon"];
138
144
  if (!allowedModels.includes(this.model.split(".")[0])) {
@@ -150,6 +156,7 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
150
156
  this.endpointHost = fields?.endpointHost ?? fields?.endpointUrl;
151
157
  this.stopSequences = fields?.stopSequences;
152
158
  this.modelKwargs = fields?.modelKwargs;
159
+ this.streaming = fields?.streaming ?? this.streaming;
153
160
  }
154
161
  /** Call out to Bedrock service model.
155
162
  Arguments:
@@ -161,10 +168,23 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
161
168
  Example:
162
169
  response = model.call("Tell me a joke.")
163
170
  */
164
- async _call(messages, options) {
171
+ async _call(messages, options, runManager) {
165
172
  const service = "bedrock-runtime";
166
173
  const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
167
174
  const provider = this.model.split(".")[0];
175
+ if (this.streaming) {
176
+ const stream = this._streamResponseChunks(messages, options, runManager);
177
+ let finalResult;
178
+ for await (const chunk of stream) {
179
+ if (finalResult === undefined) {
180
+ finalResult = chunk;
181
+ }
182
+ else {
183
+ finalResult = finalResult.concat(chunk);
184
+ }
185
+ }
186
+ return finalResult?.message.content ?? "";
187
+ }
168
188
  const response = await this._signedFetch(messages, options, {
169
189
  bedrockMethod: "invoke",
170
190
  endpointHost,
@@ -233,7 +253,6 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
233
253
  event.headers[":content-type"].value !== "application/json") {
234
254
  throw Error(`Failed to get event chunk: got ${chunk}`);
235
255
  }
236
- // console.log(decoder.decode(event.body));
237
256
  const body = JSON.parse(decoder.decode(event.body));
238
257
  if (body.message) {
239
258
  throw new Error(body.message);
@@ -245,7 +264,8 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
245
264
  text,
246
265
  message: new index_js_1.AIMessageChunk({ content: text }),
247
266
  });
248
- await runManager?.handleLLMNewToken(text);
267
+ // eslint-disable-next-line no-void
268
+ void runManager?.handleLLMNewToken(text);
249
269
  }
250
270
  }
251
271
  }
@@ -256,7 +276,8 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
256
276
  text,
257
277
  message: new index_js_1.AIMessageChunk({ content: text }),
258
278
  });
259
- await runManager?.handleLLMNewToken(text);
279
+ // eslint-disable-next-line no-void
280
+ void runManager?.handleLLMNewToken(text);
260
281
  }
261
282
  }
262
283
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -33,6 +33,7 @@ export declare class ChatBedrock extends SimpleChatModel implements BaseBedrockI
33
33
  stopSequences?: string[];
34
34
  modelKwargs?: Record<string, unknown>;
35
35
  codec: EventStreamCodec;
36
+ streaming: boolean;
36
37
  get lc_secrets(): {
37
38
  [key: string]: string;
38
39
  } | undefined;
@@ -49,7 +50,7 @@ export declare class ChatBedrock extends SimpleChatModel implements BaseBedrockI
49
50
  Example:
50
51
  response = model.call("Tell me a joke.")
51
52
  */
52
- _call(messages: BaseMessage[], options: this["ParsedCallOptions"]): Promise<string>;
53
+ _call(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
53
54
  _signedFetch(messages: BaseMessage[], options: this["ParsedCallOptions"], fields: {
54
55
  bedrockMethod: "invoke" | "invoke-with-response-stream";
55
56
  endpointHost: string;
@@ -128,6 +128,12 @@ export class ChatBedrock extends SimpleChatModel {
128
128
  writable: true,
129
129
  value: new EventStreamCodec(toUtf8, fromUtf8)
130
130
  });
131
+ Object.defineProperty(this, "streaming", {
132
+ enumerable: true,
133
+ configurable: true,
134
+ writable: true,
135
+ value: false
136
+ });
131
137
  this.model = fields?.model ?? this.model;
132
138
  const allowedModels = ["ai21", "anthropic", "amazon"];
133
139
  if (!allowedModels.includes(this.model.split(".")[0])) {
@@ -145,6 +151,7 @@ export class ChatBedrock extends SimpleChatModel {
145
151
  this.endpointHost = fields?.endpointHost ?? fields?.endpointUrl;
146
152
  this.stopSequences = fields?.stopSequences;
147
153
  this.modelKwargs = fields?.modelKwargs;
154
+ this.streaming = fields?.streaming ?? this.streaming;
148
155
  }
149
156
  /** Call out to Bedrock service model.
150
157
  Arguments:
@@ -156,10 +163,23 @@ export class ChatBedrock extends SimpleChatModel {
156
163
  Example:
157
164
  response = model.call("Tell me a joke.")
158
165
  */
159
- async _call(messages, options) {
166
+ async _call(messages, options, runManager) {
160
167
  const service = "bedrock-runtime";
161
168
  const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
162
169
  const provider = this.model.split(".")[0];
170
+ if (this.streaming) {
171
+ const stream = this._streamResponseChunks(messages, options, runManager);
172
+ let finalResult;
173
+ for await (const chunk of stream) {
174
+ if (finalResult === undefined) {
175
+ finalResult = chunk;
176
+ }
177
+ else {
178
+ finalResult = finalResult.concat(chunk);
179
+ }
180
+ }
181
+ return finalResult?.message.content ?? "";
182
+ }
163
183
  const response = await this._signedFetch(messages, options, {
164
184
  bedrockMethod: "invoke",
165
185
  endpointHost,
@@ -228,7 +248,6 @@ export class ChatBedrock extends SimpleChatModel {
228
248
  event.headers[":content-type"].value !== "application/json") {
229
249
  throw Error(`Failed to get event chunk: got ${chunk}`);
230
250
  }
231
- // console.log(decoder.decode(event.body));
232
251
  const body = JSON.parse(decoder.decode(event.body));
233
252
  if (body.message) {
234
253
  throw new Error(body.message);
@@ -240,7 +259,8 @@ export class ChatBedrock extends SimpleChatModel {
240
259
  text,
241
260
  message: new AIMessageChunk({ content: text }),
242
261
  });
243
- await runManager?.handleLLMNewToken(text);
262
+ // eslint-disable-next-line no-void
263
+ void runManager?.handleLLMNewToken(text);
244
264
  }
245
265
  }
246
266
  }
@@ -251,7 +271,8 @@ export class ChatBedrock extends SimpleChatModel {
251
271
  text,
252
272
  message: new AIMessageChunk({ content: text }),
253
273
  });
254
- await runManager?.handleLLMNewToken(text);
274
+ // eslint-disable-next-line no-void
275
+ void runManager?.handleLLMNewToken(text);
255
276
  }
256
277
  }
257
278
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -4,6 +4,7 @@ exports.ChatCloudflareWorkersAI = void 0;
4
4
  const base_js_1 = require("./base.cjs");
5
5
  const index_js_1 = require("../schema/index.cjs");
6
6
  const env_js_1 = require("../util/env.cjs");
7
+ const event_source_parse_js_1 = require("../util/event-source-parse.cjs");
7
8
  /**
8
9
  * A class that enables calls to the Cloudflare Workers AI API to access large language
9
10
  * models in a chat-like fashion. It extends the SimpleChatModel class and
@@ -45,7 +46,14 @@ class ChatCloudflareWorkersAI extends base_js_1.SimpleChatModel {
45
46
  writable: true,
46
47
  value: void 0
47
48
  });
49
+ Object.defineProperty(this, "streaming", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: false
54
+ });
48
55
  this.model = fields?.model ?? this.model;
56
+ this.streaming = fields?.streaming ?? this.streaming;
49
57
  this.cloudflareAccountId =
50
58
  fields?.cloudflareAccountId ??
51
59
  (0, env_js_1.getEnvironmentVariable)("CLOUDFLARE_ACCOUNT_ID");
@@ -88,6 +96,50 @@ class ChatCloudflareWorkersAI extends base_js_1.SimpleChatModel {
88
96
  throw new Error(`No Cloudflare API key found. Please provide it when instantiating the CloudflareWorkersAI class, or set it as "CLOUDFLARE_API_KEY" in your environment variables.`);
89
97
  }
90
98
  }
99
+ async _request(messages, options, stream) {
100
+ this.validateEnvironment();
101
+ const url = `${this.baseUrl}/${this.model}`;
102
+ const headers = {
103
+ Authorization: `Bearer ${this.cloudflareApiToken}`,
104
+ "Content-Type": "application/json",
105
+ };
106
+ const formattedMessages = this._formatMessages(messages);
107
+ const data = { messages: formattedMessages, stream };
108
+ return this.caller.call(async () => {
109
+ const response = await fetch(url, {
110
+ method: "POST",
111
+ headers,
112
+ body: JSON.stringify(data),
113
+ signal: options.signal,
114
+ });
115
+ if (!response.ok) {
116
+ const error = new Error(`Cloudflare LLM call failed with status code ${response.status}`);
117
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
118
+ error.response = response;
119
+ throw error;
120
+ }
121
+ return response;
122
+ });
123
+ }
124
+ async *_streamResponseChunks(messages, options, runManager) {
125
+ const response = await this._request(messages, options, true);
126
+ if (!response.body) {
127
+ throw new Error("Empty response from Cloudflare. Please try again.");
128
+ }
129
+ const stream = (0, event_source_parse_js_1.convertEventStreamToIterableReadableDataStream)(response.body);
130
+ for await (const chunk of stream) {
131
+ if (chunk !== "[DONE]") {
132
+ const parsedChunk = JSON.parse(chunk);
133
+ const generationChunk = new index_js_1.ChatGenerationChunk({
134
+ message: new index_js_1.AIMessageChunk({ content: parsedChunk.response }),
135
+ text: parsedChunk.response,
136
+ });
137
+ yield generationChunk;
138
+ // eslint-disable-next-line no-void
139
+ void runManager?.handleLLMNewToken(generationChunk.text ?? "");
140
+ }
141
+ }
142
+ }
91
143
  _formatMessages(messages) {
92
144
  const formattedMessages = messages.map((message) => {
93
145
  let role;
@@ -115,31 +167,25 @@ class ChatCloudflareWorkersAI extends base_js_1.SimpleChatModel {
115
167
  return formattedMessages;
116
168
  }
117
169
  /** @ignore */
118
- async _call(messages, options) {
119
- this.validateEnvironment();
120
- const url = `${this.baseUrl}/${this.model}`;
121
- const headers = {
122
- Authorization: `Bearer ${this.cloudflareApiToken}`,
123
- "Content-Type": "application/json",
124
- };
125
- const formattedMessages = this._formatMessages(messages);
126
- const data = { messages: formattedMessages };
127
- const responseData = await this.caller.call(async () => {
128
- const response = await fetch(url, {
129
- method: "POST",
130
- headers,
131
- body: JSON.stringify(data),
132
- signal: options.signal,
133
- });
134
- if (!response.ok) {
135
- const error = new Error(`Cloudflare LLM call failed with status code ${response.status}`);
136
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
137
- error.response = response;
138
- throw error;
170
+ async _call(messages, options, runManager) {
171
+ if (!this.streaming) {
172
+ const response = await this._request(messages, options);
173
+ const responseData = await response.json();
174
+ return responseData.result.response;
175
+ }
176
+ else {
177
+ const stream = this._streamResponseChunks(messages, options, runManager);
178
+ let finalResult;
179
+ for await (const chunk of stream) {
180
+ if (finalResult === undefined) {
181
+ finalResult = chunk;
182
+ }
183
+ else {
184
+ finalResult = finalResult.concat(chunk);
185
+ }
139
186
  }
140
- return response.json();
141
- });
142
- return responseData.result.response;
187
+ return finalResult?.message?.content ?? "";
188
+ }
143
189
  }
144
190
  }
145
191
  exports.ChatCloudflareWorkersAI = ChatCloudflareWorkersAI;
@@ -1,7 +1,8 @@
1
1
  import { SimpleChatModel, BaseChatModelParams } from "./base.js";
2
2
  import { BaseLanguageModelCallOptions } from "../base_language/index.js";
3
- import { BaseMessage } from "../schema/index.js";
3
+ import { BaseMessage, ChatGenerationChunk } from "../schema/index.js";
4
4
  import { CloudflareWorkersAIInput } from "../llms/cloudflare_workersai.js";
5
+ import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
5
6
  /**
6
7
  * An interface defining the options for a Cloudflare Workers AI call. It extends
7
8
  * the BaseLanguageModelCallOptions interface.
@@ -20,6 +21,7 @@ export declare class ChatCloudflareWorkersAI extends SimpleChatModel implements
20
21
  cloudflareAccountId?: string;
21
22
  cloudflareApiToken?: string;
22
23
  baseUrl: string;
24
+ streaming: boolean;
23
25
  constructor(fields?: CloudflareWorkersAIInput & BaseChatModelParams);
24
26
  _llmType(): string;
25
27
  /** Get the identifying parameters for this LLM. */
@@ -37,10 +39,12 @@ export declare class ChatCloudflareWorkersAI extends SimpleChatModel implements
37
39
  * Method to validate the environment.
38
40
  */
39
41
  validateEnvironment(): void;
42
+ _request(messages: BaseMessage[], options: this["ParsedCallOptions"], stream?: boolean): Promise<Response>;
43
+ _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
40
44
  protected _formatMessages(messages: BaseMessage[]): {
41
45
  role: string;
42
46
  content: string;
43
47
  }[];
44
48
  /** @ignore */
45
- _call(messages: BaseMessage[], options: this["ParsedCallOptions"]): Promise<string>;
49
+ _call(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
46
50
  }
@@ -1,6 +1,7 @@
1
1
  import { SimpleChatModel } from "./base.js";
2
- import { ChatMessage } from "../schema/index.js";
2
+ import { AIMessageChunk, ChatGenerationChunk, ChatMessage, } from "../schema/index.js";
3
3
  import { getEnvironmentVariable } from "../util/env.js";
4
+ import { convertEventStreamToIterableReadableDataStream } from "../util/event-source-parse.js";
4
5
  /**
5
6
  * A class that enables calls to the Cloudflare Workers AI API to access large language
6
7
  * models in a chat-like fashion. It extends the SimpleChatModel class and
@@ -42,7 +43,14 @@ export class ChatCloudflareWorkersAI extends SimpleChatModel {
42
43
  writable: true,
43
44
  value: void 0
44
45
  });
46
+ Object.defineProperty(this, "streaming", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: false
51
+ });
45
52
  this.model = fields?.model ?? this.model;
53
+ this.streaming = fields?.streaming ?? this.streaming;
46
54
  this.cloudflareAccountId =
47
55
  fields?.cloudflareAccountId ??
48
56
  getEnvironmentVariable("CLOUDFLARE_ACCOUNT_ID");
@@ -85,6 +93,50 @@ export class ChatCloudflareWorkersAI extends SimpleChatModel {
85
93
  throw new Error(`No Cloudflare API key found. Please provide it when instantiating the CloudflareWorkersAI class, or set it as "CLOUDFLARE_API_KEY" in your environment variables.`);
86
94
  }
87
95
  }
96
+ async _request(messages, options, stream) {
97
+ this.validateEnvironment();
98
+ const url = `${this.baseUrl}/${this.model}`;
99
+ const headers = {
100
+ Authorization: `Bearer ${this.cloudflareApiToken}`,
101
+ "Content-Type": "application/json",
102
+ };
103
+ const formattedMessages = this._formatMessages(messages);
104
+ const data = { messages: formattedMessages, stream };
105
+ return this.caller.call(async () => {
106
+ const response = await fetch(url, {
107
+ method: "POST",
108
+ headers,
109
+ body: JSON.stringify(data),
110
+ signal: options.signal,
111
+ });
112
+ if (!response.ok) {
113
+ const error = new Error(`Cloudflare LLM call failed with status code ${response.status}`);
114
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
115
+ error.response = response;
116
+ throw error;
117
+ }
118
+ return response;
119
+ });
120
+ }
121
+ async *_streamResponseChunks(messages, options, runManager) {
122
+ const response = await this._request(messages, options, true);
123
+ if (!response.body) {
124
+ throw new Error("Empty response from Cloudflare. Please try again.");
125
+ }
126
+ const stream = convertEventStreamToIterableReadableDataStream(response.body);
127
+ for await (const chunk of stream) {
128
+ if (chunk !== "[DONE]") {
129
+ const parsedChunk = JSON.parse(chunk);
130
+ const generationChunk = new ChatGenerationChunk({
131
+ message: new AIMessageChunk({ content: parsedChunk.response }),
132
+ text: parsedChunk.response,
133
+ });
134
+ yield generationChunk;
135
+ // eslint-disable-next-line no-void
136
+ void runManager?.handleLLMNewToken(generationChunk.text ?? "");
137
+ }
138
+ }
139
+ }
88
140
  _formatMessages(messages) {
89
141
  const formattedMessages = messages.map((message) => {
90
142
  let role;
@@ -112,30 +164,24 @@ export class ChatCloudflareWorkersAI extends SimpleChatModel {
112
164
  return formattedMessages;
113
165
  }
114
166
  /** @ignore */
115
- async _call(messages, options) {
116
- this.validateEnvironment();
117
- const url = `${this.baseUrl}/${this.model}`;
118
- const headers = {
119
- Authorization: `Bearer ${this.cloudflareApiToken}`,
120
- "Content-Type": "application/json",
121
- };
122
- const formattedMessages = this._formatMessages(messages);
123
- const data = { messages: formattedMessages };
124
- const responseData = await this.caller.call(async () => {
125
- const response = await fetch(url, {
126
- method: "POST",
127
- headers,
128
- body: JSON.stringify(data),
129
- signal: options.signal,
130
- });
131
- if (!response.ok) {
132
- const error = new Error(`Cloudflare LLM call failed with status code ${response.status}`);
133
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
134
- error.response = response;
135
- throw error;
167
+ async _call(messages, options, runManager) {
168
+ if (!this.streaming) {
169
+ const response = await this._request(messages, options);
170
+ const responseData = await response.json();
171
+ return responseData.result.response;
172
+ }
173
+ else {
174
+ const stream = this._streamResponseChunks(messages, options, runManager);
175
+ let finalResult;
176
+ for await (const chunk of stream) {
177
+ if (finalResult === undefined) {
178
+ finalResult = chunk;
179
+ }
180
+ else {
181
+ finalResult = finalResult.concat(chunk);
182
+ }
136
183
  }
137
- return response.json();
138
- });
139
- return responseData.result.response;
184
+ return finalResult?.message?.content ?? "";
185
+ }
140
186
  }
141
187
  }