Skip to content

Commit 7fc74d6

Browse files
authored
refactor: reorganize code structure (#48)
1 parent 04083da commit 7fc74d6

File tree

23 files changed

+1807
-1443
lines changed

23 files changed

+1807
-1443
lines changed

docs/api.md

Lines changed: 697 additions & 451 deletions
Large diffs are not rendered by default.

src/api.ts

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { ReadStream } from 'fs';
55
import { v4 as uuidv4 } from 'uuid';
66

77
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';
816
import {
917
GenerationsFilter,
1018
GenerationsOrderBy,
@@ -16,32 +24,23 @@ import {
1624
ThreadsFilter,
1725
ThreadsOrderBy
1826
} from './filter';
27+
import { Attachment } from './observability/attachment';
1928
import {
2029
Generation,
2130
IGenerationMessage,
2231
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';
2436
import {
25-
Attachment,
26-
CleanThreadFields,
27-
Dataset,
28-
DatasetExperiment,
29-
DatasetExperimentItem,
30-
DatasetItem,
31-
DatasetType,
3237
Environment,
3338
Maybe,
3439
OmitUtils,
3540
PaginatedResponse,
36-
Prompt,
37-
Score,
38-
ScoreConstructor,
39-
Step,
40-
StepType,
41-
Thread,
4241
User,
4342
Utils
44-
} from './types';
43+
} from './utils';
4544

4645
// eslint-disable-next-line @typescript-eslint/no-var-requires
4746
const packageJson = require('../package.json');
@@ -118,13 +117,6 @@ steps {
118117
${stepFields}
119118
}`;
120119

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-
*/
128120
function serialize(object: Utils, id: number) {
129121
const result: any = {};
130122

@@ -135,12 +127,6 @@ function serialize(object: Utils, id: number) {
135127
return result;
136128
}
137129

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-
*/
144130
function variablesBuilder(objects: Utils[]) {
145131
let variables: any = {};
146132
for (let i = 0; i < objects.length; i++) {
@@ -161,13 +147,6 @@ function generationsVariablesBuilder(
161147
return variables;
162148
}
163149

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-
*/
171150
function ingestStepsFieldsBuilder(steps: Step[]) {
172151
let generated = '';
173152
for (let id = 0; id < steps.length; id++) {
@@ -191,14 +170,6 @@ function ingestStepsFieldsBuilder(steps: Step[]) {
191170
return generated;
192171
}
193172

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-
*/
202173
function ingestStepsArgsBuilder(steps: Step[]) {
203174
let generated = '';
204175
for (let id = 0; id < steps.length; id++) {
@@ -228,12 +199,6 @@ function ingestStepsArgsBuilder(steps: Step[]) {
228199
return generated;
229200
}
230201

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-
*/
237202
function ingestStepsQueryBuilder(steps: Step[]) {
238203
return `
239204
mutation AddStep(${ingestStepsFieldsBuilder(steps)}) {
@@ -616,7 +581,6 @@ export class API {
616581
return result.data.deleteStep.id;
617582
}
618583

619-
// Upload
620584
/**
621585
* Uploads a file to a specified thread. This method supports uploading either through direct content or via a file path.
622586
* 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 {
630594
* @returns An object containing the `objectKey` of the uploaded file and the signed `url`, or `null` values if the upload fails.
631595
* @throws {Error} Throws an error if neither `content` nor `path` is provided, or if the server response is invalid.
632596
*/
633-
634597
async uploadFile(params: UploadFileParamsWithContent): Promise<{
635598
objectKey: Maybe<string>;
636599
url: Maybe<string>;
@@ -718,6 +681,21 @@ export class API {
718681
}
719682
}
720683

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+
*/
721699
async createAttachment(
722700
params: UploadFileParamsWithContent & CreateAttachmentParams
723701
): Promise<Attachment>;
@@ -764,7 +742,6 @@ export class API {
764742
return attachment;
765743
}
766744

767-
// Generation
768745
/**
769746
* Retrieves a paginated list of Generations based on the provided filters and sorting order.
770747
*
@@ -874,7 +851,6 @@ export class API {
874851
return response.data.createGeneration as PersistedGeneration;
875852
}
876853

877-
// Thread
878854
/**
879855
* Upserts a Thread with new information.
880856
*
@@ -958,6 +934,7 @@ export class API {
958934
};
959935

960936
const response = await this.makeGqlCall(query, variables);
937+
961938
return new Thread(this.client, response.data.upsertThread);
962939
}
963940

@@ -1074,10 +1051,10 @@ export class API {
10741051
const variables = { threadId: id };
10751052

10761053
const response = await this.makeGqlCall(query, variables);
1054+
10771055
return response.data.deleteThread.id;
10781056
}
10791057

1080-
// User
10811058
/**
10821059
* Retrieves a list of users with optional filters.
10831060
*
@@ -1282,7 +1259,6 @@ export class API {
12821259
return result.data.deleteParticipant.id;
12831260
}
12841261

1285-
// Score
12861262
/**
12871263
* Get all scores connected to the platform.
12881264
*
@@ -1502,8 +1478,6 @@ export class API {
15021478
return result.data.deleteScore;
15031479
}
15041480

1505-
// Dataset
1506-
15071481
/**
15081482
* List all datasets in the platform.
15091483
*
@@ -1805,6 +1779,13 @@ export class API {
18051779
return new DatasetItem(result.data.addGenerationToDataset);
18061780
}
18071781

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+
*/
18081789
public async addGenerationsToDataset(
18091790
datasetId: string,
18101791
generationIds: string[]
@@ -1816,6 +1797,15 @@ export class API {
18161797
return Object.values(result.data).map((x: any) => new DatasetItem(x));
18171798
}
18181799

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+
*/
18191809
public async createExperiment(datasetExperiment: {
18201810
name: string;
18211811
datasetId?: string;
@@ -1840,6 +1830,18 @@ export class API {
18401830
return new DatasetExperiment(this, result.data.createDatasetExperiment);
18411831
}
18421832

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+
*/
18431845
public async createExperimentItem({
18441846
datasetExperimentId,
18451847
datasetItemId,
@@ -1881,7 +1883,6 @@ export class API {
18811883
});
18821884
}
18831885

1884-
// Prompt
18851886
/**
18861887
* Create a new prompt lineage.
18871888
*
@@ -1935,6 +1936,7 @@ export class API {
19351936
* @param name The name of the prompt to retrieve or create.
19361937
* @param templateMessages A list of template messages for the prompt.
19371938
* @param settings Optional settings for the prompt.
1939+
* @param tools Optional tools for the prompt.
19381940
* @returns The prompt that was retrieved or created.
19391941
*/
19401942
public async getOrCreatePrompt(
@@ -2050,7 +2052,7 @@ export class API {
20502052
return await this.getPromptWithQuery(query, { name, version });
20512053
}
20522054

2053-
public async getPromptWithQuery(
2055+
private async getPromptWithQuery(
20542056
query: string,
20552057
variables: Record<string, any>
20562058
) {

0 commit comments

Comments
 (0)