Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yuiseki committed Dec 31, 2024
1 parent b47418f commit 03ee13a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
33 changes: 21 additions & 12 deletions scripts/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
// 速いがツールを使わずに返答しちゃう
Expand All @@ -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<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.";
"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,
Expand All @@ -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();
Expand All @@ -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 }
Expand Down
7 changes: 4 additions & 3 deletions src/utils/langchain/tools/osm/overpass/tokyo_ramen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/langchain/tools/wikipedia/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 03ee13a

Please sign in to comment.