@llumiverse/drivers 0.8.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.
Files changed (164) hide show
  1. package/lib/cjs/bedrock/index.js +338 -0
  2. package/lib/cjs/bedrock/index.js.map +1 -0
  3. package/lib/cjs/bedrock/s3.js +61 -0
  4. package/lib/cjs/bedrock/s3.js.map +1 -0
  5. package/lib/cjs/huggingface_ie.js +181 -0
  6. package/lib/cjs/huggingface_ie.js.map +1 -0
  7. package/lib/cjs/index.js +24 -0
  8. package/lib/cjs/index.js.map +1 -0
  9. package/lib/cjs/openai.js +205 -0
  10. package/lib/cjs/openai.js.map +1 -0
  11. package/lib/cjs/package.json +3 -0
  12. package/lib/cjs/replicate.js +290 -0
  13. package/lib/cjs/replicate.js.map +1 -0
  14. package/lib/cjs/test/TestErrorCompletionStream.js +20 -0
  15. package/lib/cjs/test/TestErrorCompletionStream.js.map +1 -0
  16. package/lib/cjs/test/TestValidationErrorCompletionStream.js +24 -0
  17. package/lib/cjs/test/TestValidationErrorCompletionStream.js.map +1 -0
  18. package/lib/cjs/test/index.js +109 -0
  19. package/lib/cjs/test/index.js.map +1 -0
  20. package/lib/cjs/test/utils.js +31 -0
  21. package/lib/cjs/test/utils.js.map +1 -0
  22. package/lib/cjs/togetherai/index.js +92 -0
  23. package/lib/cjs/togetherai/index.js.map +1 -0
  24. package/lib/cjs/togetherai/interfaces.js +3 -0
  25. package/lib/cjs/togetherai/interfaces.js.map +1 -0
  26. package/lib/cjs/vertexai/debug.js +13 -0
  27. package/lib/cjs/vertexai/debug.js.map +1 -0
  28. package/lib/cjs/vertexai/index.js +80 -0
  29. package/lib/cjs/vertexai/index.js.map +1 -0
  30. package/lib/cjs/vertexai/models/codey-chat.js +65 -0
  31. package/lib/cjs/vertexai/models/codey-chat.js.map +1 -0
  32. package/lib/cjs/vertexai/models/codey-text.js +35 -0
  33. package/lib/cjs/vertexai/models/codey-text.js.map +1 -0
  34. package/lib/cjs/vertexai/models/gemini.js +140 -0
  35. package/lib/cjs/vertexai/models/gemini.js.map +1 -0
  36. package/lib/cjs/vertexai/models/palm-model-base.js +65 -0
  37. package/lib/cjs/vertexai/models/palm-model-base.js.map +1 -0
  38. package/lib/cjs/vertexai/models/palm2-chat.js +65 -0
  39. package/lib/cjs/vertexai/models/palm2-chat.js.map +1 -0
  40. package/lib/cjs/vertexai/models/palm2-text.js +35 -0
  41. package/lib/cjs/vertexai/models/palm2-text.js.map +1 -0
  42. package/lib/cjs/vertexai/models.js +93 -0
  43. package/lib/cjs/vertexai/models.js.map +1 -0
  44. package/lib/cjs/vertexai/utils/prompts.js +52 -0
  45. package/lib/cjs/vertexai/utils/prompts.js.map +1 -0
  46. package/lib/cjs/vertexai/utils/tensor.js +87 -0
  47. package/lib/cjs/vertexai/utils/tensor.js.map +1 -0
  48. package/lib/esm/bedrock/index.js +331 -0
  49. package/lib/esm/bedrock/index.js.map +1 -0
  50. package/lib/esm/bedrock/s3.js +53 -0
  51. package/lib/esm/bedrock/s3.js.map +1 -0
  52. package/lib/esm/huggingface_ie.js +177 -0
  53. package/lib/esm/huggingface_ie.js.map +1 -0
  54. package/lib/esm/index.js +8 -0
  55. package/lib/esm/index.js.map +1 -0
  56. package/lib/esm/openai.js +198 -0
  57. package/lib/esm/openai.js.map +1 -0
  58. package/lib/esm/replicate.js +283 -0
  59. package/lib/esm/replicate.js.map +1 -0
  60. package/lib/esm/test/TestErrorCompletionStream.js +16 -0
  61. package/lib/esm/test/TestErrorCompletionStream.js.map +1 -0
  62. package/lib/esm/test/TestValidationErrorCompletionStream.js +20 -0
  63. package/lib/esm/test/TestValidationErrorCompletionStream.js.map +1 -0
  64. package/lib/esm/test/index.js +91 -0
  65. package/lib/esm/test/index.js.map +1 -0
  66. package/lib/esm/test/utils.js +25 -0
  67. package/lib/esm/test/utils.js.map +1 -0
  68. package/lib/esm/togetherai/index.js +88 -0
  69. package/lib/esm/togetherai/index.js.map +1 -0
  70. package/lib/esm/togetherai/interfaces.js +2 -0
  71. package/lib/esm/togetherai/interfaces.js.map +1 -0
  72. package/lib/esm/vertexai/debug.js +6 -0
  73. package/lib/esm/vertexai/debug.js.map +1 -0
  74. package/lib/esm/vertexai/index.js +76 -0
  75. package/lib/esm/vertexai/index.js.map +1 -0
  76. package/lib/esm/vertexai/models/codey-chat.js +61 -0
  77. package/lib/esm/vertexai/models/codey-chat.js.map +1 -0
  78. package/lib/esm/vertexai/models/codey-text.js +31 -0
  79. package/lib/esm/vertexai/models/codey-text.js.map +1 -0
  80. package/lib/esm/vertexai/models/gemini.js +136 -0
  81. package/lib/esm/vertexai/models/gemini.js.map +1 -0
  82. package/lib/esm/vertexai/models/palm-model-base.js +61 -0
  83. package/lib/esm/vertexai/models/palm-model-base.js.map +1 -0
  84. package/lib/esm/vertexai/models/palm2-chat.js +61 -0
  85. package/lib/esm/vertexai/models/palm2-chat.js.map +1 -0
  86. package/lib/esm/vertexai/models/palm2-text.js +31 -0
  87. package/lib/esm/vertexai/models/palm2-text.js.map +1 -0
  88. package/lib/esm/vertexai/models.js +87 -0
  89. package/lib/esm/vertexai/models.js.map +1 -0
  90. package/lib/esm/vertexai/utils/prompts.js +47 -0
  91. package/lib/esm/vertexai/utils/prompts.js.map +1 -0
  92. package/lib/esm/vertexai/utils/tensor.js +82 -0
  93. package/lib/esm/vertexai/utils/tensor.js.map +1 -0
  94. package/lib/types/bedrock/index.d.ts +88 -0
  95. package/lib/types/bedrock/index.d.ts.map +1 -0
  96. package/lib/types/bedrock/s3.d.ts +20 -0
  97. package/lib/types/bedrock/s3.d.ts.map +1 -0
  98. package/lib/types/huggingface_ie.d.ts +36 -0
  99. package/lib/types/huggingface_ie.d.ts.map +1 -0
  100. package/lib/types/index.d.ts +8 -0
  101. package/lib/types/index.d.ts.map +1 -0
  102. package/lib/types/openai.d.ts +36 -0
  103. package/lib/types/openai.d.ts.map +1 -0
  104. package/lib/types/replicate.d.ts +52 -0
  105. package/lib/types/replicate.d.ts.map +1 -0
  106. package/lib/types/test/TestErrorCompletionStream.d.ts +9 -0
  107. package/lib/types/test/TestErrorCompletionStream.d.ts.map +1 -0
  108. package/lib/types/test/TestValidationErrorCompletionStream.d.ts +9 -0
  109. package/lib/types/test/TestValidationErrorCompletionStream.d.ts.map +1 -0
  110. package/lib/types/test/index.d.ts +27 -0
  111. package/lib/types/test/index.d.ts.map +1 -0
  112. package/lib/types/test/utils.d.ts +5 -0
  113. package/lib/types/test/utils.d.ts.map +1 -0
  114. package/lib/types/togetherai/index.d.ts +23 -0
  115. package/lib/types/togetherai/index.d.ts.map +1 -0
  116. package/lib/types/togetherai/interfaces.d.ts +81 -0
  117. package/lib/types/togetherai/interfaces.d.ts.map +1 -0
  118. package/lib/types/vertexai/debug.d.ts +2 -0
  119. package/lib/types/vertexai/debug.d.ts.map +1 -0
  120. package/lib/types/vertexai/index.d.ts +26 -0
  121. package/lib/types/vertexai/index.d.ts.map +1 -0
  122. package/lib/types/vertexai/models/codey-chat.d.ts +51 -0
  123. package/lib/types/vertexai/models/codey-chat.d.ts.map +1 -0
  124. package/lib/types/vertexai/models/codey-text.d.ts +39 -0
  125. package/lib/types/vertexai/models/codey-text.d.ts.map +1 -0
  126. package/lib/types/vertexai/models/gemini.d.ts +11 -0
  127. package/lib/types/vertexai/models/gemini.d.ts.map +1 -0
  128. package/lib/types/vertexai/models/palm-model-base.d.ts +47 -0
  129. package/lib/types/vertexai/models/palm-model-base.d.ts.map +1 -0
  130. package/lib/types/vertexai/models/palm2-chat.d.ts +61 -0
  131. package/lib/types/vertexai/models/palm2-chat.d.ts.map +1 -0
  132. package/lib/types/vertexai/models/palm2-text.d.ts +39 -0
  133. package/lib/types/vertexai/models/palm2-text.d.ts.map +1 -0
  134. package/lib/types/vertexai/models.d.ts +14 -0
  135. package/lib/types/vertexai/models.d.ts.map +1 -0
  136. package/lib/types/vertexai/utils/prompts.d.ts +20 -0
  137. package/lib/types/vertexai/utils/prompts.d.ts.map +1 -0
  138. package/lib/types/vertexai/utils/tensor.d.ts +6 -0
  139. package/lib/types/vertexai/utils/tensor.d.ts.map +1 -0
  140. package/package.json +72 -0
  141. package/src/bedrock/index.ts +456 -0
  142. package/src/bedrock/s3.ts +62 -0
  143. package/src/huggingface_ie.ts +269 -0
  144. package/src/index.ts +7 -0
  145. package/src/openai.ts +254 -0
  146. package/src/replicate.ts +333 -0
  147. package/src/test/TestErrorCompletionStream.ts +17 -0
  148. package/src/test/TestValidationErrorCompletionStream.ts +21 -0
  149. package/src/test/index.ts +102 -0
  150. package/src/test/utils.ts +28 -0
  151. package/src/togetherai/index.ts +105 -0
  152. package/src/togetherai/interfaces.ts +88 -0
  153. package/src/vertexai/README.md +257 -0
  154. package/src/vertexai/debug.ts +6 -0
  155. package/src/vertexai/index.ts +99 -0
  156. package/src/vertexai/models/codey-chat.ts +115 -0
  157. package/src/vertexai/models/codey-text.ts +69 -0
  158. package/src/vertexai/models/gemini.ts +152 -0
  159. package/src/vertexai/models/palm-model-base.ts +122 -0
  160. package/src/vertexai/models/palm2-chat.ts +119 -0
  161. package/src/vertexai/models/palm2-text.ts +69 -0
  162. package/src/vertexai/models.ts +104 -0
  163. package/src/vertexai/utils/prompts.ts +66 -0
  164. package/src/vertexai/utils/tensor.ts +82 -0
@@ -0,0 +1,257 @@
1
+ ## Palm2 for text
2
+ See https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/text
3
+
4
+ ### Request
5
+ `POST https://us-central1-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/us-central1/publishers/google/models/text-bison:predict`
6
+
7
+ ```json
8
+ {
9
+ "instances": [
10
+ {
11
+ "prompt": string
12
+ }
13
+ ],
14
+ "parameters": {
15
+ "temperature": number,
16
+ "maxOutputTokens": integer,
17
+ "topK": integer,
18
+ "topP": number,
19
+ "groundingConfig": string,
20
+ "stopSequences": [ string ],
21
+ "candidateCount": integer,
22
+ "logprobs": integer,
23
+ "presencePenalty": float,
24
+ "frequencyPenalty": float,
25
+ "logitBias": map<string, float>,
26
+ "echo": boolean,
27
+ "seed": integer
28
+ }
29
+ }
30
+ ```
31
+
32
+ ### Properties
33
+
34
+ #### prompt
35
+ Text input to generate model response. Prompts can include preamble, questions, suggestions, instructions, or examples.
36
+
37
+ ### Response
38
+ ```json
39
+ {
40
+ "predictions":[
41
+ {
42
+ "content": string,
43
+ "citationMetadata": {
44
+ "citations": [
45
+ {
46
+ "startIndex": integer,
47
+ "endIndex": integer,
48
+ "url": string,
49
+ "title": string,
50
+ "license": string,
51
+ "publicationDate": string
52
+ }
53
+ ]
54
+ },
55
+ "logprobs": {
56
+ "tokenLogProbs": [ float ],
57
+ "tokens": [ string ],
58
+ "topLogProbs": [ { map<string, float> } ]
59
+ },
60
+ "safetyAttributes": {
61
+ "categories": [ string ],
62
+ "blocked": boolean,
63
+ "scores": [ float ],
64
+ "errors": [ int ]
65
+ }
66
+ }
67
+ ],
68
+ "metadata": {
69
+ "tokenMetadata": {
70
+ "input_token_count": {
71
+ "total_tokens": integer,
72
+ "total_billable_characters": integer
73
+ },
74
+ "output_token_count": {
75
+ "total_tokens": integer,
76
+ "total_billable_characters": integer
77
+ }
78
+ }
79
+ }
80
+ }
81
+ ```
82
+
83
+ ## Code completion
84
+
85
+ See https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/code-generation
86
+
87
+ ### Request:
88
+ `POST https://us-central1-googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/code-gecko:predict`
89
+
90
+ ```json
91
+ {
92
+ "instances":[
93
+ {
94
+ "prefix": string,
95
+ "suffix": string
96
+ }
97
+ ],
98
+ "parameters": {
99
+ "temperature": number,
100
+ "maxOutputTokens": integer,
101
+ "candidateCount": integer,
102
+ "stopSequences": [ string ],
103
+ "logprobs": integer,
104
+ "presencePenalty": float,
105
+ "frequencyPenalty": float,
106
+ "logitBias": map<string, float>,
107
+ "echo": boolean,
108
+ "seed": integer
109
+ }
110
+ }
111
+ ```
112
+ ### Properties
113
+
114
+ #### prefix (required)
115
+
116
+ For code models, prefix represents the beginning of a piece of meaningful programming code or a natural language prompt that describes code to be generated. The model attempts to fill in the code in between the prefix and suffix.
117
+
118
+ #### suffix (optional)
119
+ For code completion, suffix represents the end of a piece of meaningful programming code. The model attempts to fill in the code in between the prefix and suffix.
120
+
121
+
122
+ ### Response
123
+
124
+ ```json
125
+ {
126
+ "predictions": [
127
+ {
128
+ "content": string,
129
+ "citationMetadata": {
130
+ "citations": [
131
+ {
132
+ "startIndex": integer,
133
+ "endIndex": integer,
134
+ "url": string,
135
+ "title": string,
136
+ "license": string,
137
+ "publicationDate": string
138
+ }
139
+ ]
140
+ },
141
+ "logprobs": {
142
+ "tokenLogProbs": [ float ],
143
+ "tokens": [ string ],
144
+ "topLogProbs": [ { map<string, float> } ]
145
+ },
146
+ "safetyAttributes":{
147
+ "categories": [ string ],
148
+ "blocked": boolean,
149
+ "scores": [ float ],
150
+ "errors": [ int ]
151
+ },
152
+ "score": float
153
+ }
154
+ ]
155
+ }
156
+ ```
157
+
158
+
159
+ ## Code generation
160
+
161
+ See https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/code-generation
162
+
163
+ ### Request
164
+ `POST https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/code-bison:predict`
165
+
166
+
167
+ ```json
168
+ {
169
+ "instances": [
170
+ { "prefix": string }
171
+ ],
172
+ "parameters": {
173
+ "temperature": number,
174
+ "maxOutputTokens": integer,
175
+ "candidateCount": integer,
176
+ "stopSequences": [ string ]
177
+ "logprobs": integer,
178
+ "presencePenalty": float,
179
+ "frequencyPenalty": float,
180
+ "logitBias": map<string, float>,
181
+ "echo": boolean,
182
+ "seed": integer
183
+ }
184
+ }
185
+ ```
186
+ ### Properties
187
+
188
+ #### prefix (required)
189
+
190
+ For code models, prefix represents the beginning of a piece of meaningful programming code or a natural language prompt that describes code to be generated. The model attempts to fill in the code in between the prefix and suffix.
191
+
192
+ ### Response
193
+ ```json
194
+ {
195
+ "predictions": [
196
+ {
197
+ "content": string,
198
+ "citationMetadata": {
199
+ "citations": [
200
+ {
201
+ "startIndex": integer,
202
+ "endIndex": integer,
203
+ "url": string,
204
+ "title": string,
205
+ "license": string,
206
+ "publicationDate": string
207
+ }
208
+ ]
209
+ },
210
+ "logprobs": {
211
+ "tokenLogProbs": [ float ],
212
+ "tokens": [ string ],
213
+ "topLogProbs": [ { map<string, float> } ]
214
+ },
215
+ "safetyAttributes": {
216
+ "categories": [ string ],
217
+ "blocked": false,
218
+ "scores": [ float ],
219
+ "errors": [ int ]
220
+ },
221
+ "score": float
222
+ }
223
+ ]
224
+ }
225
+ ```
226
+
227
+ ## Embeddings
228
+ See https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/text-embeddings
229
+
230
+ ### Request
231
+ `POST https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/textembedding-gecko:predict`
232
+
233
+ ```json
234
+ {
235
+ "instances": [
236
+ { "content": "TEXT"}
237
+ ],
238
+ }
239
+ ```
240
+
241
+ ### Response
242
+
243
+ ```json
244
+ {
245
+ "predictions": [
246
+ {
247
+ "embeddings": {
248
+ "statistics": {
249
+ "truncated": boolean,
250
+ "token_count": integer
251
+ },
252
+ "values": [ number ]
253
+ }
254
+ }
255
+ ]
256
+ }
257
+ ```
@@ -0,0 +1,6 @@
1
+ import util from 'util';
2
+
3
+ export function logObject(prefix: string, obj: any) {
4
+ const fullObj = util.inspect(obj, { showHidden: false, depth: null, colors: true });
5
+ console.log(prefix, fullObj)
6
+ }
@@ -0,0 +1,99 @@
1
+ //import { v1 } from "@google-cloud/aiplatform";
2
+ import { GenerateContentRequest, VertexAI } from "@google-cloud/vertexai";
3
+ import { AIModel, AbstractDriver, BuiltinProviders, Completion, DriverOptions, ExecutionOptions, ModelSearchPayload, PromptFormats, PromptOptions, PromptSegment } from "@llumiverse/core";
4
+ import { FetchClient } from "api-fetch-client";
5
+ import { BuiltinModels, getModelDefinition } from "./models.js";
6
+ //import { GoogleAuth } from "google-auth-library";
7
+
8
+ export interface VertexAIDriverOptions extends DriverOptions {
9
+ project: string;
10
+ region: string;
11
+ }
12
+
13
+ export class VertexAIDriver extends AbstractDriver<VertexAIDriverOptions, GenerateContentRequest> {
14
+ provider = BuiltinProviders.vertexai;
15
+ defaultFormat = PromptFormats.genericTextLLM;
16
+
17
+ //aiplatform: v1.ModelServiceClient;
18
+ vertexai: VertexAI;
19
+ fetchClient: FetchClient;
20
+
21
+ constructor(
22
+ options: VertexAIDriverOptions
23
+ ) {
24
+ super(options);
25
+ //this.aiplatform = new v1.ModelServiceClient();
26
+ this.vertexai = new VertexAI({
27
+ project: this.options.project,
28
+ location: this.options.region,
29
+ });
30
+ this.fetchClient = createFetchClient({
31
+ region: this.options.region,
32
+ project: this.options.project,
33
+ }).withAuthCallback(async () => {
34
+ const token = await this.vertexai.preview.token;
35
+ return `Bearer ${token}`;
36
+ });
37
+ // this.aiplatform = new v1.ModelServiceClient({
38
+ // projectId: this.options.project,
39
+ // apiEndpoint: `${this.options.region}-${API_BASE_PATH}`,
40
+ // });
41
+ }
42
+
43
+ protected canStream(options: ExecutionOptions): Promise<boolean> {
44
+ return Promise.resolve(getModelDefinition(options.model).model.canStream === true);
45
+ }
46
+
47
+ public createPrompt(segments: PromptSegment[], options: PromptOptions): GenerateContentRequest {
48
+ return getModelDefinition(options.model).createPrompt(this, segments, options);
49
+ }
50
+
51
+ async requestCompletion(prompt: GenerateContentRequest, options: ExecutionOptions): Promise<Completion<any>> {
52
+ return getModelDefinition(options.model).requestCompletion(this, prompt, options);
53
+ }
54
+ async requestCompletionStream(prompt: GenerateContentRequest, options: ExecutionOptions): Promise<AsyncIterable<string>> {
55
+ return getModelDefinition(options.model).requestCompletionStream(this, prompt, options);
56
+ }
57
+
58
+ async listModels(_params?: ModelSearchPayload): Promise<AIModel<string>[]> {
59
+ return BuiltinModels;
60
+ // try {
61
+ // const response = await this.fetchClient.get('/publishers/google/models/gemini-pro');
62
+ // console.log('>>>>>>>>', response);
63
+ // } catch (err: any) {
64
+ // console.error('+++++VETREX ERROR++++++', err);
65
+ // throw err;
66
+ // }
67
+
68
+ // TODO uncomment this to use apiplatform instead of the fetch client
69
+ // const response = await this.aiplatform.listModels({
70
+ // parent: `projects/${this.options.project}/locations/${this.options.region}`,
71
+ // });
72
+
73
+ return []; //TODO
74
+ }
75
+ listTrainableModels(): Promise<AIModel<string>[]> {
76
+ throw new Error("Method not implemented.");
77
+ }
78
+ validateConnection(): Promise<boolean> {
79
+ throw new Error("Method not implemented.");
80
+ }
81
+ generateEmbeddings(_content: string, _model?: string | undefined): Promise<{ embeddings: number[]; model: string; }> {
82
+ throw new Error("Method not implemented.");
83
+ }
84
+
85
+ }
86
+
87
+ //'us-central1-aiplatform.googleapis.com',
88
+ const API_BASE_PATH = 'aiplatform.googleapis.com';
89
+ function createFetchClient({ region, project, apiEndpoint, apiVersion = 'v1' }: {
90
+ region: string;
91
+ project: string;
92
+ apiEndpoint?: string;
93
+ apiVersion?: string;
94
+ }) {
95
+ const vertexBaseEndpoint = apiEndpoint ?? `${region}-${API_BASE_PATH}`;
96
+ return new FetchClient(`https://${vertexBaseEndpoint}/${apiVersion}/projects/${project}/locations/${region}`).withHeaders({
97
+ 'Content-Type': 'application/json',
98
+ });
99
+ }
@@ -0,0 +1,115 @@
1
+ import { AIModel, ModelType, PromptOptions, PromptRole, PromptSegment } from "@llumiverse/core";
2
+ import { VertexAIDriver } from "../index.js";
3
+ import { getJSONSafetyNotice } from "../utils/prompts.js";
4
+ import { AbstractPalmModelDefinition, NonStreamingPromptBase, PalmResponseMetadata, StreamingPromptBase } from "./palm-model-base.js";
5
+
6
+ export interface CodeyChatMessage {
7
+ author: string,
8
+ content: string
9
+ }
10
+
11
+ export type CodeyChatPrompt = NonStreamingPromptBase<{
12
+ context?: string;
13
+ messages: CodeyChatMessage[];
14
+ }>
15
+
16
+
17
+ export type CodeyChatStreamingPrompt = StreamingPromptBase<{
18
+ messages: {
19
+ listVal: {
20
+ structVal: {
21
+ author: {
22
+ stringVal: string
23
+ },
24
+ content: {
25
+ stringVal: string
26
+ }
27
+ }
28
+ }[]
29
+ }
30
+ }>
31
+
32
+
33
+ interface CodeyChatResponsePrediction {
34
+ candidates: CodeyChatMessage[],
35
+ safetyAttributes: {
36
+ scores: number[],
37
+ blocked: boolean,
38
+ categories: string[],
39
+ errors: number[],
40
+ },
41
+ citationMetadata: {
42
+ citations: any[]
43
+ },
44
+ logprobs: any,
45
+ }
46
+
47
+ export interface CodeyChatResponse {
48
+ predictions: CodeyChatResponsePrediction[],
49
+ metadata: PalmResponseMetadata
50
+ }
51
+
52
+ export class CodeyChatDefinition extends AbstractPalmModelDefinition<CodeyChatPrompt, CodeyChatStreamingPrompt> {
53
+ versions: string[] = [];
54
+ model: AIModel = {
55
+ id: "codechat-bison",
56
+ name: "Codey for Code Chat",
57
+ provider: "vertexai",
58
+ owner: "google",
59
+ type: ModelType.Chat,
60
+ canStream: true,
61
+ }
62
+
63
+ createNonStreamingPrompt(_driver: VertexAIDriver, segments: PromptSegment[], opts: PromptOptions): CodeyChatPrompt {
64
+ const system: string[] = [];
65
+ const messages: CodeyChatMessage[] = [];
66
+ const safety: string[] = [];
67
+ for (const segment of segments) {
68
+ switch (segment.role) {
69
+ case PromptRole.user:
70
+ messages.push({ author: 'user', content: segment.content });
71
+ break;
72
+ case PromptRole.assistant:
73
+ messages.push({ author: 'assistant', content: segment.content });
74
+ break;
75
+ case PromptRole.system:
76
+ system.push(segment.content);
77
+ break;
78
+ case PromptRole.safety:
79
+ safety.push(segment.content);
80
+ break;
81
+ }
82
+ }
83
+
84
+ if (opts.resultSchema) {
85
+ safety.push(getJSONSafetyNotice(opts.resultSchema));
86
+ }
87
+
88
+ const context = []
89
+ if (system.length > 0) {
90
+ context.push(system.join('\n'));
91
+ }
92
+ if (safety.length > 0) {
93
+ context.push('IMPORTANT: ' + safety.join('\n'));
94
+ }
95
+
96
+ return {
97
+ instances: [{
98
+ context: context.length > 0 ? context.join('\n') : undefined,
99
+ messages
100
+ }],
101
+ parameters: {
102
+ // put defauilts here
103
+ }
104
+ } as CodeyChatPrompt;
105
+ }
106
+
107
+ extractContentFromResponse(response: CodeyChatResponse): string {
108
+ return response.predictions[0].candidates[0].content ?? '';
109
+ }
110
+
111
+ extractContentFromResponseChunk(chunk: any): string {
112
+ return chunk.outputs[0]?.structVal.candidates.listVal[0].structVal.content.stringVal || '';
113
+ }
114
+
115
+ }
@@ -0,0 +1,69 @@
1
+ import { AIModel, ModelType, PromptOptions, PromptSegment } from "@llumiverse/core";
2
+ import { VertexAIDriver } from "../index.js";
3
+ import { getPromptAsText } from "../utils/prompts.js";
4
+ import { AbstractPalmModelDefinition, NonStreamingPromptBase, PalmResponseMetadata, StreamingPromptBase } from "./palm-model-base.js";
5
+
6
+
7
+ export type CodeyTextPrompt = NonStreamingPromptBase<{
8
+ prefix: string
9
+ }>
10
+
11
+
12
+ export type CodeyTextStreamingPrompt = StreamingPromptBase<{
13
+ prefix: {
14
+ stringVal: string
15
+ }
16
+ }>
17
+
18
+ interface CodeyTextResponsePrediction {
19
+ content: string,
20
+ safetyAttributes: {
21
+ scores: number[],
22
+ safetyRatings: {
23
+ severity: string,
24
+ probabilityScore: number,
25
+ severityScore: number,
26
+ category: string
27
+ }[]
28
+ },
29
+ citationMetadata: {
30
+ citations: any[]
31
+ }
32
+ }
33
+
34
+ export interface CodeyTextResponse {
35
+ predictions: CodeyTextResponsePrediction[],
36
+ metadata: PalmResponseMetadata
37
+ }
38
+
39
+ export class CodeyTextDefinition extends AbstractPalmModelDefinition<CodeyTextPrompt, CodeyTextStreamingPrompt> {
40
+ versions: string[] = [];
41
+ model: AIModel = {
42
+ id: "code-bison",
43
+ name: "Codey for Code Generation",
44
+ provider: "vertexai",
45
+ owner: "google",
46
+ type: ModelType.Text,
47
+ canStream: true,
48
+ }
49
+
50
+ createNonStreamingPrompt(_driver: VertexAIDriver, segments: PromptSegment[], opts: PromptOptions): CodeyTextPrompt {
51
+ return {
52
+ instances: [{
53
+ prefix: getPromptAsText(segments, opts)
54
+ }],
55
+ parameters: {
56
+ // put defauilts here
57
+ }
58
+ } as CodeyTextPrompt;
59
+ }
60
+
61
+ extractContentFromResponse(response: CodeyTextResponse): string {
62
+ return response.predictions[0].content ?? '';
63
+ }
64
+
65
+ extractContentFromResponseChunk(chunk: any): string {
66
+ return chunk.outputs[0]?.structVal.content.stringVal || '';
67
+ }
68
+
69
+ }
@@ -0,0 +1,152 @@
1
+ import { Content, GenerateContentRequest, GenerativeModel, HarmBlockThreshold, HarmCategory, TextPart } from "@google-cloud/vertexai";
2
+ import { AIModel, Completion, ExecutionOptions, ExecutionTokenUsage, ModelType, PromptOptions, PromptRole, PromptSegment } from "@llumiverse/core";
3
+ import { asyncMap } from "@llumiverse/core/async";
4
+ import { VertexAIDriver } from "../index.js";
5
+ import { ModelDefinition } from "../models.js";
6
+
7
+ function getGenerativeModel(driver: VertexAIDriver, options: ExecutionOptions): GenerativeModel {
8
+ return driver.vertexai.preview.getGenerativeModel({
9
+ model: options.model,
10
+ //TODO pass in the options
11
+ safety_settings: [{
12
+ category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
13
+ threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
14
+ }],
15
+ generation_config: {
16
+ temperature: options.temperature,
17
+ max_output_tokens: options.max_tokens
18
+ },
19
+ });
20
+ }
21
+
22
+
23
+ function collectTextParts(content: Content) {
24
+ const out = [];
25
+ const parts = content.parts;
26
+ if (parts) {
27
+ for (const part of parts) {
28
+ if (part.text) {
29
+ out.push(part.text);
30
+ }
31
+ }
32
+ }
33
+ return out.join('\n');
34
+ }
35
+
36
+ export class GeminiModelDefinition implements ModelDefinition<GenerateContentRequest> {
37
+
38
+ model: AIModel = {
39
+ id: "gemini-pro",
40
+ name: "Gemini Pro",
41
+ provider: "vertexai",
42
+ owner: "google",
43
+ type: ModelType.Text,
44
+ canStream: true,
45
+ }
46
+
47
+ createPrompt(_driver: VertexAIDriver, segments: PromptSegment[], options: PromptOptions): GenerateContentRequest {
48
+ const schema = options.resultSchema;
49
+ const contents: Content[] = [];
50
+ const safety: string[] = [];
51
+
52
+ let lastUserContent: Content | undefined = undefined;
53
+ for (const msg of segments) {
54
+ if (msg.role === PromptRole.safety) {
55
+ safety.push(msg.content);
56
+ } else {
57
+ const role = msg.role === PromptRole.assistant ? "model" : "user";
58
+ if (lastUserContent && lastUserContent.role === role) {
59
+ lastUserContent.parts.push({ text: msg.content } as TextPart);
60
+ } else {
61
+ const content: Content = {
62
+ role,
63
+ parts: [{ text: msg.content } as TextPart],
64
+ }
65
+ if (role === 'user') {
66
+ lastUserContent = content;
67
+ }
68
+ contents.push(content);
69
+ }
70
+ }
71
+ }
72
+
73
+ let tools: any = undefined;
74
+ if (schema) {
75
+
76
+ // tools = [{
77
+ // function_declarations: [{
78
+ // name: "validate_json_response",
79
+ // description: "Validate the given JSON response",
80
+ // parameters: schema as any,
81
+ // }]
82
+ // } as Tool];
83
+
84
+ safety.push("The answer must be a JSON object using the following JSON Schema:\n" + JSON.stringify(schema));
85
+ }
86
+
87
+ if (safety.length > 0) {
88
+ const content = safety.join('\n');
89
+ if (lastUserContent) {
90
+ lastUserContent.parts.push({ text: content } as TextPart);
91
+ } else {
92
+ contents.push({
93
+ role: 'user',
94
+ parts: [{ text: content } as TextPart],
95
+ })
96
+ }
97
+ }
98
+
99
+ // put system mesages first and safety last
100
+ return { contents, tools } as GenerateContentRequest;
101
+ }
102
+
103
+ async requestCompletion(driver: VertexAIDriver, prompt: GenerateContentRequest, options: ExecutionOptions): Promise<Completion> {
104
+ const model = getGenerativeModel(driver, options);
105
+ const r = await model.generateContent(prompt);
106
+ const response = await r.response;
107
+ const usage = response.usageMetadata;
108
+ const token_usage: ExecutionTokenUsage = {
109
+ prompt: usage?.prompt_token_count,
110
+ result: usage?.candidates_token_count,
111
+ total: usage?.totalTokenCount,
112
+ }
113
+
114
+ let result: any;
115
+ const candidate = response.candidates[0];
116
+ if (candidate) {
117
+ const content = candidate.content;
118
+ if (content) {
119
+ result = collectTextParts(content);
120
+ // if (options.resultSchema) {
121
+ // result = candidate.;
122
+ // } else {
123
+ // }
124
+ }
125
+ }
126
+
127
+ return {
128
+ result: result ?? '',
129
+ token_usage
130
+ };
131
+ }
132
+
133
+ async requestCompletionStream(driver: VertexAIDriver, prompt: GenerateContentRequest, options: ExecutionOptions): Promise<AsyncIterable<string>> {
134
+ const model = getGenerativeModel(driver, options);
135
+ const streamingResp = await model.generateContentStream(prompt);
136
+
137
+ const stream = asyncMap(streamingResp.stream, async (item) => {
138
+ if (item.candidates.length > 0) {
139
+ for (const candidate of item.candidates) {
140
+ if (candidate.content?.role === 'model') {
141
+ const text = collectTextParts(candidate.content);
142
+ if (text) return text;
143
+ }
144
+ }
145
+ }
146
+ return '';
147
+ });
148
+
149
+ return stream;
150
+ }
151
+
152
+ }