Skip to content

Commit 9a9bd5e

Browse files
committed
feat: add test_mode flag
1 parent 78fa77d commit 9a9bd5e

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

src/backend/src/modules/puterai/AWSTextractService.js

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { TextractClient, AnalyzeDocumentCommand, InvalidS3ObjectException } = require("@aws-sdk/client-textract");
22

33
const BaseService = require("../../services/BaseService");
4+
const APIError = require("../../api/APIError");
45

56
class AWSTextractService extends BaseService {
67
_construct () {
@@ -10,6 +11,23 @@ class AWSTextractService extends BaseService {
1011
static IMPLEMENTS = {
1112
['puter-ocr']: {
1213
async recognize ({ source, test_mode }) {
14+
if ( test_mode ) {
15+
return {
16+
blocks: [
17+
{
18+
type: 'text/textract:WORD',
19+
confidence: 0.9999998807907104,
20+
text: 'Hello',
21+
},
22+
{
23+
type: 'text/puter:sample-output',
24+
confidence: 1,
25+
text: 'The test_mode flag is set to true. This is a sample output.',
26+
},
27+
]
28+
};
29+
}
30+
1331
const resp = await this.analyze_document(source);
1432

1533
// Simplify the response for common interface

src/backend/src/modules/puterai/OpenAICompletionService.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,32 @@ class OpenAICompletionService extends BaseService {
2020

2121
static IMPLEMENTS = {
2222
['puter-chat-completion']: {
23-
async complete ({ messages, vision }) {
23+
async complete ({ messages, test_mode }) {
24+
if ( test_mode ) {
25+
const { LoremIpsum } = require('lorem-ipsum');
26+
const li = new LoremIpsum({
27+
sentencesPerParagraph: {
28+
max: 8,
29+
min: 4
30+
},
31+
wordsPerSentence: {
32+
max: 20,
33+
min: 12
34+
},
35+
});
36+
return {
37+
"index": 0,
38+
"message": {
39+
"role": "assistant",
40+
"content": li.generateParagraphs(
41+
Math.floor(Math.random() * 3) + 1
42+
),
43+
},
44+
"logprobs": null,
45+
"finish_reason": "stop"
46+
}
47+
}
48+
2449
const model = 'gpt-4o';
2550
return await this.complete(messages, {
2651
model,

src/backend/src/modules/puterai/OpenAIImageGenerationService.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ class OpenAIImageGenerationService extends BaseService {
1919
static IMPLEMENTS = {
2020
['puter-image-generation']: {
2121
async generate ({ prompt, test_mode }) {
22-
const url = await this.generate(prompt, {
23-
ratio: this.constructor.RATIO_SQUARE,
24-
});
25-
2622
if ( test_mode ) {
2723
return new TypedValue({
2824
$: 'string:url:web',
2925
content_type: 'image',
3026
}, 'https://puter-sample-data.puter.site/image_example.png');
3127
}
3228

29+
const url = await this.generate(prompt, {
30+
ratio: this.constructor.RATIO_SQUARE,
31+
});
32+
3333
const image = new TypedValue({
3434
$: 'string:url:web',
3535
content_type: 'image'

0 commit comments

Comments
 (0)