Skip to content

Documentation v1.0.0


Documentation / @openassistant/osm / routing

Variable: routing

const routing: OpenAssistantTool<typeof routingParameters, RoutingLlmResult, RoutingAdditionalData, MapboxToolContext>

Defined in: routing.ts:146

Routing Tool

This tool calculates routes between two points using Mapbox's Directions API. It supports different transportation modes (driving, walking, cycling) and returns detailed route information including distance, duration, and turn-by-turn directions.

TIP

If you don't know the coordinates of the origin or destination point, you can use the geocoding tool to get it.

Example user prompts:

  • "Find the driving route from Times Square to Central Park"
  • "How do I walk from the Eiffel Tower to the Louvre?"
  • "Get cycling directions from my current location to the nearest coffee shop"

Example

typescript
import { geocoding, routing, RoutingTool, GeocodingTool } from "@openassistant/osm";
import { convertToVercelAiTool, ToolCache } from '@openassistant/utils';
import { generateText } from 'ai';

// you can use ToolCache to save the routing dataset for later use
const toolResultCache = ToolCache.getInstance();

const routingTool: RoutingTool = {
  ...routing,
  toolContext: {
    getMapboxToken: () => process.env.MAPBOX_TOKEN!,
  },
  onToolCompleted: (toolCallId, additionalData) => {
    toolResultCache.addDataset(toolCallId, additionalData);
  },
};

generateText({
  model: openai('gpt-4.1', { apiKey: key }),
  prompt: 'Find the driving route from Times Square to Central Park',
  tools: {
    geocoding: convertToVercelAiTool(geocoding),
    routing: convertToVercelAiTool(routingTool),
  },
});

Released under the MIT License.