Skip to content

Commit

Permalink
Fix ordering of recorded runs, buffer runs
Browse files Browse the repository at this point in the history
  • Loading branch information
dqbd committed Oct 22, 2024
1 parent aae789a commit 997204c
Show file tree
Hide file tree
Showing 3 changed files with 421 additions and 317 deletions.
7 changes: 7 additions & 0 deletions js/src/tests/utils/iterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export async function gatherIterator<T>(
i: AsyncIterable<T> | Promise<AsyncIterable<T>>
): Promise<Array<T>> {
const out: T[] = [];
for await (const item of await i) out.push(item);
return out;
}
135 changes: 49 additions & 86 deletions js/src/tests/vercel_exporter.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
embedMany,
} from "ai";
import { tool } from "ai";
import { gatherIterator } from "./utils/iterator.js";

const telemetrySettings = {
isEnabled: true,
Expand All @@ -22,19 +23,11 @@ const telemetrySettings = {
},
};

test.concurrent("generateText", async () => {
const traceExporter = new LangSmithAISDKExporter();
const sdk = new NodeSDK({ traceExporter });
sdk.start();

function getOrders(userId: string) {
return `User ${userId} has the following orders: 1`;
}

function getTrackingInformation(orderId: string) {
return `Here is the tracking information for ${orderId}`;
}
const traceExporter = new LangSmithAISDKExporter();
const sdk = new NodeSDK({ traceExporter });
sdk.start();

test("generateText", async () => {
await generateText({
model: openai("gpt-4o-mini"),
messages: [
Expand All @@ -47,34 +40,48 @@ test.concurrent("generateText", async () => {
listOrders: tool({
description: "list all orders",
parameters: z.object({ userId: z.string() }),
execute: async ({ userId }) => getOrders(userId),
execute: async ({ userId }) =>
`User ${userId} has the following orders: 1`,
}),
viewTrackingInformation: tool({
description: "view tracking information for a specific order",
parameters: z.object({ orderId: z.string() }),
execute: async ({ orderId }) => getTrackingInformation(orderId),
execute: async ({ orderId }) =>
`Here is the tracking information for ${orderId}`,
}),
},
experimental_telemetry: telemetrySettings,
maxSteps: 10,
});

await sdk.shutdown();
await traceExporter.forceFlush?.();
});

test.concurrent("streamText", async () => {
const traceExporter = new LangSmithAISDKExporter();
const sdk = new NodeSDK({ traceExporter });
sdk.start();

function getOrders(userId: string) {
return `User ${userId} has the following orders: 1`;
}
test("generateText with image", async () => {
await generateText({
model: openai("gpt-4o-mini"),
messages: [
{
role: "user",
content: [
{
type: "text",
text: "What's in this picture?",
},
{
type: "image",
image: new URL("https://picsum.photos/200/300"),
},
],
},
],
experimental_telemetry: telemetrySettings,
});

function getTrackingInformation(orderId: string) {
return `Here is the tracking information for ${orderId}`;
}
await traceExporter.forceFlush?.();
});

test("streamText", async () => {
const result = await streamText({
model: openai("gpt-4o-mini"),
messages: [
Expand All @@ -87,30 +94,25 @@ test.concurrent("streamText", async () => {
listOrders: tool({
description: "list all orders",
parameters: z.object({ userId: z.string() }),
execute: async ({ userId }) => getOrders(userId),
execute: async ({ userId }) =>
`User ${userId} has the following orders: 1`,
}),
viewTrackingInformation: tool({
description: "view tracking information for a specific order",
parameters: z.object({ orderId: z.string() }),
execute: async ({ orderId }) => getTrackingInformation(orderId),
execute: async ({ orderId }) =>
`Here is the tracking information for ${orderId}`,
}),
},
experimental_telemetry: { isEnabled: true },
maxSteps: 10,
});

for await (const _stream of result.fullStream) {
// consume
}

await sdk.shutdown();
await gatherIterator(result.fullStream);
await traceExporter.forceFlush?.();
});

test.concurrent("generateObject", async () => {
const traceExporter = new LangSmithAISDKExporter();
const sdk = new NodeSDK({ traceExporter });
sdk.start();

test("generateObject", async () => {
await generateObject({
model: openai("gpt-4o-mini", { structuredOutputs: true }),
schema: z.object({
Expand All @@ -123,14 +125,10 @@ test.concurrent("generateObject", async () => {
experimental_telemetry: telemetrySettings,
});

await sdk.shutdown();
await traceExporter.forceFlush?.();
});

test.concurrent("streamObject", async () => {
const traceExporter = new LangSmithAISDKExporter();
const sdk = new NodeSDK({ traceExporter });
sdk.start();

test("streamObject", async () => {
const result = await streamObject({
model: openai("gpt-4o-mini", { structuredOutputs: true }),
schema: z.object({
Expand All @@ -143,32 +141,21 @@ test.concurrent("streamObject", async () => {
experimental_telemetry: telemetrySettings,
});

for await (const _partialObject of result.partialObjectStream) {
// pass
}

await sdk.shutdown();
await gatherIterator(result.partialObjectStream);
await traceExporter.forceFlush?.();
});

test.concurrent("embed", async () => {
const traceExporter = new LangSmithAISDKExporter();
const sdk = new NodeSDK({ traceExporter });
sdk.start();

test("embed", async () => {
await embed({
model: openai.embedding("text-embedding-3-small"),
value: "prague castle at sunset",
experimental_telemetry: telemetrySettings,
});

await sdk.shutdown();
await traceExporter.forceFlush?.();
});

test.concurrent("embedMany", async () => {
const traceExporter = new LangSmithAISDKExporter();
const sdk = new NodeSDK({ traceExporter });
sdk.start();

test("embedMany", async () => {
await embedMany({
model: openai.embedding("text-embedding-3-small"),
values: [
Expand All @@ -179,33 +166,9 @@ test.concurrent("embedMany", async () => {
experimental_telemetry: telemetrySettings,
});

await sdk.shutdown();
await traceExporter.forceFlush?.();
});

test.concurrent("generateText with image", async () => {
const traceExporter = new LangSmithAISDKExporter();
const sdk = new NodeSDK({ traceExporter });
sdk.start();

await generateText({
model: openai("gpt-4o-mini"),
messages: [
{
role: "user",
content: [
{
type: "text",
text: "What's in this picture?",
},
{
type: "image",
image: new URL("https://picsum.photos/200/300"),
},
],
},
],
experimental_telemetry: telemetrySettings,
});

afterAll(async () => {
await sdk.shutdown();
});
Loading

0 comments on commit 997204c

Please sign in to comment.