diff --git a/scripts/agent.ts b/scripts/agent.ts index 04872b5f..9ac840e1 100644 --- a/scripts/agent.ts +++ b/scripts/agent.ts @@ -8,6 +8,7 @@ import { createReactAgent } from "@langchain/langgraph/prebuilt"; // tool import { OverpassTokyoRamenCount } from "../src/utils/langchain/tools/osm/overpass/tokyo_ramen/index.ts"; +import { Wikipedia } from "../src/utils/langchain/tools/wikipedia/index.ts"; const model = new ChatOllama({ // 速いがツールを使わずに返答しちゃう @@ -23,14 +24,16 @@ const model = new ChatOllama({ // 速いがツールを使わずに返答しちゃう // model: "granite3-moe:3b", // 遅いが正確に動く - model: "qwen2.5:7b", + // model: "qwen2.5:7b", + // 12GB VRAMギリギリ + model: "qwen2.5:14b", temperature: 0, }); export const loadAgent = async (model: BaseChatModel) => { const tools: Array = [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."; + "You are a specialist of ramen shops. Be sure to use overpass-tokyo-ramen-count tool and reply based on the results. You can only use one tool at a time. Before you answer, think if you are right."; return createReactAgent({ llm: model, tools: tools, @@ -41,15 +44,21 @@ export const loadAgent = async (model: BaseChatModel) => { const agent = await loadAgent(model); // Use the agent -const stream = await agent.stream( - { - messages: [new HumanMessage("東京都台東区のラーメン屋の数を教えて")], - }, - { - streamMode: "values", - recursionLimit: 10, - } -); +const stream = await agent + .withConfig({ + maxConcurrency: 1, + }) + .stream( + { + messages: [ + new HumanMessage("東京都23区で一番ラーメン屋が多いのはどこ?"), + ], + }, + { + streamMode: "values", + recursionLimit: 100, + } + ); for await (const chunk of stream) { const lastMessage = chunk.messages[chunk.messages.length - 1]; const type = lastMessage._getType(); @@ -58,7 +67,7 @@ for await (const chunk of stream) { console.dir( { type, - content: content.length < 100 ? content : content.slice(0, 100) + "...", + content: content.length < 200 ? content : content.slice(0, 200) + "...", toolCalls, }, { depth: null } diff --git a/src/utils/langchain/tools/osm/overpass/tokyo_ramen/index.ts b/src/utils/langchain/tools/osm/overpass/tokyo_ramen/index.ts index 2b98a91f..b1eaf542 100644 --- a/src/utils/langchain/tools/osm/overpass/tokyo_ramen/index.ts +++ b/src/utils/langchain/tools/osm/overpass/tokyo_ramen/index.ts @@ -30,9 +30,10 @@ out geom;`; return "failed to fetch. change query"; } - const answer = json.elements.length; - // console.debug("Tool: OverpassTokyoRamenCount, answer:"); - // console.debug(answer); + let answer = json.elements.length; + answer = `${input}には${answer}軒のラーメン屋があります。`; + console.debug("Tool: OverpassTokyoRamenCount, answer:"); + console.debug("\t" + answer); // console.debug(""); return answer; } catch (error) { diff --git a/src/utils/langchain/tools/wikipedia/index.ts b/src/utils/langchain/tools/wikipedia/index.ts index d5e6b437..53fa0c3c 100644 --- a/src/utils/langchain/tools/wikipedia/index.ts +++ b/src/utils/langchain/tools/wikipedia/index.ts @@ -2,7 +2,7 @@ import { Tool } from "langchain/tools"; export class Wikipedia extends Tool { name = "search-wikipedia"; - description = `useful for when you need to ask general questions about people, places, companies, facts, historical events, or other subjects. Input: a very simple search query for wikipedia. MUST NOT input the question as-is.`; + description = `useful for when you need to ask general questions about people, places, companies, facts, historical events, or other subjects. Input: a very simple search query for wikipedia in English. MUST NOT input the question as-is.`; async _call(input: string) { // console.debug("Tool wikipedia, input:", input);