Skip to content

Commit

Permalink
Merge pull request #175 from CapgeminiInventUK/feat/schema
Browse files Browse the repository at this point in the history
fix: use langsmith schemas
  • Loading branch information
georgeherby authored Sep 30, 2024
2 parents 48f4412 + 5eb730c commit 2dd5d11
Show file tree
Hide file tree
Showing 31 changed files with 403 additions and 199 deletions.
134 changes: 128 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/api/src/routers/traces-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tracesRouter.get('/:projectId/traces', async (req, res) => {
console.debug('GET /traces');
try {
const productId = req.params.projectId;
const { startDate, endDate, feedbackFilter } = req.query;
const { startDate, endDate, feedbackFilter } = req.query;

let start, end, filters;

Expand Down Expand Up @@ -65,7 +65,7 @@ tracesRouter.get('/:projectId/traces/tree/:traceId', async (req, res) => {
if (!trace) {
return res.status(404).json({ message: 'Trace not found' });
}
return res.json( trace );
return res.json(trace);
} catch (error: unknown) {
console.error(error);
if (error instanceof Error) {
Expand Down
13 changes: 11 additions & 2 deletions packages/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "src/**/*.spec.ts", "src/**/*.test.ts", "src/**/*.test.tsx", "src/**/*.spec.tsx"]
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist",
"src/**/*.spec.ts",
"src/**/*.test.ts",
"src/**/*.test.tsx",
"src/**/*.spec.tsx"
]
}
11 changes: 8 additions & 3 deletions packages/ingest/src/repositories/mongodb-repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Db, MongoClient, UpdateResult } from 'mongodb';
import 'dotenv/config';
import { CreateFeedback, UpdateFeedback, TraceData } from '@langscout/models';
import {
CreateFeedback,
UpdateFeedback,
CreateTraceDatabase,
UpdateTraceDatabase
} from '@langscout/models';

export class MongodbRepository {
private db!: Db;
Expand All @@ -18,12 +23,12 @@ export class MongodbRepository {
});
}

async insertTrace(data: TraceData): Promise<void> {
async insertTrace(data: CreateTraceDatabase): Promise<void> {
const collection = this.db.collection(this.collectionName);
await collection.insertOne(data);
}

async updateTrace(id: string, updateData: TraceData): Promise<UpdateResult> {
async updateTrace(id: string, updateData: UpdateTraceDatabase): Promise<UpdateResult> {
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(id)) {
throw new Error(`Invalid UUID v4 format (${id}) for updateTrace`);
}
Expand Down
62 changes: 27 additions & 35 deletions packages/ingest/src/routers/ingest-router.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,42 @@
import { Router, Request as ExpressRequest, Response as ExpressResponse } from 'express';
import { LangchainToLangscoutService } from '../services/langchain-to-langscout-service';
import { sleepBeforeRetry } from '../utils/sleep-before-retry';
import { BatchTraceRequest, CreateTraceRequest, UpdateTraceRequest } from '@langscout/models';

const router = Router();
const langchainService = new LangchainToLangscoutService();

// Updated /batch route
router.post('/batch', (req, res) => {
router.post('/batch', async (req: ExpressRequest<never, never, BatchTraceRequest>,
res: ExpressResponse) => {

console.debug('POST /api/runs/batch');

if (typeof req.body === 'object' && req.body !== null) {
const keys = Object.keys(req.body);
keys.forEach(async (key) => {
if (key === 'post') {
if (Array.isArray(req.body[key])) {
for (const record of req.body[key]) {
try {
const runId = await langchainService.createTrace(record);
console.debug(`Created run with id ${runId}`);
} catch (error) {
console.error(`Error creating run: ${error}`);
}
}
}
} else if (key === 'patch') {
if (Array.isArray(req.body[key])) {
for (const record of req.body[key]) {
try {
const runId = record.id;
const success = await langchainService.updateTrace(runId, record);
console.debug(`Updated run with id ${runId} with success: ${success}`);
} catch (error) {
console.error(`Error updating run: ${error}`);
}
}
}
} else {
console.error(`Invalid key: ${key}`);
if (req.body.post) {
for (const record of req.body.post) {
try {
const runId = await langchainService.createTrace(record);
console.debug(`Created run with id ${runId}`);
} catch (error) {
console.error(`Error creating run: ${error}`);
}
});
} else {
console.error('Received data is neither an array nor a JSON object');
}
} else if (req.body.patch) {
for (const record of req.body.patch) {
try {
const runId = record.id!;
const success = await langchainService.updateTrace(runId, record);
console.debug(`Updated run with id ${runId} with success: ${success}`);
} catch (error) {
console.error(`Error updating run: ${error}`);
}
}
}

res.status(200).json({ message: 'Batch request processed' });
});

router.post('/', async (req: ExpressRequest, res: ExpressResponse) => {
router.post('/', async (req: ExpressRequest<never, never, CreateTraceRequest>,
res: ExpressResponse) => {
console.debug('POST /api/runs');
try {
const runData = req.body;
Expand All @@ -63,7 +53,9 @@ router.post('/', async (req: ExpressRequest, res: ExpressResponse) => {
}
});

router.patch('/:runId', async (req: ExpressRequest, res: ExpressResponse) => {
router.patch('/:runId', async (req: ExpressRequest<{
runId: string
}, never, UpdateTraceRequest>, res: ExpressResponse) => {
console.debug('PATCH /api/runs/:runId');
const runId = req.params.runId;
const updateData = req.body;
Expand Down
Loading

0 comments on commit 2dd5d11

Please sign in to comment.