Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yuiseki committed Dec 29, 2024
1 parent 6e954bc commit b8f8848
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"NEOC",
"nonlatin",
"OPENAI",
"openstreetmap",
"OPFS",
"pageid",
"Panjshir",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BaseMessage, HumanMessage } from "@langchain/core/messages";
import { START, StateGraph } from "@langchain/langgraph";
import { RunnableConfig } from "@langchain/core/runnables";
import { loadSupervisorChain } from "./supervisor";
import { loadWikipediaAgent } from "./wikipedia";
import { loadWikipediaAgent } from "../../wikipedia";
import fs from "node:fs/promises";
import { ChatOllama } from "@langchain/ollama";

Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions src/utils/langchain/tools/osm/nominatim/geocode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Tool } from "langchain/tools";

export class Nominatim extends Tool {
name = "nominatim-geocode";
description = `useful for when you need to geocode address or name of location to latitude, longitude. Input: a address or a name of location.`;

async _call(input: string) {
console.debug("Tool nominatim, input:", input);
try {
const endpoint = "https://nominatim.openstreetmap.org/search";
const searchParams = new URLSearchParams();
searchParams.append("format", "jsonv2");
searchParams.append("polygon_geojson", "0");
searchParams.append("q", input);

const res = await fetch(`${endpoint}?${searchParams.toString()}`);
const json = await res.json();

if (json.length === 0) {
return "fetch failed. change query";
}

const answer = {
latitude: json[0].lat,
longitude: json[0].lon,
};

console.debug("Tool nominatim, answer:");
console.debug(answer);
console.debug("");
return answer;
} catch (error) {
return "I don't know.";
}
}
}
38 changes: 38 additions & 0 deletions src/utils/langchain/tools/osm/nominatim/osm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Tool } from "langchain/tools";

export class Nominatim extends Tool {
name = "nominatim-search-openstreetmap";
description = `useful for when you need to get OpenStreetMap entity by name of location. Input: a name of location.`;

async _call(input: string) {
console.debug("Tool nominatim, input:", input);
try {
const endpoint = "https://nominatim.openstreetmap.org/search";
const searchParams = new URLSearchParams();
searchParams.append("format", "jsonv2");
searchParams.append("polygon_geojson", "0");
searchParams.append("q", input);

const res = await fetch(`${endpoint}?${searchParams.toString()}`);
const json = await res.json();

if (json.length === 0) {
return "failed to fetch. change query";
}

const answer = {
osm_id: json[0].osm_id,
osm_type: json[0].osm_type,
name: json[0].name,
display_name: json[0].display_name,
};

console.debug("Tool nominatim, answer:");
console.debug(answer);
console.debug("");
return answer;
} catch (error) {
return "I don't know.";
}
}
}

0 comments on commit b8f8848

Please sign in to comment.