modelfusion 0.102.0 → 0.103.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.
- package/CHANGELOG.md +20 -0
- package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +1 -1
- package/model-function/generate-text/TextGenerationModel.cjs +7 -0
- package/model-function/generate-text/TextGenerationModel.d.ts +3 -1
- package/model-function/generate-text/TextGenerationModel.js +6 -1
- package/model-function/generate-text/TextGenerationResult.cjs +2 -0
- package/model-function/generate-text/TextGenerationResult.d.ts +11 -0
- package/model-function/generate-text/TextGenerationResult.js +1 -0
- package/model-function/generate-text/generateText.cjs +14 -9
- package/model-function/generate-text/generateText.d.ts +3 -0
- package/model-function/generate-text/generateText.js +14 -9
- package/model-function/generate-text/index.cjs +1 -0
- package/model-function/generate-text/index.d.ts +1 -0
- package/model-function/generate-text/index.js +1 -0
- package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +23 -8
- package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +6 -1
- package/model-provider/anthropic/AnthropicTextGenerationModel.js +24 -9
- package/model-provider/cohere/CohereTextGenerationModel.cjs +22 -6
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +6 -1
- package/model-provider/cohere/CohereTextGenerationModel.js +22 -6
- package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +2 -2
- package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +2 -2
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +9 -8
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +4 -5
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +9 -8
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +51 -51
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +14 -11
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +51 -51
- package/model-provider/mistral/MistralChatModel.cjs +19 -2
- package/model-provider/mistral/MistralChatModel.d.ts +6 -1
- package/model-provider/mistral/MistralChatModel.js +19 -2
- package/model-provider/ollama/OllamaChatModel.cjs +8 -3
- package/model-provider/ollama/OllamaChatModel.d.ts +4 -1
- package/model-provider/ollama/OllamaChatModel.js +8 -3
- package/model-provider/ollama/OllamaCompletionModel.cjs +8 -3
- package/model-provider/ollama/OllamaCompletionModel.d.ts +4 -1
- package/model-provider/ollama/OllamaCompletionModel.js +8 -3
- package/model-provider/openai/OpenAICompletionModel.cjs +20 -4
- package/model-provider/openai/OpenAICompletionModel.d.ts +6 -1
- package/model-provider/openai/OpenAICompletionModel.js +20 -4
- package/model-provider/openai/chat/AbstractOpenAIChatModel.cjs +19 -1
- package/model-provider/openai/chat/AbstractOpenAIChatModel.d.ts +6 -1
- package/model-provider/openai/chat/AbstractOpenAIChatModel.js +19 -1
- package/model-provider/openai/chat/OpenAIChatModel.cjs +2 -3
- package/model-provider/openai/chat/OpenAIChatModel.js +2 -3
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +2 -3
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +2 -3
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +5 -5
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,25 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## v0.103.0 - 2023-12-23
|
4
|
+
|
5
|
+
### Added
|
6
|
+
|
7
|
+
- `finishReason` support for `generateText`.
|
8
|
+
|
9
|
+
The finish reason can be `stop` (the model stopped because it generated a stop sequence), `length` (the model stopped because it generated the maximum number of tokens), `content-filter` (the model stopped because the content filter detected a violation), `tool-calls` (the model stopped because it triggered a tool call), `error` (the model stopped because of an error), `other` (the model stopped for another reason), or `unknown` (the model stop reason is not know or the model does not support finish reasons).
|
10
|
+
|
11
|
+
You can extract it from the full response when using `fullResponse: true`:
|
12
|
+
|
13
|
+
```ts
|
14
|
+
const { text, finishReason } = await generateText(
|
15
|
+
openai
|
16
|
+
.ChatTextGenerator({ model: "gpt-3.5-turbo", maxGenerationTokens: 200 })
|
17
|
+
.withTextPrompt(),
|
18
|
+
"Write a short story about a robot learning to love:",
|
19
|
+
{ fullResponse: true }
|
20
|
+
);
|
21
|
+
```
|
22
|
+
|
3
23
|
## v0.102.0 - 2023-12-22
|
4
24
|
|
5
25
|
### Added
|
@@ -19,7 +19,7 @@ export declare class PromptTemplateTextGenerationModel<PROMPT, MODEL_PROMPT, SET
|
|
19
19
|
get countPromptTokens(): MODEL["countPromptTokens"] extends undefined ? undefined : (prompt: PROMPT) => PromiseLike<number>;
|
20
20
|
doGenerateTexts(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
21
21
|
response: unknown;
|
22
|
-
|
22
|
+
textGenerationResults: import("./TextGenerationResult.js").TextGenerationResult[];
|
23
23
|
usage?: {
|
24
24
|
promptTokens: number;
|
25
25
|
completionTokens: number;
|
@@ -1,2 +1,9 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.textGenerationModelProperties = void 0;
|
4
|
+
exports.textGenerationModelProperties = [
|
5
|
+
"maxGenerationTokens",
|
6
|
+
"stopSequences",
|
7
|
+
"numberOfGenerations",
|
8
|
+
"trimWhitespace",
|
9
|
+
];
|
@@ -3,6 +3,8 @@ import { Delta } from "../Delta.js";
|
|
3
3
|
import { Model, ModelSettings } from "../Model.js";
|
4
4
|
import { BasicTokenizer, FullTokenizer } from "../tokenize-text/Tokenizer.js";
|
5
5
|
import { TextGenerationPromptTemplate } from "./TextGenerationPromptTemplate.js";
|
6
|
+
import { TextGenerationResult } from "./TextGenerationResult.js";
|
7
|
+
export declare const textGenerationModelProperties: readonly ["maxGenerationTokens", "stopSequences", "numberOfGenerations", "trimWhitespace"];
|
6
8
|
export interface TextGenerationModelSettings extends ModelSettings {
|
7
9
|
/**
|
8
10
|
* Specifies the maximum number of tokens (words, punctuation, parts of words) that the model can generate in a single response.
|
@@ -62,7 +64,7 @@ export interface TextGenerationModel<PROMPT, SETTINGS extends TextGenerationMode
|
|
62
64
|
readonly countPromptTokens: ((prompt: PROMPT) => PromiseLike<number>) | undefined;
|
63
65
|
doGenerateTexts(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
64
66
|
response: unknown;
|
65
|
-
|
67
|
+
textGenerationResults: TextGenerationResult[];
|
66
68
|
usage?: {
|
67
69
|
promptTokens: number;
|
68
70
|
completionTokens: number;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
export type TextGenerationResult = {
|
2
|
+
/**
|
3
|
+
* The generated text.
|
4
|
+
*/
|
5
|
+
text: string;
|
6
|
+
/**
|
7
|
+
* The reason why the generation stopped.
|
8
|
+
*/
|
9
|
+
finishReason: TextGenerationFinishReason;
|
10
|
+
};
|
11
|
+
export type TextGenerationFinishReason = "stop" | "length" | "content-filter" | "tool-calls" | "error" | "other" | "unknown";
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -11,25 +11,30 @@ async function generateText(model, prompt, options) {
|
|
11
11
|
generateResponse: async (options) => {
|
12
12
|
const result = await model.doGenerateTexts(prompt, options);
|
13
13
|
const shouldTrimWhitespace = model.settings.trimWhitespace ?? true;
|
14
|
-
const
|
15
|
-
? result.
|
16
|
-
|
14
|
+
const textGenerationResults = shouldTrimWhitespace
|
15
|
+
? result.textGenerationResults.map((textGeneration) => ({
|
16
|
+
text: textGeneration.text.trim(),
|
17
|
+
finishReason: textGeneration.finishReason,
|
18
|
+
}))
|
19
|
+
: result.textGenerationResults;
|
17
20
|
return {
|
18
21
|
response: result.response,
|
19
|
-
extractedValue:
|
22
|
+
extractedValue: textGenerationResults,
|
20
23
|
usage: result.usage,
|
21
24
|
};
|
22
25
|
},
|
23
26
|
});
|
24
|
-
const
|
25
|
-
const
|
27
|
+
const textGenerationResults = fullResponse.value;
|
28
|
+
const firstResult = textGenerationResults[0];
|
26
29
|
return options?.fullResponse
|
27
30
|
? {
|
28
|
-
text,
|
29
|
-
|
31
|
+
text: firstResult.text,
|
32
|
+
finishReason: firstResult.finishReason,
|
33
|
+
texts: textGenerationResults.map((textGeneration) => textGeneration.text),
|
34
|
+
textGenerationResults,
|
30
35
|
response: fullResponse.response,
|
31
36
|
metadata: fullResponse.metadata,
|
32
37
|
}
|
33
|
-
: text;
|
38
|
+
: firstResult.text;
|
34
39
|
}
|
35
40
|
exports.generateText = generateText;
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
2
|
import { ModelCallMetadata } from "../ModelCallMetadata.js";
|
3
3
|
import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
|
4
|
+
import { TextGenerationFinishReason, TextGenerationResult } from "./TextGenerationResult.js";
|
4
5
|
/**
|
5
6
|
* Generate text for a prompt and return it as a string.
|
6
7
|
*
|
@@ -29,7 +30,9 @@ export declare function generateText<PROMPT>(model: TextGenerationModel<PROMPT,
|
|
29
30
|
fullResponse: true;
|
30
31
|
}): Promise<{
|
31
32
|
text: string;
|
33
|
+
finishReason: TextGenerationFinishReason;
|
32
34
|
texts: string[];
|
35
|
+
textGenerationResults: TextGenerationResult[];
|
33
36
|
response: unknown;
|
34
37
|
metadata: ModelCallMetadata;
|
35
38
|
}>;
|
@@ -8,24 +8,29 @@ export async function generateText(model, prompt, options) {
|
|
8
8
|
generateResponse: async (options) => {
|
9
9
|
const result = await model.doGenerateTexts(prompt, options);
|
10
10
|
const shouldTrimWhitespace = model.settings.trimWhitespace ?? true;
|
11
|
-
const
|
12
|
-
? result.
|
13
|
-
|
11
|
+
const textGenerationResults = shouldTrimWhitespace
|
12
|
+
? result.textGenerationResults.map((textGeneration) => ({
|
13
|
+
text: textGeneration.text.trim(),
|
14
|
+
finishReason: textGeneration.finishReason,
|
15
|
+
}))
|
16
|
+
: result.textGenerationResults;
|
14
17
|
return {
|
15
18
|
response: result.response,
|
16
|
-
extractedValue:
|
19
|
+
extractedValue: textGenerationResults,
|
17
20
|
usage: result.usage,
|
18
21
|
};
|
19
22
|
},
|
20
23
|
});
|
21
|
-
const
|
22
|
-
const
|
24
|
+
const textGenerationResults = fullResponse.value;
|
25
|
+
const firstResult = textGenerationResults[0];
|
23
26
|
return options?.fullResponse
|
24
27
|
? {
|
25
|
-
text,
|
26
|
-
|
28
|
+
text: firstResult.text,
|
29
|
+
finishReason: firstResult.finishReason,
|
30
|
+
texts: textGenerationResults.map((textGeneration) => textGeneration.text),
|
31
|
+
textGenerationResults,
|
27
32
|
response: fullResponse.response,
|
28
33
|
metadata: fullResponse.metadata,
|
29
34
|
}
|
30
|
-
: text;
|
35
|
+
: firstResult.text;
|
31
36
|
}
|
@@ -19,6 +19,7 @@ __exportStar(require("./PromptTemplateTextStreamingModel.cjs"), exports);
|
|
19
19
|
__exportStar(require("./TextGenerationEvent.cjs"), exports);
|
20
20
|
__exportStar(require("./TextGenerationModel.cjs"), exports);
|
21
21
|
__exportStar(require("./TextGenerationPromptTemplate.cjs"), exports);
|
22
|
+
__exportStar(require("./TextGenerationResult.cjs"), exports);
|
22
23
|
__exportStar(require("./generateText.cjs"), exports);
|
23
24
|
__exportStar(require("./prompt-template/index.cjs"), exports);
|
24
25
|
__exportStar(require("./streamText.cjs"), exports);
|
@@ -3,6 +3,7 @@ export * from "./PromptTemplateTextStreamingModel.js";
|
|
3
3
|
export * from "./TextGenerationEvent.js";
|
4
4
|
export * from "./TextGenerationModel.js";
|
5
5
|
export * from "./TextGenerationPromptTemplate.js";
|
6
|
+
export * from "./TextGenerationResult.js";
|
6
7
|
export * from "./generateText.js";
|
7
8
|
export * from "./prompt-template/index.js";
|
8
9
|
export * from "./streamText.js";
|
@@ -3,6 +3,7 @@ export * from "./PromptTemplateTextStreamingModel.js";
|
|
3
3
|
export * from "./TextGenerationEvent.js";
|
4
4
|
export * from "./TextGenerationModel.js";
|
5
5
|
export * from "./TextGenerationPromptTemplate.js";
|
6
|
+
export * from "./TextGenerationResult.js";
|
6
7
|
export * from "./generateText.js";
|
7
8
|
export * from "./prompt-template/index.js";
|
8
9
|
export * from "./streamText.js";
|
@@ -4,12 +4,13 @@ exports.AnthropicTextGenerationResponseFormat = exports.AnthropicTextGenerationM
|
|
4
4
|
const zod_1 = require("zod");
|
5
5
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
6
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
7
|
-
const
|
7
|
+
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
8
|
+
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
8
9
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
9
10
|
const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextStreamingModel.cjs");
|
11
|
+
const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
|
10
12
|
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
11
|
-
const
|
12
|
-
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
13
|
+
const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSourceStream.cjs");
|
13
14
|
const AnthropicApiConfiguration_js_1 = require("./AnthropicApiConfiguration.cjs");
|
14
15
|
const AnthropicError_js_1 = require("./AnthropicError.cjs");
|
15
16
|
const AnthropicPromptTemplate_js_1 = require("./AnthropicPromptTemplate.cjs");
|
@@ -74,8 +75,8 @@ class AnthropicTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
74
75
|
const abortSignal = options.run?.abortSignal;
|
75
76
|
const userId = this.settings.userId;
|
76
77
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
77
|
-
retry:
|
78
|
-
throttle:
|
78
|
+
retry: api.retry,
|
79
|
+
throttle: api.throttle,
|
79
80
|
call: async () => {
|
80
81
|
return (0, postToApi_js_1.postJsonToApi)({
|
81
82
|
url: api.assembleUrl(`/complete`),
|
@@ -100,8 +101,7 @@ class AnthropicTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
100
101
|
}
|
101
102
|
get settingsForEvent() {
|
102
103
|
const eventSettingProperties = [
|
103
|
-
|
104
|
-
"stopSequences",
|
104
|
+
...TextGenerationModel_js_1.textGenerationModelProperties,
|
105
105
|
"temperature",
|
106
106
|
"topK",
|
107
107
|
"topP",
|
@@ -116,9 +116,24 @@ class AnthropicTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
116
116
|
});
|
117
117
|
return {
|
118
118
|
response,
|
119
|
-
|
119
|
+
textGenerationResults: [
|
120
|
+
{
|
121
|
+
text: response.completion,
|
122
|
+
finishReason: this.translateFinishReason(response.stop_reason),
|
123
|
+
},
|
124
|
+
],
|
120
125
|
};
|
121
126
|
}
|
127
|
+
translateFinishReason(finishReason) {
|
128
|
+
switch (finishReason) {
|
129
|
+
case "stop_sequence":
|
130
|
+
return "stop";
|
131
|
+
case "max_tokens":
|
132
|
+
return "length";
|
133
|
+
default:
|
134
|
+
return "unknown";
|
135
|
+
}
|
136
|
+
}
|
122
137
|
doStreamText(prompt, options) {
|
123
138
|
return this.callAPI(prompt, {
|
124
139
|
...options,
|
@@ -7,6 +7,7 @@ import { Delta } from "../../model-function/Delta.js";
|
|
7
7
|
import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
|
8
8
|
import { TextGenerationModelSettings, TextStreamingModel } from "../../model-function/generate-text/TextGenerationModel.js";
|
9
9
|
import { TextGenerationPromptTemplate } from "../../model-function/generate-text/TextGenerationPromptTemplate.js";
|
10
|
+
import { TextGenerationFinishReason } from "../../model-function/generate-text/TextGenerationResult.js";
|
10
11
|
export declare const ANTHROPIC_TEXT_GENERATION_MODELS: {
|
11
12
|
"claude-instant-1": {
|
12
13
|
contextWindowSize: number;
|
@@ -55,8 +56,12 @@ export declare class AnthropicTextGenerationModel extends AbstractModel<Anthropi
|
|
55
56
|
completion: string;
|
56
57
|
stop_reason: string;
|
57
58
|
};
|
58
|
-
|
59
|
+
textGenerationResults: {
|
60
|
+
text: string;
|
61
|
+
finishReason: TextGenerationFinishReason;
|
62
|
+
}[];
|
59
63
|
}>;
|
64
|
+
private translateFinishReason;
|
60
65
|
doStreamText(prompt: string, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
|
61
66
|
/**
|
62
67
|
* Returns this model with a text prompt template.
|
@@ -1,15 +1,16 @@
|
|
1
1
|
import { z } from "zod";
|
2
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
|
-
import {
|
4
|
+
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
5
|
+
import { parseJSON } from "../../core/schema/parseJSON.js";
|
5
6
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
6
7
|
import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
|
8
|
+
import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
|
7
9
|
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
8
|
-
import {
|
9
|
-
import { parseJSON } from "../../core/schema/parseJSON.js";
|
10
|
+
import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
|
10
11
|
import { AnthropicApiConfiguration } from "./AnthropicApiConfiguration.js";
|
11
12
|
import { failedAnthropicCallResponseHandler } from "./AnthropicError.js";
|
12
|
-
import {
|
13
|
+
import { chat, instruction, text } from "./AnthropicPromptTemplate.js";
|
13
14
|
export const ANTHROPIC_TEXT_GENERATION_MODELS = {
|
14
15
|
"claude-instant-1": {
|
15
16
|
contextWindowSize: 100000,
|
@@ -71,8 +72,8 @@ export class AnthropicTextGenerationModel extends AbstractModel {
|
|
71
72
|
const abortSignal = options.run?.abortSignal;
|
72
73
|
const userId = this.settings.userId;
|
73
74
|
return callWithRetryAndThrottle({
|
74
|
-
retry:
|
75
|
-
throttle:
|
75
|
+
retry: api.retry,
|
76
|
+
throttle: api.throttle,
|
76
77
|
call: async () => {
|
77
78
|
return postJsonToApi({
|
78
79
|
url: api.assembleUrl(`/complete`),
|
@@ -97,8 +98,7 @@ export class AnthropicTextGenerationModel extends AbstractModel {
|
|
97
98
|
}
|
98
99
|
get settingsForEvent() {
|
99
100
|
const eventSettingProperties = [
|
100
|
-
|
101
|
-
"stopSequences",
|
101
|
+
...textGenerationModelProperties,
|
102
102
|
"temperature",
|
103
103
|
"topK",
|
104
104
|
"topP",
|
@@ -113,9 +113,24 @@ export class AnthropicTextGenerationModel extends AbstractModel {
|
|
113
113
|
});
|
114
114
|
return {
|
115
115
|
response,
|
116
|
-
|
116
|
+
textGenerationResults: [
|
117
|
+
{
|
118
|
+
text: response.completion,
|
119
|
+
finishReason: this.translateFinishReason(response.stop_reason),
|
120
|
+
},
|
121
|
+
],
|
117
122
|
};
|
118
123
|
}
|
124
|
+
translateFinishReason(finishReason) {
|
125
|
+
switch (finishReason) {
|
126
|
+
case "stop_sequence":
|
127
|
+
return "stop";
|
128
|
+
case "max_tokens":
|
129
|
+
return "length";
|
130
|
+
default:
|
131
|
+
return "unknown";
|
132
|
+
}
|
133
|
+
}
|
119
134
|
doStreamText(prompt, options) {
|
120
135
|
return this.callAPI(prompt, {
|
121
136
|
...options,
|
@@ -7,6 +7,7 @@ const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
|
7
7
|
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
8
8
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
9
9
|
const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextStreamingModel.cjs");
|
10
|
+
const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
|
10
11
|
const TextPromptTemplate_js_1 = require("../../model-function/generate-text/prompt-template/TextPromptTemplate.cjs");
|
11
12
|
const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
|
12
13
|
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
@@ -84,8 +85,8 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
84
85
|
const responseFormat = options.responseFormat;
|
85
86
|
const abortSignal = options.run?.abortSignal;
|
86
87
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
87
|
-
retry:
|
88
|
-
throttle:
|
88
|
+
retry: api.retry,
|
89
|
+
throttle: api.throttle,
|
89
90
|
call: async () => {
|
90
91
|
return (0, postToApi_js_1.postJsonToApi)({
|
91
92
|
url: api.assembleUrl(`/generate`),
|
@@ -116,9 +117,7 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
116
117
|
}
|
117
118
|
get settingsForEvent() {
|
118
119
|
const eventSettingProperties = [
|
119
|
-
|
120
|
-
"stopSequences",
|
121
|
-
"numberOfGenerations",
|
120
|
+
...TextGenerationModel_js_1.textGenerationModelProperties,
|
122
121
|
"temperature",
|
123
122
|
"k",
|
124
123
|
"p",
|
@@ -138,9 +137,26 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
138
137
|
});
|
139
138
|
return {
|
140
139
|
response,
|
141
|
-
|
140
|
+
textGenerationResults: response.generations.map((generation) => ({
|
141
|
+
text: generation.text,
|
142
|
+
finishReason: this.translateFinishReason(generation.finish_reason),
|
143
|
+
})),
|
142
144
|
};
|
143
145
|
}
|
146
|
+
translateFinishReason(finishReason) {
|
147
|
+
switch (finishReason) {
|
148
|
+
case "COMPLETE":
|
149
|
+
return "stop";
|
150
|
+
case "MAX_TOKENS":
|
151
|
+
return "length";
|
152
|
+
case "ERROR_TOXIC":
|
153
|
+
return "content-filter";
|
154
|
+
case "ERROR":
|
155
|
+
return "error";
|
156
|
+
default:
|
157
|
+
return "unknown";
|
158
|
+
}
|
159
|
+
}
|
144
160
|
doStreamText(prompt, options) {
|
145
161
|
return this.callAPI(prompt, {
|
146
162
|
...options,
|
@@ -7,6 +7,7 @@ import { Delta } from "../../model-function/Delta.js";
|
|
7
7
|
import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
|
8
8
|
import { TextGenerationModelSettings, TextStreamingModel } from "../../model-function/generate-text/TextGenerationModel.js";
|
9
9
|
import { TextGenerationPromptTemplate } from "../../model-function/generate-text/TextGenerationPromptTemplate.js";
|
10
|
+
import { TextGenerationFinishReason } from "../../model-function/generate-text/TextGenerationResult.js";
|
10
11
|
import { CohereTokenizer } from "./CohereTokenizer.js";
|
11
12
|
export declare const COHERE_TEXT_GENERATION_MODELS: {
|
12
13
|
command: {
|
@@ -79,8 +80,12 @@ export declare class CohereTextGenerationModel extends AbstractModel<CohereTextG
|
|
79
80
|
};
|
80
81
|
} | undefined;
|
81
82
|
};
|
82
|
-
|
83
|
+
textGenerationResults: {
|
84
|
+
text: string;
|
85
|
+
finishReason: TextGenerationFinishReason;
|
86
|
+
}[];
|
83
87
|
}>;
|
88
|
+
private translateFinishReason;
|
84
89
|
doStreamText(prompt: string, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
|
85
90
|
extractTextDelta(fullDelta: CohereTextGenerationDelta): string | undefined;
|
86
91
|
/**
|
@@ -4,6 +4,7 @@ import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postTo
|
|
4
4
|
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
5
5
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
6
6
|
import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
|
7
|
+
import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
|
7
8
|
import { chat, instruction, } from "../../model-function/generate-text/prompt-template/TextPromptTemplate.js";
|
8
9
|
import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
|
9
10
|
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
@@ -81,8 +82,8 @@ export class CohereTextGenerationModel extends AbstractModel {
|
|
81
82
|
const responseFormat = options.responseFormat;
|
82
83
|
const abortSignal = options.run?.abortSignal;
|
83
84
|
return callWithRetryAndThrottle({
|
84
|
-
retry:
|
85
|
-
throttle:
|
85
|
+
retry: api.retry,
|
86
|
+
throttle: api.throttle,
|
86
87
|
call: async () => {
|
87
88
|
return postJsonToApi({
|
88
89
|
url: api.assembleUrl(`/generate`),
|
@@ -113,9 +114,7 @@ export class CohereTextGenerationModel extends AbstractModel {
|
|
113
114
|
}
|
114
115
|
get settingsForEvent() {
|
115
116
|
const eventSettingProperties = [
|
116
|
-
|
117
|
-
"stopSequences",
|
118
|
-
"numberOfGenerations",
|
117
|
+
...textGenerationModelProperties,
|
119
118
|
"temperature",
|
120
119
|
"k",
|
121
120
|
"p",
|
@@ -135,9 +134,26 @@ export class CohereTextGenerationModel extends AbstractModel {
|
|
135
134
|
});
|
136
135
|
return {
|
137
136
|
response,
|
138
|
-
|
137
|
+
textGenerationResults: response.generations.map((generation) => ({
|
138
|
+
text: generation.text,
|
139
|
+
finishReason: this.translateFinishReason(generation.finish_reason),
|
140
|
+
})),
|
139
141
|
};
|
140
142
|
}
|
143
|
+
translateFinishReason(finishReason) {
|
144
|
+
switch (finishReason) {
|
145
|
+
case "COMPLETE":
|
146
|
+
return "stop";
|
147
|
+
case "MAX_TOKENS":
|
148
|
+
return "length";
|
149
|
+
case "ERROR_TOXIC":
|
150
|
+
return "content-filter";
|
151
|
+
case "ERROR":
|
152
|
+
return "error";
|
153
|
+
default:
|
154
|
+
return "unknown";
|
155
|
+
}
|
156
|
+
}
|
141
157
|
doStreamText(prompt, options) {
|
142
158
|
return this.callAPI(prompt, {
|
143
159
|
...options,
|
@@ -101,7 +101,7 @@ class ElevenLabsSpeechModel extends AbstractModel_js_1.AbstractModel {
|
|
101
101
|
// The JS WebSocket API does not support authorization headers, so we send the API key in the BOS message.
|
102
102
|
// See https://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api
|
103
103
|
xi_api_key: api.apiKey,
|
104
|
-
text: " ",
|
104
|
+
text: " ", // first message
|
105
105
|
voice_settings: toApiVoiceSettings(this.settings.voiceSettings),
|
106
106
|
generation_config: toGenerationConfig(this.settings.generationConfig),
|
107
107
|
}));
|
@@ -125,7 +125,7 @@ class ElevenLabsSpeechModel extends AbstractModel_js_1.AbstractModel {
|
|
125
125
|
// send remaining text:
|
126
126
|
if (textBuffer.length > 0) {
|
127
127
|
socket.send(JSON.stringify({
|
128
|
-
text: `${textBuffer} `,
|
128
|
+
text: `${textBuffer} `, // append space
|
129
129
|
try_trigger_generation: true,
|
130
130
|
}));
|
131
131
|
}
|
@@ -98,7 +98,7 @@ export class ElevenLabsSpeechModel extends AbstractModel {
|
|
98
98
|
// The JS WebSocket API does not support authorization headers, so we send the API key in the BOS message.
|
99
99
|
// See https://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api
|
100
100
|
xi_api_key: api.apiKey,
|
101
|
-
text: " ",
|
101
|
+
text: " ", // first message
|
102
102
|
voice_settings: toApiVoiceSettings(this.settings.voiceSettings),
|
103
103
|
generation_config: toGenerationConfig(this.settings.generationConfig),
|
104
104
|
}));
|
@@ -122,7 +122,7 @@ export class ElevenLabsSpeechModel extends AbstractModel {
|
|
122
122
|
// send remaining text:
|
123
123
|
if (textBuffer.length > 0) {
|
124
124
|
socket.send(JSON.stringify({
|
125
|
-
text: `${textBuffer} `,
|
125
|
+
text: `${textBuffer} `, // append space
|
126
126
|
try_trigger_generation: true,
|
127
127
|
}));
|
128
128
|
}
|
@@ -6,6 +6,7 @@ const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndTh
|
|
6
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
7
7
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
8
8
|
const PromptTemplateTextGenerationModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextGenerationModel.cjs");
|
9
|
+
const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
|
9
10
|
const HuggingFaceApiConfiguration_js_1 = require("./HuggingFaceApiConfiguration.cjs");
|
10
11
|
const HuggingFaceError_js_1 = require("./HuggingFaceError.cjs");
|
11
12
|
/**
|
@@ -61,8 +62,8 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
61
62
|
const api = this.settings.api ?? new HuggingFaceApiConfiguration_js_1.HuggingFaceApiConfiguration();
|
62
63
|
const abortSignal = options?.run?.abortSignal;
|
63
64
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
64
|
-
retry:
|
65
|
-
throttle:
|
65
|
+
retry: api.retry,
|
66
|
+
throttle: api.throttle,
|
66
67
|
call: async () => {
|
67
68
|
return (0, postToApi_js_1.postJsonToApi)({
|
68
69
|
url: api.assembleUrl(`/${this.settings.model}`),
|
@@ -91,16 +92,13 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
91
92
|
}
|
92
93
|
get settingsForEvent() {
|
93
94
|
const eventSettingProperties = [
|
94
|
-
|
95
|
-
"maxGenerationTokens",
|
96
|
-
"numberOfGenerations",
|
95
|
+
...TextGenerationModel_js_1.textGenerationModelProperties,
|
97
96
|
"topK",
|
98
97
|
"topP",
|
99
98
|
"temperature",
|
100
99
|
"repetitionPenalty",
|
101
100
|
"maxTime",
|
102
101
|
"doSample",
|
103
|
-
"options",
|
104
102
|
];
|
105
103
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
106
104
|
}
|
@@ -108,12 +106,15 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
108
106
|
const response = await this.callAPI(prompt, options);
|
109
107
|
return {
|
110
108
|
response,
|
111
|
-
|
109
|
+
textGenerationResults: response.map((response) => ({
|
110
|
+
text: response.generated_text,
|
111
|
+
finishReason: "unknown",
|
112
|
+
})),
|
112
113
|
};
|
113
114
|
}
|
114
115
|
withPromptTemplate(promptTemplate) {
|
115
116
|
return new PromptTemplateTextGenerationModel_js_1.PromptTemplateTextGenerationModel({
|
116
|
-
model: this,
|
117
|
+
model: this, // stop tokens are not supported by this model
|
117
118
|
promptTemplate,
|
118
119
|
});
|
119
120
|
}
|
@@ -14,10 +14,6 @@ export interface HuggingFaceTextGenerationModelSettings extends TextGenerationMo
|
|
14
14
|
repetitionPenalty?: number;
|
15
15
|
maxTime?: number;
|
16
16
|
doSample?: boolean;
|
17
|
-
options?: {
|
18
|
-
useCache?: boolean;
|
19
|
-
waitForModel?: boolean;
|
20
|
-
};
|
21
17
|
}
|
22
18
|
/**
|
23
19
|
* Create a text generation model that calls a Hugging Face Inference API Text Generation Task.
|
@@ -50,7 +46,10 @@ export declare class HuggingFaceTextGenerationModel extends AbstractModel<Huggin
|
|
50
46
|
response: {
|
51
47
|
generated_text: string;
|
52
48
|
}[];
|
53
|
-
|
49
|
+
textGenerationResults: {
|
50
|
+
text: string;
|
51
|
+
finishReason: "unknown";
|
52
|
+
}[];
|
54
53
|
}>;
|
55
54
|
withPromptTemplate<INPUT_PROMPT>(promptTemplate: TextGenerationPromptTemplate<INPUT_PROMPT, string>): PromptTemplateTextGenerationModel<INPUT_PROMPT, string, HuggingFaceTextGenerationModelSettings, this>;
|
56
55
|
withSettings(additionalSettings: Partial<HuggingFaceTextGenerationModelSettings>): this;
|