modelfusion 0.98.0 → 0.99.0

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 (58) hide show
  1. package/README.md +4 -4
  2. package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs +1 -1
  3. package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js +1 -1
  4. package/model-function/Model.d.ts +2 -2
  5. package/model-function/generate-text/PromptTemplateTextGenerationModel.cjs +2 -2
  6. package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +2 -2
  7. package/model-function/generate-text/PromptTemplateTextGenerationModel.js +2 -2
  8. package/model-function/generate-text/TextGenerationModel.d.ts +31 -5
  9. package/model-function/generate-text/generateText.cjs +10 -4
  10. package/model-function/generate-text/generateText.d.ts +1 -0
  11. package/model-function/generate-text/generateText.js +10 -4
  12. package/model-function/generate-text/prompt-template/trimChatPrompt.cjs +1 -1
  13. package/model-function/generate-text/prompt-template/trimChatPrompt.js +1 -1
  14. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +27 -31
  15. package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +2 -2
  16. package/model-provider/anthropic/AnthropicTextGenerationModel.js +27 -31
  17. package/model-provider/cohere/CohereFacade.cjs +1 -1
  18. package/model-provider/cohere/CohereFacade.d.ts +1 -1
  19. package/model-provider/cohere/CohereFacade.js +1 -1
  20. package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
  21. package/model-provider/cohere/CohereTextGenerationModel.cjs +34 -43
  22. package/model-provider/cohere/CohereTextGenerationModel.d.ts +3 -4
  23. package/model-provider/cohere/CohereTextGenerationModel.js +34 -43
  24. package/model-provider/huggingface/HuggingFaceFacade.cjs +1 -1
  25. package/model-provider/huggingface/HuggingFaceFacade.d.ts +1 -1
  26. package/model-provider/huggingface/HuggingFaceFacade.js +1 -1
  27. package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +31 -41
  28. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +3 -4
  29. package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +31 -41
  30. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +4 -4
  31. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +2 -2
  32. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +4 -4
  33. package/model-provider/mistral/MistralTextGenerationModel.cjs +5 -5
  34. package/model-provider/mistral/MistralTextGenerationModel.d.ts +2 -2
  35. package/model-provider/mistral/MistralTextGenerationModel.js +5 -5
  36. package/model-provider/ollama/OllamaTextGenerationModel.cjs +4 -4
  37. package/model-provider/ollama/OllamaTextGenerationModel.d.ts +2 -2
  38. package/model-provider/ollama/OllamaTextGenerationModel.js +4 -4
  39. package/model-provider/openai/OpenAICompletionModel.cjs +48 -53
  40. package/model-provider/openai/OpenAICompletionModel.d.ts +3 -6
  41. package/model-provider/openai/OpenAICompletionModel.js +48 -53
  42. package/model-provider/openai/OpenAIFacade.cjs +2 -2
  43. package/model-provider/openai/OpenAIFacade.d.ts +2 -2
  44. package/model-provider/openai/OpenAIFacade.js +2 -2
  45. package/model-provider/openai/chat/AbstractOpenAIChatModel.cjs +50 -54
  46. package/model-provider/openai/chat/AbstractOpenAIChatModel.d.ts +6 -27
  47. package/model-provider/openai/chat/AbstractOpenAIChatModel.js +50 -54
  48. package/model-provider/openai/chat/OpenAIChatModel.cjs +3 -3
  49. package/model-provider/openai/chat/OpenAIChatModel.d.ts +1 -1
  50. package/model-provider/openai/chat/OpenAIChatModel.js +3 -3
  51. package/model-provider/openai/chat/OpenAIChatModel.test.cjs +1 -1
  52. package/model-provider/openai/chat/OpenAIChatModel.test.js +1 -1
  53. package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +2 -2
  54. package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +2 -2
  55. package/model-provider/openai-compatible/OpenAICompatibleFacade.cjs +1 -1
  56. package/model-provider/openai-compatible/OpenAICompatibleFacade.d.ts +1 -1
  57. package/model-provider/openai-compatible/OpenAICompatibleFacade.js +1 -1
  58. package/package.json +1 -1
package/README.md CHANGED
@@ -211,7 +211,7 @@ const sentiment = await generateStructure(
211
211
  .ChatTextGenerator({
212
212
  model: "gpt-3.5-turbo",
213
213
  temperature: 0,
214
- maxCompletionTokens: 50,
214
+ maxGenerationTokens: 50,
215
215
  })
216
216
  .asFunctionCallStructureGenerationModel({ fnName: "sentiment" })
217
217
  .withInstructionPrompt(),
@@ -541,7 +541,7 @@ const text = await generateText(
541
541
  llamacpp
542
542
  .TextGenerator({
543
543
  contextWindowSize: 4096, // Llama 2 context window size
544
- maxCompletionTokens: 1000,
544
+ maxGenerationTokens: 1000,
545
545
  })
546
546
  .withTextPromptTemplate(Llama2Prompt.instruction()),
547
547
  {
@@ -619,10 +619,10 @@ ModelFusion model functions return rich responses that include the original resp
619
619
 
620
620
  ```ts
621
621
  // access the full response (needs to be typed) and the metadata:
622
- const { value, response, metadata } = await generateText(
622
+ const { text, response, metadata } = await generateText(
623
623
  openai.CompletionTextGenerator({
624
624
  model: "gpt-3.5-turbo-instruct",
625
- maxCompletionTokens: 1000,
625
+ maxGenerationTokens: 1000,
626
626
  n: 2, // generate 2 completions
627
627
  }),
628
628
  "Write a short story about a robot learning to love:\n\n",
@@ -10,7 +10,7 @@ const summarizeRecursively_js_1 = require("./summarizeRecursively.cjs");
10
10
  * while leaving enough space for the model to generate text.
11
11
  */
12
12
  async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, model, prompt, tokenLimit = model.contextWindowSize -
13
- (model.settings.maxCompletionTokens ?? model.contextWindowSize / 4), join, }, options) {
13
+ (model.settings.maxGenerationTokens ?? model.contextWindowSize / 4), join, }, options) {
14
14
  const emptyPromptTokens = await model.countPromptTokens(await prompt({ text: "" }));
15
15
  return (0, summarizeRecursively_js_1.summarizeRecursively)({
16
16
  split: (0, splitRecursively_js_1.splitAtToken)({
@@ -7,7 +7,7 @@ import { summarizeRecursively } from "./summarizeRecursively.js";
7
7
  * while leaving enough space for the model to generate text.
8
8
  */
9
9
  export async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, model, prompt, tokenLimit = model.contextWindowSize -
10
- (model.settings.maxCompletionTokens ?? model.contextWindowSize / 4), join, }, options) {
10
+ (model.settings.maxGenerationTokens ?? model.contextWindowSize / 4), join, }, options) {
11
11
  const emptyPromptTokens = await model.countPromptTokens(await prompt({ text: "" }));
12
12
  return summarizeRecursively({
13
13
  split: splitAtToken({
@@ -20,11 +20,11 @@ export interface Model<SETTINGS extends ModelSettings> {
20
20
  * @example
21
21
  * const model = new OpenAICompletionModel({
22
22
  * model: "gpt-3.5-turbo-instruct",
23
- * maxCompletionTokens: 500,
23
+ * maxGenerationTokens: 500,
24
24
  * });
25
25
  *
26
26
  * const modelWithMoreTokens = model.withSettings({
27
- * maxCompletionTokens: 1000,
27
+ * maxGenerationTokens: 1000,
28
28
  * });
29
29
  */
30
30
  withSettings(additionalSettings: Partial<SETTINGS>): this;
@@ -40,9 +40,9 @@ class PromptTemplateTextGenerationModel {
40
40
  }
41
41
  return ((prompt) => originalCountPromptTokens(this.promptTemplate.format(prompt)));
42
42
  }
43
- doGenerateText(prompt, options) {
43
+ doGenerateTexts(prompt, options) {
44
44
  const mappedPrompt = this.promptTemplate.format(prompt);
45
- return this.model.doGenerateText(mappedPrompt, options);
45
+ return this.model.doGenerateTexts(mappedPrompt, options);
46
46
  }
47
47
  get settingsForEvent() {
48
48
  return this.model.settingsForEvent;
@@ -17,9 +17,9 @@ export declare class PromptTemplateTextGenerationModel<PROMPT, MODEL_PROMPT, SET
17
17
  get tokenizer(): MODEL["tokenizer"];
18
18
  get contextWindowSize(): MODEL["contextWindowSize"];
19
19
  get countPromptTokens(): MODEL["countPromptTokens"] extends undefined ? undefined : (prompt: PROMPT) => PromiseLike<number>;
20
- doGenerateText(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
20
+ doGenerateTexts(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
21
21
  response: unknown;
22
- text: string;
22
+ texts: string[];
23
23
  usage?: {
24
24
  promptTokens: number;
25
25
  completionTokens: number;
@@ -37,9 +37,9 @@ export class PromptTemplateTextGenerationModel {
37
37
  }
38
38
  return ((prompt) => originalCountPromptTokens(this.promptTemplate.format(prompt)));
39
39
  }
40
- doGenerateText(prompt, options) {
40
+ doGenerateTexts(prompt, options) {
41
41
  const mappedPrompt = this.promptTemplate.format(prompt);
42
- return this.model.doGenerateText(mappedPrompt, options);
42
+ return this.model.doGenerateTexts(mappedPrompt, options);
43
43
  }
44
44
  get settingsForEvent() {
45
45
  return this.model.settingsForEvent;
@@ -5,18 +5,44 @@ import { BasicTokenizer, FullTokenizer } from "../tokenize-text/Tokenizer.js";
5
5
  import { TextGenerationPromptTemplate } from "./TextGenerationPromptTemplate.js";
6
6
  export interface TextGenerationModelSettings extends ModelSettings {
7
7
  /**
8
- * Maximum number of tokens to generate.
8
+ * Specifies the maximum number of tokens (words, punctuation, parts of words) that the model can generate in a single response.
9
+ * It helps to control the length of the output.
10
+ *
9
11
  * Does nothing if the model does not support this setting.
12
+ *
13
+ * Example: `maxGenerationTokens: 1000`
10
14
  */
11
- maxCompletionTokens?: number | undefined;
15
+ maxGenerationTokens?: number | undefined;
12
16
  /**
13
- * Stop sequences to use. Stop sequences are not included in the generated text.
17
+ * Stop sequences to use.
18
+ * Stop sequences are an array of strings or a single string that the model will recognize as end-of-text indicators.
19
+ * The model stops generating more content when it encounters any of these strings.
20
+ * This is particularly useful in scripted or formatted text generation, where a specific end point is required.
21
+ * Stop sequences not included in the generated text.
22
+ *
14
23
  * Does nothing if the model does not support this setting.
24
+ *
25
+ * Example: `stopSequences: ['\n', 'END']`
15
26
  */
16
27
  stopSequences?: string[] | undefined;
28
+ /**
29
+ * Number of texts to generate.
30
+ *
31
+ * Specifies the number of responses or completions the model should generate for a given prompt.
32
+ * This is useful when you need multiple different outputs or ideas for a single prompt.
33
+ * The model will generate 'n' distinct responses, each based on the same initial prompt.
34
+ * In a streaming model this will result in both responses streamed back in real time.
35
+ *
36
+ * Does nothing if the model does not support this setting.
37
+ *
38
+ * Example: `numberOfGenerations: 3` // The model will produce 3 different responses.
39
+ */
40
+ numberOfGenerations?: number;
17
41
  /**
18
42
  * When true, the leading and trailing white space and line terminator characters
19
43
  * are removed from the generated text.
44
+ *
45
+ * Default: true.
20
46
  */
21
47
  trimWhitespace?: boolean;
22
48
  }
@@ -34,9 +60,9 @@ export interface TextGenerationModel<PROMPT, SETTINGS extends TextGenerationMode
34
60
  * Optional. Implement if you have a tokenizer and want to count the number of tokens in a prompt.
35
61
  */
36
62
  readonly countPromptTokens: ((prompt: PROMPT) => PromiseLike<number>) | undefined;
37
- doGenerateText(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
63
+ doGenerateTexts(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
38
64
  response: unknown;
39
- text: string;
65
+ texts: string[];
40
66
  usage?: {
41
67
  promptTokens: number;
42
68
  completionTokens: number;
@@ -9,21 +9,27 @@ async function generateText(model, prompt, options) {
9
9
  model,
10
10
  options,
11
11
  generateResponse: async (options) => {
12
- const result = await model.doGenerateText(prompt, options);
12
+ const result = await model.doGenerateTexts(prompt, options);
13
13
  const shouldTrimWhitespace = model.settings.trimWhitespace ?? true;
14
+ const texts = shouldTrimWhitespace
15
+ ? result.texts.map((text) => text.trim())
16
+ : result.texts;
14
17
  return {
15
18
  response: result.response,
16
- extractedValue: shouldTrimWhitespace ? result.text.trim() : result.text,
19
+ extractedValue: texts,
17
20
  usage: result.usage,
18
21
  };
19
22
  },
20
23
  });
24
+ const texts = fullResponse.value;
25
+ const text = texts[0];
21
26
  return options?.fullResponse
22
27
  ? {
23
- text: fullResponse.value,
28
+ text,
29
+ texts,
24
30
  response: fullResponse.response,
25
31
  metadata: fullResponse.metadata,
26
32
  }
27
- : fullResponse.value;
33
+ : text;
28
34
  }
29
35
  exports.generateText = generateText;
@@ -29,6 +29,7 @@ export declare function generateText<PROMPT>(model: TextGenerationModel<PROMPT,
29
29
  fullResponse: true;
30
30
  }): Promise<{
31
31
  text: string;
32
+ texts: string[];
32
33
  response: unknown;
33
34
  metadata: ModelCallMetadata;
34
35
  }>;
@@ -6,20 +6,26 @@ export async function generateText(model, prompt, options) {
6
6
  model,
7
7
  options,
8
8
  generateResponse: async (options) => {
9
- const result = await model.doGenerateText(prompt, options);
9
+ const result = await model.doGenerateTexts(prompt, options);
10
10
  const shouldTrimWhitespace = model.settings.trimWhitespace ?? true;
11
+ const texts = shouldTrimWhitespace
12
+ ? result.texts.map((text) => text.trim())
13
+ : result.texts;
11
14
  return {
12
15
  response: result.response,
13
- extractedValue: shouldTrimWhitespace ? result.text.trim() : result.text,
16
+ extractedValue: texts,
14
17
  usage: result.usage,
15
18
  };
16
19
  },
17
20
  });
21
+ const texts = fullResponse.value;
22
+ const text = texts[0];
18
23
  return options?.fullResponse
19
24
  ? {
20
- text: fullResponse.value,
25
+ text,
26
+ texts,
21
27
  response: fullResponse.response,
22
28
  metadata: fullResponse.metadata,
23
29
  }
24
- : fullResponse.value;
30
+ : text;
25
31
  }
@@ -13,7 +13,7 @@ const ChatPrompt_js_1 = require("./ChatPrompt.cjs");
13
13
  * @see https://modelfusion.dev/guide/function/generate-text#limiting-the-chat-length
14
14
  */
15
15
  async function trimChatPrompt({ prompt, model, tokenLimit = model.contextWindowSize -
16
- (model.settings.maxCompletionTokens ?? model.contextWindowSize / 4), }) {
16
+ (model.settings.maxGenerationTokens ?? model.contextWindowSize / 4), }) {
17
17
  (0, ChatPrompt_js_1.validateChatPrompt)(prompt);
18
18
  let minimalPrompt = {
19
19
  system: prompt.system,
@@ -10,7 +10,7 @@ import { validateChatPrompt } from "./ChatPrompt.js";
10
10
  * @see https://modelfusion.dev/guide/function/generate-text#limiting-the-chat-length
11
11
  */
12
12
  export async function trimChatPrompt({ prompt, model, tokenLimit = model.contextWindowSize -
13
- (model.settings.maxCompletionTokens ?? model.contextWindowSize / 4), }) {
13
+ (model.settings.maxGenerationTokens ?? model.contextWindowSize / 4), }) {
14
14
  validateChatPrompt(prompt);
15
15
  let minimalPrompt = {
16
16
  system: prompt.system,
@@ -69,22 +69,38 @@ class AnthropicTextGenerationModel extends AbstractModel_js_1.AbstractModel {
69
69
  return this.settings.model;
70
70
  }
71
71
  async callAPI(prompt, options) {
72
+ const api = this.settings.api ?? new AnthropicApiConfiguration_js_1.AnthropicApiConfiguration();
73
+ const responseFormat = options.responseFormat;
74
+ const abortSignal = options.run?.abortSignal;
75
+ const userId = this.settings.userId;
72
76
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
73
77
  retry: this.settings.api?.retry,
74
78
  throttle: this.settings.api?.throttle,
75
- call: async () => callAnthropicTextGenerationAPI({
76
- ...this.settings,
77
- stopSequences: this.settings.stopSequences,
78
- maxTokens: this.settings.maxCompletionTokens,
79
- abortSignal: options.run?.abortSignal,
80
- responseFormat: options.responseFormat,
81
- prompt,
82
- }),
79
+ call: async () => {
80
+ return (0, postToApi_js_1.postJsonToApi)({
81
+ url: api.assembleUrl(`/complete`),
82
+ headers: api.headers,
83
+ body: {
84
+ model: this.settings.model,
85
+ prompt,
86
+ stream: responseFormat.stream,
87
+ max_tokens_to_sample: this.settings.maxGenerationTokens,
88
+ temperature: this.settings.temperature,
89
+ top_k: this.settings.topK,
90
+ top_p: this.settings.topP,
91
+ stop_sequences: this.settings.stopSequences,
92
+ metadata: userId != null ? { user_id: userId } : undefined,
93
+ },
94
+ failedResponseHandler: AnthropicError_js_1.failedAnthropicCallResponseHandler,
95
+ successfulResponseHandler: responseFormat.handler,
96
+ abortSignal,
97
+ });
98
+ },
83
99
  });
84
100
  }
85
101
  get settingsForEvent() {
86
102
  const eventSettingProperties = [
87
- "maxCompletionTokens",
103
+ "maxGenerationTokens",
88
104
  "stopSequences",
89
105
  "temperature",
90
106
  "topK",
@@ -93,14 +109,14 @@ class AnthropicTextGenerationModel extends AbstractModel_js_1.AbstractModel {
93
109
  ];
94
110
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
95
111
  }
96
- async doGenerateText(prompt, options) {
112
+ async doGenerateTexts(prompt, options) {
97
113
  const response = await this.callAPI(prompt, {
98
114
  ...options,
99
115
  responseFormat: exports.AnthropicTextGenerationResponseFormat.json,
100
116
  });
101
117
  return {
102
118
  response,
103
- text: response.completion,
119
+ texts: [response.completion],
104
120
  };
105
121
  }
106
122
  doStreamText(prompt, options) {
@@ -148,26 +164,6 @@ const anthropicTextGenerationResponseSchema = zod_1.z.object({
148
164
  stop_reason: zod_1.z.string(),
149
165
  model: zod_1.z.string(),
150
166
  });
151
- async function callAnthropicTextGenerationAPI({ api = new AnthropicApiConfiguration_js_1.AnthropicApiConfiguration(), abortSignal, responseFormat, model, prompt, maxTokens, stopSequences, temperature, topK, topP, userId, }) {
152
- return (0, postToApi_js_1.postJsonToApi)({
153
- url: api.assembleUrl(`/complete`),
154
- headers: api.headers,
155
- body: {
156
- model,
157
- prompt,
158
- stream: responseFormat.stream,
159
- max_tokens_to_sample: maxTokens,
160
- temperature,
161
- top_k: topK,
162
- top_p: topP,
163
- stop_sequences: stopSequences,
164
- metadata: userId != null ? { user_id: userId } : undefined,
165
- },
166
- failedResponseHandler: AnthropicError_js_1.failedAnthropicCallResponseHandler,
167
- successfulResponseHandler: responseFormat.handler,
168
- abortSignal,
169
- });
170
- }
171
167
  const anthropicTextStreamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
172
168
  completion: zod_1.z.string(),
173
169
  stop_reason: zod_1.z.string().nullable(),
@@ -49,13 +49,13 @@ export declare class AnthropicTextGenerationModel extends AbstractModel<Anthropi
49
49
  responseFormat: AnthropicTextGenerationResponseFormatType<RESPONSE>;
50
50
  } & FunctionOptions): Promise<RESPONSE>;
51
51
  get settingsForEvent(): Partial<AnthropicTextGenerationModelSettings>;
52
- doGenerateText(prompt: string, options?: FunctionOptions): Promise<{
52
+ doGenerateTexts(prompt: string, options?: FunctionOptions): Promise<{
53
53
  response: {
54
54
  model: string;
55
55
  completion: string;
56
56
  stop_reason: string;
57
57
  };
58
- text: string;
58
+ texts: string[];
59
59
  }>;
60
60
  doStreamText(prompt: string, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
61
61
  /**
@@ -66,22 +66,38 @@ export class AnthropicTextGenerationModel extends AbstractModel {
66
66
  return this.settings.model;
67
67
  }
68
68
  async callAPI(prompt, options) {
69
+ const api = this.settings.api ?? new AnthropicApiConfiguration();
70
+ const responseFormat = options.responseFormat;
71
+ const abortSignal = options.run?.abortSignal;
72
+ const userId = this.settings.userId;
69
73
  return callWithRetryAndThrottle({
70
74
  retry: this.settings.api?.retry,
71
75
  throttle: this.settings.api?.throttle,
72
- call: async () => callAnthropicTextGenerationAPI({
73
- ...this.settings,
74
- stopSequences: this.settings.stopSequences,
75
- maxTokens: this.settings.maxCompletionTokens,
76
- abortSignal: options.run?.abortSignal,
77
- responseFormat: options.responseFormat,
78
- prompt,
79
- }),
76
+ call: async () => {
77
+ return postJsonToApi({
78
+ url: api.assembleUrl(`/complete`),
79
+ headers: api.headers,
80
+ body: {
81
+ model: this.settings.model,
82
+ prompt,
83
+ stream: responseFormat.stream,
84
+ max_tokens_to_sample: this.settings.maxGenerationTokens,
85
+ temperature: this.settings.temperature,
86
+ top_k: this.settings.topK,
87
+ top_p: this.settings.topP,
88
+ stop_sequences: this.settings.stopSequences,
89
+ metadata: userId != null ? { user_id: userId } : undefined,
90
+ },
91
+ failedResponseHandler: failedAnthropicCallResponseHandler,
92
+ successfulResponseHandler: responseFormat.handler,
93
+ abortSignal,
94
+ });
95
+ },
80
96
  });
81
97
  }
82
98
  get settingsForEvent() {
83
99
  const eventSettingProperties = [
84
- "maxCompletionTokens",
100
+ "maxGenerationTokens",
85
101
  "stopSequences",
86
102
  "temperature",
87
103
  "topK",
@@ -90,14 +106,14 @@ export class AnthropicTextGenerationModel extends AbstractModel {
90
106
  ];
91
107
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
92
108
  }
93
- async doGenerateText(prompt, options) {
109
+ async doGenerateTexts(prompt, options) {
94
110
  const response = await this.callAPI(prompt, {
95
111
  ...options,
96
112
  responseFormat: AnthropicTextGenerationResponseFormat.json,
97
113
  });
98
114
  return {
99
115
  response,
100
- text: response.completion,
116
+ texts: [response.completion],
101
117
  };
102
118
  }
103
119
  doStreamText(prompt, options) {
@@ -144,26 +160,6 @@ const anthropicTextGenerationResponseSchema = z.object({
144
160
  stop_reason: z.string(),
145
161
  model: z.string(),
146
162
  });
147
- async function callAnthropicTextGenerationAPI({ api = new AnthropicApiConfiguration(), abortSignal, responseFormat, model, prompt, maxTokens, stopSequences, temperature, topK, topP, userId, }) {
148
- return postJsonToApi({
149
- url: api.assembleUrl(`/complete`),
150
- headers: api.headers,
151
- body: {
152
- model,
153
- prompt,
154
- stream: responseFormat.stream,
155
- max_tokens_to_sample: maxTokens,
156
- temperature,
157
- top_k: topK,
158
- top_p: topP,
159
- stop_sequences: stopSequences,
160
- metadata: userId != null ? { user_id: userId } : undefined,
161
- },
162
- failedResponseHandler: failedAnthropicCallResponseHandler,
163
- successfulResponseHandler: responseFormat.handler,
164
- abortSignal,
165
- });
166
- }
167
163
  const anthropicTextStreamingResponseSchema = new ZodSchema(z.object({
168
164
  completion: z.string(),
169
165
  stop_reason: z.string().nullable(),
@@ -13,7 +13,7 @@ const CohereTokenizer_js_1 = require("./CohereTokenizer.cjs");
13
13
  * const model = cohere.TextGenerator({
14
14
  * model: "command-nightly",
15
15
  * temperature: 0.7,
16
- * maxCompletionTokens: 500,
16
+ * maxGenerationTokens: 500,
17
17
  * });
18
18
  *
19
19
  * const text = await generateText(
@@ -10,7 +10,7 @@ import { CohereTokenizer, CohereTokenizerSettings } from "./CohereTokenizer.js";
10
10
  * const model = cohere.TextGenerator({
11
11
  * model: "command-nightly",
12
12
  * temperature: 0.7,
13
- * maxCompletionTokens: 500,
13
+ * maxGenerationTokens: 500,
14
14
  * });
15
15
  *
16
16
  * const text = await generateText(
@@ -10,7 +10,7 @@ import { CohereTokenizer } from "./CohereTokenizer.js";
10
10
  * const model = cohere.TextGenerator({
11
11
  * model: "command-nightly",
12
12
  * temperature: 0.7,
13
- * maxCompletionTokens: 500,
13
+ * maxGenerationTokens: 500,
14
14
  * });
15
15
  *
16
16
  * const text = await generateText(
@@ -74,6 +74,7 @@ export declare class CohereTextEmbeddingModel extends AbstractModel<CohereTextEm
74
74
  get settingsForEvent(): Partial<CohereTextEmbeddingModelSettings>;
75
75
  doEmbedValues(texts: string[], options?: FunctionOptions): Promise<{
76
76
  response: {
77
+ texts: string[];
77
78
  embeddings: number[][];
78
79
  id: string;
79
80
  meta: {
@@ -81,7 +82,6 @@ export declare class CohereTextEmbeddingModel extends AbstractModel<CohereTextEm
81
82
  version: string;
82
83
  };
83
84
  };
84
- texts: string[];
85
85
  };
86
86
  embeddings: number[][];
87
87
  }>;
@@ -109,6 +109,7 @@ declare const cohereTextEmbeddingResponseSchema: z.ZodObject<{
109
109
  };
110
110
  }>;
111
111
  }, "strip", z.ZodTypeAny, {
112
+ texts: string[];
112
113
  embeddings: number[][];
113
114
  id: string;
114
115
  meta: {
@@ -116,8 +117,8 @@ declare const cohereTextEmbeddingResponseSchema: z.ZodObject<{
116
117
  version: string;
117
118
  };
118
119
  };
119
- texts: string[];
120
120
  }, {
121
+ texts: string[];
121
122
  embeddings: number[][];
122
123
  id: string;
123
124
  meta: {
@@ -125,7 +126,6 @@ declare const cohereTextEmbeddingResponseSchema: z.ZodObject<{
125
126
  version: string;
126
127
  };
127
128
  };
128
- texts: string[];
129
129
  }>;
130
130
  export type CohereTextEmbeddingResponse = z.infer<typeof cohereTextEmbeddingResponseSchema>;
131
131
  export {};
@@ -37,7 +37,7 @@ exports.COHERE_TEXT_GENERATION_MODELS = {
37
37
  * const model = new CohereTextGenerationModel({
38
38
  * model: "command-nightly",
39
39
  * temperature: 0.7,
40
- * maxCompletionTokens: 500,
40
+ * maxGenerationTokens: 500,
41
41
  * });
42
42
  *
43
43
  * const text = await generateText(
@@ -80,28 +80,45 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
80
80
  return (0, countTokens_js_1.countTokens)(this.tokenizer, input);
81
81
  }
82
82
  async callAPI(prompt, options) {
83
+ const api = this.settings.api ?? new CohereApiConfiguration_js_1.CohereApiConfiguration();
84
+ const responseFormat = options.responseFormat;
85
+ const abortSignal = options.run?.abortSignal;
83
86
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
84
87
  retry: this.settings.api?.retry,
85
88
  throttle: this.settings.api?.throttle,
86
- call: async () => callCohereTextGenerationAPI({
87
- ...this.settings,
88
- // use endSequences instead of stopSequences
89
- // to exclude stop tokens from the generated text
90
- endSequences: this.settings.stopSequences,
91
- maxTokens: this.settings.maxCompletionTokens,
92
- // mapped name because of conflict with stopSequences:
93
- stopSequences: this.settings.cohereStopSequences,
94
- abortSignal: options.run?.abortSignal,
95
- responseFormat: options.responseFormat,
96
- prompt,
97
- }),
89
+ call: async () => {
90
+ return (0, postToApi_js_1.postJsonToApi)({
91
+ url: api.assembleUrl(`/generate`),
92
+ headers: api.headers,
93
+ body: {
94
+ stream: responseFormat.stream,
95
+ model: this.settings.model,
96
+ prompt,
97
+ num_generations: this.settings.numberOfGenerations,
98
+ max_tokens: this.settings.maxGenerationTokens,
99
+ temperature: this.settings.temperature,
100
+ k: this.settings.k,
101
+ p: this.settings.p,
102
+ frequency_penalty: this.settings.frequencyPenalty,
103
+ presence_penalty: this.settings.presencePenalty,
104
+ end_sequences: this.settings.stopSequences,
105
+ stop_sequences: this.settings.cohereStopSequences,
106
+ return_likelihoods: this.settings.returnLikelihoods,
107
+ logit_bias: this.settings.logitBias,
108
+ truncate: this.settings.truncate,
109
+ },
110
+ failedResponseHandler: CohereError_js_1.failedCohereCallResponseHandler,
111
+ successfulResponseHandler: responseFormat.handler,
112
+ abortSignal,
113
+ });
114
+ },
98
115
  });
99
116
  }
100
117
  get settingsForEvent() {
101
118
  const eventSettingProperties = [
102
- "maxCompletionTokens",
119
+ "maxGenerationTokens",
103
120
  "stopSequences",
104
- "numGenerations",
121
+ "numberOfGenerations",
105
122
  "temperature",
106
123
  "k",
107
124
  "p",
@@ -114,14 +131,14 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
114
131
  ];
115
132
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
116
133
  }
117
- async doGenerateText(prompt, options) {
134
+ async doGenerateTexts(prompt, options) {
118
135
  const response = await this.callAPI(prompt, {
119
136
  ...options,
120
137
  responseFormat: exports.CohereTextGenerationResponseFormat.json,
121
138
  });
122
139
  return {
123
140
  response,
124
- text: response.generations[0].text,
141
+ texts: response.generations.map((generation) => generation.text),
125
142
  };
126
143
  }
127
144
  doStreamText(prompt, options) {
@@ -177,32 +194,6 @@ const cohereTextGenerationResponseSchema = zod_1.z.object({
177
194
  })
178
195
  .optional(),
179
196
  });
180
- async function callCohereTextGenerationAPI({ api = new CohereApiConfiguration_js_1.CohereApiConfiguration(), abortSignal, responseFormat, model, prompt, numGenerations, maxTokens, temperature, k, p, frequencyPenalty, presencePenalty, endSequences, stopSequences, returnLikelihoods, logitBias, truncate, }) {
181
- return (0, postToApi_js_1.postJsonToApi)({
182
- url: api.assembleUrl(`/generate`),
183
- headers: api.headers,
184
- body: {
185
- stream: responseFormat.stream,
186
- model,
187
- prompt,
188
- num_generations: numGenerations,
189
- max_tokens: maxTokens,
190
- temperature,
191
- k,
192
- p,
193
- frequency_penalty: frequencyPenalty,
194
- presence_penalty: presencePenalty,
195
- end_sequences: endSequences,
196
- stop_sequences: stopSequences,
197
- return_likelihoods: returnLikelihoods,
198
- logit_bias: logitBias,
199
- truncate,
200
- },
201
- failedResponseHandler: CohereError_js_1.failedCohereCallResponseHandler,
202
- successfulResponseHandler: responseFormat.handler,
203
- abortSignal,
204
- });
205
- }
206
197
  const cohereTextStreamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discriminatedUnion("is_finished", [
207
198
  zod_1.z.object({
208
199
  text: zod_1.z.string(),