Skip to content

Commit

Permalink
Merge pull request #5 from parea-ai/PAI-542-enable-projects-for-exper…
Browse files Browse the repository at this point in the history
…iments-logs

Pai 542 enable projects for experiments logs
  • Loading branch information
joschkabraun authored Jan 30, 2024
2 parents 807d7d4 + 40b2bae commit ee288dc
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

### Features

* initial commit ([ddfc431](https://github.com/parea-ai/parea-sdk-js/commit/ddfc431d0c1767cbce80f6fa4466fc7f0a850bea))
* initial commit ([ddfc431](https://github.com/parea-ai/parea-sdk-ts/commit/ddfc431d0c1767cbce80f6fa4466fc7f0a850bea))
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# parea-sdk-js
# parea-sdk-ts

<div align="center">

[![License](https://img.shields.io/github/license/parea-ai/parea-sdk-js)](https://github.com/parea-ai/parea-sdk-js/blob/master/LICENSE)
[![License](https://img.shields.io/github/license/parea-ai/parea-sdk-ts)](https://github.com/parea-ai/parea-sdk-ts/blob/master/LICENSE)

Parea typescript sdk

Expand All @@ -14,6 +14,8 @@ Parea typescript sdk
npm install parea-ai
```

[TypeScript SDK Docs](https://docs.parea.ai/api-reference/sdk/typescript)

## Getting Started

```typescript
Expand Down Expand Up @@ -74,20 +76,20 @@ main().then((result) => console.log(result));

## 🛡 License

[![License](https://img.shields.io/github/license/parea-ai/parea-sdk-js)](https://github.com/parea-ai/parea-sdk-js/blob/master/LICENSE)
[![License](https://img.shields.io/github/license/parea-ai/parea-sdk-ts)](https://github.com/parea-ai/parea-sdk-ts/blob/master/LICENSE)

This project is licensed under the terms of the `Apache Software License 2.0` license.
See [LICENSE](https://github.com/parea-ai/parea-sdk/blob/master/LICENSE) for more details.

## 📃 Citation

```bibtex
@misc{parea-sdk-js,
@misc{parea-sdk-ts,
author = {parea-ai},
title = {Parea typescript sdk},
year = {2023},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/parea-ai/parea-sdk-js}}
howpublished = {\url{https://github.com/parea-ai/parea-sdk-ts}}
}
```
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Client SDK library to connect to Parea AI.",
"repository": {
"type": "git",
"url": "git+https://github.com/parea-ai/parea-sdk-js.git"
"url": "git+https://github.com/parea-ai/parea-sdk-ts.git"
},
"keywords": [
"LLM",
Expand All @@ -13,10 +13,10 @@
"Parea"
],
"author": "Parea AI",
"homepage": "https://github.com/parea-ai/parea-sdk-js#readme",
"homepage": "https://github.com/parea-ai/parea-sdk-ts#readme",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/parea-ai/parea-sdk-js/issues"
"url": "https://github.com/parea-ai/parea-sdk-ts/issues"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
23 changes: 20 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { HTTPClient } from './api-client';
import { pareaLogger } from './parea_logger';
import { genTraceId } from './helpers';
import { asyncLocalStorage } from './utils/trace_utils';
import { pareaProject } from './project';
import { Experiment } from './experiment/experiment';

const COMPLETION_ENDPOINT = '/completion';
Expand All @@ -27,11 +28,13 @@ export class Parea {
private apiKey: string;
private client: HTTPClient;

constructor(apiKey: string = '') {
constructor(apiKey: string = '', projectName: string = 'default') {
this.apiKey = apiKey;
this.client = HTTPClient.getInstance();
this.client.setApiKey(this.apiKey);
pareaLogger.setClient(this.client);
pareaProject.setProjectName(projectName);
pareaProject.setClient(this.client);
}

public async completion(data: Completion): Promise<CompletionResponse> {
Expand All @@ -48,7 +51,14 @@ export class Parea {
data.experiment_uuid = experiment_uuid;
}

const response = await this.client.request({ method: 'POST', endpoint: COMPLETION_ENDPOINT, data });
const response = await this.client.request({
method: 'POST',
endpoint: COMPLETION_ENDPOINT,
data: {
project_uuid: await pareaProject.getProjectUUID(),
...data,
},
});

if (parentStore && parentTraceId) {
const parentTraceLog = parentStore.get(parentTraceId);
Expand All @@ -74,7 +84,14 @@ export class Parea {
}

public async createExperiment(data: CreateExperimentRequest): Promise<ExperimentSchema> {
const response = await this.client.request({ method: 'POST', endpoint: EXPERIMENT_ENDPOINT, data });
const response = await this.client.request({
method: 'POST',
endpoint: EXPERIMENT_ENDPOINT,
data: {
...data,
project_uuid: await pareaProject.getProjectUUID(),
},
});
return response.data;
}

Expand Down
2 changes: 1 addition & 1 deletion src/cookbook/tracing_with_openai_endpoint_directly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const NestedChain = trace(
return await TRefinedArgument(query, refined, additionalDescription);
},
{
evalFuncNames: ['Is equal'],
evalFuncNames: ['Is equal'], // this a deployed evaluation function
accessOutputOfFunc: (arg0: any) => arg0[0],
},
);
Expand Down
7 changes: 6 additions & 1 deletion src/parea_logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TraceLog, UpdateLog } from './types.js';
import { AxiosResponse } from 'axios';
import { HTTPClient } from './api-client';
import { pareaProject } from './project';

const LOG_ENDPOINT = '/trace_log';
// const VENDOR_LOG_ENDPOINT = '/trace_log/{vendor}';
Expand All @@ -17,7 +18,11 @@ class PareaLogger {
}

public async recordLog(data: TraceLog): Promise<AxiosResponse<any>> {
return await this.client.request({ method: 'POST', endpoint: LOG_ENDPOINT, data });
return await this.client.request({
method: 'POST',
endpoint: LOG_ENDPOINT,
data: { ...data, project_uuid: await pareaProject.getProjectUUID() },
});
}

public async updateLog(data: UpdateLog): Promise<AxiosResponse<any>> {
Expand Down
49 changes: 49 additions & 0 deletions src/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { HTTPClient } from './api-client';
import { ProjectSchema } from './types';

const PROJECT_ENDPOINT = '/project';

class Project {
private client: HTTPClient;
private projectName: string;
private project: ProjectSchema;

constructor() {
this.client = HTTPClient.getInstance();
}

public setClient(client: HTTPClient): void {
this.client = client;
}

public setProjectName(projectName: string): void {
this.projectName = projectName;
}

private async getOrCreateProjectIfNecessary(): Promise<void> {
const projectName = this.projectName;
const response = await this.client.request({
method: 'POST',
endpoint: PROJECT_ENDPOINT,
data: { name: projectName },
});
const data = response.data;
if (data.was_created) {
console.log(`Created project ${projectName} with UUID ${data.uuid}`);
}
this.project = {
name: projectName,
uuid: data.uuid,
createdAt: data.created_at,
};
}

public async getProjectUUID(): Promise<string> {
if (!this.project) {
await this.getOrCreateProjectIfNecessary();
}
return this.project.uuid;
}
}

export const pareaProject = new Project();
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,12 @@ export class ExperimentStatsSchema {
return scores.length > 0 ? scores.reduce((acc, curr) => acc + curr, 0) / scores.length : 0.0;
}
}

export interface CreateGetProjectSchema {
name: string;
}

export type ProjectSchema = CreateGetProjectSchema & {
uuid: string;
createdAt: string;
};

0 comments on commit ee288dc

Please sign in to comment.