@sjts/ai-shared 31.1.3 → 31.2.5
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/backend/LICENCE +5 -0
- package/backend/dist/_/amazon/_/getAmazonClient.d.ts +4 -0
- package/backend/dist/_/amazon/_/getAmazonClient.d.ts.map +1 -0
- package/backend/dist/_/amazon/_/getAmazonClient.js +10 -0
- package/backend/dist/_/amazon/_/index.d.ts +2 -0
- package/backend/dist/_/amazon/_/index.d.ts.map +1 -0
- package/backend/dist/_/amazon/_/index.js +1 -0
- package/backend/dist/_/amazon/index.d.ts +2 -0
- package/backend/dist/_/amazon/index.d.ts.map +1 -0
- package/backend/dist/_/amazon/index.js +1 -0
- package/backend/dist/_/amazon/makeAmazonQuickie.d.ts +3 -0
- package/backend/dist/_/amazon/makeAmazonQuickie.d.ts.map +1 -0
- package/backend/dist/_/amazon/makeAmazonQuickie.js +65 -0
- package/backend/dist/_/anthropic/_/aiEvent2AnthropicMessage.d.ts +4 -0
- package/backend/dist/_/anthropic/_/aiEvent2AnthropicMessage.d.ts.map +1 -0
- package/backend/dist/_/anthropic/_/aiEvent2AnthropicMessage.js +59 -0
- package/backend/dist/_/anthropic/_/aiTool2AnthropicTool.d.ts +4 -0
- package/backend/dist/_/anthropic/_/aiTool2AnthropicTool.d.ts.map +1 -0
- package/backend/dist/_/anthropic/_/aiTool2AnthropicTool.js +11 -0
- package/backend/dist/_/anthropic/_/compileAnthropicThought.d.ts +7 -0
- package/backend/dist/_/anthropic/_/compileAnthropicThought.d.ts.map +1 -0
- package/backend/dist/_/anthropic/_/compileAnthropicThought.js +17 -0
- package/backend/dist/_/anthropic/_/getAnthropicClient.d.ts +4 -0
- package/backend/dist/_/anthropic/_/getAnthropicClient.d.ts.map +1 -0
- package/backend/dist/_/anthropic/_/getAnthropicClient.js +13 -0
- package/backend/dist/_/anthropic/_/index.d.ts +5 -0
- package/backend/dist/_/anthropic/_/index.d.ts.map +1 -0
- package/backend/dist/_/anthropic/_/index.js +4 -0
- package/backend/dist/_/anthropic/index.d.ts +4 -0
- package/backend/dist/_/anthropic/index.d.ts.map +1 -0
- package/backend/dist/_/anthropic/index.js +3 -0
- package/backend/dist/_/anthropic/makeAnthropicQuickie.d.ts +3 -0
- package/backend/dist/_/anthropic/makeAnthropicQuickie.d.ts.map +1 -0
- package/backend/dist/_/anthropic/makeAnthropicQuickie.js +53 -0
- package/backend/dist/_/anthropic/makeAnthropicStream.d.ts +3 -0
- package/backend/dist/_/anthropic/makeAnthropicStream.d.ts.map +1 -0
- package/backend/dist/_/anthropic/makeAnthropicStream.js +35 -0
- package/backend/dist/_/anthropic/makeAnthropicThink.d.ts +3 -0
- package/backend/dist/_/anthropic/makeAnthropicThink.d.ts.map +1 -0
- package/backend/dist/_/anthropic/makeAnthropicThink.js +84 -0
- package/backend/dist/_/callAI.d.ts +2 -0
- package/backend/dist/_/callAI.d.ts.map +1 -0
- package/backend/dist/_/callAI.js +10 -0
- package/backend/dist/_/cerebras/_/aiEvent2CerebrasMessage.d.ts +3 -0
- package/backend/dist/_/cerebras/_/aiEvent2CerebrasMessage.d.ts.map +1 -0
- package/backend/dist/_/cerebras/_/aiEvent2CerebrasMessage.js +47 -0
- package/backend/dist/_/cerebras/_/getCerebrasClient.d.ts +4 -0
- package/backend/dist/_/cerebras/_/getCerebrasClient.d.ts.map +1 -0
- package/backend/dist/_/cerebras/_/getCerebrasClient.js +8 -0
- package/backend/dist/_/cerebras/_/index.d.ts +3 -0
- package/backend/dist/_/cerebras/_/index.d.ts.map +1 -0
- package/backend/dist/_/cerebras/_/index.js +2 -0
- package/backend/dist/_/cerebras/index.d.ts +3 -0
- package/backend/dist/_/cerebras/index.d.ts.map +1 -0
- package/backend/dist/_/cerebras/index.js +2 -0
- package/backend/dist/_/cerebras/makeCerebrasQuickie.d.ts +3 -0
- package/backend/dist/_/cerebras/makeCerebrasQuickie.d.ts.map +1 -0
- package/backend/dist/_/cerebras/makeCerebrasQuickie.js +44 -0
- package/backend/dist/_/cerebras/makeCerebrasThink.d.ts +3 -0
- package/backend/dist/_/cerebras/makeCerebrasThink.d.ts.map +1 -0
- package/backend/dist/_/cerebras/makeCerebrasThink.js +32 -0
- package/backend/dist/_/compileObject.d.ts +12 -0
- package/backend/dist/_/compileObject.d.ts.map +1 -0
- package/backend/dist/_/compileObject.js +13 -0
- package/backend/dist/_/compileParam.d.ts +4 -0
- package/backend/dist/_/compileParam.d.ts.map +1 -0
- package/backend/dist/_/compileParam.js +80 -0
- package/backend/dist/_/google/_/aiEvent2GoogleContent.d.ts +4 -0
- package/backend/dist/_/google/_/aiEvent2GoogleContent.d.ts.map +1 -0
- package/backend/dist/_/google/_/aiEvent2GoogleContent.js +56 -0
- package/backend/dist/_/google/_/aiTool2GoogleFunctionDeclaration.d.ts +4 -0
- package/backend/dist/_/google/_/aiTool2GoogleFunctionDeclaration.d.ts.map +1 -0
- package/backend/dist/_/google/_/aiTool2GoogleFunctionDeclaration.js +12 -0
- package/backend/dist/_/google/_/compileGoogleThought.d.ts +8 -0
- package/backend/dist/_/google/_/compileGoogleThought.d.ts.map +1 -0
- package/backend/dist/_/google/_/compileGoogleThought.js +22 -0
- package/backend/dist/_/google/_/getGoogleClient.d.ts +4 -0
- package/backend/dist/_/google/_/getGoogleClient.d.ts.map +1 -0
- package/backend/dist/_/google/_/getGoogleClient.js +8 -0
- package/backend/dist/_/google/_/getGoogleToolMode.d.ts +3 -0
- package/backend/dist/_/google/_/getGoogleToolMode.d.ts.map +1 -0
- package/backend/dist/_/google/_/getGoogleToolMode.js +11 -0
- package/backend/dist/_/google/_/index.d.ts +6 -0
- package/backend/dist/_/google/_/index.d.ts.map +1 -0
- package/backend/dist/_/google/_/index.js +5 -0
- package/backend/dist/_/google/index.d.ts +3 -0
- package/backend/dist/_/google/index.d.ts.map +1 -0
- package/backend/dist/_/google/index.js +2 -0
- package/backend/dist/_/google/makeGoogleQuickie.d.ts +3 -0
- package/backend/dist/_/google/makeGoogleQuickie.d.ts.map +1 -0
- package/backend/dist/_/google/makeGoogleQuickie.js +52 -0
- package/backend/dist/_/google/makeGoogleThink.d.ts +3 -0
- package/backend/dist/_/google/makeGoogleThink.d.ts.map +1 -0
- package/backend/dist/_/google/makeGoogleThink.js +93 -0
- package/backend/dist/_/index.d.ts +13 -0
- package/backend/dist/_/index.d.ts.map +1 -0
- package/backend/dist/_/index.js +12 -0
- package/backend/dist/_/logAI.d.ts +3 -0
- package/backend/dist/_/logAI.d.ts.map +1 -0
- package/backend/dist/_/logAI.js +7 -0
- package/backend/dist/_/mistral/_/aiEvent2MistralAIMessage.d.ts +3 -0
- package/backend/dist/_/mistral/_/aiEvent2MistralAIMessage.d.ts.map +1 -0
- package/backend/dist/_/mistral/_/aiEvent2MistralAIMessage.js +47 -0
- package/backend/dist/_/mistral/_/aiTool2MistralAITool.d.ts +3 -0
- package/backend/dist/_/mistral/_/aiTool2MistralAITool.d.ts.map +1 -0
- package/backend/dist/_/mistral/_/aiTool2MistralAITool.js +14 -0
- package/backend/dist/_/mistral/_/compileMistralAIThought.d.ts +6 -0
- package/backend/dist/_/mistral/_/compileMistralAIThought.d.ts.map +1 -0
- package/backend/dist/_/mistral/_/compileMistralAIThought.js +15 -0
- package/backend/dist/_/mistral/_/getMistralAIClient.d.ts +4 -0
- package/backend/dist/_/mistral/_/getMistralAIClient.d.ts.map +1 -0
- package/backend/dist/_/mistral/_/getMistralAIClient.js +8 -0
- package/backend/dist/_/mistral/_/index.d.ts +5 -0
- package/backend/dist/_/mistral/_/index.d.ts.map +1 -0
- package/backend/dist/_/mistral/_/index.js +4 -0
- package/backend/dist/_/mistral/index.d.ts +3 -0
- package/backend/dist/_/mistral/index.d.ts.map +1 -0
- package/backend/dist/_/mistral/index.js +2 -0
- package/backend/dist/_/mistral/makeMistralAIQuickie.d.ts +3 -0
- package/backend/dist/_/mistral/makeMistralAIQuickie.d.ts.map +1 -0
- package/backend/dist/_/mistral/makeMistralAIQuickie.js +45 -0
- package/backend/dist/_/mistral/makeMistralAIThink.d.ts +3 -0
- package/backend/dist/_/mistral/makeMistralAIThink.d.ts.map +1 -0
- package/backend/dist/_/mistral/makeMistralAIThink.js +74 -0
- package/backend/dist/_/openai/_/aiEvent2OpenAIMessage.d.ts +4 -0
- package/backend/dist/_/openai/_/aiEvent2OpenAIMessage.d.ts.map +1 -0
- package/backend/dist/_/openai/_/aiEvent2OpenAIMessage.js +47 -0
- package/backend/dist/_/openai/_/aiTool2OpenAITool.d.ts +4 -0
- package/backend/dist/_/openai/_/aiTool2OpenAITool.d.ts.map +1 -0
- package/backend/dist/_/openai/_/aiTool2OpenAITool.js +14 -0
- package/backend/dist/_/openai/_/compileOpenAIThought.d.ts +7 -0
- package/backend/dist/_/openai/_/compileOpenAIThought.d.ts.map +1 -0
- package/backend/dist/_/openai/_/compileOpenAIThought.js +17 -0
- package/backend/dist/_/openai/_/getOpenAIClient.d.ts +4 -0
- package/backend/dist/_/openai/_/getOpenAIClient.d.ts.map +1 -0
- package/backend/dist/_/openai/_/getOpenAIClient.js +12 -0
- package/backend/dist/_/openai/_/index.d.ts +5 -0
- package/backend/dist/_/openai/_/index.d.ts.map +1 -0
- package/backend/dist/_/openai/_/index.js +4 -0
- package/backend/dist/_/openai/index.d.ts +4 -0
- package/backend/dist/_/openai/index.d.ts.map +1 -0
- package/backend/dist/_/openai/index.js +3 -0
- package/backend/dist/_/openai/makeOpenAIEmbed.d.ts +3 -0
- package/backend/dist/_/openai/makeOpenAIEmbed.d.ts.map +1 -0
- package/backend/dist/_/openai/makeOpenAIEmbed.js +15 -0
- package/backend/dist/_/openai/makeOpenAIQuickie.d.ts +3 -0
- package/backend/dist/_/openai/makeOpenAIQuickie.d.ts.map +1 -0
- package/backend/dist/_/openai/makeOpenAIQuickie.js +47 -0
- package/backend/dist/_/openai/makeOpenAIThink.d.ts +3 -0
- package/backend/dist/_/openai/makeOpenAIThink.d.ts.map +1 -0
- package/backend/dist/_/openai/makeOpenAIThink.js +75 -0
- package/backend/dist/_/types/errors.d.ts +4 -0
- package/backend/dist/_/types/errors.d.ts.map +1 -0
- package/backend/dist/_/types/errors.js +3 -0
- package/backend/dist/_/types/index.d.ts +2 -0
- package/backend/dist/_/types/index.d.ts.map +1 -0
- package/backend/dist/_/types/index.js +1 -0
- package/backend/dist/_/voyageai/_/getVoyageAIClient.d.ts +4 -0
- package/backend/dist/_/voyageai/_/getVoyageAIClient.d.ts.map +1 -0
- package/backend/dist/_/voyageai/_/getVoyageAIClient.js +7 -0
- package/backend/dist/_/voyageai/_/index.d.ts +2 -0
- package/backend/dist/_/voyageai/_/index.d.ts.map +1 -0
- package/backend/dist/_/voyageai/_/index.js +1 -0
- package/backend/dist/_/voyageai/index.d.ts +2 -0
- package/backend/dist/_/voyageai/index.d.ts.map +1 -0
- package/backend/dist/_/voyageai/index.js +1 -0
- package/backend/dist/_/voyageai/makeVoyageAIEmbed.d.ts +3 -0
- package/backend/dist/_/voyageai/makeVoyageAIEmbed.d.ts.map +1 -0
- package/backend/dist/_/voyageai/makeVoyageAIEmbed.js +13 -0
- package/backend/dist/doEmbed.d.ts +5 -0
- package/backend/dist/doEmbed.d.ts.map +1 -0
- package/backend/dist/doEmbed.js +17 -0
- package/backend/dist/doQuickie.d.ts +5 -0
- package/backend/dist/doQuickie.d.ts.map +1 -0
- package/backend/dist/doQuickie.js +39 -0
- package/backend/dist/doStream.d.ts +5 -0
- package/backend/dist/doStream.d.ts.map +1 -0
- package/backend/dist/doStream.js +14 -0
- package/backend/dist/doThink.d.ts +4 -0
- package/backend/dist/doThink.d.ts.map +1 -0
- package/backend/dist/doThink.js +33 -0
- package/backend/dist/index.d.ts +5 -0
- package/backend/dist/index.d.ts.map +1 -0
- package/backend/dist/index.js +4 -0
- package/backend/package.json +46 -0
- package/package.json +4 -4
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { AIEventType } from '@sjts/ai-shared';
|
|
2
|
+
export function aiEvent2MistralAIMessage(event) {
|
|
3
|
+
switch (event.type) {
|
|
4
|
+
case AIEventType.USER_CHAT:
|
|
5
|
+
// Include Images
|
|
6
|
+
if (event.images?.length) {
|
|
7
|
+
return {
|
|
8
|
+
role: 'user',
|
|
9
|
+
content: [
|
|
10
|
+
{ type: 'text', text: event.content },
|
|
11
|
+
...event.images.map(image => ({
|
|
12
|
+
type: 'image_url',
|
|
13
|
+
image_url: { url: `data:${image.mime};base64,${image.data}` },
|
|
14
|
+
})),
|
|
15
|
+
],
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
// Plain Text
|
|
19
|
+
return {
|
|
20
|
+
role: 'user',
|
|
21
|
+
content: event.content,
|
|
22
|
+
};
|
|
23
|
+
case AIEventType.AI_CHAT:
|
|
24
|
+
return {
|
|
25
|
+
role: 'assistant',
|
|
26
|
+
content: event.content,
|
|
27
|
+
};
|
|
28
|
+
case AIEventType.TOOL_REQUEST:
|
|
29
|
+
return {
|
|
30
|
+
role: 'assistant',
|
|
31
|
+
tool_calls: event.toolCalls.map(tc => ({
|
|
32
|
+
id: tc.callId,
|
|
33
|
+
type: 'function',
|
|
34
|
+
function: {
|
|
35
|
+
name: tc.tool,
|
|
36
|
+
arguments: JSON.stringify(tc.params),
|
|
37
|
+
},
|
|
38
|
+
})),
|
|
39
|
+
};
|
|
40
|
+
case AIEventType.TOOL_RESPONSE:
|
|
41
|
+
return {
|
|
42
|
+
role: 'tool',
|
|
43
|
+
tool_call_id: event.callId,
|
|
44
|
+
content: event.content,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aiTool2MistralAITool.d.ts","sourceRoot":"","sources":["../../../../src/_/mistral/_/aiTool2MistralAITool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAGxC,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAYtD"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { compileObject } from '../../compileObject.js';
|
|
2
|
+
export function aiTool2MistralAITool(tool) {
|
|
3
|
+
return {
|
|
4
|
+
type: 'function',
|
|
5
|
+
function: {
|
|
6
|
+
name: tool.key,
|
|
7
|
+
description: tool.description,
|
|
8
|
+
parameters: {
|
|
9
|
+
...compileObject(tool.params),
|
|
10
|
+
type: 'object',
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compileMistralAIThought.d.ts","sourceRoot":"","sources":["../../../../src/_/mistral/_/compileMistralAIThought.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAIhD,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,cAAc,GAAG;IAC/D,QAAQ,EAAE,GAAG,EAAE,CAAA;IACf,KAAK,EAAE,GAAG,EAAE,CAAA;CACZ,CAeA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { aiEvent2MistralAIMessage } from './aiEvent2MistralAIMessage.js';
|
|
2
|
+
import { aiTool2MistralAITool } from './aiTool2MistralAITool.js';
|
|
3
|
+
export function compileMistralAIThought(input) {
|
|
4
|
+
const messages = [];
|
|
5
|
+
if (input.context)
|
|
6
|
+
messages.push({
|
|
7
|
+
role: 'system',
|
|
8
|
+
content: input.context,
|
|
9
|
+
});
|
|
10
|
+
const historyMessages = input.history.map(aiEvent2MistralAIMessage).filter(Boolean);
|
|
11
|
+
if (historyMessages?.length)
|
|
12
|
+
messages.push(...historyMessages);
|
|
13
|
+
const tools = input?.tools?.map(aiTool2MistralAITool) ?? [];
|
|
14
|
+
return { messages, tools };
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getMistralAIClient.d.ts","sourceRoot":"","sources":["../../../../src/_/mistral/_/getMistralAIClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI3C,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,SAAS,GAAG,OAAO,CAK1D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/_/mistral/_/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAA;AAC7C,cAAc,2BAA2B,CAAA;AACzC,cAAc,8BAA8B,CAAA;AAC5C,cAAc,yBAAyB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/_/mistral/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAA;AACzC,cAAc,yBAAyB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeMistralAIQuickie.d.ts","sourceRoot":"","sources":["../../../src/_/mistral/makeMistralAIQuickie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAe,OAAO,EAAmB,MAAM,iBAAiB,CAAA;AAMjF,wBAAgB,oBAAoB,KACrB,IAAI,QAAQ,EAAE,SAAS,MAAM,EAAE,SAAQ,OAAO,EAAO,KAAG,OAAO,CAAC,MAAM,CAAC,CAIrF"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { AIEventType, defaultAIConfig } from '@sjts/ai-shared';
|
|
2
|
+
import { Logger } from '@sjts/lib-log';
|
|
3
|
+
import { callAI } from '../callAI.js';
|
|
4
|
+
import { aiEvent2MistralAIMessage } from './_/aiEvent2MistralAIMessage.js';
|
|
5
|
+
import { getMistralAIClient } from './_/getMistralAIClient.js';
|
|
6
|
+
export function makeMistralAIQuickie() {
|
|
7
|
+
return async (ai, content, images = []) => {
|
|
8
|
+
const response = await handleRequest(ai, content, images);
|
|
9
|
+
return handleResponse(response);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
// ---------------
|
|
13
|
+
// HANDLE REQUEST
|
|
14
|
+
// ---------------
|
|
15
|
+
async function handleRequest(ai, content, images = []) {
|
|
16
|
+
try {
|
|
17
|
+
const client = getMistralAIClient(ai.api);
|
|
18
|
+
const c = { ...defaultAIConfig, ...ai };
|
|
19
|
+
const request = {
|
|
20
|
+
messages: [aiEvent2MistralAIMessage({ type: AIEventType.USER_CHAT, content, images })],
|
|
21
|
+
model: c.llm.model,
|
|
22
|
+
max_tokens: c.max_tokens,
|
|
23
|
+
// temperature: c.temperature,
|
|
24
|
+
// frequency_penalty: c.frequency_penalty,
|
|
25
|
+
response_format: ai.jsonMode ? { type: 'json_object' } : undefined,
|
|
26
|
+
};
|
|
27
|
+
return callAI(r => client.chat.complete(r), request, c.timeout);
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
Logger.error('Problem processing AI Request', { message: e.message });
|
|
31
|
+
throw e;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// ---------------
|
|
35
|
+
// HANDLE RESPONSE
|
|
36
|
+
// ---------------
|
|
37
|
+
async function handleResponse(response) {
|
|
38
|
+
try {
|
|
39
|
+
return response.choices[0].message.content;
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
Logger.error('Problem processing AI response', { message: e.message });
|
|
43
|
+
throw e;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeMistralAIThink.d.ts","sourceRoot":"","sources":["../../../src/_/mistral/makeMistralAIThink.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,QAAQ,EAER,cAAc,EACd,eAAe,EAGf,MAAM,iBAAiB,CAAA;AAMxB,wBAAgB,kBAAkB,KACnB,IAAI,QAAQ,EAAE,OAAO,cAAc,KAAG,OAAO,CAAC,eAAe,CAAC,CAI5E"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { AIEventType, defaultAIConfig, parseJson, } from '@sjts/ai-shared';
|
|
2
|
+
import { Logger } from '@sjts/lib-log';
|
|
3
|
+
import { callAI } from '../callAI.js';
|
|
4
|
+
import { compileMistralAIThought } from './_/compileMistralAIThought.js';
|
|
5
|
+
import { getMistralAIClient } from './_/getMistralAIClient.js';
|
|
6
|
+
export function makeMistralAIThink() {
|
|
7
|
+
return async (ai, input) => {
|
|
8
|
+
const response = await handleRequest(ai, input);
|
|
9
|
+
return handleResponse(response);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
// ---------------
|
|
13
|
+
// HANDLE REQUEST
|
|
14
|
+
// ---------------
|
|
15
|
+
async function handleRequest(ai, input) {
|
|
16
|
+
try {
|
|
17
|
+
const { messages, tools } = compileMistralAIThought(input);
|
|
18
|
+
const hasTools = !!tools?.length;
|
|
19
|
+
const client = getMistralAIClient(ai.api);
|
|
20
|
+
const c = { ...defaultAIConfig, ...ai };
|
|
21
|
+
const request = {
|
|
22
|
+
messages,
|
|
23
|
+
model: c.llm.model,
|
|
24
|
+
tools: hasTools ? tools : undefined,
|
|
25
|
+
tool_choice: hasTools ? c.tool_choice : undefined,
|
|
26
|
+
max_tokens: c.max_tokens,
|
|
27
|
+
// top_p: c.top_p,
|
|
28
|
+
// temperature: c.temperature,
|
|
29
|
+
// frequency_penalty: c.frequency_penalty,
|
|
30
|
+
// presence_penalty: c.presence_penalty,
|
|
31
|
+
response_format: ai.jsonMode ? { type: 'json_object' } : undefined,
|
|
32
|
+
};
|
|
33
|
+
return callAI(r => client.chat.complete(r), request, c.timeout);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
Logger.error('Problem processing AI Request', { message: e.message });
|
|
37
|
+
throw e;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// ---------------
|
|
41
|
+
// HANDLE RESPONSE
|
|
42
|
+
// ---------------
|
|
43
|
+
async function handleResponse(response) {
|
|
44
|
+
try {
|
|
45
|
+
const message = response.choices[0].message;
|
|
46
|
+
//----------------
|
|
47
|
+
// AI TOOL CALL MESSAGE
|
|
48
|
+
//----------------
|
|
49
|
+
if (message.tool_calls?.length) {
|
|
50
|
+
return {
|
|
51
|
+
type: AIEventType.TOOL_REQUEST,
|
|
52
|
+
content: message.content ?? '',
|
|
53
|
+
toolCalls: message.tool_calls.map(t => ({
|
|
54
|
+
callId: t.id,
|
|
55
|
+
tool: t.function.name,
|
|
56
|
+
params: (parseJson(t.function.arguments) ?? []),
|
|
57
|
+
})),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
//----------------
|
|
61
|
+
// AI CHAT MESSAGE
|
|
62
|
+
//----------------
|
|
63
|
+
else {
|
|
64
|
+
return {
|
|
65
|
+
type: AIEventType.AI_CHAT,
|
|
66
|
+
content: message.content ?? '',
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
Logger.error('Problem processing AI response', { message: e.message });
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aiEvent2OpenAIMessage.d.ts","sourceRoot":"","sources":["../../../../src/_/openai/_/aiEvent2OpenAIMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,iBAAiB,CAAA;AACtD,OAAO,MAAM,MAAM,QAAQ,CAAA;AAE3B,wBAAgB,qBAAqB,CACpC,KAAK,EAAE,OAAO,GACZ,MAAM,CAAC,0BAA0B,GAAG,SAAS,CAmD/C"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { AIEventType } from '@sjts/ai-shared';
|
|
2
|
+
export function aiEvent2OpenAIMessage(event) {
|
|
3
|
+
switch (event.type) {
|
|
4
|
+
case AIEventType.USER_CHAT:
|
|
5
|
+
// Include Images
|
|
6
|
+
if (event.images?.length) {
|
|
7
|
+
return {
|
|
8
|
+
role: 'user',
|
|
9
|
+
content: [
|
|
10
|
+
{ type: 'text', text: event.content },
|
|
11
|
+
...event.images.map(image => ({
|
|
12
|
+
type: 'image_url',
|
|
13
|
+
image_url: { url: `data:${image.mime};base64,${image.data}` },
|
|
14
|
+
})),
|
|
15
|
+
],
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
// Plain Text
|
|
19
|
+
return {
|
|
20
|
+
role: 'user',
|
|
21
|
+
content: event.content,
|
|
22
|
+
};
|
|
23
|
+
case AIEventType.AI_CHAT:
|
|
24
|
+
return {
|
|
25
|
+
role: 'assistant',
|
|
26
|
+
content: event.content,
|
|
27
|
+
};
|
|
28
|
+
case AIEventType.TOOL_REQUEST:
|
|
29
|
+
return {
|
|
30
|
+
role: 'assistant',
|
|
31
|
+
tool_calls: event.toolCalls.map(tc => ({
|
|
32
|
+
id: tc.callId,
|
|
33
|
+
type: 'function',
|
|
34
|
+
function: {
|
|
35
|
+
name: tc.tool,
|
|
36
|
+
arguments: JSON.stringify(tc.params),
|
|
37
|
+
},
|
|
38
|
+
})),
|
|
39
|
+
};
|
|
40
|
+
case AIEventType.TOOL_RESPONSE:
|
|
41
|
+
return {
|
|
42
|
+
role: 'tool',
|
|
43
|
+
tool_call_id: event.callId,
|
|
44
|
+
content: event.content,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aiTool2OpenAITool.d.ts","sourceRoot":"","sources":["../../../../src/_/openai/_/aiTool2OpenAITool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AACxC,OAAO,MAAM,MAAM,QAAQ,CAAA;AAG3B,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,kBAAkB,CAYzE"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { compileObject } from '../../compileObject.js';
|
|
2
|
+
export function aiTool2OpenAITool(tool) {
|
|
3
|
+
return {
|
|
4
|
+
type: 'function',
|
|
5
|
+
function: {
|
|
6
|
+
name: tool.key,
|
|
7
|
+
description: tool.description,
|
|
8
|
+
parameters: {
|
|
9
|
+
...compileObject(tool.params),
|
|
10
|
+
type: 'object',
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AIThoughtInput } from '@sjts/ai-shared';
|
|
2
|
+
import OpenAI from 'openai';
|
|
3
|
+
export declare function compileOpenAIThought(input: AIThoughtInput): {
|
|
4
|
+
messages: OpenAI.ChatCompletionMessageParam[];
|
|
5
|
+
tools: OpenAI.ChatCompletionTool[];
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=compileOpenAIThought.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compileOpenAIThought.d.ts","sourceRoot":"","sources":["../../../../src/_/openai/_/compileOpenAIThought.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,MAAM,MAAM,QAAQ,CAAA;AAI3B,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,cAAc,GAAG;IAC5D,QAAQ,EAAE,MAAM,CAAC,0BAA0B,EAAE,CAAA;IAC7C,KAAK,EAAE,MAAM,CAAC,kBAAkB,EAAE,CAAA;CAClC,CAiBA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { aiEvent2OpenAIMessage } from './aiEvent2OpenAIMessage.js';
|
|
2
|
+
import { aiTool2OpenAITool } from './aiTool2OpenAITool.js';
|
|
3
|
+
export function compileOpenAIThought(input) {
|
|
4
|
+
const messages = [];
|
|
5
|
+
if (input.context)
|
|
6
|
+
messages.push({
|
|
7
|
+
role: 'system',
|
|
8
|
+
content: input.context,
|
|
9
|
+
});
|
|
10
|
+
const historyMessages = input.history
|
|
11
|
+
.map(aiEvent2OpenAIMessage)
|
|
12
|
+
.filter(Boolean);
|
|
13
|
+
if (historyMessages?.length)
|
|
14
|
+
messages.push(...historyMessages);
|
|
15
|
+
const tools = input?.tools?.map(aiTool2OpenAITool) ?? [];
|
|
16
|
+
return { messages, tools };
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getOpenAIClient.d.ts","sourceRoot":"","sources":["../../../../src/_/openai/_/getOpenAIClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,MAAM,MAAM,QAAQ,CAAA;AAI3B,wBAAgB,eAAe,CAAC,GAAG,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CASxE"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
// const _cache: { current: OpenAI | undefined } = { current: undefined }
|
|
3
|
+
export function getOpenAIClient(cfg, baseURL) {
|
|
4
|
+
const options = {
|
|
5
|
+
apiKey: cfg.apiKey,
|
|
6
|
+
};
|
|
7
|
+
if (!!baseURL)
|
|
8
|
+
options.baseURL = baseURL;
|
|
9
|
+
else if (!!cfg.customAPISettings?.organization)
|
|
10
|
+
options.organization = cfg.customAPISettings.organization;
|
|
11
|
+
return new OpenAI(options);
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/_/openai/_/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAA;AAC1C,cAAc,wBAAwB,CAAA;AACtC,cAAc,2BAA2B,CAAA;AACzC,cAAc,sBAAsB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/_/openai/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,wBAAwB,CAAA;AACtC,cAAc,sBAAsB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeOpenAIEmbed.d.ts","sourceRoot":"","sources":["../../../src/_/openai/makeOpenAIEmbed.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAmB,MAAM,iBAAiB,CAAA;AAGxE,wBAAgB,eAAe,KAChB,IAAI,QAAQ,EAAE,OAAO,MAAM,KAAG,OAAO,CAAC,WAAW,CAAC,CAWhE"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defaultAIConfig } from '@sjts/ai-shared';
|
|
2
|
+
import { getOpenAIClient } from './_/getOpenAIClient.js';
|
|
3
|
+
export function makeOpenAIEmbed() {
|
|
4
|
+
return async (ai, input) => {
|
|
5
|
+
const client = getOpenAIClient(ai.api);
|
|
6
|
+
const c = { ...defaultAIConfig, ...ai };
|
|
7
|
+
const request = {
|
|
8
|
+
input,
|
|
9
|
+
dimensions: c.dimensions,
|
|
10
|
+
model: c.llm.model,
|
|
11
|
+
};
|
|
12
|
+
const response = await client.embeddings.create(request);
|
|
13
|
+
return response.data[0]?.embedding;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeOpenAIQuickie.d.ts","sourceRoot":"","sources":["../../../src/_/openai/makeOpenAIQuickie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAe,OAAO,EAAmB,MAAM,iBAAiB,CAAA;AAMjF,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,MAAW,IACvC,IAAI,QAAQ,EAAE,SAAS,MAAM,EAAE,SAAQ,OAAO,EAAO,KAAG,OAAO,CAAC,MAAM,CAAC,CAIrF"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { AIEventType, defaultAIConfig } from '@sjts/ai-shared';
|
|
2
|
+
import { Logger } from '@sjts/lib-log';
|
|
3
|
+
import { callAI } from '../callAI.js';
|
|
4
|
+
import { getOpenAIClient } from './_/getOpenAIClient.js';
|
|
5
|
+
import { aiEvent2OpenAIMessage } from './_/index.js';
|
|
6
|
+
export function makeOpenAIQuickie(baseUrl = '') {
|
|
7
|
+
return async (ai, content, images = []) => {
|
|
8
|
+
const response = await handleRequest(ai, baseUrl, content, images);
|
|
9
|
+
return handleResponse(response);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
// ---------------
|
|
13
|
+
// HANDLE REQUEST
|
|
14
|
+
// ---------------
|
|
15
|
+
async function handleRequest(ai, baseUrl, content, images = []) {
|
|
16
|
+
try {
|
|
17
|
+
const client = getOpenAIClient(ai.api, baseUrl);
|
|
18
|
+
const c = { ...defaultAIConfig, ...ai };
|
|
19
|
+
const request = {
|
|
20
|
+
messages: [aiEvent2OpenAIMessage({ type: AIEventType.USER_CHAT, content, images })],
|
|
21
|
+
model: c.llm.model,
|
|
22
|
+
max_completion_tokens: c.max_tokens,
|
|
23
|
+
// top_p: c.top_p,
|
|
24
|
+
// temperature: c.temperature,
|
|
25
|
+
// frequency_penalty: c.frequency_penalty,
|
|
26
|
+
// presence_penalty: c.presence_penalty,
|
|
27
|
+
response_format: ai.jsonMode ? { type: 'json_object' } : undefined,
|
|
28
|
+
};
|
|
29
|
+
return callAI(r => client.chat.completions.create(r), request, c.timeout);
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
Logger.error('Problem processing AI Request', { message: e.message });
|
|
33
|
+
throw e;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// ---------------
|
|
37
|
+
// HANDLE RESPONSE
|
|
38
|
+
// ---------------
|
|
39
|
+
async function handleResponse(response) {
|
|
40
|
+
try {
|
|
41
|
+
return response.choices[0].message.content;
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
Logger.error('Problem processing AI response', { message: e.message });
|
|
45
|
+
throw e;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeOpenAIThink.d.ts","sourceRoot":"","sources":["../../../src/_/openai/makeOpenAIThink.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,QAAQ,EAER,cAAc,EACd,eAAe,EAGf,MAAM,iBAAiB,CAAA;AAOxB,wBAAgB,eAAe,CAAC,OAAO,GAAE,MAAW,IACrC,IAAI,QAAQ,EAAE,OAAO,cAAc,KAAG,OAAO,CAAC,eAAe,CAAC,CAI5E"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { AIEventType, defaultAIConfig, parseJson, } from '@sjts/ai-shared';
|
|
2
|
+
import { Logger } from '@sjts/lib-log';
|
|
3
|
+
import { callAI } from '../callAI.js';
|
|
4
|
+
import { compileOpenAIThought } from './_/compileOpenAIThought.js';
|
|
5
|
+
import { getOpenAIClient } from './_/getOpenAIClient.js';
|
|
6
|
+
export function makeOpenAIThink(baseUrl = '') {
|
|
7
|
+
return async (ai, input) => {
|
|
8
|
+
const response = await handleRequest(ai, baseUrl, input);
|
|
9
|
+
return handleResponse(response);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
// ---------------
|
|
13
|
+
// HANDLE REQUEST
|
|
14
|
+
// ---------------
|
|
15
|
+
async function handleRequest(ai, baseUrl, input) {
|
|
16
|
+
try {
|
|
17
|
+
const { messages, tools } = compileOpenAIThought(input);
|
|
18
|
+
const hasTools = !!tools?.length;
|
|
19
|
+
const client = getOpenAIClient(ai.api, baseUrl);
|
|
20
|
+
const c = { ...defaultAIConfig, ...ai };
|
|
21
|
+
const request = {
|
|
22
|
+
messages,
|
|
23
|
+
model: c.llm.model,
|
|
24
|
+
tools: hasTools ? tools : undefined,
|
|
25
|
+
tool_choice: hasTools ? c.tool_choice : undefined,
|
|
26
|
+
max_completion_tokens: c.max_tokens,
|
|
27
|
+
// top_p: c.top_p,
|
|
28
|
+
// temperature: c.temperature,
|
|
29
|
+
// frequency_penalty: c.frequency_penalty,
|
|
30
|
+
// presence_penalty: c.presence_penalty,
|
|
31
|
+
response_format: ai.jsonMode ? { type: 'json_object' } : undefined,
|
|
32
|
+
};
|
|
33
|
+
return callAI(r => client.chat.completions.create(r), request, c.timeout);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
Logger.error('Problem processing AI request', { message: e.message });
|
|
37
|
+
throw e;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// ---------------
|
|
41
|
+
// HANDLE RESPONSE
|
|
42
|
+
// ---------------
|
|
43
|
+
async function handleResponse(response) {
|
|
44
|
+
try {
|
|
45
|
+
const message = response.choices[0].message;
|
|
46
|
+
//----------------
|
|
47
|
+
// AI TOOL CALL MESSAGE
|
|
48
|
+
//----------------
|
|
49
|
+
if (message.tool_calls?.length) {
|
|
50
|
+
return {
|
|
51
|
+
type: AIEventType.TOOL_REQUEST,
|
|
52
|
+
content: message.content ?? '',
|
|
53
|
+
toolCalls: message.tool_calls.map(t => ({
|
|
54
|
+
callId: t.id,
|
|
55
|
+
tool: t.function.name,
|
|
56
|
+
params: (parseJson(t.function.arguments) ?? []),
|
|
57
|
+
})),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
//----------------
|
|
61
|
+
// AI CHAT MESSAGE
|
|
62
|
+
//----------------
|
|
63
|
+
else {
|
|
64
|
+
return {
|
|
65
|
+
type: AIEventType.AI_CHAT,
|
|
66
|
+
content: message.content ?? '',
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
Logger.error('Problem processing AI response', { message: e.message });
|
|
72
|
+
Logger.error(JSON.stringify(response, null, 2));
|
|
73
|
+
throw e;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../src/_/types/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,cAAe,SAAQ,KAAK;IACxC,OAAO,SAAiB;CACxB"}
|