@weaviate/agents 0.0.0-stage → 0.0.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 +27 -0
- package/README.md +130 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/query/agent.d.ts +539 -0
- package/dist/query/agent.js +373 -0
- package/dist/query/agent.test.d.ts +1 -0
- package/dist/query/agent.test.js +1118 -0
- package/dist/query/collection.d.ts +22 -0
- package/dist/query/collection.js +12 -0
- package/dist/query/connection.d.ts +10 -0
- package/dist/query/connection.js +19 -0
- package/dist/query/filter.d.ts +2 -0
- package/dist/query/filter.js +20 -0
- package/dist/query/index.d.ts +4 -0
- package/dist/query/index.js +3 -0
- package/dist/query/media.d.ts +25 -0
- package/dist/query/media.js +24 -0
- package/dist/query/response/api-response-mapping.d.ts +3 -0
- package/dist/query/response/api-response-mapping.js +172 -0
- package/dist/query/response/api-response.d.ts +219 -0
- package/dist/query/response/api-response.js +1 -0
- package/dist/query/response/error.d.ts +6 -0
- package/dist/query/response/error.js +34 -0
- package/dist/query/response/index.d.ts +2 -0
- package/dist/query/response/index.js +2 -0
- package/dist/query/response/response-mapping.d.ts +23 -0
- package/dist/query/response/response-mapping.js +308 -0
- package/dist/query/response/response.d.ts +332 -0
- package/dist/query/response/response.js +44 -0
- package/dist/query/response/server-sent-events.d.ts +14 -0
- package/dist/query/response/server-sent-events.js +85 -0
- package/dist/query/search.d.ts +37 -0
- package/dist/query/search.js +87 -0
- package/package.json +46 -3
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
|
2
|
+
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
3
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
4
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
5
|
+
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
6
|
+
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
|
|
7
|
+
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
|
|
8
|
+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
9
|
+
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
10
|
+
function fulfill(value) { resume("next", value); }
|
|
11
|
+
function reject(value) { resume("throw", value); }
|
|
12
|
+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Fetch Server-Sent Events (SSE) from a URL.
|
|
16
|
+
*
|
|
17
|
+
* All fields other than "event" and "data" are ignored
|
|
18
|
+
*
|
|
19
|
+
* @param input - The URL to fetch the SSE from.
|
|
20
|
+
* @param init - The request init options.
|
|
21
|
+
* @returns An async generator of ServerSentEvent objects.
|
|
22
|
+
*/
|
|
23
|
+
export function fetchServerSentEvents(input, init) {
|
|
24
|
+
return __asyncGenerator(this, arguments, function* fetchServerSentEvents_1() {
|
|
25
|
+
const response = yield __await(fetch(input, Object.assign(Object.assign({}, init), { headers: Object.assign(Object.assign({}, init === null || init === void 0 ? void 0 : init.headers), { Accept: "text/event-stream" }) })));
|
|
26
|
+
if (!response.ok || !response.body) {
|
|
27
|
+
throw Error(`Query agent streaming failed. ${yield __await(response.text())}`);
|
|
28
|
+
}
|
|
29
|
+
const reader = response.body.getReader();
|
|
30
|
+
const textDecoder = new TextDecoder("utf-8");
|
|
31
|
+
let buffer = "";
|
|
32
|
+
while (true) {
|
|
33
|
+
const { done, value } = yield __await(reader.read());
|
|
34
|
+
if (done) {
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
// Use a buffer to accumulate text until we have a complete SSE (delimited by blank lines)
|
|
38
|
+
buffer += textDecoder.decode(value, { stream: true });
|
|
39
|
+
const { events, remainingBuffer } = parseServerSentEvents(buffer);
|
|
40
|
+
for (const event of events) {
|
|
41
|
+
yield yield __await(event);
|
|
42
|
+
}
|
|
43
|
+
buffer = remainingBuffer;
|
|
44
|
+
}
|
|
45
|
+
// Flush the remaining buffer
|
|
46
|
+
const { events } = parseServerSentEvents(buffer, true);
|
|
47
|
+
for (const event of events) {
|
|
48
|
+
yield yield __await(event);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function parseServerSentEvents(buffer, flush) {
|
|
53
|
+
var _a;
|
|
54
|
+
// Server sent events are delimited by blank lines,
|
|
55
|
+
// and may be spread across multiple chunks from the API
|
|
56
|
+
const sseChunks = buffer.split(/\r?\n\r?\n/);
|
|
57
|
+
let remainingBuffer = "";
|
|
58
|
+
if (flush !== true) {
|
|
59
|
+
// Put the (possibly incomplete) final event back into the buffer
|
|
60
|
+
remainingBuffer = (_a = sseChunks.pop()) !== null && _a !== void 0 ? _a : "";
|
|
61
|
+
}
|
|
62
|
+
const events = [];
|
|
63
|
+
for (const chunk of sseChunks) {
|
|
64
|
+
const lines = chunk.split(/\r?\n/);
|
|
65
|
+
let event = "message";
|
|
66
|
+
let data = "";
|
|
67
|
+
for (const line of lines) {
|
|
68
|
+
if (line.startsWith("event:")) {
|
|
69
|
+
// Replace event name if we get one
|
|
70
|
+
event = line.slice("event:".length).trim();
|
|
71
|
+
}
|
|
72
|
+
else if (line.startsWith("data:")) {
|
|
73
|
+
if (data) {
|
|
74
|
+
// Data was spread across multiple lines
|
|
75
|
+
data += "\n";
|
|
76
|
+
}
|
|
77
|
+
data += line.slice("data:".length).trim();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (data) {
|
|
81
|
+
events.push({ event, data });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return { events, remainingBuffer };
|
|
85
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { WeaviateClient } from "@weaviate/core";
|
|
2
|
+
import { SearchExecutionOptions, SearchModeResponse } from "./response/response.js";
|
|
3
|
+
import { QueryAgentQuery } from "./agent.js";
|
|
4
|
+
import { QueryAgentCollection } from "./collection.js";
|
|
5
|
+
/**
|
|
6
|
+
* A configured searcher for the QueryAgent.
|
|
7
|
+
*
|
|
8
|
+
* This is used internally by the QueryAgent class to run search-mode queries.
|
|
9
|
+
* After the first request is made, the underlying searches are cached and can
|
|
10
|
+
* be reused for paginating through the a consistent set of results.
|
|
11
|
+
*/
|
|
12
|
+
export declare class QueryAgentSearcher {
|
|
13
|
+
private client;
|
|
14
|
+
private query;
|
|
15
|
+
private collections;
|
|
16
|
+
private systemPrompt;
|
|
17
|
+
private agentsHost;
|
|
18
|
+
private diversityWeight;
|
|
19
|
+
private filtering;
|
|
20
|
+
private effort;
|
|
21
|
+
private cachedSearches?;
|
|
22
|
+
constructor(client: WeaviateClient, query: QueryAgentQuery, collections: QueryAgentCollection[], systemPrompt: string | undefined, agentsHost: string, diversityWeight: number | undefined, filtering: "recall" | "precision" | undefined, effort: "medium" | "high" | "ultrahigh" | undefined);
|
|
23
|
+
private buildRequestBody;
|
|
24
|
+
/**
|
|
25
|
+
* Run the search-only agent with the given limit and offset values.
|
|
26
|
+
*
|
|
27
|
+
* Calling this method multiple times on the same QueryAgentSearcher instance will result
|
|
28
|
+
* in the same underlying searches being performed each time, allowing you to paginate
|
|
29
|
+
* over a consistent results set.
|
|
30
|
+
*
|
|
31
|
+
* @param [options] - Options for executing the search
|
|
32
|
+
* @param [options.limit] - The maximum number of results to return. Defaults to 20 if not specified.
|
|
33
|
+
* @param [options.offset] - The offset to start from.
|
|
34
|
+
* @returns A SearchModeResponse object containing the results, usage, and underlying searches performed.
|
|
35
|
+
*/
|
|
36
|
+
run({ limit, offset, }: SearchExecutionOptions): Promise<SearchModeResponse>;
|
|
37
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { mapSearchOnlyResponse } from "./response/response-mapping.js";
|
|
11
|
+
import { mapCollections } from "./collection.js";
|
|
12
|
+
import { handleError } from "./response/error.js";
|
|
13
|
+
import { getHeaders } from "./connection.js";
|
|
14
|
+
/**
|
|
15
|
+
* A configured searcher for the QueryAgent.
|
|
16
|
+
*
|
|
17
|
+
* This is used internally by the QueryAgent class to run search-mode queries.
|
|
18
|
+
* After the first request is made, the underlying searches are cached and can
|
|
19
|
+
* be reused for paginating through the a consistent set of results.
|
|
20
|
+
*/
|
|
21
|
+
export class QueryAgentSearcher {
|
|
22
|
+
constructor(client, query, collections, systemPrompt, agentsHost, diversityWeight, filtering, effort) {
|
|
23
|
+
this.client = client;
|
|
24
|
+
this.query = query;
|
|
25
|
+
this.collections = collections;
|
|
26
|
+
this.systemPrompt = systemPrompt;
|
|
27
|
+
this.agentsHost = agentsHost;
|
|
28
|
+
this.diversityWeight = diversityWeight;
|
|
29
|
+
this.filtering = filtering;
|
|
30
|
+
this.effort = effort;
|
|
31
|
+
}
|
|
32
|
+
buildRequestBody(limit, offset, connectionHeaders) {
|
|
33
|
+
// Fields that apply to every request (both the initial search generation
|
|
34
|
+
// and subsequent pagination requests) belong on `base`.
|
|
35
|
+
const base = {
|
|
36
|
+
headers: connectionHeaders,
|
|
37
|
+
original_query: typeof this.query === "string" ? this.query : { messages: this.query },
|
|
38
|
+
collections: mapCollections(this.collections),
|
|
39
|
+
limit,
|
|
40
|
+
offset,
|
|
41
|
+
diversity_weight: this.diversityWeight,
|
|
42
|
+
filtering: this.filtering,
|
|
43
|
+
effort: this.effort,
|
|
44
|
+
};
|
|
45
|
+
if (this.cachedSearches === undefined) {
|
|
46
|
+
return Object.assign(Object.assign({}, base), { searches: null, system_prompt: this.systemPrompt || null });
|
|
47
|
+
}
|
|
48
|
+
return Object.assign(Object.assign({}, base), { searches: this.cachedSearches });
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Run the search-only agent with the given limit and offset values.
|
|
52
|
+
*
|
|
53
|
+
* Calling this method multiple times on the same QueryAgentSearcher instance will result
|
|
54
|
+
* in the same underlying searches being performed each time, allowing you to paginate
|
|
55
|
+
* over a consistent results set.
|
|
56
|
+
*
|
|
57
|
+
* @param [options] - Options for executing the search
|
|
58
|
+
* @param [options.limit] - The maximum number of results to return. Defaults to 20 if not specified.
|
|
59
|
+
* @param [options.offset] - The offset to start from.
|
|
60
|
+
* @returns A SearchModeResponse object containing the results, usage, and underlying searches performed.
|
|
61
|
+
*/
|
|
62
|
+
run(_a) {
|
|
63
|
+
return __awaiter(this, arguments, void 0, function* ({ limit = 20, offset, }) {
|
|
64
|
+
if (!this.collections || this.collections.length === 0) {
|
|
65
|
+
throw Error("No collections provided to the query agent.");
|
|
66
|
+
}
|
|
67
|
+
const { requestHeaders, connectionHeaders } = yield getHeaders(this.client);
|
|
68
|
+
const response = yield fetch(`${this.agentsHost}/query/search_only`, {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: requestHeaders,
|
|
71
|
+
body: JSON.stringify(this.buildRequestBody(limit, offset, connectionHeaders)),
|
|
72
|
+
});
|
|
73
|
+
if (!response.ok) {
|
|
74
|
+
yield handleError(yield response.text());
|
|
75
|
+
}
|
|
76
|
+
const parsedResponse = (yield response.json());
|
|
77
|
+
const { mappedResponse, apiSearches } = mapSearchOnlyResponse(parsedResponse);
|
|
78
|
+
// If we successfully mapped the searches, cache them for the next request.
|
|
79
|
+
// Since this cache is a private internal value, there's not point in mapping
|
|
80
|
+
// back and forth between the exported and API types, so we cache apiSearches
|
|
81
|
+
if (mappedResponse.searches !== undefined) {
|
|
82
|
+
this.cachedSearches = apiSearches;
|
|
83
|
+
}
|
|
84
|
+
return Object.assign(Object.assign({}, mappedResponse), { next: (options) => __awaiter(this, void 0, void 0, function* () { return this.run(options); }) });
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weaviate/agents",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "JS/TS client for Weaviate Agents",
|
|
5
|
+
"exports": "./dist/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=20.0.0"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/weaviate/agents-typescript-client.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"weaviate"
|
|
16
|
+
],
|
|
17
|
+
"author": "Weaviate",
|
|
18
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"@weaviate/core": "^3.15.0",
|
|
24
|
+
"zod": "^4.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@eslint/js": "^9.26.0",
|
|
28
|
+
"@types/jest": "^30.0.0",
|
|
29
|
+
"@types/node": "^20.19.43",
|
|
30
|
+
"@weaviate/core": "3.15.0",
|
|
31
|
+
"eslint": "^9.26.0",
|
|
32
|
+
"globals": "^16.1.0",
|
|
33
|
+
"jest": "^30.0.4",
|
|
34
|
+
"prettier": "^3.6.2",
|
|
35
|
+
"ts-jest": "^29.4.0",
|
|
36
|
+
"typedoc": "^0.28.15",
|
|
37
|
+
"typedoc-plugin-extras": "^4.0.1",
|
|
38
|
+
"typescript": "^5.8.3",
|
|
39
|
+
"typescript-eslint": "^8.32.1",
|
|
40
|
+
"zod": "^4.4.3"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"lint": "eslint",
|
|
44
|
+
"format": "prettier --check .",
|
|
45
|
+
"build": "tsc",
|
|
46
|
+
"test": "jest",
|
|
47
|
+
"docs": "typedoc --plugin typedoc-plugin-extras --out docs/ src/index.ts"
|
|
48
|
+
}
|
|
6
49
|
}
|