modelfusion 0.98.0 → 0.100.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/README.md +13 -19
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs +1 -1
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js +1 -1
- package/guard/fixStructure.cjs +3 -3
- package/guard/fixStructure.d.ts +3 -3
- package/guard/fixStructure.js +3 -3
- package/model-function/Model.d.ts +2 -2
- package/model-function/generate-structure/generateStructure.d.ts +2 -2
- package/model-function/generate-structure/streamStructure.d.ts +1 -1
- package/model-function/generate-text/PromptTemplateTextGenerationModel.cjs +2 -2
- package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +2 -2
- package/model-function/generate-text/PromptTemplateTextGenerationModel.js +2 -2
- package/model-function/generate-text/TextGenerationModel.d.ts +31 -5
- package/model-function/generate-text/generateText.cjs +10 -4
- package/model-function/generate-text/generateText.d.ts +1 -0
- package/model-function/generate-text/generateText.js +10 -4
- package/model-function/generate-text/prompt-template/trimChatPrompt.cjs +1 -1
- package/model-function/generate-text/prompt-template/trimChatPrompt.js +1 -1
- package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +27 -31
- package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +2 -2
- package/model-provider/anthropic/AnthropicTextGenerationModel.js +27 -31
- package/model-provider/cohere/CohereFacade.cjs +1 -1
- package/model-provider/cohere/CohereFacade.d.ts +1 -1
- package/model-provider/cohere/CohereFacade.js +1 -1
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
- package/model-provider/cohere/CohereTextGenerationModel.cjs +34 -43
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +3 -4
- package/model-provider/cohere/CohereTextGenerationModel.js +34 -43
- package/model-provider/huggingface/HuggingFaceFacade.cjs +1 -1
- package/model-provider/huggingface/HuggingFaceFacade.d.ts +1 -1
- package/model-provider/huggingface/HuggingFaceFacade.js +1 -1
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +31 -41
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +3 -4
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +31 -41
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +4 -4
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +2 -2
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +4 -4
- package/model-provider/mistral/{MistralTextGenerationModel.cjs → MistralChatModel.cjs} +18 -18
- package/model-provider/mistral/{MistralTextGenerationModel.d.ts → MistralChatModel.d.ts} +22 -21
- package/model-provider/mistral/{MistralTextGenerationModel.js → MistralChatModel.js} +16 -16
- package/model-provider/mistral/MistralFacade.cjs +5 -5
- package/model-provider/mistral/MistralFacade.d.ts +3 -2
- package/model-provider/mistral/MistralFacade.js +3 -3
- package/model-provider/mistral/MistralPromptTemplate.d.ts +4 -4
- package/model-provider/mistral/index.cjs +1 -1
- package/model-provider/mistral/index.d.ts +1 -1
- package/model-provider/mistral/index.js +1 -1
- package/model-provider/ollama/OllamaApiConfiguration.d.ts +6 -5
- package/model-provider/ollama/OllamaChatModel.cjs +303 -0
- package/model-provider/ollama/OllamaChatModel.d.ts +171 -0
- package/model-provider/ollama/OllamaChatModel.js +299 -0
- package/model-provider/ollama/OllamaChatPromptTemplate.cjs +76 -0
- package/model-provider/ollama/OllamaChatPromptTemplate.d.ts +20 -0
- package/model-provider/ollama/OllamaChatPromptTemplate.js +69 -0
- package/model-provider/ollama/{OllamaTextGenerationModel.cjs → OllamaCompletionModel.cjs} +17 -15
- package/model-provider/ollama/OllamaCompletionModel.d.ts +159 -0
- package/model-provider/ollama/{OllamaTextGenerationModel.js → OllamaCompletionModel.js} +15 -13
- package/model-provider/ollama/{OllamaTextGenerationModel.test.cjs → OllamaCompletionModel.test.cjs} +3 -3
- package/model-provider/ollama/{OllamaTextGenerationModel.test.js → OllamaCompletionModel.test.js} +3 -3
- package/model-provider/ollama/OllamaFacade.cjs +15 -5
- package/model-provider/ollama/OllamaFacade.d.ts +7 -2
- package/model-provider/ollama/OllamaFacade.js +11 -3
- package/model-provider/ollama/OllamaTextGenerationSettings.cjs +2 -0
- package/model-provider/ollama/OllamaTextGenerationSettings.d.ts +87 -0
- package/model-provider/ollama/OllamaTextGenerationSettings.js +1 -0
- package/model-provider/ollama/index.cjs +4 -1
- package/model-provider/ollama/index.d.ts +4 -1
- package/model-provider/ollama/index.js +4 -1
- package/model-provider/openai/OpenAICompletionModel.cjs +48 -53
- package/model-provider/openai/OpenAICompletionModel.d.ts +3 -6
- package/model-provider/openai/OpenAICompletionModel.js +48 -53
- package/model-provider/openai/OpenAIFacade.cjs +6 -4
- package/model-provider/openai/OpenAIFacade.d.ts +5 -3
- package/model-provider/openai/OpenAIFacade.js +4 -3
- package/model-provider/openai/chat/AbstractOpenAIChatModel.cjs +50 -54
- package/model-provider/openai/chat/AbstractOpenAIChatModel.d.ts +7 -28
- package/model-provider/openai/chat/AbstractOpenAIChatModel.js +50 -54
- package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts +3 -3
- package/model-provider/openai/chat/OpenAIChatModel.cjs +4 -4
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +3 -3
- package/model-provider/openai/chat/OpenAIChatModel.js +4 -4
- package/model-provider/openai/chat/OpenAIChatModel.test.cjs +1 -1
- package/model-provider/openai/chat/OpenAIChatModel.test.js +1 -1
- package/model-provider/openai/chat/OpenAIChatPromptTemplate.d.ts +5 -5
- package/model-provider/openai/chat/OpenAIChatPromptTemplate.js +1 -1
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +2 -2
- package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +2 -2
- package/model-provider/openai-compatible/OpenAICompatibleFacade.cjs +2 -2
- package/model-provider/openai-compatible/OpenAICompatibleFacade.d.ts +2 -2
- package/model-provider/openai-compatible/OpenAICompatibleFacade.js +2 -2
- package/package.json +1 -1
- package/model-provider/ollama/OllamaTextGenerationModel.d.ts +0 -230
- /package/model-provider/ollama/{OllamaTextGenerationModel.test.d.ts → OllamaCompletionModel.test.d.ts} +0 -0
package/README.md
CHANGED
@@ -85,7 +85,7 @@ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai),
|
|
85
85
|
Multi-modal vision models such as GPT 4 Vision can process images as part of the prompt.
|
86
86
|
|
87
87
|
```ts
|
88
|
-
import { streamText, openai
|
88
|
+
import { streamText, openai } from "modelfusion";
|
89
89
|
import { readFileSync } from "fs";
|
90
90
|
|
91
91
|
const image = readFileSync("./image.png").toString("base64");
|
@@ -93,7 +93,7 @@ const image = readFileSync("./image.png").toString("base64");
|
|
93
93
|
const textStream = await streamText(
|
94
94
|
openai.ChatTextGenerator({ model: "gpt-4-vision-preview" }),
|
95
95
|
[
|
96
|
-
|
96
|
+
openai.ChatMessage.user([
|
97
97
|
{ type: "text", text: "Describe the image in detail:" },
|
98
98
|
{ type: "image", base64Image: image, mimeType: "image/png" },
|
99
99
|
]),
|
@@ -211,7 +211,7 @@ const sentiment = await generateStructure(
|
|
211
211
|
.ChatTextGenerator({
|
212
212
|
model: "gpt-3.5-turbo",
|
213
213
|
temperature: 0,
|
214
|
-
|
214
|
+
maxGenerationTokens: 50,
|
215
215
|
})
|
216
216
|
.asFunctionCallStructureGenerationModel({ fnName: "sentiment" })
|
217
217
|
.withInstructionPrompt(),
|
@@ -355,14 +355,14 @@ const result = await guard(
|
|
355
355
|
fixStructure({
|
356
356
|
modifyInputForRetry: async ({ input, error }) => [
|
357
357
|
...input,
|
358
|
-
|
358
|
+
openai.ChatMessage.assistant(null, {
|
359
359
|
functionCall: {
|
360
360
|
name: "sentiment",
|
361
361
|
arguments: JSON.stringify(error.valueText),
|
362
362
|
},
|
363
363
|
}),
|
364
|
-
|
365
|
-
|
364
|
+
openai.ChatMessage.user(error.message),
|
365
|
+
openai.ChatMessage.user("Please fix the error and try again."),
|
366
366
|
],
|
367
367
|
})
|
368
368
|
);
|
@@ -418,7 +418,7 @@ With `generateToolCall`, you can generate a tool call for a specific tool with a
|
|
418
418
|
const { id, name, args } = await generateToolCall(
|
419
419
|
openai.ChatTextGenerator({ model: "gpt-3.5-turbo" }),
|
420
420
|
calculator,
|
421
|
-
[
|
421
|
+
[openai.ChatMessage.user("What's fourteen times twelve?")]
|
422
422
|
);
|
423
423
|
```
|
424
424
|
|
@@ -430,7 +430,7 @@ With `generateToolCallsOrText`, you can ask a language model to generate several
|
|
430
430
|
const { text, toolCalls } = await generateToolCallsOrText(
|
431
431
|
openai.ChatTextGenerator({ model: "gpt-3.5-turbo" }),
|
432
432
|
[toolA, toolB, toolC],
|
433
|
-
[
|
433
|
+
[openai.ChatMessage.user(query)]
|
434
434
|
);
|
435
435
|
```
|
436
436
|
|
@@ -454,7 +454,7 @@ With `useTool`, you can use a tool with a language model that supports tools cal
|
|
454
454
|
const { tool, toolCall, args, ok, result } = await useTool(
|
455
455
|
openai.ChatTextGenerator({ model: "gpt-3.5-turbo" }),
|
456
456
|
calculator,
|
457
|
-
[
|
457
|
+
[openai.ChatMessage.user("What's fourteen times twelve?")]
|
458
458
|
);
|
459
459
|
|
460
460
|
console.log(`Tool call:`, toolCall);
|
@@ -472,7 +472,7 @@ With `useToolsOrGenerateText`, you can ask a language model to generate several
|
|
472
472
|
const { text, toolResults } = await useToolsOrGenerateText(
|
473
473
|
openai.ChatTextGenerator({ model: "gpt-3.5-turbo" }),
|
474
474
|
[calculator /* ... */],
|
475
|
-
[
|
475
|
+
[openai.ChatMessage.user("What's fourteen times twelve?")]
|
476
476
|
);
|
477
477
|
```
|
478
478
|
|
@@ -541,7 +541,7 @@ const text = await generateText(
|
|
541
541
|
llamacpp
|
542
542
|
.TextGenerator({
|
543
543
|
contextWindowSize: 4096, // Llama 2 context window size
|
544
|
-
|
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 {
|
622
|
+
const { text, response, metadata } = await generateText(
|
623
623
|
openai.CompletionTextGenerator({
|
624
624
|
model: "gpt-3.5-turbo-instruct",
|
625
|
-
|
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",
|
@@ -767,12 +767,6 @@ Examples for almost all of the individual functions and objects. Highly recommen
|
|
767
767
|
|
768
768
|
StoryTeller is an exploratory web application that creates short audio stories for pre-school kids.
|
769
769
|
|
770
|
-
### [Chatbot (Terminal)](https://github.com/lgrammel/modelfusion/tree/main/examples/chatbot-terminal)
|
771
|
-
|
772
|
-
> _Terminal app_, _chat_, _llama.cpp_
|
773
|
-
|
774
|
-
A chat with an AI assistant, implemented as a terminal app.
|
775
|
-
|
776
770
|
### [Chatbot (Next.JS)](https://github.com/lgrammel/modelfusion/tree/main/examples/chatbot-next-js)
|
777
771
|
|
778
772
|
> _Next.js app_, _OpenAI GPT-3.5-turbo_, _streaming_, _abort handling_
|
package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs
CHANGED
@@ -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.
|
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)({
|
package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js
CHANGED
@@ -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.
|
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({
|
package/guard/fixStructure.cjs
CHANGED
@@ -45,14 +45,14 @@ const StructureValidationError_js_1 = require("../model-function/generate-struct
|
|
45
45
|
* fixStructure({
|
46
46
|
* modifyInputForRetry: async ({ input, error }) => [
|
47
47
|
* ...input,
|
48
|
-
*
|
48
|
+
* openai.ChatMessage.assistant(null, {
|
49
49
|
* functionCall: {
|
50
50
|
* name: "sentiment",
|
51
51
|
* arguments: JSON.stringify(error.valueText),
|
52
52
|
* },
|
53
53
|
* }),
|
54
|
-
*
|
55
|
-
*
|
54
|
+
* openai.ChatMessage.user(error.message),
|
55
|
+
* openai.ChatMessage.user("Please fix the error and try again."),
|
56
56
|
* ],
|
57
57
|
* })
|
58
58
|
* );
|
package/guard/fixStructure.d.ts
CHANGED
@@ -43,14 +43,14 @@ import { Guard } from "./guard.js";
|
|
43
43
|
* fixStructure({
|
44
44
|
* modifyInputForRetry: async ({ input, error }) => [
|
45
45
|
* ...input,
|
46
|
-
*
|
46
|
+
* openai.ChatMessage.assistant(null, {
|
47
47
|
* functionCall: {
|
48
48
|
* name: "sentiment",
|
49
49
|
* arguments: JSON.stringify(error.valueText),
|
50
50
|
* },
|
51
51
|
* }),
|
52
|
-
*
|
53
|
-
*
|
52
|
+
* openai.ChatMessage.user(error.message),
|
53
|
+
* openai.ChatMessage.user("Please fix the error and try again."),
|
54
54
|
* ],
|
55
55
|
* })
|
56
56
|
* );
|
package/guard/fixStructure.js
CHANGED
@@ -42,14 +42,14 @@ import { StructureValidationError } from "../model-function/generate-structure/S
|
|
42
42
|
* fixStructure({
|
43
43
|
* modifyInputForRetry: async ({ input, error }) => [
|
44
44
|
* ...input,
|
45
|
-
*
|
45
|
+
* openai.ChatMessage.assistant(null, {
|
46
46
|
* functionCall: {
|
47
47
|
* name: "sentiment",
|
48
48
|
* arguments: JSON.stringify(error.valueText),
|
49
49
|
* },
|
50
50
|
* }),
|
51
|
-
*
|
52
|
-
*
|
51
|
+
* openai.ChatMessage.user(error.message),
|
52
|
+
* openai.ChatMessage.user("Please fix the error and try again."),
|
53
53
|
* ],
|
54
54
|
* })
|
55
55
|
* );
|
@@ -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
|
-
*
|
23
|
+
* maxGenerationTokens: 500,
|
24
24
|
* });
|
25
25
|
*
|
26
26
|
* const modelWithMoreTokens = model.withSettings({
|
27
|
-
*
|
27
|
+
* maxGenerationTokens: 1000,
|
28
28
|
* });
|
29
29
|
*/
|
30
30
|
withSettings(additionalSettings: Partial<SETTINGS>): this;
|
@@ -17,11 +17,11 @@ import { StructureGenerationModel, StructureGenerationModelSettings } from "./St
|
|
17
17
|
* .describe("Sentiment."),
|
18
18
|
* })),
|
19
19
|
* [
|
20
|
-
*
|
20
|
+
* openai.ChatMessage.system(
|
21
21
|
* "You are a sentiment evaluator. " +
|
22
22
|
* "Analyze the sentiment of the following product review:"
|
23
23
|
* ),
|
24
|
-
*
|
24
|
+
* openai.ChatMessage.user(
|
25
25
|
* "After I opened the package, I was met by a very unpleasant smell " +
|
26
26
|
* "that did not disappear even after washing. Never again!"
|
27
27
|
* ),
|
@@ -40,9 +40,9 @@ class PromptTemplateTextGenerationModel {
|
|
40
40
|
}
|
41
41
|
return ((prompt) => originalCountPromptTokens(this.promptTemplate.format(prompt)));
|
42
42
|
}
|
43
|
-
|
43
|
+
doGenerateTexts(prompt, options) {
|
44
44
|
const mappedPrompt = this.promptTemplate.format(prompt);
|
45
|
-
return this.model.
|
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
|
-
|
20
|
+
doGenerateTexts(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
21
21
|
response: unknown;
|
22
|
-
|
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
|
-
|
40
|
+
doGenerateTexts(prompt, options) {
|
41
41
|
const mappedPrompt = this.promptTemplate.format(prompt);
|
42
|
-
return this.model.
|
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
|
-
*
|
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
|
-
|
15
|
+
maxGenerationTokens?: number | undefined;
|
12
16
|
/**
|
13
|
-
* Stop sequences to use.
|
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
|
-
|
63
|
+
doGenerateTexts(prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
|
38
64
|
response: unknown;
|
39
|
-
|
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.
|
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:
|
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
|
28
|
+
text,
|
29
|
+
texts,
|
24
30
|
response: fullResponse.response,
|
25
31
|
metadata: fullResponse.metadata,
|
26
32
|
}
|
27
|
-
:
|
33
|
+
: text;
|
28
34
|
}
|
29
35
|
exports.generateText = generateText;
|
@@ -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.
|
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:
|
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
|
25
|
+
text,
|
26
|
+
texts,
|
21
27
|
response: fullResponse.response,
|
22
28
|
metadata: fullResponse.metadata,
|
23
29
|
}
|
24
|
-
:
|
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.
|
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.
|
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 () =>
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
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
|
-
"
|
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
|
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
|
-
|
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
|
-
|
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
|
-
|
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 () =>
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
-
"
|
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
|
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
|
-
|
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
|
-
*
|
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
|
-
*
|
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
|
-
*
|
13
|
+
* maxGenerationTokens: 500,
|
14
14
|
* });
|
15
15
|
*
|
16
16
|
* const text = await generateText(
|