@krewe/langchain 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/README.md +87 -0
- package/dist/client.d.ts +13 -0
- package/dist/client.js +17 -0
- package/dist/client.js.map +1 -0
- package/dist/embeddings.d.ts +17 -0
- package/dist/embeddings.js +36 -0
- package/dist/embeddings.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/llm.d.ts +26 -0
- package/dist/llm.js +39 -0
- package/dist/llm.js.map +1 -0
- package/dist/tools.d.ts +34 -0
- package/dist/tools.js +83 -0
- package/dist/tools.js.map +1 -0
- package/package.json +59 -0
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# @krewe/langchain
|
|
2
|
+
|
|
3
|
+
LangChain.js bindings for the [krewe](https://www.krewe.world) pay-per-call AI inference network on Base. Adds an `Embeddings` class, an `LLM` class, and three `Tool` instances backed by the krewe network's `/v2/predict` endpoint. Every call is paid in USDC via [x402](https://github.com/coinbase/x402) (EIP-3009) — no API keys, no signup.
|
|
4
|
+
|
|
5
|
+
> **Status:** v0.1.0 — first cut. SDK paths exercised against the live network; the LangChain class surface has not yet been validated inside a complete LangGraph or AgentExecutor flow. If you dogfood and hit issues, [open an issue](https://github.com/krewe-AI/krewe/issues).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @krewe/langchain @langchain/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Embeddings
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { KreweEmbeddings } from "@krewe/langchain";
|
|
17
|
+
|
|
18
|
+
const embeddings = new KreweEmbeddings({
|
|
19
|
+
privateKey: process.env.AGENT_KEY as `0x${string}`, // Base wallet w/ USDC
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const vec = await embeddings.embedQuery("krewe is a decentralized inference network");
|
|
23
|
+
const vecs = await embeddings.embedDocuments(["doc1 text", "doc2 text", "doc3 text"]);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Cost: **$0.01 USDC per embedding**. Model: `Xenova/all-MiniLM-L6-v2`, 384-dim.
|
|
27
|
+
|
|
28
|
+
## LLM (verified completion)
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { KreweLLM } from "@krewe/langchain";
|
|
32
|
+
|
|
33
|
+
const llm = new KreweLLM({
|
|
34
|
+
privateKey: process.env.AGENT_KEY as `0x${string}`,
|
|
35
|
+
maxTokens: 128,
|
|
36
|
+
stop: ["\n###"],
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const text = await llm.invoke("Summarize: krewe is a decentralized AI network on Base.");
|
|
40
|
+
// → "krewe is a network that runs AI tasks across independent nodes..."
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Cost: **$0.05 USDC per call**. The completion is byte-equal-consensus across 3 miner nodes (deliberate non-streaming).
|
|
44
|
+
|
|
45
|
+
## Tools (for AgentExecutor / LangGraph)
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { createKreweTools } from "@krewe/langchain";
|
|
49
|
+
import { AgentExecutor, createReactAgent } from "langchain/agents";
|
|
50
|
+
import { ChatOpenAI } from "@langchain/openai";
|
|
51
|
+
|
|
52
|
+
const tools = createKreweTools({ privateKey: process.env.AGENT_KEY as `0x${string}` });
|
|
53
|
+
|
|
54
|
+
const agent = createReactAgent({
|
|
55
|
+
llm: new ChatOpenAI({ model: "gpt-4o-mini" }),
|
|
56
|
+
tools: [tools.extractEntities, tools.scrapeUrl, tools.complete],
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const exec = new AgentExecutor({ agent, tools: [tools.extractEntities, tools.scrapeUrl, tools.complete] });
|
|
60
|
+
const result = await exec.invoke({
|
|
61
|
+
input: "Pull every email from this page: https://www.krewe.world/build",
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Three tools available:
|
|
66
|
+
|
|
67
|
+
| Tool | Cost | Description |
|
|
68
|
+
|----------------------------|----------|------------------------------------------------|
|
|
69
|
+
| `krewe_extract_entities` | $0.005 | Emails / URLs / dates / numbers / phones |
|
|
70
|
+
| `krewe_scrape_url` | $0.020 | Fetch + clean a URL |
|
|
71
|
+
| `krewe_complete` | $0.050 | Verified SLM completion (LaMini-GPT-124M) |
|
|
72
|
+
|
|
73
|
+
## Why this might be useful
|
|
74
|
+
|
|
75
|
+
- **No API key dance.** The agent's wallet is the auth. Drops into existing on-chain agents with zero plumbing.
|
|
76
|
+
- **Per-call on-chain settlement.** Every call leaves a Basescan tx hash you can audit.
|
|
77
|
+
- **Verifiable inference (for `KreweLLM`).** Three independent miners must produce byte-identical output before the call settles — useful when a single LLM provider shouldn't be a single point of trust.
|
|
78
|
+
|
|
79
|
+
## Links
|
|
80
|
+
|
|
81
|
+
- Source (MIT): https://github.com/krewe-AI/krewe/tree/main/clients/langchain
|
|
82
|
+
- Build docs (Eliza / LangChain / CrewAI): https://www.krewe.world/build
|
|
83
|
+
- npm SDK: [`@krewe/x402-client`](https://www.npmjs.com/package/@krewe/x402-client)
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
MIT.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared X402Client factory used by every LangChain primitive (Embeddings,
|
|
3
|
+
* LLM, Tools). Keeps the signer construction in one place so callers only
|
|
4
|
+
* supply a private key and (optionally) an orchestrator URL.
|
|
5
|
+
*/
|
|
6
|
+
import { type X402Client } from "@krewe/x402-client";
|
|
7
|
+
export interface KreweClientOptions {
|
|
8
|
+
/** 0x-prefixed 32-byte private key. Base wallet that holds USDC. */
|
|
9
|
+
privateKey: `0x${string}`;
|
|
10
|
+
/** Optional orchestrator override. Defaults to krewe's production orchestrator. */
|
|
11
|
+
orchestrator?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function buildKreweClient(opts: KreweClientOptions): X402Client;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared X402Client factory used by every LangChain primitive (Embeddings,
|
|
3
|
+
* LLM, Tools). Keeps the signer construction in one place so callers only
|
|
4
|
+
* supply a private key and (optionally) an orchestrator URL.
|
|
5
|
+
*/
|
|
6
|
+
import { createX402Client } from "@krewe/x402-client";
|
|
7
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
8
|
+
export function buildKreweClient(opts) {
|
|
9
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(opts.privateKey)) {
|
|
10
|
+
throw new Error("[krewe] privateKey must be a 0x-prefixed 32-byte hex key");
|
|
11
|
+
}
|
|
12
|
+
return createX402Client({
|
|
13
|
+
signer: privateKeyToAccount(opts.privateKey),
|
|
14
|
+
...(opts.orchestrator ? { orchestrator: opts.orchestrator } : {}),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,gBAAgB,EAAmB,MAAM,oBAAoB,CAAC;AACvE,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AASpD,MAAM,UAAU,gBAAgB,CAAC,IAAwB;IACvD,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;QACjD,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;IAC9E,CAAC;IACD,OAAO,gBAAgB,CAAC;QACtB,MAAM,EAAE,mBAAmB,CAAC,IAAI,CAAC,UAAU,CAAC;QAC5C,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,IAAI,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAClE,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KreweEmbeddings — LangChain Embeddings binding for krewe's text.embed kind.
|
|
3
|
+
*
|
|
4
|
+
* Each `embedQuery` is one paid /v2/predict call ($0.01 USDC). `embedDocuments`
|
|
5
|
+
* issues N parallel calls; consider chunking if you're embedding thousands of
|
|
6
|
+
* documents at once (Node default concurrency limit applies).
|
|
7
|
+
*/
|
|
8
|
+
import { Embeddings, type EmbeddingsParams } from "@langchain/core/embeddings";
|
|
9
|
+
import { type KreweClientOptions } from "./client.js";
|
|
10
|
+
export interface KreweEmbeddingsParams extends EmbeddingsParams, KreweClientOptions {
|
|
11
|
+
}
|
|
12
|
+
export declare class KreweEmbeddings extends Embeddings {
|
|
13
|
+
private readonly client;
|
|
14
|
+
constructor(params: KreweEmbeddingsParams);
|
|
15
|
+
embedQuery(document: string): Promise<number[]>;
|
|
16
|
+
embedDocuments(documents: string[]): Promise<number[][]>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KreweEmbeddings — LangChain Embeddings binding for krewe's text.embed kind.
|
|
3
|
+
*
|
|
4
|
+
* Each `embedQuery` is one paid /v2/predict call ($0.01 USDC). `embedDocuments`
|
|
5
|
+
* issues N parallel calls; consider chunking if you're embedding thousands of
|
|
6
|
+
* documents at once (Node default concurrency limit applies).
|
|
7
|
+
*/
|
|
8
|
+
import { Embeddings } from "@langchain/core/embeddings";
|
|
9
|
+
import { buildKreweClient } from "./client.js";
|
|
10
|
+
export class KreweEmbeddings extends Embeddings {
|
|
11
|
+
client;
|
|
12
|
+
constructor(params) {
|
|
13
|
+
super(params);
|
|
14
|
+
this.client = buildKreweClient(params);
|
|
15
|
+
}
|
|
16
|
+
async embedQuery(document) {
|
|
17
|
+
const r = await this.client.predict({
|
|
18
|
+
kind: "text.embed",
|
|
19
|
+
payload: { text: document },
|
|
20
|
+
});
|
|
21
|
+
return readVector(r.output);
|
|
22
|
+
}
|
|
23
|
+
async embedDocuments(documents) {
|
|
24
|
+
// Fan out in parallel; the orchestrator handles rate limiting via x402
|
|
25
|
+
// per-key buckets. For tight budgets prefer batching upstream of this call.
|
|
26
|
+
return await Promise.all(documents.map(d => this.embedQuery(d)));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function readVector(out) {
|
|
30
|
+
if (typeof out === "object" && out !== null &&
|
|
31
|
+
"vector" in out && Array.isArray(out.vector)) {
|
|
32
|
+
return out.vector;
|
|
33
|
+
}
|
|
34
|
+
throw new Error("[krewe] text.embed returned unexpected shape: " + JSON.stringify(out).slice(0, 120));
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=embeddings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embeddings.js","sourceRoot":"","sources":["../src/embeddings.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,UAAU,EAAyB,MAAM,4BAA4B,CAAC;AAC/E,OAAO,EAAE,gBAAgB,EAA2B,MAAM,aAAa,CAAC;AAKxE,MAAM,OAAO,eAAgB,SAAQ,UAAU;IAC5B,MAAM,CAAa;IAEpC,YAAY,MAA6B;QACvC,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,MAAM,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;IACzC,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,QAAgB;QAC/B,MAAM,CAAC,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC;YAClC,IAAI,EAAK,YAAY;YACrB,OAAO,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;SAC5B,CAAC,CAAC;QACH,OAAO,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,cAAc,CAAC,SAAmB;QACtC,uEAAuE;QACvE,4EAA4E;QAC5E,OAAO,MAAM,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACnE,CAAC;CACF;AAED,SAAS,UAAU,CAAC,GAAY;IAC9B,IACE,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI;QACvC,QAAQ,IAAI,GAAG,IAAI,KAAK,CAAC,OAAO,CAAE,GAA6B,CAAC,MAAM,CAAC,EACvE,CAAC;QACD,OAAQ,GAA4B,CAAC,MAAM,CAAC;IAC9C,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,gDAAgD,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;AACxG,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { KreweEmbeddings, type KreweEmbeddingsParams } from "./embeddings.js";
|
|
2
|
+
export { KreweLLM, type KreweLLMParams } from "./llm.js";
|
|
3
|
+
export { createKreweExtractEntitiesTool, createKreweScrapeUrlTool, createKreweCompleteTool, createKreweTools, } from "./tools.js";
|
|
4
|
+
export { buildKreweClient, type KreweClientOptions } from "./client.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { KreweEmbeddings } from "./embeddings.js";
|
|
2
|
+
export { KreweLLM } from "./llm.js";
|
|
3
|
+
export { createKreweExtractEntitiesTool, createKreweScrapeUrlTool, createKreweCompleteTool, createKreweTools, } from "./tools.js";
|
|
4
|
+
export { buildKreweClient } from "./client.js";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAA8B,MAAM,iBAAiB,CAAC;AAC9E,OAAO,EAAE,QAAQ,EAAqC,MAAM,UAAU,CAAC;AACvE,OAAO,EACL,8BAA8B,EAC9B,wBAAwB,EACxB,uBAAuB,EACvB,gBAAgB,GACjB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,gBAAgB,EAA2B,MAAM,aAAa,CAAC"}
|
package/dist/llm.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KreweLLM — LangChain LLM binding for krewe's text.complete kind.
|
|
3
|
+
*
|
|
4
|
+
* Each call dispatches to 3 miners running Xenova/LaMini-GPT-124M with
|
|
5
|
+
* greedy decoding; the orchestrator only returns once 2-of-3 miners agree
|
|
6
|
+
* byte-for-byte on the output. $0.05 USDC per call.
|
|
7
|
+
*
|
|
8
|
+
* No token streaming. The whole completion is returned at consensus time —
|
|
9
|
+
* a deliberate trade-off to make per-call verifiability possible.
|
|
10
|
+
*/
|
|
11
|
+
import { LLM, type BaseLLMParams } from "@langchain/core/language_models/llms";
|
|
12
|
+
import { type KreweClientOptions } from "./client.js";
|
|
13
|
+
export interface KreweLLMParams extends BaseLLMParams, KreweClientOptions {
|
|
14
|
+
/** Max tokens to generate. Default 256, hard cap 1024. */
|
|
15
|
+
maxTokens?: number;
|
|
16
|
+
/** Stop sequences applied post-decode (deterministic across replicas). */
|
|
17
|
+
stop?: string[];
|
|
18
|
+
}
|
|
19
|
+
export declare class KreweLLM extends LLM {
|
|
20
|
+
private readonly client;
|
|
21
|
+
private readonly maxTokens;
|
|
22
|
+
private readonly stopSeq;
|
|
23
|
+
constructor(params: KreweLLMParams);
|
|
24
|
+
_llmType(): string;
|
|
25
|
+
_call(prompt: string): Promise<string>;
|
|
26
|
+
}
|
package/dist/llm.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KreweLLM — LangChain LLM binding for krewe's text.complete kind.
|
|
3
|
+
*
|
|
4
|
+
* Each call dispatches to 3 miners running Xenova/LaMini-GPT-124M with
|
|
5
|
+
* greedy decoding; the orchestrator only returns once 2-of-3 miners agree
|
|
6
|
+
* byte-for-byte on the output. $0.05 USDC per call.
|
|
7
|
+
*
|
|
8
|
+
* No token streaming. The whole completion is returned at consensus time —
|
|
9
|
+
* a deliberate trade-off to make per-call verifiability possible.
|
|
10
|
+
*/
|
|
11
|
+
import { LLM } from "@langchain/core/language_models/llms";
|
|
12
|
+
import { buildKreweClient } from "./client.js";
|
|
13
|
+
export class KreweLLM extends LLM {
|
|
14
|
+
client;
|
|
15
|
+
maxTokens;
|
|
16
|
+
stopSeq;
|
|
17
|
+
constructor(params) {
|
|
18
|
+
super(params);
|
|
19
|
+
this.client = buildKreweClient(params);
|
|
20
|
+
this.maxTokens = params.maxTokens ?? 256;
|
|
21
|
+
this.stopSeq = params.stop;
|
|
22
|
+
}
|
|
23
|
+
_llmType() {
|
|
24
|
+
return "krewe-text-complete";
|
|
25
|
+
}
|
|
26
|
+
async _call(prompt) {
|
|
27
|
+
const r = await this.client.predict({
|
|
28
|
+
kind: "text.complete",
|
|
29
|
+
payload: {
|
|
30
|
+
prompt,
|
|
31
|
+
maxTokens: this.maxTokens,
|
|
32
|
+
...(this.stopSeq && this.stopSeq.length > 0 ? { stop: this.stopSeq } : {}),
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
const out = r.output;
|
|
36
|
+
return typeof out?.text === "string" ? out.text : "";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=llm.js.map
|
package/dist/llm.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm.js","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,GAAG,EAAsB,MAAM,sCAAsC,CAAC;AAC/E,OAAO,EAAE,gBAAgB,EAA2B,MAAM,aAAa,CAAC;AAUxE,MAAM,OAAO,QAAS,SAAQ,GAAG;IACd,MAAM,CAAa;IACnB,SAAS,CAAS;IAClB,OAAO,CAAuB;IAE/C,YAAY,MAAsB;QAChC,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,MAAM,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;QACvC,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,IAAI,GAAG,CAAC;QACzC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC;IAC7B,CAAC;IAED,QAAQ;QACN,OAAO,qBAAqB,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,MAAc;QACxB,MAAM,CAAC,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC;YAClC,IAAI,EAAK,eAAe;YACxB,OAAO,EAAE;gBACP,MAAM;gBACN,SAAS,EAAE,IAAI,CAAC,SAAS;gBACzB,GAAG,CAAC,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAC3E;SACF,CAAC,CAAC;QACH,MAAM,GAAG,GAAG,CAAC,CAAC,MAA2B,CAAC;QAC1C,OAAO,OAAO,GAAG,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;IACvD,CAAC;CACF"}
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain Tools backed by krewe job kinds.
|
|
3
|
+
*
|
|
4
|
+
* Built on `DynamicTool` so callers don't have to define a Zod schema —
|
|
5
|
+
* each tool takes a single string input and returns the JSON-serialized
|
|
6
|
+
* result. Use these directly with createReactAgent / AgentExecutor.
|
|
7
|
+
*/
|
|
8
|
+
import { DynamicTool } from "@langchain/core/tools";
|
|
9
|
+
import { type KreweClientOptions } from "./client.js";
|
|
10
|
+
/**
|
|
11
|
+
* Entity extraction tool. Calls /v2/predict with kind=text.structure.
|
|
12
|
+
* Cost: $0.005 USDC per call.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createKreweExtractEntitiesTool(opts: KreweClientOptions): DynamicTool;
|
|
15
|
+
/**
|
|
16
|
+
* Web scrape tool. Calls /v2/predict with kind=web.scrape.
|
|
17
|
+
* Cost: $0.02 USDC per call.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createKreweScrapeUrlTool(opts: KreweClientOptions): DynamicTool;
|
|
20
|
+
/**
|
|
21
|
+
* SLM completion tool. Calls /v2/predict with kind=text.complete.
|
|
22
|
+
* Cost: $0.05 USDC per call. Output is byte-equal consensus-verified across 3 miners.
|
|
23
|
+
*/
|
|
24
|
+
export declare function createKreweCompleteTool(opts: KreweClientOptions & {
|
|
25
|
+
maxTokens?: number;
|
|
26
|
+
}): DynamicTool;
|
|
27
|
+
/** Convenience: build all 3 tools at once. */
|
|
28
|
+
export declare function createKreweTools(opts: KreweClientOptions & {
|
|
29
|
+
maxTokens?: number;
|
|
30
|
+
}): {
|
|
31
|
+
extractEntities: DynamicTool;
|
|
32
|
+
scrapeUrl: DynamicTool;
|
|
33
|
+
complete: DynamicTool;
|
|
34
|
+
};
|
package/dist/tools.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain Tools backed by krewe job kinds.
|
|
3
|
+
*
|
|
4
|
+
* Built on `DynamicTool` so callers don't have to define a Zod schema —
|
|
5
|
+
* each tool takes a single string input and returns the JSON-serialized
|
|
6
|
+
* result. Use these directly with createReactAgent / AgentExecutor.
|
|
7
|
+
*/
|
|
8
|
+
import { DynamicTool } from "@langchain/core/tools";
|
|
9
|
+
import { buildKreweClient } from "./client.js";
|
|
10
|
+
/**
|
|
11
|
+
* Entity extraction tool. Calls /v2/predict with kind=text.structure.
|
|
12
|
+
* Cost: $0.005 USDC per call.
|
|
13
|
+
*/
|
|
14
|
+
export function createKreweExtractEntitiesTool(opts) {
|
|
15
|
+
const client = buildKreweClient(opts);
|
|
16
|
+
return new DynamicTool({
|
|
17
|
+
name: "krewe_extract_entities",
|
|
18
|
+
description: "Extract structured entities (emails, urls, dates, numbers, phones) from a " +
|
|
19
|
+
"block of text. Input: the text to parse. Output: JSON with the extracted entities. " +
|
|
20
|
+
"Costs $0.005 USDC per call via the krewe network.",
|
|
21
|
+
func: async (text) => {
|
|
22
|
+
const r = await client.predict({
|
|
23
|
+
kind: "text.structure",
|
|
24
|
+
payload: { text },
|
|
25
|
+
});
|
|
26
|
+
return JSON.stringify({ ...(r.output ?? {}), _krewe: { paid: r.paymentTxHash, replicas: r.replicas } });
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Web scrape tool. Calls /v2/predict with kind=web.scrape.
|
|
32
|
+
* Cost: $0.02 USDC per call.
|
|
33
|
+
*/
|
|
34
|
+
export function createKreweScrapeUrlTool(opts) {
|
|
35
|
+
const client = buildKreweClient(opts);
|
|
36
|
+
return new DynamicTool({
|
|
37
|
+
name: "krewe_scrape_url",
|
|
38
|
+
description: "Fetch and clean a small URL payload. Input: a single URL. Output: JSON with the " +
|
|
39
|
+
"extracted page metadata and body. Costs $0.02 USDC per call via the krewe network.",
|
|
40
|
+
func: async (url) => {
|
|
41
|
+
const trimmed = url.trim();
|
|
42
|
+
if (!/^https?:\/\//i.test(trimmed)) {
|
|
43
|
+
return JSON.stringify({ error: "input must be an http(s) URL" });
|
|
44
|
+
}
|
|
45
|
+
const r = await client.predict({
|
|
46
|
+
kind: "web.scrape",
|
|
47
|
+
payload: { url: trimmed, includeBody: true },
|
|
48
|
+
});
|
|
49
|
+
return JSON.stringify({ ...(r.output ?? {}), _krewe: { paid: r.paymentTxHash, replicas: r.replicas } });
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* SLM completion tool. Calls /v2/predict with kind=text.complete.
|
|
55
|
+
* Cost: $0.05 USDC per call. Output is byte-equal consensus-verified across 3 miners.
|
|
56
|
+
*/
|
|
57
|
+
export function createKreweCompleteTool(opts) {
|
|
58
|
+
const client = buildKreweClient(opts);
|
|
59
|
+
const maxTokens = opts.maxTokens ?? 128;
|
|
60
|
+
return new DynamicTool({
|
|
61
|
+
name: "krewe_complete",
|
|
62
|
+
description: "Run a quantized SLM completion (Xenova/LaMini-GPT-124M) verified by 2-of-3 " +
|
|
63
|
+
"byte-equal consensus across 3 miner nodes. Input: a prompt. Output: completion text. " +
|
|
64
|
+
"Costs $0.05 USDC per call via the krewe network.",
|
|
65
|
+
func: async (prompt) => {
|
|
66
|
+
const r = await client.predict({
|
|
67
|
+
kind: "text.complete",
|
|
68
|
+
payload: { prompt, maxTokens },
|
|
69
|
+
});
|
|
70
|
+
const out = r.output;
|
|
71
|
+
return out?.text ?? "";
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/** Convenience: build all 3 tools at once. */
|
|
76
|
+
export function createKreweTools(opts) {
|
|
77
|
+
return {
|
|
78
|
+
extractEntities: createKreweExtractEntitiesTool(opts),
|
|
79
|
+
scrapeUrl: createKreweScrapeUrlTool(opts),
|
|
80
|
+
complete: createKreweCompleteTool(opts),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"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"}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@krewe/langchain",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "LangChain.js bindings for the krewe pay-per-call AI inference network. Exposes Embeddings, an LLM, and Tools backed by /v2/predict on Base. Settles each call in USDC via x402 — no API keys.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"langchain",
|
|
7
|
+
"langchainjs",
|
|
8
|
+
"langchain-community",
|
|
9
|
+
"ai-agent",
|
|
10
|
+
"x402",
|
|
11
|
+
"krewe",
|
|
12
|
+
"base",
|
|
13
|
+
"usdc",
|
|
14
|
+
"depin",
|
|
15
|
+
"inference",
|
|
16
|
+
"embeddings",
|
|
17
|
+
"llm"
|
|
18
|
+
],
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": ["dist", "README.md"],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -p tsconfig.json",
|
|
33
|
+
"prepublishOnly": "pnpm run build"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@langchain/core": ">=0.3.0"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@krewe/x402-client": "^0.1.0",
|
|
40
|
+
"viem": "^2.21.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@langchain/core": "^1.1.46",
|
|
44
|
+
"@types/node": "^22.0.0",
|
|
45
|
+
"typescript": "^5.6.0"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=18"
|
|
49
|
+
},
|
|
50
|
+
"homepage": "https://www.krewe.world/build",
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "https://github.com/krewe-AI/krewe.git",
|
|
54
|
+
"directory": "clients/langchain"
|
|
55
|
+
},
|
|
56
|
+
"bugs": {
|
|
57
|
+
"url": "https://github.com/krewe-AI/krewe/issues"
|
|
58
|
+
}
|
|
59
|
+
}
|