Created
September 9, 2024 18:58
-
-
Save kleneway/e07c94e66133d65ec93480766407dd63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
defineBehaviorModule, | |
defineTool, | |
createVdotMachine, | |
sendMessage, | |
Content, | |
} from "@pioneersquarelabs/vdot"; | |
import { z } from "zod"; | |
// Define the weather tool | |
const getWeatherForecast = defineTool({ | |
name: "getWeatherForecast", | |
description: "Get weather forecast for a specific location and date range", | |
parameters: { | |
location: z.string(), | |
startDate: z.string(), | |
endDate: z.string(), | |
}, | |
}); | |
// Define the activity suggestion tool | |
const suggestActivities = defineTool({ | |
name: "suggestActivities", | |
description: "Suggest activities based on location and weather conditions", | |
parameters: { | |
location: z.string(), | |
weather: z.string(), | |
}, | |
}); | |
// Define the user data type | |
type TravelPlannerData = { | |
destination: string; | |
startDate: string; | |
endDate: string; | |
weatherForecast: string; | |
suggestedActivities: string[]; | |
}; | |
// Define the behavior module | |
const travelPlannerUDB = defineBehaviorModule({ | |
toolbox: [getWeatherForecast, suggestActivities], | |
initialize: () => { | |
return { | |
systemPrompt: "You are a helpful travel planning assistant. Ask the user for their travel destination and dates, then provide weather information and suggest activities.", | |
toolChoice: "auto", | |
options: { | |
model: "claude-3-sonnet-20240229", | |
tokenConstraints: { maxOutput: 1024 }, | |
temperature: 0.7, | |
}, | |
userData: { | |
destination: "", | |
startDate: "", | |
endDate: "", | |
weatherForecast: "", | |
suggestedActivities: [], | |
} as TravelPlannerData, | |
}; | |
}, | |
onConversationMessage: async (systemState, message) => { | |
// Update user data based on the conversation | |
const content = message.content[0].content.toLowerCase(); | |
if (content.includes("destination")) { | |
systemState.userData.destination = content.split("destination:")[1].trim(); | |
} else if (content.includes("dates")) { | |
const dates = content.split("dates:")[1].trim().split("to"); | |
systemState.userData.startDate = dates[0].trim(); | |
systemState.userData.endDate = dates[1].trim(); | |
} | |
}, | |
onToolResult: async (systemState, toolCall) => { | |
if (toolCall.name === "getWeatherForecast") { | |
systemState.userData.weatherForecast = toolCall.args.forecast; | |
} else if (toolCall.name === "suggestActivities") { | |
systemState.userData.suggestedActivities = toolCall.args.activities; | |
} | |
}, | |
}); | |
// Create the VDOT machine | |
const travelPlannerMachine = createVdotMachine( | |
"travel-planner", | |
chat, // Assume we have a chat function defined elsewhere | |
travelPlannerUDB | |
); | |
// Example usage | |
async function runTravelPlanner() { | |
const machine = travelPlannerMachine; | |
// Start the conversation | |
let output = await sendMessage(machine, [ | |
{ type: "text", content: "Hi, I'd like to plan a trip.", block: 1 }, | |
]); | |
console.log(output.messageSequence.messages[1].content[0].content); | |
// Provide destination | |
output = await sendMessage(machine, [ | |
{ type: "text", content: "My destination is Paris, France.", block: 2 }, | |
]); | |
console.log(output.messageSequence.messages[3].content[0].content); | |
// Provide dates | |
output = await sendMessage(machine, [ | |
{ type: "text", content: "My travel dates are from 2024-06-15 to 2024-06-22.", block: 3 }, | |
]); | |
console.log(output.messageSequence.messages[5].content[0].content); | |
// Ask for weather information | |
output = await sendMessage(machine, [ | |
{ type: "text", content: "What's the weather forecast for my trip?", block: 4 }, | |
]); | |
console.log(output.messageSequence.messages[7].content[0].content); | |
// Ask for activity suggestions | |
output = await sendMessage(machine, [ | |
{ type: "text", content: "Can you suggest some activities based on the weather?", block: 5 }, | |
]); | |
console.log(output.messageSequence.messages[9].content[0].content); | |
} | |
runTravelPlanner(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment