Skip to content

Commit

Permalink
Add as new entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dqbd committed Oct 23, 2024
1 parent 0fd9611 commit 7a3424d
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 108 deletions.
4 changes: 4 additions & 0 deletions js/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Chinook_Sqlite.sql
/langchain.js
/langchain.d.ts
/langchain.d.cts
/vercel.cjs
/vercel.js
/vercel.d.ts
/vercel.d.cts
/wrappers.cjs
/wrappers.js
/wrappers.d.ts
Expand Down
13 changes: 13 additions & 0 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"langchain.js",
"langchain.d.ts",
"langchain.d.cts",
"vercel.cjs",
"vercel.js",
"vercel.d.ts",
"vercel.d.cts",
"wrappers.cjs",
"wrappers.js",
"wrappers.d.ts",
Expand Down Expand Up @@ -223,6 +227,15 @@
"import": "./langchain.js",
"require": "./langchain.cjs"
},
"./vercel": {
"types": {
"import": "./vercel.d.ts",
"require": "./vercel.d.cts",
"default": "./vercel.d.ts"
},
"import": "./vercel.js",
"require": "./vercel.cjs"
},
"./wrappers": {
"types": {
"import": "./wrappers.d.ts",
Expand Down
1 change: 1 addition & 0 deletions js/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const entrypoints = {
"evaluation/langchain": "evaluation/langchain",
schemas: "schemas",
langchain: "langchain",
vercel: "vercel",
wrappers: "wrappers/index",
anonymizer: "anonymizer/index",
"wrappers/openai": "wrappers/openai",
Expand Down
7 changes: 0 additions & 7 deletions js/src/tests/utils/iterator.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
import { openai } from "@ai-sdk/openai";

import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { z } from "zod";
import { LangSmithAISDKExporter } from "../wrappers/vercel.js";

import {
generateText,
streamText,
generateObject,
streamObject,
tool,
} from "ai";
import { openai } from "@ai-sdk/openai";

import { v4 as uuid } from "uuid";
import { generateText, streamText, generateObject, streamObject } from "ai";
import { tool } from "ai";
import { gatherIterator } from "./utils/iterator.js";
import { z } from "zod";
import { AISDKExporter } from "../vercel.js";
import { Client } from "../index.js";
import { waitUntilRunFound } from "./utils.js";
import { getCurrentRunTree, traceable } from "../traceable.js";

const getTelemetrySettings = (runId?: string) => {
const metadata: Record<string, string> = {
userId: "123",
language: "english",
};

if (runId) metadata["langsmith:runId"] = runId;
return {
isEnabled: true,
functionId: "functionId",
metadata,
};
};
import { traceable } from "../traceable.js";
import { waitUntilRunFound, toArray } from "./utils.js";

const client = new Client();
// Not using @opentelemetry/sdk-node because we need to force flush
// the spans to ensure they are sent to LangSmith between tests
const provider = new NodeTracerProvider();
provider.addSpanProcessor(
new BatchSpanProcessor(new LangSmithAISDKExporter({ client }))
new BatchSpanProcessor(new AISDKExporter({ client }))
);
provider.register();

test("generateText", async () => {
const traceId = uuid();
const runId = uuid();

await generateText({
model: openai("gpt-4o-mini"),
Expand All @@ -60,19 +51,23 @@ test("generateText", async () => {
`Here is the tracking information for ${orderId}`,
}),
},
experimental_telemetry: getTelemetrySettings(traceId),
experimental_telemetry: AISDKExporter.getSettings({
runId,
functionId: "functionId",
metadata: { userId: "123", language: "english" },
}),
maxSteps: 10,
});

await provider.forceFlush();
await waitUntilRunFound(client, traceId, true);
await waitUntilRunFound(client, runId, true);

const storedRun = await client.readRun(traceId);
expect(storedRun.id).toEqual(traceId);
const storedRun = await client.readRun(runId);
expect(storedRun.id).toEqual(runId);
});

test("generateText with image", async () => {
const traceId = uuid();
const runId = uuid();
await generateText({
model: openai("gpt-4o-mini"),
messages: [
Expand All @@ -90,18 +85,22 @@ test("generateText with image", async () => {
],
},
],
experimental_telemetry: getTelemetrySettings(traceId),
experimental_telemetry: AISDKExporter.getSettings({
runId,
functionId: "functionId",
metadata: { userId: "123", language: "english" },
}),
});

await provider.forceFlush();
await waitUntilRunFound(client, traceId, true);
await waitUntilRunFound(client, runId, true);

const storedRun = await client.readRun(traceId);
expect(storedRun.id).toEqual(traceId);
const storedRun = await client.readRun(runId);
expect(storedRun.id).toEqual(runId);
});

test("streamText", async () => {
const traceId = uuid();
const runId = uuid();
const result = await streamText({
model: openai("gpt-4o-mini"),
messages: [
Expand All @@ -124,20 +123,24 @@ test("streamText", async () => {
`Here is the tracking information for ${orderId}`,
}),
},
experimental_telemetry: getTelemetrySettings(traceId),
experimental_telemetry: AISDKExporter.getSettings({
runId,
functionId: "functionId",
metadata: { userId: "123", language: "english" },
}),
maxSteps: 10,
});

await gatherIterator(result.fullStream);
await toArray(result.fullStream);
await provider.forceFlush();
await waitUntilRunFound(client, traceId, true);
await waitUntilRunFound(client, runId, true);

const storedRun = await client.readRun(traceId);
expect(storedRun.id).toEqual(traceId);
const storedRun = await client.readRun(runId);
expect(storedRun.id).toEqual(runId);
});

test("generateObject", async () => {
const traceId = uuid();
const runId = uuid();
await generateObject({
model: openai("gpt-4o-mini", { structuredOutputs: true }),
schema: z.object({
Expand All @@ -147,18 +150,22 @@ test("generateObject", async () => {
}),
}),
prompt: "What's the weather in Prague?",
experimental_telemetry: getTelemetrySettings(traceId),
experimental_telemetry: AISDKExporter.getSettings({
runId,
functionId: "functionId",
metadata: { userId: "123", language: "english" },
}),
});

await provider.forceFlush();
await waitUntilRunFound(client, traceId, true);
await waitUntilRunFound(client, runId, true);

const storedRun = await client.readRun(traceId);
expect(storedRun.id).toEqual(traceId);
const storedRun = await client.readRun(runId);
expect(storedRun.id).toEqual(runId);
});

test("streamObject", async () => {
const traceId = uuid();
const runId = uuid();
const result = await streamObject({
model: openai("gpt-4o-mini", { structuredOutputs: true }),
schema: z.object({
Expand All @@ -168,27 +175,26 @@ test("streamObject", async () => {
}),
}),
prompt: "What's the weather in Prague?",
experimental_telemetry: getTelemetrySettings(traceId),
experimental_telemetry: AISDKExporter.getSettings({
runId,
functionId: "functionId",
metadata: { userId: "123", language: "english" },
}),
});

await gatherIterator(result.partialObjectStream);
await toArray(result.partialObjectStream);
await provider.forceFlush();
await waitUntilRunFound(client, traceId, true);
await waitUntilRunFound(client, runId, true);

const storedRun = await client.readRun(traceId);
expect(storedRun.id).toEqual(traceId);
const storedRun = await client.readRun(runId);
expect(storedRun.id).toEqual(runId);
});

test("traceable", async () => {
const runId = uuid();

const wrappedText = traceable(
async (content: string) => {
const runTree = getCurrentRunTree();
const headers = runTree.toHeaders();

const telemetry = getTelemetrySettings();

const { text } = await generateText({
model: openai("gpt-4o-mini"),
messages: [{ role: "user", content }],
Expand All @@ -206,14 +212,10 @@ test("traceable", async () => {
`Here is the tracking information for ${orderId}`,
}),
},
experimental_telemetry: {
...telemetry,
metadata: {
...telemetry.metadata,
"langsmith:trace": headers["langsmith-trace"],
"langsmith:baggage": headers["baggage"],
},
},
experimental_telemetry: AISDKExporter.getSettings({
functionId: "functionId",
metadata: { userId: "123", language: "english" },
}),
maxSteps: 10,
});

Expand Down
Loading

0 comments on commit 7a3424d

Please sign in to comment.