Skip to content

Commit 3f622ef

Browse files
committed
update our example again :)
1 parent 6997fef commit 3f622ef

File tree

2 files changed

+80
-17
lines changed

2 files changed

+80
-17
lines changed

public/docs/examples/osmControl.html

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,28 @@
6666
}).addTo(map);
6767

6868
// Function to zoom to a place
69-
async function zoomToPlace(placeName, isAiCall = false) {
70-
if (isAiCall) {
71-
document.getElementById('place-input').value = placeName;
72-
}
73-
74-
const response = await fetch(
75-
`https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(placeName)}`,
76-
);
77-
const data = await response.json();
69+
async function zoomToPlace(placeName) {
70+
try {
71+
const response = await fetch(
72+
`https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(placeName)}`,
73+
);
74+
const data = await response.json();
7875

79-
if (data && data.length > 0) {
80-
const { lat, lon } = data[0];
81-
map.setView([lat, lon], 13); // Adjust zoom level as needed
82-
return `Zoomed to ${placeName} at (${lat}, ${lon})`;
83-
} else {
84-
return `Place ${placeName} not found!`;
76+
if (data && data.length > 0) {
77+
const { lat, lon } = data[0];
78+
map.setView([lat, lon], 13); // Adjust zoom level as needed
79+
return {
80+
success: true,
81+
message: `Zoomed to ${placeName} at (${lat}, ${lon})`,
82+
};
83+
} else {
84+
return { success: false, message: `Place ${placeName} not found!` };
85+
}
86+
} catch (error) {
87+
return {
88+
success: false,
89+
message: `Error fetching data: ${error.message}`,
90+
};
8591
}
8692
}
8793

@@ -118,7 +124,7 @@
118124
id: 'mapControl.V1',
119125
name: 'zoomToPlace',
120126
description:
121-
'This function can zoom to places on a map on the same webpage.',
127+
'This function can be used by the AI to zoom a map displayed on the webpage to a specified place. It returns feedback based on the success of the operation.',
122128
parameters: {
123129
type: 'object',
124130
properties: {
@@ -129,7 +135,7 @@
129135
},
130136
required: ['placeName'],
131137
},
132-
function: (params) => zoomToPlace(params.placeName, true), // Assign the zoom function here
138+
function: (params) => zoomToPlace(params.placeName),
133139
},
134140
];
135141
</script>

public/docs/tools.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,63 @@ Users can add custom tools through the [Tools Manager](/tools), where they can c
2222
#### Best Practice
2323

2424
- Tools should try to always return a value. This give taskyon the feedback whether a tool was succesful or not.
25+
for example in the function below instead of simply zooming in to the location, we return a string
26+
based on the success of the function.
27+
28+
```javascript
29+
async function zoomToPlace(placeName) {
30+
try {
31+
const response = await fetch(
32+
`https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(placeName)}`,
33+
);
34+
const data = await response.json();
35+
36+
if (data && data.length > 0) {
37+
const { lat, lon } = data[0];
38+
map.setView([lat, lon], 13); // Adjust zoom level as needed
39+
return {
40+
success: true,
41+
message: `Zoomed to ${placeName} at (${lat}, ${lon})`,
42+
};
43+
} else {
44+
return { success: false, message: `Place ${placeName} not found!` };
45+
}
46+
} catch (error) {
47+
return { success: false, message: `Error fetching data: ${error.message}` };
48+
}
49+
}
50+
```
51+
52+
- Be sure that the function description tells the AI That it can use this function. Consider this function definition here for example:
53+
54+
```javascript
55+
const tools = [
56+
{
57+
id: 'mapControl.V1',
58+
name: 'zoomToPlace',
59+
description: 'Function to zoom in on a map',
60+
parameters: {
61+
type: 'object',
62+
properties: {
63+
placeName: {
64+
type: 'string',
65+
description: 'The name of the place to zoom to.',
66+
},
67+
},
68+
required: ['placeName'],
69+
},
70+
function: (params) => zoomToPlace(params.placeName, true), // Assign the zoom function here
71+
},
72+
];
73+
```
74+
75+
Better would be a description like this:
76+
77+
```javascript
78+
description: 'This function can be used by the AI to zoom a map displayed on the webpage to a specified place. It returns feedback based on the success of the operation.',
79+
```
80+
81+
Here we address the AI directly and also describe the context of this function in order to make the AI aware of it.
2582

2683
### AI-Assisted Tool Creation
2784

0 commit comments

Comments
 (0)