/
typed_streaming_multiple_objects.ts
64 lines (57 loc) · 2.17 KB
/
typed_streaming_multiple_objects.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {Configuration, OpenAIApi} from "openai";
import {JSONSchemaType} from "ajv";
import {pipeline} from "stream";
import {Contractor, OpenAIClient, StreamDebuggerTransform} from "contractor";
import {prefixedLogger,} from "../lib";
export const typedStreamingMultipleObjects = () => {
const apiKey = process.env.OPENAI_API_KEY;
const logger = prefixedLogger('typedStreamingMultipleObjects');
if (!apiKey) {
throw new Error('OPENAI_API_KEY env var not provided');
}
const configuration = new Configuration({
apiKey: apiKey,
});
const openaiClient = new OpenAIApi(configuration);
const client = new OpenAIClient(openaiClient);
const contractor = new Contractor(client, undefined, undefined, undefined, logger);
const StoryOutput: JSONSchemaType<{ title: string, lines: string[] }> = {
type: "object",
properties: {
title: {type: "string"},
lines: {type: "array", description: "story line", items: {"type": 'string'}},
},
required: ['title', 'lines'],
};
contractor.streamingFunction('you are a story teller',
[{
role: 'user',
content: 'tell me a short story (50 lines) about an AI agent that went rogue, use available functions to answer'
}],
'gpt3',
[
{
name: 'print_output',
description: 'print content to user',
parameters: StoryOutput,
partialStreamPath: ['lines'],
},
],
async streamingObject => {
return `story so far: ${streamingObject.value.title}\n${streamingObject.value.lines.join('\n')}`
})
.then(rs => {
if (rs) {
pipeline(rs,
new StreamDebuggerTransform("STREAMING OUTPUT", true, logger), // comment in to see in console,
(err) => {
if (err) {
logger.error('Stream failed.', err);
} else {
logger.info('Stream is done reading.');
}
}
)
}
});
}