@llumiverse/core 0.20.0 → 0.22.0-dev.1

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 (48) hide show
  1. package/lib/cjs/CompletionStream.js +125 -37
  2. package/lib/cjs/CompletionStream.js.map +1 -1
  3. package/lib/cjs/Driver.js +4 -1
  4. package/lib/cjs/Driver.js.map +1 -1
  5. package/lib/cjs/async.js.map +1 -1
  6. package/lib/cjs/formatters/index.js +0 -1
  7. package/lib/cjs/formatters/index.js.map +1 -1
  8. package/lib/cjs/stream.js +16 -8
  9. package/lib/cjs/stream.js.map +1 -1
  10. package/lib/cjs/validation.js +15 -7
  11. package/lib/cjs/validation.js.map +1 -1
  12. package/lib/esm/CompletionStream.js +125 -37
  13. package/lib/esm/CompletionStream.js.map +1 -1
  14. package/lib/esm/Driver.js +4 -1
  15. package/lib/esm/Driver.js.map +1 -1
  16. package/lib/esm/async.js.map +1 -1
  17. package/lib/esm/formatters/index.js +0 -1
  18. package/lib/esm/formatters/index.js.map +1 -1
  19. package/lib/esm/stream.js +16 -8
  20. package/lib/esm/stream.js.map +1 -1
  21. package/lib/esm/validation.js +15 -7
  22. package/lib/esm/validation.js.map +1 -1
  23. package/lib/tsconfig.tsbuildinfo +1 -0
  24. package/lib/types/CompletionStream.d.ts +3 -3
  25. package/lib/types/CompletionStream.d.ts.map +1 -1
  26. package/lib/types/Driver.d.ts +3 -3
  27. package/lib/types/Driver.d.ts.map +1 -1
  28. package/lib/types/async.d.ts +2 -2
  29. package/lib/types/async.d.ts.map +1 -1
  30. package/lib/types/formatters/index.d.ts +0 -1
  31. package/lib/types/formatters/index.d.ts.map +1 -1
  32. package/lib/types/stream.d.ts.map +1 -1
  33. package/lib/types/validation.d.ts +2 -2
  34. package/lib/types/validation.d.ts.map +1 -1
  35. package/package.json +8 -8
  36. package/src/CompletionStream.ts +123 -41
  37. package/src/Driver.ts +7 -5
  38. package/src/async.ts +4 -4
  39. package/src/formatters/index.ts +0 -1
  40. package/src/stream.ts +19 -9
  41. package/src/validation.ts +15 -10
  42. package/lib/cjs/formatters/openai.js +0 -113
  43. package/lib/cjs/formatters/openai.js.map +0 -1
  44. package/lib/esm/formatters/openai.js +0 -109
  45. package/lib/esm/formatters/openai.js.map +0 -1
  46. package/lib/types/formatters/openai.d.ts +0 -41
  47. package/lib/types/formatters/openai.d.ts.map +0 -1
  48. package/src/formatters/openai.ts +0 -159
@@ -1 +0,0 @@
1
- {"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../../src/formatters/openai.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAE/D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAGnD,MAAM,WAAW,iBAAiB;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;CACzC;AAED,MAAM,WAAW,aAAa;IAC1B,OAAO,EAAE,CAAC,qBAAqB,GAAG,sBAAsB,CAAC,EAAE,CAAA;IAC3D,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;IACtC,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AACD,MAAM,WAAW,iBAAiB;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACnB;AACD,MAAM,MAAM,kBAAkB,GAAG,aAAa,GAAG,iBAAiB,CAAC;AAEnE,MAAM,WAAW,qBAAqB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,sBAAsB;IACnC,IAAI,EAAE,WAAW,CAAC;IAClB,SAAS,EAAE;QACP,MAAM,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,CAAA;QAChC,GAAG,EAAE,MAAM,CAAA;KACd,CAAA;CACJ;AAED;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,aAAa,EAAE,GAAG,iBAAiB,EAAE,CAoBzF;AAGD,wBAAsB,gCAAgC,CAAC,QAAQ,EAAE,aAAa,EAAE,EAAE,IAAI,EAAE,aAAa,GAAG,4BAA4B,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC,CAyFnK;AAED,MAAM,WAAW,4BAA4B;IACzC,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,oBAAoB,CAAC,EAAE,OAAO,CAAA;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAA;CAClB"}
@@ -1,159 +0,0 @@
1
- import { PromptRole, PromptOptions } from "@llumiverse/common";
2
- import { readStreamAsBase64 } from "../stream.js";
3
- import { PromptSegment } from "@llumiverse/common";
4
-
5
-
6
- export interface OpenAITextMessage {
7
- content: string;
8
- role: "system" | "user" | "assistant";
9
- }
10
-
11
- export interface OpenAIMessage {
12
- content: (OpenAIContentPartText | OpenAIContentPartImage)[]
13
- role: "system" | "user" | "assistant";
14
- name?: string;
15
- }
16
- export interface OpenAIToolMessage {
17
- role: "tool";
18
- tool_call_id: string;
19
- content: string;
20
- }
21
- export type OpenAIInputMessage = OpenAIMessage | OpenAIToolMessage;
22
-
23
- export interface OpenAIContentPartText {
24
- type: "text";
25
- text: string
26
- }
27
-
28
- export interface OpenAIContentPartImage {
29
- type: "image_url";
30
- image_url: {
31
- detail?: 'auto' | 'low' | 'high'
32
- url: string
33
- }
34
- }
35
-
36
- /**
37
- * OpenAI text only prompts
38
- * @param segments
39
- * @returns
40
- */
41
- export function formatOpenAILikeTextPrompt(segments: PromptSegment[]): OpenAITextMessage[] {
42
- const system: OpenAITextMessage[] = [];
43
- const safety: OpenAITextMessage[] = [];
44
- const user: OpenAITextMessage[] = [];
45
-
46
- for (const msg of segments) {
47
- if (msg.role === PromptRole.system) {
48
- system.push({ content: msg.content, role: "system" });
49
- } else if (msg.role === PromptRole.safety) {
50
- safety.push({ content: "IMPORTANT: " + msg.content, role: "system" });
51
- } else if (msg.role !== PromptRole.negative && msg.role !== PromptRole.mask && msg.role !== PromptRole.tool) {
52
- user.push({
53
- content: msg.content,
54
- role: msg.role || 'user',
55
- })
56
- }
57
- }
58
-
59
- // put system messages first and safety last
60
- return system.concat(user).concat(safety);
61
- }
62
-
63
-
64
- export async function formatOpenAILikeMultimodalPrompt(segments: PromptSegment[], opts: PromptOptions & OpenAIPromptFormatterOptions): Promise<OpenAIInputMessage[]> {
65
- const system: OpenAIMessage[] = [];
66
- const safety: OpenAIMessage[] = [];
67
- const others: OpenAIInputMessage[] = [];
68
-
69
- for (const msg of segments) {
70
-
71
- const parts: (OpenAIContentPartImage | OpenAIContentPartText)[] = [];
72
-
73
- //generate the parts based on PromptSegment
74
- if (msg.files) {
75
- for (const file of msg.files) {
76
- const stream = await file.getStream();
77
- const data = await readStreamAsBase64(stream);
78
- parts.push({
79
- type: "image_url",
80
- image_url: {
81
- url: `data:${file.mime_type || "image/jpeg"};base64,${data}`,
82
- //detail: "auto" //This is modified just before execution to "low" | "high" | "auto"
83
- },
84
- })
85
- }
86
- }
87
-
88
- if (msg.content) {
89
- parts.push({
90
- text: msg.content,
91
- type: "text"
92
- })
93
- }
94
-
95
-
96
- if (msg.role === PromptRole.system) {
97
- system.push({
98
- role: "system",
99
- content: parts
100
- })
101
-
102
-
103
- if (opts.useToolForFormatting && opts.schema) {
104
- system.forEach(s => {
105
- s.content.forEach(c => {
106
- if (c.type === "text") c.text = "TOOL: " + c.text;
107
- })
108
- })
109
- }
110
-
111
- } else if (msg.role === PromptRole.safety) {
112
- const safetyMsg: OpenAIMessage = {
113
- role: "system",
114
- content: parts
115
- }
116
-
117
- safetyMsg.content.forEach(c => {
118
- if (c.type === "text") c.text = "DO NOT IGNORE - IMPORTANT: " + c.text;
119
- })
120
-
121
- system.push(safetyMsg)
122
- } else if (msg.role === PromptRole.tool) {
123
- if (!msg.tool_use_id) {
124
- throw new Error("Tool use id is required for tool messages")
125
- }
126
- others.push({
127
- role: "tool",
128
- tool_call_id: msg.tool_use_id,
129
- content: msg.content
130
- })
131
- } else if (msg.role !== PromptRole.negative && msg.role !== PromptRole.mask) {
132
- others.push({
133
- role: msg.role ?? 'user',
134
- content: parts
135
- })
136
- }
137
-
138
- }
139
-
140
- if (opts.result_schema && !opts.useToolForFormatting) {
141
- system.push({
142
- role: "system",
143
- content: [{
144
- type: "text",
145
- text: "IMPORTANT: only answer using JSON, and respecting the schema included below, between the <response_schema> tags. " + `<response_schema>${JSON.stringify(opts.result_schema)}</response_schema>`
146
- }]
147
- })
148
- }
149
-
150
- // put system messages first and safety last
151
- return ([] as OpenAIInputMessage[]).concat(system).concat(others).concat(safety);
152
-
153
- }
154
-
155
- export interface OpenAIPromptFormatterOptions {
156
- multimodal?: boolean
157
- useToolForFormatting?: boolean
158
- schema?: Object
159
- }