Skip to content

Documentation v1.0.0


Documentation / @openassistant/map / leaflet

Variable: leaflet

const leaflet: OpenAssistantTool<typeof leafletParameters, LeafletToolLlmResult, LeafletToolAdditionalData, MapToolContext>

Defined in: leaflet/tool.ts:109

The leaflet tool is used to create a leaflet map from GeoJSON data.

INFO

This tool should be used in Browser environment.

Example

ts
import { leaflet, LeafletTool } from '@openassistant/map';
import { convertToVercelAiTool, ToolCache } from '@openassistant/utils';
import { generateText } from 'ai';

const leafletTool: LeafletTool = {
  ...leaflet,
  context: {
    getDataset: async (datasetName) => {
      return YOUR_DATASET;
    },
  },
};

generateText({
  model: openai('gpt-4.1', { apiKey: key }),
  prompt: 'Create a leaflet map using the dataset "my_venues"',
  tools: {createMap: convertToVercelAiTool(leafletTool)},
});

TIP

You can use the downloadMapData tool with the leaflet tool to download a dataset from a geojson from a url and use it to create a map.

Example

ts
import { downloadMapData, isDownloadMapAdditionalData, leaflet, LeafletTool } from '@openassistant/map';
import { convertToVercelAiTool, ToolCache } from '@openassistant/utils';
import { generateText } from 'ai';

const toolResultCache = ToolCache.getInstance();

const downloadMapTool = {
  ...downloadMapData,
  onToolCompleted: (toolCallId: string, additionalData?: unknown) => {
    toolResultCache.addDataset(toolCallId, additionalData);
  },
};

const leafletTool: LeafletTool = {
  ...leaflet,
  context: {
    getDataset: async (datasetName) => {
      // find dataset based on datasetName first
      // return MYDATASETS[datasetName];

      // if no dataset is found, check if dataset is in toolResultCache
      if (toolResultCache.hasDataset(datasetName)) {
        return toolResultCache.getDataset(datasetName);
      }
      throw new Error(`Dataset ${datasetName} not found`);
    },
  },
};

generateText({
  model: openai('gpt-4.1', { apiKey: key }),
  prompt: 'Create a leaflet map using the dataset "my_venues"',
  tools: {createMap: convertToVercelAiTool(leafletTool), downloadMapData: convertToVercelAiTool(downloadMapTool)},
});

Released under the MIT License.