Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/sample-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"run:with": "npm run build && node dist/src/sample_with.js",
"run:prompt_mgmt": "npm run build && node dist/src/sample_prompt_mgmt.js",
"run:vercel": "npm run build && node dist/src/sample_vercel_ai.js",
"run:vercel_object": "npm run build && node dist/src/sample_vercel_ai_object.js",
"run:vercel_tools": "npm run build && node dist/src/sample_vercel_ai_tools.js",
"debug:tool_calls": "npm run build && node dist/src/debug_tool_calls.js",
Comment thread
nirga marked this conversation as resolved.
Outdated
"run:sample_vision": "npm run build && node dist/src/sample_vision_prompt.js",
"run:sample_azure": "npm run build && node dist/src/sample_azure.js",
"run:openai_streaming": "npm run build && node dist/src/sample_openai_streaming.js",
Expand Down
22 changes: 14 additions & 8 deletions packages/sample-app/src/sample_experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import type {
TaskOutput,
} from "@traceloop/node-server-sdk";

import "dotenv/config";


const main = async () => {
console.log("Starting sample experiment");
traceloop.initialize({
Expand Down Expand Up @@ -70,9 +73,12 @@ const main = async () => {
max_tokens: 500,
});

const completion = answer.choices?.[0]?.message?.content || "";

return {
completion: answer.choices?.[0]?.message?.content || "",
completion: completion,
prompt: promptText,
answer: completion,
};
};

Expand All @@ -88,7 +94,7 @@ const main = async () => {
return {
completion: answer,
prompt: promptText,
strategy: "provide_info",
answer,
};
};

Expand All @@ -115,10 +121,10 @@ const main = async () => {
const loader1 = startLoader(" Processing experiment");

const results1 = await client.experiment.run(medicalTaskRefuseAdvice, {
datasetSlug: "medical-q",
datasetSlug: "ai-doctor-dataset",
datasetVersion: "v1",
evaluators: ["medical_advice"],
experimentSlug: "medical-advice-exp-ts",
evaluators: ["Medical Advice Given"],
experimentSlug: "medical-advice-experiment",
stopOnError: false,
});

Expand All @@ -137,10 +143,10 @@ const main = async () => {
const loader2 = startLoader(" Processing experiment");

const results2 = await client.experiment.run(medicalTaskProvideInfo, {
datasetSlug: "medical-q",
datasetSlug: "ai-doctor-dataset",
datasetVersion: "v1",
evaluators: ["medical_advice"],
experimentSlug: "medical-advice-exp-ts",
evaluators: ["Medical Advice Given"],
experimentSlug: "medical-advice-experiment",
stopOnError: false,
waitForResults: true,
});
Expand Down
2 changes: 2 additions & 0 deletions packages/sample-app/src/sample_vercel_ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as traceloop from "@traceloop/node-server-sdk";
import { openai } from "@ai-sdk/openai";
import { generateText } from "ai";

import "dotenv/config";

traceloop.initialize({
appName: "sample_vercel_ai",
disableBatch: true,
Expand Down
49 changes: 49 additions & 0 deletions packages/sample-app/src/sample_vercel_ai_object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as traceloop from "@traceloop/node-server-sdk";
import { openai } from "@ai-sdk/openai";
import { generateObject } from "ai";
import { z } from "zod";

import "dotenv/config";

traceloop.initialize({
appName: "sample_vercel_ai_object",
disableBatch: true,
});

const PersonSchema = z.object({
name: z.string(),
age: z.number(),
occupation: z.string(),
skills: z.array(z.string()),
location: z.object({
city: z.string(),
country: z.string(),
}),
});

async function generatePersonProfile(description: string) {
return await traceloop.withWorkflow(
{ name: "generate_person_profile" },
async () => {
const { object } = await generateObject({
model: openai("gpt-4o"),
Comment thread
nirga marked this conversation as resolved.
schema: PersonSchema,
prompt: `Based on this description, generate a detailed person profile: ${description}`,
experimental_telemetry: { isEnabled: true },
});

return object;
},
{ description },
);
}

async function main() {
const profile = await generatePersonProfile(
"A talented software engineer from Paris who loves working with AI and machine learning, speaks multiple languages, and enjoys traveling.",
);

console.log("Generated person profile:", JSON.stringify(profile, null, 2));
}

main().catch(console.error);
138 changes: 138 additions & 0 deletions packages/sample-app/src/sample_vercel_ai_tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import * as traceloop from "@traceloop/node-server-sdk";
import { openai } from "@ai-sdk/openai";
import { generateText, tool } from "ai";
import { z } from "zod";

import "dotenv/config";

traceloop.initialize({
appName: "sample_vercel_ai_tools",
disableBatch: true,
});

// Define tools
const getWeather = tool({
description: "Get the current weather for a specified location",
parameters: z.object({
location: z.string().describe("The location to get the weather for"),
}),
execute: async ({ location }) => {
console.log(`🔧 Tool 'getWeather' called with location: ${location}`);

// Simulate API call delay
await new Promise(resolve => setTimeout(resolve, 100));

// Simulate weather data
const weatherData = {
location,
temperature: Math.floor(Math.random() * 30) + 60, // 60-90°F
condition: ["Sunny", "Cloudy", "Rainy", "Snowy"][Math.floor(Math.random() * 4)],
humidity: Math.floor(Math.random() * 40) + 40, // 40-80%
};

console.log(`🌤️ Weather data retrieved for ${location}:`, weatherData);
return weatherData;
},
});

const calculateDistance = tool({
description: "Calculate the distance between two cities",
parameters: z.object({
fromCity: z.string().describe("The starting city"),
toCity: z.string().describe("The destination city"),
}),
execute: async ({ fromCity, toCity }) => {
console.log(`🔧 Tool 'calculateDistance' called from ${fromCity} to ${toCity}`);

// Simulate API call delay
await new Promise(resolve => setTimeout(resolve, 150));

// Simulate distance calculation
const distance = Math.floor(Math.random() * 2000) + 100; // 100-2100 miles
const result = {
from: fromCity,
to: toCity,
distance: `${distance} miles`,
drivingTime: `${Math.floor(distance / 60)} hours`,
};

console.log(`🗺️ Distance calculated:`, result);
return result;
},
});

const searchRestaurants = tool({
description: "Search for restaurants in a specific city",
parameters: z.object({
city: z.string().describe("The city to search for restaurants"),
cuisine: z.string().optional().describe("Optional cuisine type (e.g., Italian, Mexican)"),
}),
execute: async ({ city, cuisine }) => {
console.log(`🔧 Tool 'searchRestaurants' called for ${city}${cuisine ? ` (${cuisine} cuisine)` : ""}`);

// Simulate API call delay
await new Promise(resolve => setTimeout(resolve, 200));

// Simulate restaurant data
const restaurantNames = [
"The Golden Fork", "Sunset Bistro", "Ocean View", "Mountain Top",
"Urban Kitchen", "Garden Cafe", "Heritage House", "Modern Table"
];

const restaurants = Array.from({ length: 3 }, (_, i) => ({
name: restaurantNames[Math.floor(Math.random() * restaurantNames.length)],
cuisine: cuisine || ["Italian", "Mexican", "Asian", "American"][Math.floor(Math.random() * 4)],
rating: (Math.random() * 2 + 3).toFixed(1), // 3.0-5.0 rating
priceRange: ["$", "$$", "$$$"][Math.floor(Math.random() * 3)],
}));

console.log(`🍽️ Found ${restaurants.length} restaurants in ${city}:`, restaurants);
return { city, restaurants };
},
});

async function planTrip(destination: string) {
return await traceloop.withWorkflow(
{ name: "plan_trip" },
async () => {
console.log(`\n🌟 Planning a trip to ${destination}...\n`);

const result = await generateText({
model: openai("gpt-4o"),
prompt: `Help me plan a trip to ${destination}. I'd like to know:
1. What's the weather like there?
2. Find some good restaurants to try
3. If I'm traveling from New York, how far is it?

Please use the available tools to get current information and provide a comprehensive travel guide.`,
tools: {
getWeather,
calculateDistance,
searchRestaurants,
},
maxSteps: 5, // Allow multiple tool calls
experimental_telemetry: { isEnabled: true },
});

return result.text;
},
{ destination },
);
}

async function main() {
try {
const travelGuide = await planTrip("San Francisco");

console.log("\n" + "=".repeat(80));
console.log("🗺️ TRAVEL GUIDE");
console.log("=".repeat(80));
console.log(travelGuide);
console.log("=".repeat(80));

} catch (error) {
console.error("❌ Error planning trip:", error);
}
}

main().catch(console.error);
Loading
Loading