-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
aiplugin.ts
81 lines (68 loc) · 2.28 KB
/
aiplugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Tool, type ToolParams } from "@langchain/core/tools";
/**
* Interface for parameters required to create an instance of
* AIPluginTool.
*/
export interface AIPluginToolParams extends ToolParams {
name: string;
description: string;
apiSpec: string;
}
/**
* Class for creating instances of AI tools from plugins. It extends the
* Tool class and implements the AIPluginToolParams interface.
*/
export class AIPluginTool extends Tool implements AIPluginToolParams {
static lc_name() {
return "AIPluginTool";
}
private _name: string;
private _description: string;
apiSpec: string;
get name() {
return this._name;
}
get description() {
return this._description;
}
constructor(params: AIPluginToolParams) {
super(params);
this._name = params.name;
this._description = params.description;
this.apiSpec = params.apiSpec;
}
/** @ignore */
async _call(_input: string) {
return this.apiSpec;
}
/**
* Static method that creates an instance of AIPluginTool from a given
* plugin URL. It fetches the plugin and its API specification from the
* provided URL and returns a new instance of AIPluginTool with the
* fetched data.
* @param url The URL of the AI plugin.
* @returns A new instance of AIPluginTool.
*/
static async fromPluginUrl(url: string) {
const aiPluginRes = await fetch(url);
if (!aiPluginRes.ok) {
throw new Error(
`Failed to fetch plugin from ${url} with status ${aiPluginRes.status}`
);
}
const aiPluginJson = await aiPluginRes.json();
const apiUrlRes = await fetch(aiPluginJson.api.url);
if (!apiUrlRes.ok) {
throw new Error(
`Failed to fetch API spec from ${aiPluginJson.api.url} with status ${apiUrlRes.status}`
);
}
const apiUrlJson = await apiUrlRes.text();
return new AIPluginTool({
name: aiPluginJson.name_for_model,
description: `Call this tool to get the OpenAPI spec (and usage guide) for interacting with the ${aiPluginJson.name_for_human} API. You should only call this ONCE! What is the ${aiPluginJson.name_for_human} API useful for? ${aiPluginJson.description_for_human}`,
apiSpec: `Usage Guide: ${aiPluginJson.description_for_model}
OpenAPI Spec in JSON or YAML format:\n${apiUrlJson}`,
});
}
}