Skip to content

Commit

Permalink
ラーメン屋エージェント
Browse files Browse the repository at this point in the history
  • Loading branch information
yuiseki committed Dec 31, 2024
1 parent 322b1bc commit b47418f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test:counter": "node --loader ts-node/esm ./tests/counter.ts",
"test:sortByArea": "node --loader ts-node/esm ./tests/sortByArea.ts",
"test:sortByDistance": "node --loader ts-node/esm ./tests/sortByDistance.ts",
"agents": "tsx ./scripts/agents/index.ts",
"agent": "tsx --network-family-autoselection-attempt-timeout=500 ./scripts/agent.ts",
"site:api.reliefweb.int:fetch": "node --loader ts-node/esm ./scripts/api.reliefweb.int/fetch.ts",
"site:api.reliefweb.int:update": "node --loader ts-node/esm ./scripts/api.reliefweb.int/update_disasters.ts",
"site:api.reliefweb.int:extract": "node --loader ts-node/esm ./scripts/api.reliefweb.int/extract.ts",
Expand Down
22 changes: 19 additions & 3 deletions scripts/agent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ChatOllama } from "@langchain/ollama";
import { HumanMessage } from "@langchain/core/messages";
import { Tool } from "@langchain/core/tools";
import { BaseChatModel } from "@langchain/core/language_models/chat_models";

// langgraph
import { loadWikipediaAgent } from "./agents/wikipedia.ts";
import { createReactAgent } from "@langchain/langgraph/prebuilt";

// tool
import { OverpassTokyoRamenCount } from "../src/utils/langchain/tools/osm/overpass/tokyo_ramen/index.ts";

const model = new ChatOllama({
// 速いがツールを使わずに返答しちゃう
Expand All @@ -22,12 +27,23 @@ const model = new ChatOllama({
temperature: 0,
});

const agent = await loadWikipediaAgent(model);
export const loadAgent = async (model: BaseChatModel) => {
const tools: Array<Tool> = [new OverpassTokyoRamenCount()];
const prompt =
"You are a specialist of ramen shops. Be sure to use overpass-tokyo-ramen-count tool and reply based on the results. You have up to 10 chances to use tool.";
return createReactAgent({
llm: model,
tools: tools,
stateModifier: prompt,
});
};

const agent = await loadAgent(model);

// Use the agent
const stream = await agent.stream(
{
messages: [new HumanMessage("Who is the president of the United States?")],
messages: [new HumanMessage("東京都台東区のラーメン屋の数を教えて")],
},
{
streamMode: "values",
Expand Down
22 changes: 22 additions & 0 deletions scripts/tool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { OverpassTokyoRamenCount } from "../src/utils/langchain/tools/osm/overpass/tokyo_ramen";
import { AIMessage } from "@langchain/core/messages";

import { ToolNode } from "@langchain/langgraph/prebuilt";

const tools = [new OverpassTokyoRamenCount()];
const toolNode = new ToolNode(tools);

const messageWithSingleToolCall = new AIMessage({
content: "",
tool_calls: [
{
name: "overpass-tokyo-ramen-count",
args: { input: "台東区" },
id: "tool_call_id",
type: "tool_call",
},
],
});

const res = await toolNode.invoke({ messages: [messageWithSingleToolCall] });
console.log(res);
23 changes: 15 additions & 8 deletions src/utils/langchain/tools/osm/overpass/tokyo_ramen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Tool } from "langchain/tools";

export class OverpassTokyoRamenCount extends Tool {
name = "overpass-tokyo-ramen-count";
description = `useful for when you need to count number of ramen shops by a name of area. Input: a name of area.`;
description = `useful for when you need to count number of ramen shops by a name of area. Input: a name of area in Tokyo in Japanese.`;

async _call(input: string) {
console.debug("Tool: OverpassTokyoRamenCount, input:", input);
// console.debug("Tool: OverpassTokyoRamenCount, input:", input);
try {
const overpassQuery = `[out:json][timeout:30000];
area["name"="東京都"]->.outer;
Expand All @@ -15,22 +15,29 @@ area["name"="${input}"]->.inner;
);
out geom;`;
const queryString = `data=${encodeURIComponent(overpassQuery)}`;
const overpassApiUrl = `https://z.overpass-api.de/api/interpreter?${queryString}`;
const res = await fetch(overpassApiUrl);
const overpassApiUrl = `https://overpass-api.de/api/interpreter`;

const res = await fetch(overpassApiUrl, {
method: "POST",
body: queryString,
headers: {
"Content-Type": "application/json",
},
});
const json = await res.json();

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

const answer = json.elements.length;
console.debug("Tool: OverpassTokyoRamenCount, answer:");
console.debug(answer);
console.debug("");
// console.debug("Tool: OverpassTokyoRamenCount, answer:");
// console.debug(answer);
// console.debug("");
return answer;
} catch (error) {
return "I don't know.";
console.error("Tool: OverpassTokyoRamenCount, error:", error);
return "Error. Please try again.";
}
}
}

0 comments on commit b47418f

Please sign in to comment.