@@ -5,6 +5,14 @@ import { ReadStream } from 'fs';
5
5
import { v4 as uuidv4 } from 'uuid' ;
6
6
7
7
import { LiteralClient } from '.' ;
8
+ import {
9
+ Dataset ,
10
+ DatasetExperiment ,
11
+ DatasetExperimentItem ,
12
+ DatasetItem ,
13
+ DatasetType
14
+ } from './evaluation/dataset' ;
15
+ import { Score , ScoreConstructor } from './evaluation/score' ;
8
16
import {
9
17
GenerationsFilter ,
10
18
GenerationsOrderBy ,
@@ -16,32 +24,23 @@ import {
16
24
ThreadsFilter ,
17
25
ThreadsOrderBy
18
26
} from './filter' ;
27
+ import { Attachment } from './observability/attachment' ;
19
28
import {
20
29
Generation ,
21
30
IGenerationMessage ,
22
31
PersistedGeneration
23
- } from './generation' ;
32
+ } from './observability/generation' ;
33
+ import { Step , StepType } from './observability/step' ;
34
+ import { CleanThreadFields , Thread } from './observability/thread' ;
35
+ import { Prompt } from './prompt-engineering/prompt' ;
24
36
import {
25
- Attachment ,
26
- CleanThreadFields ,
27
- Dataset ,
28
- DatasetExperiment ,
29
- DatasetExperimentItem ,
30
- DatasetItem ,
31
- DatasetType ,
32
37
Environment ,
33
38
Maybe ,
34
39
OmitUtils ,
35
40
PaginatedResponse ,
36
- Prompt ,
37
- Score ,
38
- ScoreConstructor ,
39
- Step ,
40
- StepType ,
41
- Thread ,
42
41
User ,
43
42
Utils
44
- } from './types ' ;
43
+ } from './utils ' ;
45
44
46
45
// eslint-disable-next-line @typescript-eslint/no-var-requires
47
46
const packageJson = require ( '../package.json' ) ;
@@ -118,13 +117,6 @@ steps {
118
117
${ stepFields }
119
118
}` ;
120
119
121
- /**
122
- * Serializes the step object with a suffix ID to each key.
123
- *
124
- * @param object - The step object to serialize.
125
- * @param id - The numeric identifier to append to each key in the serialized object.
126
- * @returns A new object with serialized key-value pairs where each key is suffixed with the provided id.
127
- */
128
120
function serialize ( object : Utils , id : number ) {
129
121
const result : any = { } ;
130
122
@@ -135,12 +127,6 @@ function serialize(object: Utils, id: number) {
135
127
return result ;
136
128
}
137
129
138
- /**
139
- * Constructs a variables object for GraphQL queries by serializing each step with a unique suffix.
140
- *
141
- * @param objects - An array of `Step` objects to be serialized and added to the variables object.
142
- * @returns An object containing serialized steps with keys suffixed by their index in the input array.
143
- */
144
130
function variablesBuilder ( objects : Utils [ ] ) {
145
131
let variables : any = { } ;
146
132
for ( let i = 0 ; i < objects . length ; i ++ ) {
@@ -161,13 +147,6 @@ function generationsVariablesBuilder(
161
147
return variables ;
162
148
}
163
149
164
- /**
165
- * Builds a string for GraphQL field definitions for ingesting multiple steps.
166
- * Each step's fields are suffixed with its index to create unique variable names.
167
- *
168
- * @param steps - An array of `Step` objects. Each `Step` object represents a step to be ingested.
169
- * @returns A string containing GraphQL field definitions for all provided steps.
170
- */
171
150
function ingestStepsFieldsBuilder ( steps : Step [ ] ) {
172
151
let generated = '' ;
173
152
for ( let id = 0 ; id < steps . length ; id ++ ) {
@@ -191,14 +170,6 @@ function ingestStepsFieldsBuilder(steps: Step[]) {
191
170
return generated ;
192
171
}
193
172
194
- /**
195
- * Constructs the arguments for a GraphQL mutation to ingest multiple steps.
196
- * Each step is transformed into a call to the `ingestStep` mutation with parameters
197
- * suffixed by the step's index to ensure uniqueness.
198
- *
199
- * @param steps - An array of `Step` objects. Each `Step` object represents a step to be ingested.
200
- * @returns A string containing the GraphQL mutation arguments for all provided steps.
201
- */
202
173
function ingestStepsArgsBuilder ( steps : Step [ ] ) {
203
174
let generated = '' ;
204
175
for ( let id = 0 ; id < steps . length ; id ++ ) {
@@ -228,12 +199,6 @@ function ingestStepsArgsBuilder(steps: Step[]) {
228
199
return generated ;
229
200
}
230
201
231
- /**
232
- * Constructs a complete GraphQL mutation query for adding multiple steps.
233
- *
234
- * @param steps - An array of `Step` objects to be ingested. This parameter is required.
235
- * @returns A string representing the complete GraphQL mutation for adding steps.
236
- */
237
202
function ingestStepsQueryBuilder ( steps : Step [ ] ) {
238
203
return `
239
204
mutation AddStep(${ ingestStepsFieldsBuilder ( steps ) } ) {
@@ -616,7 +581,6 @@ export class API {
616
581
return result . data . deleteStep . id ;
617
582
}
618
583
619
- // Upload
620
584
/**
621
585
* Uploads a file to a specified thread. This method supports uploading either through direct content or via a file path.
622
586
* It first signs the upload through a pre-configured endpoint and then proceeds to upload the file using the signed URL.
@@ -630,7 +594,6 @@ export class API {
630
594
* @returns An object containing the `objectKey` of the uploaded file and the signed `url`, or `null` values if the upload fails.
631
595
* @throws {Error } Throws an error if neither `content` nor `path` is provided, or if the server response is invalid.
632
596
*/
633
-
634
597
async uploadFile ( params : UploadFileParamsWithContent ) : Promise < {
635
598
objectKey : Maybe < string > ;
636
599
url : Maybe < string > ;
@@ -718,6 +681,21 @@ export class API {
718
681
}
719
682
}
720
683
684
+ /**
685
+ * Uploads a file to a specified thread and creates an attachment object.
686
+ * If called inside a context, the attachment will be added to the current step and thread.
687
+ *
688
+ * @param params - The parameters for uploading a file, including:
689
+ * @param params.name - The name of the file.
690
+ * @param params.metadata - Additional metadata for the file as a key-value pair object.
691
+ * @param params.content - The content of the file to upload. Optional if `path` is provided.
692
+ * @param params.path - The path to the file to upload. Optional if `content` is provided.
693
+ * @param params.id - The unique identifier for the file. If not provided, a UUID will be generated.
694
+ * @param params.threadId - The unique identifier of the thread to which the file is being uploaded.
695
+ * @param params.mime - The MIME type of the file. Defaults to 'application/octet-stream' if not provided.
696
+ * @returns An object containing the `objectKey` of the uploaded file and the signed `url`, or `null` values if the upload fails.
697
+ * @throws {Error } Throws an error if neither `content` nor `path` is provided, or if the server response is invalid.
698
+ */
721
699
async createAttachment (
722
700
params : UploadFileParamsWithContent & CreateAttachmentParams
723
701
) : Promise < Attachment > ;
@@ -764,7 +742,6 @@ export class API {
764
742
return attachment ;
765
743
}
766
744
767
- // Generation
768
745
/**
769
746
* Retrieves a paginated list of Generations based on the provided filters and sorting order.
770
747
*
@@ -874,7 +851,6 @@ export class API {
874
851
return response . data . createGeneration as PersistedGeneration ;
875
852
}
876
853
877
- // Thread
878
854
/**
879
855
* Upserts a Thread with new information.
880
856
*
@@ -958,6 +934,7 @@ export class API {
958
934
} ;
959
935
960
936
const response = await this . makeGqlCall ( query , variables ) ;
937
+
961
938
return new Thread ( this . client , response . data . upsertThread ) ;
962
939
}
963
940
@@ -1074,10 +1051,10 @@ export class API {
1074
1051
const variables = { threadId : id } ;
1075
1052
1076
1053
const response = await this . makeGqlCall ( query , variables ) ;
1054
+
1077
1055
return response . data . deleteThread . id ;
1078
1056
}
1079
1057
1080
- // User
1081
1058
/**
1082
1059
* Retrieves a list of users with optional filters.
1083
1060
*
@@ -1282,7 +1259,6 @@ export class API {
1282
1259
return result . data . deleteParticipant . id ;
1283
1260
}
1284
1261
1285
- // Score
1286
1262
/**
1287
1263
* Get all scores connected to the platform.
1288
1264
*
@@ -1502,8 +1478,6 @@ export class API {
1502
1478
return result . data . deleteScore ;
1503
1479
}
1504
1480
1505
- // Dataset
1506
-
1507
1481
/**
1508
1482
* List all datasets in the platform.
1509
1483
*
@@ -1805,6 +1779,13 @@ export class API {
1805
1779
return new DatasetItem ( result . data . addGenerationToDataset ) ;
1806
1780
}
1807
1781
1782
+ /**
1783
+ * Adds multiple generation items to a dataset.
1784
+ *
1785
+ * @param datasetId - The unique identifier of the dataset. This parameter is required.
1786
+ * @param generationIds - An array of unique identifiers for the generations to be added. This parameter is required.
1787
+ * @returns An array of `DatasetItem` instances populated with the data of the newly added generations
1788
+ */
1808
1789
public async addGenerationsToDataset (
1809
1790
datasetId : string ,
1810
1791
generationIds : string [ ]
@@ -1816,6 +1797,15 @@ export class API {
1816
1797
return Object . values ( result . data ) . map ( ( x : any ) => new DatasetItem ( x ) ) ;
1817
1798
}
1818
1799
1800
+ /**
1801
+ * Creates a new dataset experiment.
1802
+ * @param datasetExperiment
1803
+ * @param datasetExperiment.name The name of the dataset experiment.
1804
+ * @param datasetExperiment.datasetId The dataset ID to associate with the experiment.
1805
+ * @param datasetExperiment.promptId The prompt ID to associate with the experiment.
1806
+ * @param datasetExperiment.params The parameters for the experiment as a key-value pair object or an array of the same.
1807
+ * @returns The newly created dataset experiment object.
1808
+ */
1819
1809
public async createExperiment ( datasetExperiment : {
1820
1810
name : string ;
1821
1811
datasetId ?: string ;
@@ -1840,6 +1830,18 @@ export class API {
1840
1830
return new DatasetExperiment ( this , result . data . createDatasetExperiment ) ;
1841
1831
}
1842
1832
1833
+ /**
1834
+ * Creates a new dataset experiment item.
1835
+ *
1836
+ * @param parameters
1837
+ * @param parameters.datasetExperimentId The dataset experiment ID to associate with the item (required)
1838
+ * @param parameters.scores An array of scores to associate with the item (required)
1839
+ * @param parameters.datasetItemId The ID of the dataset item (optional)
1840
+ * @param parameters.experimentRunId The ID of the experiment run (optional)
1841
+ * @param parameters.input The input data for the item (optional)
1842
+ * @param parameters.output The output data for the item (optional)
1843
+ * @returns The dataset experiment object.
1844
+ */
1843
1845
public async createExperimentItem ( {
1844
1846
datasetExperimentId,
1845
1847
datasetItemId,
@@ -1881,7 +1883,6 @@ export class API {
1881
1883
} ) ;
1882
1884
}
1883
1885
1884
- // Prompt
1885
1886
/**
1886
1887
* Create a new prompt lineage.
1887
1888
*
@@ -1935,6 +1936,7 @@ export class API {
1935
1936
* @param name The name of the prompt to retrieve or create.
1936
1937
* @param templateMessages A list of template messages for the prompt.
1937
1938
* @param settings Optional settings for the prompt.
1939
+ * @param tools Optional tools for the prompt.
1938
1940
* @returns The prompt that was retrieved or created.
1939
1941
*/
1940
1942
public async getOrCreatePrompt (
@@ -2050,7 +2052,7 @@ export class API {
2050
2052
return await this . getPromptWithQuery ( query , { name, version } ) ;
2051
2053
}
2052
2054
2053
- public async getPromptWithQuery (
2055
+ private async getPromptWithQuery (
2054
2056
query : string ,
2055
2057
variables : Record < string , any >
2056
2058
) {
0 commit comments