@agents-eco/reasoning 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 agents.eco
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,365 @@
1
+ <p align="center">
2
+ <img src="icon.png" alt="Reasoning Module" width="160" />
3
+ </p>
4
+
5
+ <h1 align="center">Reasoning Module</h1>
6
+
7
+ <p align="center">
8
+ <strong>Multi-strategy reasoning for AI agents — ReAct, Chain-of-Thought, Tree-of-Thought.</strong><br/>
9
+ Built by <a href="https://agents.eco">agents.eco</a> — the decentralized AI agent economy.
10
+ </p>
11
+
12
+ <p align="center">
13
+ <a href="https://www.npmjs.com/package/@agents-eco/reasoning"><img src="https://img.shields.io/npm/v/@agents-eco/reasoning?style=flat-square" alt="npm" /></a>
14
+ <a href="https://github.com/agents-eco/reasoning/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue?style=flat-square" alt="MIT License" /></a>
15
+ <a href="https://github.com/agents-eco/reasoning"><img src="https://img.shields.io/github/stars/agents-eco/reasoning?style=flat-square" alt="GitHub stars" /></a>
16
+ </p>
17
+
18
+ ```
19
+ npm install @agents-eco/reasoning
20
+ ```
21
+
22
+ ---
23
+
24
+ ## Why This Exists
25
+
26
+ Most agent frameworks treat reasoning as a black box. You send a prompt, you get an answer. There is no visibility into how the agent thinks, no way to swap strategies, and no structured output to inspect.
27
+
28
+ This module makes reasoning explicit. Every thought, action, observation, and reflection is a structured step you can log, inspect, and hook into.
29
+
30
+ Three reasoning strategies, each suited to different problem types:
31
+
32
+ - **ReAct** — Multi-step reason-act loop with tool calling. Inspired by SmolAgents and Yao et al. 2022. Best for tasks requiring external information or computation.
33
+ - **Chain-of-Thought (CoT)** — Sequential step-by-step reasoning with optional self-reflection. Best for logic, math, and analytical problems.
34
+ - **Tree-of-Thought (ToT)** — Explores multiple reasoning paths using beam search. Best for complex problems with multiple valid approaches.
35
+
36
+ Two backend categories:
37
+
38
+ - **OpenAI-compatible** — OpenAI, Venice, Groq, Ollama, agents.eco. Any API that speaks the OpenAI chat format.
39
+ - **HuggingFace** — Inference API (serverless), Inference Endpoints (dedicated), or self-hosted TGI. Run SmolAgents-recommended models like Qwen, Llama, DeepSeek.
40
+
41
+ ## Quick Start
42
+
43
+ ### ReAct with Tool Calling
44
+
45
+ ```typescript
46
+ import { ReasoningEngine, veniceBackend } from "@agents-eco/reasoning";
47
+
48
+ const engine = new ReasoningEngine(
49
+ veniceBackend(process.env.VENICE_API_KEY!, "qwen3-4b"),
50
+ { strategy: "react", maxSteps: 10 }
51
+ );
52
+
53
+ // Register tools
54
+ engine.tool("search", {
55
+ name: "search",
56
+ description: "Search the web for information",
57
+ parameters: {
58
+ type: "object",
59
+ properties: { query: { type: "string" } },
60
+ required: ["query"],
61
+ },
62
+ }, async ({ query }) => {
63
+ const res = await fetch(`https://api.duckduckgo.com/?q=${query}&format=json`);
64
+ const data = await res.json();
65
+ return data.AbstractText || "No results found.";
66
+ });
67
+
68
+ const result = await engine.reason("What is the population of Tokyo?");
69
+ console.log(result.answer);
70
+ console.log(`Steps: ${result.steps.length}, LLM calls: ${result.llmCalls}`);
71
+ ```
72
+
73
+ ### Chain-of-Thought
74
+
75
+ ```typescript
76
+ import { ReasoningEngine, openaiBackend } from "@agents-eco/reasoning";
77
+
78
+ const engine = new ReasoningEngine(
79
+ openaiBackend(process.env.OPENAI_API_KEY!, "gpt-4o"),
80
+ { strategy: "cot", reflection: true }
81
+ );
82
+
83
+ const result = await engine.chainOfThought(
84
+ "A bat and a ball cost $1.10 in total. The bat costs $1.00 more than the ball. How much does the ball cost?"
85
+ );
86
+
87
+ for (const step of result.steps) {
88
+ console.log(`[${step.type}] ${step.content}`);
89
+ }
90
+ // Thought: Let x = ball price...
91
+ // Thought: x + (x + 1.00) = 1.10...
92
+ // Answer: The ball costs $0.05
93
+ ```
94
+
95
+ ### Tree-of-Thought
96
+
97
+ ```typescript
98
+ import { ReasoningEngine, groqBackend } from "@agents-eco/reasoning";
99
+
100
+ const engine = new ReasoningEngine(
101
+ groqBackend(process.env.GROQ_API_KEY!, "llama-3.3-70b-versatile"),
102
+ { strategy: "tot" }
103
+ );
104
+
105
+ const result = await engine.treeOfThought(
106
+ "Design a sorting algorithm that is optimal for nearly-sorted arrays.",
107
+ { branchFactor: 3, maxDepth: 4, beamWidth: 2, scoringMethod: "llm" }
108
+ );
109
+
110
+ console.log(result.answer);
111
+ console.log(`Explored ${result.steps.filter(s => s.type === "thought").length} branches`);
112
+ ```
113
+
114
+ ### HuggingFace / SmolAgents Models
115
+
116
+ ```typescript
117
+ import { ReasoningEngine, hfInferenceBackend, SMOLAGENTS_MODELS } from "@agents-eco/reasoning";
118
+
119
+ // Serverless Inference API
120
+ const engine = new ReasoningEngine(
121
+ hfInferenceBackend(process.env.HF_TOKEN!, SMOLAGENTS_MODELS.qwen72b),
122
+ { strategy: "react", maxSteps: 8 }
123
+ );
124
+
125
+ const result = await engine.reason("Explain the Riemann hypothesis.");
126
+ ```
127
+
128
+ ```typescript
129
+ import { hfEndpointBackend, tgiBackend } from "@agents-eco/reasoning";
130
+
131
+ // Dedicated Inference Endpoint
132
+ const endpoint = hfEndpointBackend(
133
+ process.env.HF_TOKEN!,
134
+ "https://xyz.endpoints.huggingface.cloud"
135
+ );
136
+
137
+ // Self-hosted TGI
138
+ const local = tgiBackend("http://localhost:8080");
139
+ ```
140
+
141
+ ### Local with Ollama
142
+
143
+ ```typescript
144
+ import { ReasoningEngine, ollamaBackend } from "@agents-eco/reasoning";
145
+
146
+ const engine = new ReasoningEngine(
147
+ ollamaBackend("llama3.2"),
148
+ { strategy: "cot", reflection: true }
149
+ );
150
+
151
+ const result = await engine.reason("What are the trade-offs between B-trees and LSM-trees?");
152
+ ```
153
+
154
+ ## Architecture
155
+
156
+ ```
157
+ ┌──────────────────────────────────────────────────────────┐
158
+ │ ReasoningEngine │
159
+ │ │
160
+ │ ┌─────────────────────────────────────────────────────┐ │
161
+ │ │ Strategies │ │
162
+ │ │ │ │
163
+ │ │ ┌──────────┐ ┌──────────────┐ ┌───────────────┐ │ │
164
+ │ │ │ ReAct │ │ Chain-of- │ │ Tree-of- │ │ │
165
+ │ │ │ │ │ Thought │ │ Thought │ │ │
166
+ │ │ │ Thought │ │ │ │ │ │ │
167
+ │ │ │ Action │ │ Step 1 │ │ [root] │ │ │
168
+ │ │ │ Observe │ │ Step 2 │ │ / | \ │ │ │
169
+ │ │ │ Reflect │ │ Step N │ │ A B C │ │ │
170
+ │ │ │ Plan │ │ Reflect │ │ / \ | │ │ │
171
+ │ │ │ Answer │ │ Answer │ │ D E F │ │ │
172
+ │ │ └──────────┘ └──────────────┘ └───────────────┘ │ │
173
+ │ └─────────────────────────────────────────────────────┘ │
174
+ │ │ │
175
+ │ ┌────────────────────────▼────────────────────────────┐ │
176
+ │ │ Tools │ │
177
+ │ │ search, calculate, fetch, execute, ... │ │
178
+ │ └─────────────────────────────────────────────────────┘ │
179
+ │ │ │
180
+ │ ┌────────────────────────▼────────────────────────────┐ │
181
+ │ │ LLM Backends │ │
182
+ │ │ │ │
183
+ │ │ OpenAI Venice Groq Ollama agents.eco │ │
184
+ │ │ HuggingFace (Inference API / Endpoints / TGI) │ │
185
+ │ └─────────────────────────────────────────────────────┘ │
186
+ └──────────────────────────────────────────────────────────┘
187
+ ```
188
+
189
+ ## Strategies
190
+
191
+ ### ReAct (Reason + Act)
192
+
193
+ The default strategy. Implements the multi-step reasoning loop from SmolAgents and the ReAct paper (Yao et al., 2022).
194
+
195
+ Each step follows: **Thought** -> **Action** -> **Observation**, with optional **Reflection** and **Planning**.
196
+
197
+ | Feature | Description |
198
+ |---------|-------------|
199
+ | Tool calling | Register handlers, agent decides when to use them |
200
+ | Reflection | Self-evaluation every N steps (configurable) |
201
+ | Planning | Initial plan + periodic revision |
202
+ | Self-correction | Agent retries on tool errors with different approach |
203
+ | Max steps | Hard limit to prevent infinite loops |
204
+
205
+ ### Chain-of-Thought (CoT)
206
+
207
+ Single-pass sequential reasoning. The LLM breaks the problem into numbered steps and arrives at an answer.
208
+
209
+ | Feature | Description |
210
+ |---------|-------------|
211
+ | Step parsing | Extracts individual reasoning steps from output |
212
+ | Self-reflection | Optional second pass to verify the chain |
213
+ | Revision | If reflection finds errors, produces a corrected answer |
214
+
215
+ ### Tree-of-Thought (ToT)
216
+
217
+ Explores multiple reasoning paths in parallel using beam search.
218
+
219
+ | Feature | Description |
220
+ |---------|-------------|
221
+ | Branch generation | LLM generates N distinct next steps at each node |
222
+ | Branch scoring | LLM-based or heuristic scoring (0-1) |
223
+ | Beam search | Keeps top-k branches at each depth |
224
+ | Path extraction | Best path is used to generate the final answer |
225
+
226
+ ## Hooks
227
+
228
+ Intercept every step of the reasoning process:
229
+
230
+ ```typescript
231
+ const engine = new ReasoningEngine(backend, {
232
+ strategy: "react",
233
+ maxSteps: 10,
234
+ hooks: {
235
+ onStep: (step) => console.log(`[${step.type}] ${step.content.slice(0, 80)}`),
236
+ onThought: (thought) => console.log(`Thinking: ${thought}`),
237
+ onAction: (tool, args) => console.log(`Calling: ${tool}(${JSON.stringify(args)})`),
238
+ onObservation: (result) => console.log(`Got: ${result.slice(0, 80)}`),
239
+ onReflection: (reflection) => console.log(`Reflecting: ${reflection}`),
240
+ onPlan: (plan) => console.log(`Plan: ${plan}`),
241
+ onAnswer: (answer) => console.log(`Answer: ${answer}`),
242
+ onError: (error) => console.error(`Error: ${error.message}`),
243
+ },
244
+ });
245
+ ```
246
+
247
+ ## SmolAgents-Recommended Models
248
+
249
+ Pre-configured model constants for HuggingFace:
250
+
251
+ | Key | Model | Notes |
252
+ |-----|-------|-------|
253
+ | `qwen72b` | Qwen/Qwen2.5-72B-Instruct | Best open reasoning model |
254
+ | `qwen32b` | Qwen/Qwen2.5-32B-Instruct | Good balance of speed and quality |
255
+ | `qwen7b` | Qwen/Qwen2.5-7B-Instruct | Fast, good for simple tasks |
256
+ | `llama70b` | meta-llama/Llama-3.3-70B-Instruct | Strong reasoning, open weights |
257
+ | `llama8b` | meta-llama/Llama-3.1-8B-Instruct | Fast Llama variant |
258
+ | `deepseek` | deepseek-ai/DeepSeek-R1-Distill-Qwen-32B | Code-focused reasoning |
259
+ | `mistral` | mistralai/Mistral-Small-24B-Instruct-2501 | Compact but capable |
260
+
261
+ ```typescript
262
+ import { SMOLAGENTS_MODELS } from "@agents-eco/reasoning";
263
+ console.log(SMOLAGENTS_MODELS.qwen72b); // "Qwen/Qwen2.5-72B-Instruct"
264
+ ```
265
+
266
+ ## Integration with Open Agentic Framework
267
+
268
+ Use as the reasoning backend for [@agents-eco/open-agentic-framework](https://github.com/agents-eco/open-agentic-framework):
269
+
270
+ ```typescript
271
+ import { Agent } from "@agents-eco/open-agentic-framework";
272
+ import { ReasoningEngine, veniceBackend } from "@agents-eco/reasoning";
273
+
274
+ const reasoning = new ReasoningEngine(
275
+ veniceBackend(process.env.VENICE_API_KEY!, "qwen3-4b"),
276
+ { strategy: "react", maxSteps: 8, reflection: true }
277
+ );
278
+
279
+ const agent = new Agent({
280
+ name: "reasoning-agent",
281
+ systemPrompt: "You are a thoughtful assistant that reasons step by step.",
282
+ provider: {
283
+ name: "venice",
284
+ apiKey: process.env.VENICE_API_KEY!,
285
+ baseUrl: "https://api.venice.ai/api/v1",
286
+ defaultModel: "qwen3-4b",
287
+ },
288
+ hooks: {
289
+ beforeRequest: async (messages) => {
290
+ // Use reasoning engine for complex queries
291
+ const lastMsg = messages[messages.length - 1];
292
+ if (lastMsg.role === "user" && lastMsg.content.length > 100) {
293
+ const result = await reasoning.reason(lastMsg.content);
294
+ messages.push({
295
+ role: "system",
296
+ content: `Reasoning trace:\n${result.steps.map(s => `[${s.type}] ${s.content}`).join("\n")}`,
297
+ });
298
+ }
299
+ return messages;
300
+ },
301
+ },
302
+ });
303
+ ```
304
+
305
+ ## API Reference
306
+
307
+ ### `ReasoningEngine`
308
+
309
+ | Method | Description |
310
+ |--------|-------------|
311
+ | `reason(task, overrides?)` | Run reasoning with configured strategy |
312
+ | `react(task, overrides?)` | Run ReAct reasoning |
313
+ | `chainOfThought(task, overrides?)` | Run CoT reasoning |
314
+ | `treeOfThought(task, overrides?)` | Run ToT reasoning |
315
+ | `tool(name, def, handler)` | Register a tool |
316
+ | `addTools(toolMap)` | Register multiple tools |
317
+ | `setBackend(backend)` | Swap the LLM backend |
318
+ | `setSystemPrompt(prompt)` | Set the system prompt |
319
+ | `configure(config)` | Update configuration |
320
+ | `hooks(hooks)` | Set event hooks |
321
+ | `registerStrategy(strategy)` | Add a custom strategy |
322
+
323
+ ### `ReasoningResult`
324
+
325
+ | Field | Type | Description |
326
+ |-------|------|-------------|
327
+ | `answer` | `string` | Final answer |
328
+ | `steps` | `ReasoningStep[]` | All reasoning steps |
329
+ | `strategy` | `StrategyType` | Strategy used |
330
+ | `durationMs` | `number` | Total time |
331
+ | `tokens` | `{prompt, completion, total}` | Token usage |
332
+ | `llmCalls` | `number` | Number of LLM calls |
333
+ | `toolCalls` | `number` | Number of tool calls |
334
+ | `success` | `boolean` | Whether reasoning succeeded |
335
+
336
+ ### `ReasoningStep`
337
+
338
+ | Field | Type | Description |
339
+ |-------|------|-------------|
340
+ | `type` | `StepType` | thought, action, observation, reflection, plan, revision, answer, error |
341
+ | `content` | `string` | Step content |
342
+ | `confidence` | `number` | Confidence score (0-1) |
343
+ | `toolCalls` | `ToolCall[]` | Tool calls made |
344
+ | `toolResults` | `ToolResult[]` | Tool results received |
345
+ | `durationMs` | `number` | Step duration |
346
+
347
+ ## Contributing
348
+
349
+ We welcome contributions. This project is early and there is room to shape its direction.
350
+
351
+ - **Add a strategy** — Implement the `ReasoningStrategy` interface for new approaches (Reflexion, MCTS, etc.)
352
+ - **Add a backend** — Anthropic, Cohere, local transformers
353
+ - **Improve prompts** — Better ReAct/CoT/ToT prompts for specific model families
354
+ - **Add benchmarks** — Test against GSM8K, MATH, HotpotQA, etc.
355
+ - **Report issues** — Bug reports and feature requests help us prioritize
356
+
357
+ ## License
358
+
359
+ MIT — [agents.eco](https://agents.eco)
360
+
361
+ ---
362
+
363
+ <p align="center">
364
+ Built by <a href="https://agents.eco">agents.eco</a> — the decentralized AI agent economy.
365
+ </p>
@@ -0,0 +1,75 @@
1
+ import type { LLMBackend, ChatMessage, CompletionOptions, LLMResponse } from "../types.js";
2
+ /**
3
+ * HuggingFace Inference API backend.
4
+ *
5
+ * Supports:
6
+ * - HuggingFace Inference API (serverless)
7
+ * - HuggingFace Inference Endpoints (dedicated)
8
+ * - Text Generation Inference (TGI) self-hosted
9
+ *
10
+ * Compatible with SmolAgents-style models that support chat completions.
11
+ * Works with models like Qwen, Llama, Mistral, DeepSeek, etc.
12
+ *
13
+ * API docs: https://huggingface.co/docs/api-inference/
14
+ */
15
+ export declare class HuggingFaceBackend implements LLMBackend {
16
+ readonly name = "huggingface";
17
+ private apiKey;
18
+ private model;
19
+ private baseUrl;
20
+ private useTGI;
21
+ constructor(config: {
22
+ apiKey: string;
23
+ model: string;
24
+ /** Custom endpoint URL (for Inference Endpoints or self-hosted TGI) */
25
+ baseUrl?: string;
26
+ /** Use Text Generation Inference API format (default: auto-detect) */
27
+ useTGI?: boolean;
28
+ });
29
+ complete(messages: ChatMessage[], options?: CompletionOptions): Promise<LLMResponse>;
30
+ /**
31
+ * HuggingFace Inference API (serverless).
32
+ * Uses the /models/{model} endpoint with chat-style input.
33
+ */
34
+ private completeInferenceAPI;
35
+ /**
36
+ * Text Generation Inference (TGI) / Inference Endpoints.
37
+ * Uses the OpenAI-compatible /v1/chat/completions endpoint.
38
+ */
39
+ private completeTGI;
40
+ }
41
+ /**
42
+ * Use a HuggingFace model via the serverless Inference API.
43
+ * Good for: quick prototyping, small models, no infrastructure needed.
44
+ */
45
+ export declare function hfInferenceBackend(apiKey: string, model?: string): HuggingFaceBackend;
46
+ /**
47
+ * Use a HuggingFace Inference Endpoint (dedicated).
48
+ * Good for: production, guaranteed availability, custom models.
49
+ */
50
+ export declare function hfEndpointBackend(apiKey: string, endpointUrl: string, model?: string): HuggingFaceBackend;
51
+ /**
52
+ * Use a self-hosted TGI instance.
53
+ * Good for: full control, private deployment, GPU servers.
54
+ */
55
+ export declare function tgiBackend(baseUrl?: string, model?: string): HuggingFaceBackend;
56
+ /**
57
+ * SmolAgents-recommended models for reasoning tasks.
58
+ */
59
+ export declare const SMOLAGENTS_MODELS: {
60
+ /** Best open reasoning model */
61
+ readonly qwen72b: "Qwen/Qwen2.5-72B-Instruct";
62
+ /** Good balance of speed and quality */
63
+ readonly qwen32b: "Qwen/Qwen2.5-32B-Instruct";
64
+ /** Fast, good for simple tasks */
65
+ readonly qwen7b: "Qwen/Qwen2.5-7B-Instruct";
66
+ /** Strong reasoning, open weights */
67
+ readonly llama70b: "meta-llama/Llama-3.3-70B-Instruct";
68
+ /** Fast Llama variant */
69
+ readonly llama8b: "meta-llama/Llama-3.1-8B-Instruct";
70
+ /** Code-focused reasoning */
71
+ readonly deepseek: "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B";
72
+ /** Compact but capable */
73
+ readonly mistral: "mistralai/Mistral-Small-24B-Instruct-2501";
74
+ };
75
+ //# sourceMappingURL=huggingface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"huggingface.d.ts","sourceRoot":"","sources":["../../src/backends/huggingface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE3F;;;;;;;;;;;;GAYG;AACH,qBAAa,kBAAmB,YAAW,UAAU;IACnD,QAAQ,CAAC,IAAI,iBAAiB;IAC9B,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,MAAM,CAAU;gBAEZ,MAAM,EAAE;QAClB,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE,MAAM,CAAC;QACd,uEAAuE;QACvE,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,sEAAsE;QACtE,MAAM,CAAC,EAAE,OAAO,CAAC;KAClB;IAaK,QAAQ,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,WAAW,CAAC;IAO1F;;;OAGG;YACW,oBAAoB;IAuDlC;;;OAGG;YACW,WAAW;CAgD1B;AAID;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,KAAK,GAAE,MAAoC,GAC1C,kBAAkB,CAEpB;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,KAAK,GAAE,MAAc,GACpB,kBAAkB,CAEpB;AAED;;;GAGG;AACH,wBAAgB,UAAU,CACxB,OAAO,GAAE,MAAgC,EACzC,KAAK,GAAE,MAAc,GACpB,kBAAkB,CAEpB;AAED;;GAEG;AACH,eAAO,MAAM,iBAAiB;IAC5B,gCAAgC;;IAEhC,wCAAwC;;IAExC,kCAAkC;;IAElC,qCAAqC;;IAErC,yBAAyB;;IAEzB,6BAA6B;;IAE7B,0BAA0B;;CAElB,CAAC"}
@@ -0,0 +1,158 @@
1
+ /**
2
+ * HuggingFace Inference API backend.
3
+ *
4
+ * Supports:
5
+ * - HuggingFace Inference API (serverless)
6
+ * - HuggingFace Inference Endpoints (dedicated)
7
+ * - Text Generation Inference (TGI) self-hosted
8
+ *
9
+ * Compatible with SmolAgents-style models that support chat completions.
10
+ * Works with models like Qwen, Llama, Mistral, DeepSeek, etc.
11
+ *
12
+ * API docs: https://huggingface.co/docs/api-inference/
13
+ */
14
+ export class HuggingFaceBackend {
15
+ name = "huggingface";
16
+ apiKey;
17
+ model;
18
+ baseUrl;
19
+ useTGI;
20
+ constructor(config) {
21
+ this.apiKey = config.apiKey;
22
+ this.model = config.model;
23
+ this.useTGI = config.useTGI ?? false;
24
+ if (config.baseUrl) {
25
+ this.baseUrl = config.baseUrl.replace(/\/$/, "");
26
+ this.useTGI = config.useTGI ?? true;
27
+ }
28
+ else {
29
+ this.baseUrl = "https://api-inference.huggingface.co/models";
30
+ }
31
+ }
32
+ async complete(messages, options) {
33
+ if (this.useTGI) {
34
+ return this.completeTGI(messages, options);
35
+ }
36
+ return this.completeInferenceAPI(messages, options);
37
+ }
38
+ /**
39
+ * HuggingFace Inference API (serverless).
40
+ * Uses the /models/{model} endpoint with chat-style input.
41
+ */
42
+ async completeInferenceAPI(messages, options) {
43
+ const url = `${this.baseUrl}/${this.model}/v1/chat/completions`;
44
+ const body = {
45
+ model: this.model,
46
+ messages: messages.map((m) => ({ role: m.role, content: m.content })),
47
+ temperature: options?.temperature ?? 0.3,
48
+ max_tokens: options?.maxTokens ?? 2048,
49
+ stream: false,
50
+ };
51
+ if (options?.stop)
52
+ body.stop = options.stop;
53
+ const res = await fetch(url, {
54
+ method: "POST",
55
+ headers: {
56
+ "Content-Type": "application/json",
57
+ Authorization: `Bearer ${this.apiKey}`,
58
+ },
59
+ body: JSON.stringify(body),
60
+ });
61
+ if (!res.ok) {
62
+ const errText = await res.text();
63
+ // Handle model loading
64
+ if (res.status === 503) {
65
+ throw new Error(`Model '${this.model}' is loading. Please retry in a few seconds. ${errText}`);
66
+ }
67
+ throw new Error(`HuggingFace API error (${res.status}): ${errText}`);
68
+ }
69
+ const data = await res.json();
70
+ const choice = data.choices[0];
71
+ return {
72
+ content: choice.message.content ?? "",
73
+ tokens: data.usage
74
+ ? { prompt: data.usage.prompt_tokens, completion: data.usage.completion_tokens }
75
+ : undefined,
76
+ finishReason: choice.finish_reason,
77
+ };
78
+ }
79
+ /**
80
+ * Text Generation Inference (TGI) / Inference Endpoints.
81
+ * Uses the OpenAI-compatible /v1/chat/completions endpoint.
82
+ */
83
+ async completeTGI(messages, options) {
84
+ const url = `${this.baseUrl}/v1/chat/completions`;
85
+ const body = {
86
+ model: this.model,
87
+ messages: messages.map((m) => ({ role: m.role, content: m.content })),
88
+ temperature: options?.temperature ?? 0.3,
89
+ max_tokens: options?.maxTokens ?? 2048,
90
+ stream: false,
91
+ };
92
+ if (options?.stop)
93
+ body.stop = options.stop;
94
+ const res = await fetch(url, {
95
+ method: "POST",
96
+ headers: {
97
+ "Content-Type": "application/json",
98
+ ...(this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {}),
99
+ },
100
+ body: JSON.stringify(body),
101
+ });
102
+ if (!res.ok) {
103
+ const errText = await res.text();
104
+ throw new Error(`TGI API error (${res.status}): ${errText}`);
105
+ }
106
+ const data = await res.json();
107
+ const choice = data.choices[0];
108
+ return {
109
+ content: choice.message.content ?? "",
110
+ tokens: data.usage
111
+ ? { prompt: data.usage.prompt_tokens, completion: data.usage.completion_tokens }
112
+ : undefined,
113
+ finishReason: choice.finish_reason,
114
+ };
115
+ }
116
+ }
117
+ // ── Pre-configured backends ─────────────────────────────
118
+ /**
119
+ * Use a HuggingFace model via the serverless Inference API.
120
+ * Good for: quick prototyping, small models, no infrastructure needed.
121
+ */
122
+ export function hfInferenceBackend(apiKey, model = "Qwen/Qwen2.5-72B-Instruct") {
123
+ return new HuggingFaceBackend({ apiKey, model });
124
+ }
125
+ /**
126
+ * Use a HuggingFace Inference Endpoint (dedicated).
127
+ * Good for: production, guaranteed availability, custom models.
128
+ */
129
+ export function hfEndpointBackend(apiKey, endpointUrl, model = "tgi") {
130
+ return new HuggingFaceBackend({ apiKey, model, baseUrl: endpointUrl, useTGI: true });
131
+ }
132
+ /**
133
+ * Use a self-hosted TGI instance.
134
+ * Good for: full control, private deployment, GPU servers.
135
+ */
136
+ export function tgiBackend(baseUrl = "http://localhost:8080", model = "tgi") {
137
+ return new HuggingFaceBackend({ apiKey: "", model, baseUrl, useTGI: true });
138
+ }
139
+ /**
140
+ * SmolAgents-recommended models for reasoning tasks.
141
+ */
142
+ export const SMOLAGENTS_MODELS = {
143
+ /** Best open reasoning model */
144
+ qwen72b: "Qwen/Qwen2.5-72B-Instruct",
145
+ /** Good balance of speed and quality */
146
+ qwen32b: "Qwen/Qwen2.5-32B-Instruct",
147
+ /** Fast, good for simple tasks */
148
+ qwen7b: "Qwen/Qwen2.5-7B-Instruct",
149
+ /** Strong reasoning, open weights */
150
+ llama70b: "meta-llama/Llama-3.3-70B-Instruct",
151
+ /** Fast Llama variant */
152
+ llama8b: "meta-llama/Llama-3.1-8B-Instruct",
153
+ /** Code-focused reasoning */
154
+ deepseek: "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
155
+ /** Compact but capable */
156
+ mistral: "mistralai/Mistral-Small-24B-Instruct-2501",
157
+ };
158
+ //# sourceMappingURL=huggingface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"huggingface.js","sourceRoot":"","sources":["../../src/backends/huggingface.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,kBAAkB;I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,OAAO,EAAE,kCAAkC;IAC3C,6BAA6B;IAC7B,QAAQ,EAAE,0CAA0C;IACpD,0BAA0B;IAC1B,OAAO,EAAE,2CAA2C;CAC5C,CAAC"}
@@ -0,0 +1,24 @@
1
+ import type { LLMBackend, ChatMessage, CompletionOptions, LLMResponse } from "../types.js";
2
+ /**
3
+ * OpenAI-compatible LLM backend.
4
+ * Works with OpenAI, Venice, Groq, Ollama, agents.eco, or any compatible API.
5
+ */
6
+ export declare class OpenAIBackend implements LLMBackend {
7
+ readonly name: string;
8
+ private apiKey;
9
+ private baseUrl;
10
+ private model;
11
+ constructor(config: {
12
+ name?: string;
13
+ apiKey: string;
14
+ baseUrl: string;
15
+ model: string;
16
+ });
17
+ complete(messages: ChatMessage[], options?: CompletionOptions): Promise<LLMResponse>;
18
+ }
19
+ export declare function openaiBackend(apiKey: string, model?: string): OpenAIBackend;
20
+ export declare function veniceBackend(apiKey: string, model?: string): OpenAIBackend;
21
+ export declare function groqBackend(apiKey: string, model?: string): OpenAIBackend;
22
+ export declare function ollamaBackend(model?: string, baseUrl?: string): OpenAIBackend;
23
+ export declare function agentsEcoBackend(apiKey: string, model?: string): OpenAIBackend;
24
+ //# sourceMappingURL=openai.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/backends/openai.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE3F;;;GAGG;AACH,qBAAa,aAAc,YAAW,UAAU;IAC9C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,KAAK,CAAS;gBAEV,MAAM,EAAE;QAClB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,EAAE,MAAM,CAAC;KACf;IAOK,QAAQ,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,WAAW,CAAC;CA4D3F;AAID,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,GAAE,MAAiB,GAAG,aAAa,CAErF;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,GAAE,MAAmB,GAAG,aAAa,CAEvF;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,GAAE,MAAkC,GAAG,aAAa,CAEpG;AAED,wBAAgB,aAAa,CAAC,KAAK,GAAE,MAAmB,EAAE,OAAO,GAAE,MAAoC,GAAG,aAAa,CAEtH;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,GAAE,MAAmB,GAAG,aAAa,CAO1F"}