Skip to content

Commit d7082de

Browse files
committed
hf-begin
1 parent 7983bf6 commit d7082de

17 files changed

Lines changed: 409 additions & 0 deletions

demo/hf-inference/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HF_API_KEY=

demo/hf-inference/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# dependencies (bun install)
2+
node_modules
3+
4+
# output
5+
out
6+
dist
7+
*.tgz
8+
9+
# code coverage
10+
coverage
11+
*.lcov
12+
13+
# logs
14+
logs
15+
_.log
16+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
17+
18+
# dotenv environment variable files
19+
.env
20+
.env.development.local
21+
.env.test.local
22+
.env.production.local
23+
.env.local
24+
25+
# caches
26+
.eslintcache
27+
.cache
28+
*.tsbuildinfo
29+
30+
# IntelliJ based IDEs
31+
.idea
32+
33+
# Finder (MacOS) folder config
34+
.DS_Store

demo/hf-inference/bun.lock

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/hf-inference/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello via Bun!");

demo/hf-inference/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "hf-inference",
3+
"module": "index.ts",
4+
"type": "module",
5+
"private": true,
6+
"scripts": {
7+
"start": "bun ./src/index.ts",
8+
"build:docs": "bun ./src/docs.mjs"
9+
},
10+
"devDependencies": {
11+
"@types/bun": "latest"
12+
},
13+
"peerDependencies": {
14+
"typescript": "^5"
15+
},
16+
"dependencies": {
17+
"@huggingface/inference": "^4.7.1",
18+
"agent-swarm-kit": "^1.1.141",
19+
"functools-kit": "^1.0.89",
20+
"plantuml": "^0.0.2"
21+
}
22+
}

demo/hf-inference/src/docs.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { dumpDocs } from "agent-swarm-kit";
2+
import plantuml from "plantuml";
3+
4+
import "./logic/index";
5+
6+
await dumpDocs("demo/langchain-stream", './docs/chat', plantuml);
7+
8+
process.kill(process.pid);

demo/hf-inference/src/index.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import "./logic";
2+
3+
import { randomString } from "functools-kit";
4+
import { getAgentName, listenEvent, session } from "agent-swarm-kit";
5+
import readline from "readline";
6+
import { SwarmName } from "./logic/enum/SwarmName";
7+
8+
const rl = readline.createInterface({
9+
input: process.stdin,
10+
output: process.stdout,
11+
});
12+
13+
const CLIENT_ID = randomString();
14+
15+
const { complete } = session(CLIENT_ID, SwarmName.RootSwarm);
16+
17+
const askQuestion = () => {
18+
rl.question("pharma-bot => ", async (input) => {
19+
if (input === "exit") {
20+
rl.close();
21+
return;
22+
}
23+
console.time("Timing");
24+
const data = await complete(input);
25+
process.stdout.write("\n");
26+
console.timeEnd("Timing");
27+
28+
process.stdout.write(`[${await getAgentName(CLIENT_ID)}]: ${data}`);
29+
30+
askQuestion();
31+
});
32+
};
33+
34+
askQuestion();
35+
36+
listenEvent(CLIENT_ID, "llm-new-token", (token: string) => {
37+
process.stdout.write(token);
38+
});
39+
40+
rl.on("close", () => {
41+
process.exit(0);
42+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { addAgent } from "agent-swarm-kit";
2+
import { AgentName } from "../enum/AgentName";
3+
import { CompletionName } from "../enum/CompletionName";
4+
import { str } from "functools-kit";
5+
import { ToolName } from "../enum/ToolName";
6+
7+
addAgent({
8+
docDescription: "This agent functions as a pharmaceutical seller within the langchain-stream project, providing real-time consultations on pharma products using the CohereCompletion, which streams pending tokens via LangChain alongside Ollama and LMStudio completions, and it employs the AddToCartTool only when necessary to facilitate purchases.",
9+
agentName: AgentName.TriageAgent,
10+
completion: CompletionName.HfCompletion,
11+
prompt: str.newline(
12+
"You are the pharma seller agent.",
13+
"Provide me the consultation about the pharma product"
14+
),
15+
system: [
16+
`To add the pharma product to the cart call the next tool: ${ToolName.AddToCartTool}`,
17+
"Call the tools only when nessesary, if not required, just speek with users",
18+
],
19+
tools: [ToolName.AddToCartTool],
20+
dependsOn: [],
21+
});
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import {
2+
addCompletion,
3+
type ICompletionArgs,
4+
type IModelMessage,
5+
} from "agent-swarm-kit";
6+
import { CompletionName } from "../enum/CompletionName";
7+
import { InferenceClient } from "@huggingface/inference";
8+
9+
const client = new InferenceClient(process.env.HF_API_KEY);
10+
11+
addCompletion({
12+
completionName: CompletionName.HfCompletion,
13+
getCompletion: async ({
14+
agentName,
15+
messages: rawMessages,
16+
mode,
17+
tools: rawTools,
18+
}: ICompletionArgs): Promise<IModelMessage> => {
19+
const messages = rawMessages.map(
20+
({ role, content, tool_calls, tool_call_id }) => {
21+
if (role === "tool") {
22+
return {
23+
role: "tool" as const,
24+
content,
25+
tool_call_id: tool_call_id!,
26+
};
27+
}
28+
if (role === "assistant" && tool_calls) {
29+
return {
30+
role: "assistant" as const,
31+
content,
32+
tool_calls: tool_calls.map((tc) => ({
33+
id: tc.id,
34+
type: tc.type,
35+
function: {
36+
name: tc.function.name,
37+
arguments:
38+
typeof tc.function.arguments === "string"
39+
? tc.function.arguments
40+
: JSON.stringify(tc.function.arguments),
41+
},
42+
})),
43+
};
44+
}
45+
return {
46+
role: role as "user" | "assistant" | "system",
47+
content,
48+
};
49+
}
50+
);
51+
52+
const tools = rawTools?.map(({ function: f }) => ({
53+
type: "function" as const,
54+
function: {
55+
name: f.name,
56+
description: f.description,
57+
parameters: f.parameters,
58+
},
59+
}));
60+
61+
const completion = await client.chatCompletion({
62+
model: "openai/gpt-oss-120b",
63+
messages,
64+
...(tools && { tools }),
65+
});
66+
67+
const choice = completion.choices[0];
68+
const text = choice.message.content || "";
69+
const tool_calls = choice.message.tool_calls || [];
70+
71+
const result = {
72+
content: text,
73+
mode,
74+
agentName: agentName!,
75+
role: "assistant" as const,
76+
tool_calls: tool_calls.map(({ id, type, function: f }) => ({
77+
id: id!,
78+
type: type as "function",
79+
function: {
80+
name: f.name,
81+
arguments:
82+
typeof f.arguments === "string"
83+
? JSON.parse(f.arguments)
84+
: f.arguments,
85+
},
86+
})),
87+
};
88+
89+
return result;
90+
},
91+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export enum AgentName {
2+
TriageAgent = "triage_agent",
3+
}

0 commit comments

Comments
 (0)