@drax/ai-back 0.13.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/dist/config/OpenAiConfig.js +7 -0
- package/dist/factory/AiProviderFactory.js +17 -0
- package/dist/factory/OpenAiProviderFactory.js +13 -0
- package/dist/index.js +5 -0
- package/dist/interfaces/IAIProvider.js +1 -0
- package/dist/providers/OpenAiProvider.js +75 -0
- package/dist/services/KnowledgeService.js +52 -0
- package/dist/vectors/ChromaVector.js +65 -0
- package/package.json +39 -0
- package/src/config/OpenAiConfig.ts +12 -0
- package/src/factory/AiProviderFactory.ts +25 -0
- package/src/factory/OpenAiProviderFactory.ts +24 -0
- package/src/index.ts +20 -0
- package/src/interfaces/IAIProvider.ts +41 -0
- package/src/providers/OpenAiProvider.ts +106 -0
- package/src/services/KnowledgeService.ts +66 -0
- package/src/vectors/ChromaVector.ts +80 -0
- package/test/KnowledgeService.test.ts +36 -0
- package/test/OpenAiProvider.test.ts +184 -0
- package/tsconfig.json +16 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/types/config/OpenAiConfig.d.ts +7 -0
- package/types/config/OpenAiConfig.d.ts.map +1 -0
- package/types/factory/AiProviderFactory.d.ts +8 -0
- package/types/factory/AiProviderFactory.d.ts.map +1 -0
- package/types/factory/OpenAiProviderFactory.d.ts +8 -0
- package/types/factory/OpenAiProviderFactory.d.ts.map +1 -0
- package/types/index.d.ts +8 -0
- package/types/index.d.ts.map +1 -0
- package/types/interfaces/IAIProvider.d.ts +34 -0
- package/types/interfaces/IAIProvider.d.ts.map +1 -0
- package/types/providers/OpenAiProvider.d.ts +17 -0
- package/types/providers/OpenAiProvider.d.ts.map +1 -0
- package/types/services/KnowledgeService.d.ts +10 -0
- package/types/services/KnowledgeService.d.ts.map +1 -0
- package/types/vectors/ChromaVector.d.ts +21 -0
- package/types/vectors/ChromaVector.d.ts.map +1 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {describe, test, expect} from 'vitest'
|
|
2
|
+
import {KnowledgeService, OpenAiProvider, OpenAiProviderFactory} from "../src";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
describe('KnowledgeService Test', () => {
|
|
6
|
+
|
|
7
|
+
test('KnowledgeService URL', async () => {
|
|
8
|
+
const ks = new KnowledgeService()
|
|
9
|
+
const content = await ks.getFromUrl('https://exigroup.com.ar/preguntas-frecuentes/')
|
|
10
|
+
console.log(content)
|
|
11
|
+
|
|
12
|
+
const openAi = OpenAiProviderFactory.instance()
|
|
13
|
+
expect(openAi).toBeInstanceOf(OpenAiProvider)
|
|
14
|
+
|
|
15
|
+
const systemPrompt = `
|
|
16
|
+
Por favor, optimiza el siguiente contenido extraído de una página web o documento PDF:
|
|
17
|
+
1. Elimina cualquier código JavaScript, CSS o HTML residual.
|
|
18
|
+
2. Organiza el contenido de manera lógica y coherente.
|
|
19
|
+
3. Elimina información redundante o irrelevante.
|
|
20
|
+
4. Asegúrate de que el contenido sea claro y fácil de entender.
|
|
21
|
+
5. No uses marcadores de posición, formato markdown o etiquetas HTML, solo texto simple.
|
|
22
|
+
`
|
|
23
|
+
const userInput = content
|
|
24
|
+
const r = await openAi.prompt({systemPrompt, userInput})
|
|
25
|
+
|
|
26
|
+
const contentOptimal = r.output
|
|
27
|
+
|
|
28
|
+
console.log(contentOptimal)
|
|
29
|
+
const chunks = ks.chunkTextBySentence(contentOptimal)
|
|
30
|
+
console.log(chunks)
|
|
31
|
+
|
|
32
|
+
expect(chunks).toBeInstanceOf(Array)
|
|
33
|
+
|
|
34
|
+
}, 30000) // 10 seconds timeout
|
|
35
|
+
|
|
36
|
+
})
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import {describe, test, expect} from 'vitest'
|
|
2
|
+
import {OpenAiProvider, OpenAiProviderFactory} from "../src";
|
|
3
|
+
import z from "zod";
|
|
4
|
+
import {IPromptMemory, IPromptMessage} from "../src/interfaces/IAIProvider";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
describe('OpenAi Test', () => {
|
|
8
|
+
|
|
9
|
+
test('OpenAi Factory', () => {
|
|
10
|
+
const openAi = OpenAiProviderFactory.instance()
|
|
11
|
+
expect(openAi).toBeInstanceOf(OpenAiProvider)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('OpenAi prompt', async () => {
|
|
15
|
+
const openAi = OpenAiProviderFactory.instance()
|
|
16
|
+
expect(openAi).toBeInstanceOf(OpenAiProvider)
|
|
17
|
+
|
|
18
|
+
const systemPrompt = "You are an AI assistant."
|
|
19
|
+
const userInput = "What is the capital of Argentina?"
|
|
20
|
+
const r = await openAi.prompt({systemPrompt, userInput})
|
|
21
|
+
console.log(r)
|
|
22
|
+
expect(r.output).toBeDefined()
|
|
23
|
+
expect(/Buenos Aires/i.test(r.output)).toBeTruthy()
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('OpenAi prompt jsonSchema', async () => {
|
|
27
|
+
const openAi = OpenAiProviderFactory.instance()
|
|
28
|
+
expect(openAi).toBeInstanceOf(OpenAiProvider)
|
|
29
|
+
|
|
30
|
+
const systemPrompt = "You are an AI assistant."
|
|
31
|
+
const userInput = "What is the most famous pokemon"
|
|
32
|
+
const jsonSchema = {
|
|
33
|
+
type: "json_schema",
|
|
34
|
+
json_schema: {
|
|
35
|
+
name: "element_description",
|
|
36
|
+
schema: {
|
|
37
|
+
type: "object",
|
|
38
|
+
properties: {
|
|
39
|
+
name: {
|
|
40
|
+
type: "string",
|
|
41
|
+
description: "El nombre del elemento"
|
|
42
|
+
},
|
|
43
|
+
type: {
|
|
44
|
+
type: "string",
|
|
45
|
+
description: "El tipo de elemento, como archivo, carpeta, enlace, etc."
|
|
46
|
+
},
|
|
47
|
+
description: {
|
|
48
|
+
type: "string",
|
|
49
|
+
description: "Una breve descripción del elemento"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
required: ["name", "type", "description"]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
const r = await openAi.prompt({systemPrompt, userInput, jsonSchema})
|
|
59
|
+
console.log(r)
|
|
60
|
+
expect(r.output).toBeDefined
|
|
61
|
+
const pokemon = JSON.parse(r.output)
|
|
62
|
+
expect(pokemon.name).toBe("Pikachu")
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('OpenAi prompt zodSchema', async () => {
|
|
66
|
+
const openAi = OpenAiProviderFactory.instance()
|
|
67
|
+
expect(openAi).toBeInstanceOf(OpenAiProvider)
|
|
68
|
+
|
|
69
|
+
const systemPrompt = "You are an AI assistant."
|
|
70
|
+
const userInput = "What is the most famous pokemon"
|
|
71
|
+
const zodSchema = z.object({
|
|
72
|
+
name: z.string(),
|
|
73
|
+
type: z.string(),
|
|
74
|
+
description: z.string()
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
const r = await openAi.prompt({systemPrompt, userInput, zodSchema})
|
|
78
|
+
console.log(r)
|
|
79
|
+
expect(r.output).toBeDefined
|
|
80
|
+
const pokemon = zodSchema.parse(JSON.parse(r.output))
|
|
81
|
+
expect(pokemon.name).toBe("Pikachu")
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('OpenAi prompt zodSchema and Knowledge base', async () => {
|
|
85
|
+
const openAi = OpenAiProviderFactory.instance()
|
|
86
|
+
expect(openAi).toBeInstanceOf(OpenAiProvider)
|
|
87
|
+
|
|
88
|
+
const systemPrompt = "You are an AI assistant."
|
|
89
|
+
const knowledgeBase = ["Charmander is a legendary and most famous Pokémon from the Pokémon series. It has a type of fire."]
|
|
90
|
+
const zodSchema = z.object({
|
|
91
|
+
name: z.string(),
|
|
92
|
+
type: z.string(),
|
|
93
|
+
description: z.string()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
const userInput = "What is the most famous pokemon"
|
|
97
|
+
const r = await openAi.prompt({systemPrompt, userInput, zodSchema, knowledgeBase})
|
|
98
|
+
console.log(r)
|
|
99
|
+
expect(r.output).toBeDefined
|
|
100
|
+
const pokemon = zodSchema.parse(JSON.parse(r.output))
|
|
101
|
+
expect(pokemon.name).toBe("Charmander")
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('OpenAi prompt zodSchema, Knowledge base and history', async () => {
|
|
105
|
+
const openAi = OpenAiProviderFactory.instance()
|
|
106
|
+
expect(openAi).toBeInstanceOf(OpenAiProvider)
|
|
107
|
+
|
|
108
|
+
const systemPrompt = "You are an AI assistant."
|
|
109
|
+
|
|
110
|
+
const knowledgeBase = ["Charmander is a legendary and most famous Pokémon from the Pokémon series. It has a type of fire."]
|
|
111
|
+
const history: IPromptMessage[] = [
|
|
112
|
+
{role: 'assistant', content: 'what is your favourite pokemon'},
|
|
113
|
+
{role: 'user', content: 'My favourite pokemon is Bulbasaur and is a water type'},
|
|
114
|
+
{role: 'assistant', content: 'Do you like team Rocket?'},
|
|
115
|
+
{role: 'user', content: 'No, never!'},
|
|
116
|
+
]
|
|
117
|
+
|
|
118
|
+
const zodSchema = z.object({
|
|
119
|
+
famous: z.object({
|
|
120
|
+
name: z.string(),
|
|
121
|
+
type: z.string(),
|
|
122
|
+
description: z.string()
|
|
123
|
+
}),
|
|
124
|
+
favorite: z.object({
|
|
125
|
+
name: z.string(),
|
|
126
|
+
type: z.string(),
|
|
127
|
+
description: z.string()
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
const userInput = "What is the most famous pokemon and what is my favorite?"
|
|
133
|
+
|
|
134
|
+
const r = await openAi.prompt({systemPrompt, userInput, zodSchema, knowledgeBase, history})
|
|
135
|
+
console.log(r)
|
|
136
|
+
expect(r.output).toBeDefined
|
|
137
|
+
const pokemons = zodSchema.parse(JSON.parse(r.output))
|
|
138
|
+
expect(pokemons.famous.name).toBe("Charmander")
|
|
139
|
+
expect(pokemons.famous.type).toBeOneOf(["fire", "Fire"])
|
|
140
|
+
expect(pokemons.favorite.name).toBe("Bulbasaur")
|
|
141
|
+
expect(pokemons.favorite.type).toBeOneOf(["water", "Water"]) //Lie in this test, but it's a part of the schema and history for demonstration purposes.'
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
test('OpenAi prompt zodSchema, Knowledge base and memory', async () => {
|
|
145
|
+
const openAi = OpenAiProviderFactory.instance()
|
|
146
|
+
expect(openAi).toBeInstanceOf(OpenAiProvider)
|
|
147
|
+
|
|
148
|
+
const systemPrompt = "You are an AI assistant."
|
|
149
|
+
|
|
150
|
+
const knowledgeBase = ["Charmander is a legendary and most famous Pokémon from the Pokémon series. It has a type of fire."]
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
const memory: IPromptMemory[] = [
|
|
154
|
+
{key: 'favorite_pokemon', value: 'Squirtle'},
|
|
155
|
+
{key: 'favourite_pokemon_type', value: 'water'},
|
|
156
|
+
]
|
|
157
|
+
|
|
158
|
+
const zodSchema = z.object({
|
|
159
|
+
famous: z.object({
|
|
160
|
+
name: z.string(),
|
|
161
|
+
type: z.string(),
|
|
162
|
+
description: z.string()
|
|
163
|
+
}),
|
|
164
|
+
favorite: z.object({
|
|
165
|
+
name: z.string(),
|
|
166
|
+
type: z.string(),
|
|
167
|
+
description: z.string()
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
const userInput = "What is the most famous pokemon and what is my favorite?"
|
|
173
|
+
|
|
174
|
+
const r = await openAi.prompt({systemPrompt, userInput, zodSchema, knowledgeBase, memory})
|
|
175
|
+
console.log(r)
|
|
176
|
+
expect(r.output).toBeDefined
|
|
177
|
+
const pokemons = zodSchema.parse(JSON.parse(r.output))
|
|
178
|
+
expect(pokemons.famous.name).toBe("Charmander")
|
|
179
|
+
expect(pokemons.famous.type).toBe("fire")
|
|
180
|
+
expect(pokemons.favorite.name).toBe("Squirtle")
|
|
181
|
+
expect(pokemons.favorite.type).toBe("water") //Lie in this test, but it's a part of the schema and history for demonstration purposes.'
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
})
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "src",
|
|
5
|
+
"outDir": "dist",
|
|
6
|
+
"declarationDir": "./types",
|
|
7
|
+
},
|
|
8
|
+
"exclude": ["test", "types","dist"],
|
|
9
|
+
"ts-node": {
|
|
10
|
+
"esm": true
|
|
11
|
+
},
|
|
12
|
+
"tsc-alias": {
|
|
13
|
+
"resolveFullPaths": true,
|
|
14
|
+
"verbose": false
|
|
15
|
+
}
|
|
16
|
+
}
|