@dnax/core 0.17.4 → 0.17.6

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/index.ts CHANGED
@@ -6,11 +6,8 @@ import Joi from "joi";
6
6
  import { $ } from "bun";
7
7
  import define from "./define";
8
8
  import * as utils from "./utils";
9
- import { searchEngine } from "./lib/orama";
10
9
  import { dataCache } from "./lib/bento";
11
- import { ai } from "./lib/ai";
12
10
  import { crypt } from "./lib/crypto";
13
- import { Document as document } from "./lib/docs";
14
11
  import { contextStorage } from "./lib/asyncLocalStorage";
15
12
 
16
13
  /**
@@ -27,9 +24,7 @@ export {
27
24
  useRest,
28
25
  v,
29
26
  dataCache,
30
- ai,
31
27
  crypt,
32
- document,
33
28
  $,
34
29
  contextStorage,
35
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.17.4",
3
+ "version": "0.17.6",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/lib/ai/index.ts DELETED
@@ -1,21 +0,0 @@
1
- import { Ollama } from "./provider/ollama";
2
- import { CohereClient } from "./provider/cohere";
3
- import { Mistral } from "./provider/mistral";
4
- import { OpenAI } from "./provider/openai";
5
- import { Groq } from "./provider/groq";
6
- import { Agent } from "./lib/Agent";
7
- import { Space } from "./lib/Space";
8
- import { Task } from "./lib/Task";
9
-
10
- const ai = {
11
- Ollama,
12
- CohereClient,
13
- Mistral,
14
- OpenAI,
15
- Groq,
16
- Agent,
17
- Space,
18
- Task,
19
- };
20
-
21
- export { ai };
@@ -1,159 +0,0 @@
1
- import { Groq } from "groq-sdk";
2
- import { CohereClient } from "../provider/cohere";
3
- import { OpenAI } from "../provider/openai";
4
- import { Mistral } from "../provider/mistral";
5
- type outputFunction = (content: any) => Promise<any>;
6
-
7
- type AgentOptions = {
8
- config: {
9
- provider: "groq" | "cohere" | "openai" | "mistral";
10
- model: string;
11
- apiKey: string;
12
- token?: string;
13
- };
14
- name?: string;
15
- role: string;
16
- goal: string;
17
- backstory: string;
18
- tools?: any[];
19
- temperature?: number;
20
- max_tokens?: number;
21
- output_format?: "string" | "json";
22
- fn: (
23
- ctx: {
24
- instruction: string;
25
- data: object | any;
26
- chat_history: any[];
27
- activities: any[];
28
- },
29
- ai?: {
30
- groq?: InstanceType<typeof Groq>;
31
- cohere?: InstanceType<typeof CohereClient>;
32
- openai?: InstanceType<typeof OpenAI>;
33
- mistral?: InstanceType<typeof Mistral>;
34
- }
35
- ) => Promise<any>;
36
- };
37
-
38
- export type AgentType = InstanceType<typeof Agent>;
39
-
40
- class Agent {
41
- public cfg: AgentOptions;
42
- #groq?: InstanceType<typeof Groq>;
43
- #cohere?: InstanceType<typeof CohereClient>;
44
- #openai?: InstanceType<typeof OpenAI>;
45
- #mistral?: InstanceType<typeof Mistral>;
46
- constructor(cfg: AgentOptions) {
47
- this.cfg = cfg;
48
-
49
- // Init default value
50
- if (!this.cfg.temperature) this.cfg.temperature = 0.5;
51
-
52
- // set groq client
53
- if (cfg.config.provider === "groq") {
54
- this.#groq = new Groq({
55
- apiKey: cfg.config.apiKey,
56
- });
57
- }
58
-
59
- // set cohere client
60
- if (cfg.config.provider === "cohere") {
61
- this.#cohere = new CohereClient({
62
- token: cfg.config.apiKey || cfg.config.token,
63
- });
64
- }
65
-
66
- // set openai client
67
- if (cfg.config.provider === "openai") {
68
- this.#openai = new OpenAI({
69
- apiKey: cfg.config.apiKey,
70
- });
71
- }
72
-
73
- // set mistral client
74
- if (cfg.config.provider === "mistral") {
75
- this.#mistral = new Mistral({
76
- apiKey: cfg.config.apiKey,
77
- });
78
- }
79
- }
80
-
81
- async exec(
82
- instruction: string,
83
- data?: object
84
- ): Promise<string | object | null | Array<any>> {
85
- const messages: any = [];
86
- var response: any = null;
87
-
88
- return new Promise(async (resolve, reject) => {
89
- try {
90
- // Groq AI
91
- if (this.#groq) {
92
- response = await this.#groq.chat.completions.create({
93
- messages: messages,
94
- model: this.cfg.config.model,
95
-
96
- temperature: Number(this?.cfg?.temperature),
97
- tools: this.cfg?.tools || [],
98
- response_format: {
99
- type: this.cfg.output_format == "json" ? "json_object" : "text",
100
- },
101
- });
102
- }
103
-
104
- // Cohere AI
105
- if (this.#cohere) {
106
- response = await this.#cohere.chat({
107
- chatHistory: messages,
108
- message: instruction,
109
- model: this.cfg.config.model,
110
- temperature: Number(this?.cfg?.temperature),
111
- preamble: this.cfg?.backstory || "",
112
- maxTokens: Number(this?.cfg?.max_tokens),
113
-
114
- tools: this.cfg?.tools || [],
115
- responseFormat: {
116
- type: this.cfg.output_format == "json" ? "json_object" : "text",
117
- },
118
- });
119
- }
120
-
121
- // OpenAI AI
122
- if (this.#openai) {
123
- response = await this.#openai.chat.completions.create({
124
- max_tokens: Number(this?.cfg?.max_tokens),
125
- messages: messages,
126
- model: this.cfg.config.model,
127
- temperature: Number(this?.cfg?.temperature),
128
- tools: this.cfg?.tools || [],
129
-
130
- response_format: {
131
- type: this.cfg.output_format == "json" ? "json_object" : "text",
132
- },
133
- });
134
- }
135
-
136
- // Mistral AI
137
- if (this.#mistral) {
138
- response = await this.#mistral.chat.complete({
139
- messages: messages,
140
- model: this.cfg.config.model,
141
- temperature: Number(this?.cfg?.temperature),
142
- tools: this.cfg?.tools || [],
143
- maxTokens: Number(this?.cfg?.max_tokens),
144
- responseFormat: {
145
- type: this.cfg.output_format == "json" ? "json_object" : "text",
146
- },
147
- });
148
- }
149
-
150
- resolve(response);
151
- } catch (error) {
152
- console?.error(error);
153
- reject(error);
154
- }
155
- });
156
- }
157
- }
158
-
159
- export { Agent };
@@ -1,34 +0,0 @@
1
- import type { TaskType } from "./Task";
2
- import type { AgentType } from "./Agent";
3
- type SpaceOptions = {
4
- name: string;
5
- tasks: TaskType[];
6
- agents: AgentType[];
7
- arbitrator?: AgentType;
8
- evaluators?: AgentType[];
9
- summarizers?: AgentType[];
10
- };
11
-
12
- type responseType = {
13
- task_name: string;
14
- response: string;
15
- };
16
-
17
- class Space {
18
- #tasks: TaskType[];
19
- #agents: AgentType[];
20
- #summarizers?: AgentType[];
21
- #evaluator?: AgentType;
22
- #arbitrator?: AgentType;
23
- #activities?: any[];
24
- constructor(cfg: SpaceOptions) {
25
- this.#tasks = cfg.tasks;
26
- this.#agents = cfg.agents;
27
- this.#summarizers = cfg.summarizers;
28
- this.#evaluator = cfg.evaluator;
29
- this.#arbitrator = cfg.arbitrator;
30
- }
31
- run(content: string, data?: object) {}
32
- }
33
-
34
- export { Space };
@@ -1,19 +0,0 @@
1
- import type { AgentType } from "./Agent";
2
- export type TaskType = InstanceType<typeof Task>;
3
- type TaskOptions = {
4
- title?: string;
5
- description: string;
6
- expected_output: string;
7
- output_format: "string" | "json" | boolean;
8
- tools?: any[];
9
- agent?: AgentType;
10
- };
11
- class Task {
12
- public cfg: TaskOptions;
13
- constructor(cfg: TaskOptions) {
14
- this.cfg = cfg;
15
- }
16
- exec(instruction: string, data?: object) {}
17
- }
18
-
19
- export { Task };
@@ -1,3 +0,0 @@
1
- import { CohereClient } from "cohere-ai";
2
-
3
- export { CohereClient };
@@ -1,3 +0,0 @@
1
- import { Groq } from "groq-sdk";
2
-
3
- export { Groq };
@@ -1,3 +0,0 @@
1
- import { Mistral } from "@mistralai/mistralai";
2
-
3
- export { Mistral };
@@ -1,3 +0,0 @@
1
- import { Ollama } from "ollama";
2
-
3
- export { Ollama };
@@ -1,3 +0,0 @@
1
- import OpenAI from "openai";
2
-
3
- export { OpenAI };
package/lib/docs/index.ts DELETED
@@ -1,109 +0,0 @@
1
- import Docxtemplater from "docxtemplater";
2
- import PizZip from "pizzip";
3
- import libre from "libreoffice-convert";
4
- import fs from "fs";
5
- import path from "path";
6
-
7
- const convertToPdf = (docBuffer: any) => {
8
- return new Promise((resolve, reject) => {
9
- libre.convert(docBuffer, ".pdf", undefined, (err, done) => {
10
- if (err) reject(err);
11
- else resolve(done);
12
- });
13
- });
14
- };
15
-
16
- type optionTypeBuffer = {
17
- /**
18
- * default is "nodebuffer"
19
- @default "nodebuffer"
20
- */
21
- type?: "nodebuffer" | "base64" | "blob" | "arraybuffer" | "base64";
22
- compression?: "DEFLATE" | "STORE";
23
- };
24
-
25
- type documentConfig = {
26
- dir: string;
27
- delimiters?: { start: string; end: string };
28
- };
29
-
30
- class Document {
31
- dir?: string | null;
32
- delimiters?: { start?: string; end?: string };
33
- constructor(config?: documentConfig) {
34
- this.dir = config?.dir || null;
35
- this.delimiters = config?.delimiters || { start: "{{", end: "}}" };
36
- }
37
- getBuffer(file: string, tags?: object, options?: optionTypeBuffer): Buffer {
38
- let pathfile = "";
39
- if (!this?.dir) {
40
- pathfile = path.resolve(file);
41
- } else {
42
- path?.resolve(this.dir + file);
43
- }
44
- let templateFile = fs.readFileSync(pathfile, "binary");
45
- let zip = new PizZip(templateFile);
46
- let doc = new Docxtemplater(zip, {
47
- paragraphLoop: true,
48
- linebreaks: true,
49
- delimiters: {
50
- start: this.delimiters?.start || "{{",
51
- end: this.delimiters?.end || "}}",
52
- },
53
- nullGetter: () => {
54
- return "";
55
- },
56
- });
57
-
58
- if (tags) {
59
- doc.render(tags);
60
- }
61
-
62
- let buffer = doc.getZip().generate({
63
- type: "nodebuffer",
64
- compression: "DEFLATE",
65
- });
66
- return buffer;
67
- }
68
-
69
- async getPdfBuffer(file: string, tags?: object, options?: optionTypeBuffer) {
70
- let pathfile = "";
71
- if (!this?.dir) {
72
- pathfile = path.resolve(file);
73
- } else {
74
- path?.resolve(this.dir + file);
75
- }
76
- let templateFile = fs.readFileSync(pathfile, "binary");
77
- let zip = new PizZip(templateFile);
78
- let doc = new Docxtemplater(zip, {
79
- paragraphLoop: true,
80
- linebreaks: true,
81
- delimiters: {
82
- start: this.delimiters?.start || "{{",
83
- end: this.delimiters?.end || "}}",
84
- },
85
- nullGetter: () => {
86
- return "";
87
- },
88
- });
89
-
90
- if (tags) {
91
- doc.render(tags);
92
- }
93
-
94
- let docBuffer = doc.getZip().generate({
95
- type: "nodebuffer",
96
- compression: "DEFLATE",
97
- });
98
-
99
- const pdfBuffer = await convertToPdf(docBuffer);
100
-
101
- return pdfBuffer;
102
- }
103
-
104
- saveTo(path: string, buffer: Buffer) {
105
- fs.writeFileSync(path, buffer);
106
- }
107
- }
108
-
109
- export { Document };
@@ -1,44 +0,0 @@
1
- type Vector<N extends number> = `vector[${N}]`;
2
- export type optionsType = {
3
- store: {
4
- path?: string;
5
- filename: string;
6
- };
7
- schema: {
8
- [key: string]:
9
- | "string"
10
- | "number"
11
- | "boolean"
12
- | `vector[${number}]`
13
- | "enum[]"
14
- | "enum"
15
- | "string[]"
16
- | "number[]"
17
- | "boolean[]"
18
- | "geopoint";
19
- };
20
- settings?: {
21
- primaryKey?: string;
22
- language: "french" | "english";
23
- };
24
- };
25
-
26
- export type searchType = {
27
- mode?: "fulltext" | "hybrid" | "vector";
28
- term?: string;
29
- limit?: number;
30
- offset?: number;
31
- tolerance?: number;
32
- properties?: string[];
33
- exact?: boolean;
34
- vector?: {
35
- value: number[];
36
- property: string;
37
- };
38
- hybridWeights?: {
39
- text: number;
40
- vector: number;
41
- };
42
- similarity?: number;
43
- includeVectors?: boolean;
44
- };
@@ -1,108 +0,0 @@
1
- import type { optionsType, searchType } from "./@types";
2
- import * as orama from "@orama/orama";
3
- import fs from "fs-extra";
4
- import _ from "lodash";
5
- import cleanDeep from "clean-deep";
6
- import { ObjectId } from "mongodb";
7
- import path from "path";
8
- class searchEngine {
9
- #options: optionsType;
10
- #db: any;
11
- #dataPath: string;
12
- constructor(options: optionsType) {
13
- this.#options = options;
14
- this.#dataPath = path.join(
15
- this.#options.store?.path || process.cwd() + "/data",
16
- this.#options.store?.filename || "search.db"
17
- );
18
- }
19
-
20
- async initialize() {
21
- this.#db = await orama.create({
22
- schema: this.#options.schema,
23
- language: this.#options.settings?.language || "english",
24
- });
25
-
26
- try {
27
- let restoreData = await Bun.file(this.#dataPath).json();
28
-
29
- if (restoreData) {
30
- // console.log(restoreData);
31
- await orama.load(this.#db, restoreData);
32
- }
33
- } catch (err) {}
34
- }
35
-
36
- async save() {
37
- try {
38
- let jsonExport = await orama.save(this.#db);
39
- let jsonData = JSON.stringify(jsonExport);
40
- let saveTo = Bun.write(this.#dataPath, jsonData);
41
- } catch (error) {
42
- console.log(error?.message);
43
- }
44
- }
45
-
46
- async add(documents: Array<object>) {
47
- try {
48
- let ids = [];
49
- if (Array.isArray(documents)) {
50
- documents.map((doc) => {
51
- doc = cleanDeep(doc);
52
- doc.id = doc?._id || doc?.id || new ObjectId().toString();
53
- doc._id = doc?.id;
54
- });
55
- let ids = await orama.insertMultiple(this.#db, documents);
56
- await this.save();
57
- return ids;
58
- }
59
- } catch (error) {
60
- console.log(error?.message);
61
- }
62
- }
63
-
64
- async search(search: searchType) {
65
- let results = await orama.search(this.#db, search);
66
- if (results?.hits.length) {
67
- results?.hits?.map((e) => {
68
- if (e?.id) {
69
- e._id = e.id;
70
- }
71
- });
72
- }
73
-
74
- return results;
75
- }
76
-
77
- async find() {
78
- let docs: Array<object> = [];
79
- let data = await orama.save(this.#db);
80
- docs = Object.values(data.docs?.docs || []);
81
- //let docs = Object.values(data.docs[0]);
82
- return docs;
83
- }
84
-
85
- async remove(ids: Array<string>) {
86
- try {
87
- await orama.remove(this.#db, ids);
88
- await this.save();
89
- } catch (error) {
90
- console.log(error?.message);
91
- }
92
- }
93
-
94
- async drop() {
95
- fs.unlink(this.#dataPath).catch((err) => {
96
- //console.log(err);
97
- });
98
- }
99
- }
100
-
101
- const engine = new searchEngine({
102
- schema: {},
103
- store: {
104
- filename: "data.db",
105
- },
106
- });
107
-
108
- export { searchEngine };