@sjts/ai-shared 31.1.2 → 31.2.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/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/dist/utils/indent.d.ts +1 -1
- package/dist/utils/indent.d.ts.map +1 -1
- package/dist/utils/indent.js +2 -2
- package/dist/utils/trimdent.d.ts +1 -1
- package/dist/utils/trimdent.d.ts.map +1 -1
- package/dist/utils/trimdent.js +4 -3
- package/package.json +4 -4
package/backend/LICENCE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getAmazonClient.d.ts","sourceRoot":"","sources":["../../../../src/_/amazon/_/getAmazonClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAA;AACtE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI3C,wBAAgB,eAAe,CAAC,GAAG,EAAE,SAAS,GAAG,oBAAoB,CAOpE"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BedrockRuntimeClient } from '@aws-sdk/client-bedrock-runtime';
|
|
2
|
+
const _cache = { current: undefined };
|
|
3
|
+
export function getAmazonClient(cfg) {
|
|
4
|
+
if (!_cache.current) {
|
|
5
|
+
_cache.current = new BedrockRuntimeClient({
|
|
6
|
+
region: cfg.customAPISettings?.region ?? 'ap-southeast-2',
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
return _cache.current;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/_/amazon/_/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './getAmazonClient.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/_/amazon/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './makeAmazonQuickie.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeAmazonQuickie.d.ts","sourceRoot":"","sources":["../../../src/_/amazon/makeAmazonQuickie.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAmB,MAAM,iBAAiB,CAAA;AAIpE,wBAAgB,iBAAiB,KAClB,IAAI,QAAQ,EAAE,SAAS,MAAM,EAAE,SAAQ,OAAO,EAAO,KAAG,OAAO,CAAC,MAAM,CAAC,CAIrF"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { ConversationRole, ConverseCommand } from '@aws-sdk/client-bedrock-runtime';
|
|
2
|
+
import { defaultAIConfig } from '@sjts/ai-shared';
|
|
3
|
+
import { Logger } from '@sjts/lib-log';
|
|
4
|
+
import { getAmazonClient } from './_/index.js';
|
|
5
|
+
export function makeAmazonQuickie() {
|
|
6
|
+
return async (ai, content, images = []) => {
|
|
7
|
+
const response = await handleRequest(ai, content, images);
|
|
8
|
+
return handleResponse(response);
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
// ---------------
|
|
12
|
+
// HANDLE REQUEST
|
|
13
|
+
// ---------------
|
|
14
|
+
async function handleRequest(ai, content, images = []) {
|
|
15
|
+
const client = getAmazonClient(ai.api);
|
|
16
|
+
const c = { ...defaultAIConfig, ...ai };
|
|
17
|
+
const generationConfig = {
|
|
18
|
+
maxOutputTokens: c.max_tokens,
|
|
19
|
+
};
|
|
20
|
+
const payload = {
|
|
21
|
+
textGenerationConfig: {
|
|
22
|
+
maxTokenCount: c.max_tokens,
|
|
23
|
+
stopSequences: [],
|
|
24
|
+
temperature: 0.7,
|
|
25
|
+
topP: 1,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
// Step 4: Send and process the request
|
|
29
|
+
// - Embed the payload in a request object
|
|
30
|
+
// - Send the request to the model
|
|
31
|
+
// - Extract and return the generated image data from the response
|
|
32
|
+
try {
|
|
33
|
+
const request = {
|
|
34
|
+
modelId: c.llm.model,
|
|
35
|
+
messages: [
|
|
36
|
+
{
|
|
37
|
+
content: [{ text: content }],
|
|
38
|
+
role: ConversationRole.USER,
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
inferenceConfig: {
|
|
42
|
+
maxTokens: c.max_tokens, // The maximum response length
|
|
43
|
+
temperature: 0.5, // Using temperature for randomness control
|
|
44
|
+
//topP: 0.9, // Alternative: use topP instead of temperature
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
return client.send(new ConverseCommand(request));
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
Logger.error('Problem processing AI Request', { message: e.message });
|
|
51
|
+
throw e;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// ---------------
|
|
55
|
+
// HANDLE RESPONSE
|
|
56
|
+
// ---------------
|
|
57
|
+
async function handleResponse(response) {
|
|
58
|
+
try {
|
|
59
|
+
return response.output.message.content[0].text;
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
Logger.error('Problem processing AI response', { message: e.message });
|
|
63
|
+
throw e;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aiEvent2AnthropicMessage.d.ts","sourceRoot":"","sources":["../../../../src/_/anthropic/_/aiEvent2AnthropicMessage.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAA;AACzC,OAAO,EAAE,OAAO,EAAe,MAAM,iBAAiB,CAAA;AAEtD,wBAAgB,wBAAwB,CACvC,OAAO,EAAE,OAAO,GACd,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,qBAAqB,CAkErD"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { AIEventType } from '@sjts/ai-shared';
|
|
2
|
+
export function aiEvent2AnthropicMessage(message) {
|
|
3
|
+
switch (message.type) {
|
|
4
|
+
case AIEventType.USER_CHAT:
|
|
5
|
+
// Include Images
|
|
6
|
+
if (message.images?.length) {
|
|
7
|
+
return {
|
|
8
|
+
role: 'user',
|
|
9
|
+
content: [
|
|
10
|
+
{ type: 'text', text: message.content },
|
|
11
|
+
...message.images.map(image => ({
|
|
12
|
+
type: 'image',
|
|
13
|
+
source: {
|
|
14
|
+
type: 'base64',
|
|
15
|
+
media_type: image.mime,
|
|
16
|
+
data: image.data,
|
|
17
|
+
},
|
|
18
|
+
})),
|
|
19
|
+
],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
// Plain Text
|
|
23
|
+
return {
|
|
24
|
+
role: 'user',
|
|
25
|
+
content: [{ type: 'text', text: message.content }],
|
|
26
|
+
};
|
|
27
|
+
case AIEventType.AI_CHAT:
|
|
28
|
+
return {
|
|
29
|
+
role: 'assistant',
|
|
30
|
+
content: [{ type: 'text', text: message.content }],
|
|
31
|
+
};
|
|
32
|
+
case AIEventType.TOOL_REQUEST:
|
|
33
|
+
return {
|
|
34
|
+
role: 'assistant',
|
|
35
|
+
content: [
|
|
36
|
+
message.content
|
|
37
|
+
? { type: 'text', text: message.content }
|
|
38
|
+
: undefined,
|
|
39
|
+
...message.toolCalls.map(action => ({
|
|
40
|
+
type: 'tool_use',
|
|
41
|
+
id: action.callId,
|
|
42
|
+
name: action.tool,
|
|
43
|
+
input: action.params,
|
|
44
|
+
})),
|
|
45
|
+
].filter(Boolean),
|
|
46
|
+
};
|
|
47
|
+
case AIEventType.TOOL_RESPONSE:
|
|
48
|
+
return {
|
|
49
|
+
role: 'user',
|
|
50
|
+
content: [
|
|
51
|
+
{
|
|
52
|
+
type: 'tool_result',
|
|
53
|
+
tool_use_id: message.callId,
|
|
54
|
+
content: [{ type: 'text', text: message.content }],
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aiTool2AnthropicTool.d.ts","sourceRoot":"","sources":["../../../../src/_/anthropic/_/aiTool2AnthropicTool.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAA;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAGxC,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CASrF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compileAnthropicThought.d.ts","sourceRoot":"","sources":["../../../../src/_/anthropic/_/compileAnthropicThought.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAIhD,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,cAAc,GAAG;IAC/D,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,QAAQ,EAAE,GAAG,EAAE,CAAA;IACf,KAAK,EAAE,GAAG,EAAE,GAAG,SAAS,CAAA;CACxB,CAgBA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { aiEvent2AnthropicMessage } from './aiEvent2AnthropicMessage.js';
|
|
2
|
+
import { aiTool2AnthropicTool } from './aiTool2AnthropicTool.js';
|
|
3
|
+
export function compileAnthropicThought(input) {
|
|
4
|
+
let system = input.context;
|
|
5
|
+
let messages = input.history?.map(aiEvent2AnthropicMessage) ?? [];
|
|
6
|
+
const tools = input.tools?.map(aiTool2AnthropicTool) ?? undefined;
|
|
7
|
+
if (!messages?.length && system) {
|
|
8
|
+
messages = [
|
|
9
|
+
{
|
|
10
|
+
role: 'user',
|
|
11
|
+
content: [{ type: 'text', text: system }],
|
|
12
|
+
},
|
|
13
|
+
];
|
|
14
|
+
system = '';
|
|
15
|
+
}
|
|
16
|
+
return { system, messages, tools };
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getAnthropicClient.d.ts","sourceRoot":"","sources":["../../../../src/_/anthropic/_/getAnthropicClient.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI3C,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,CAUlE"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
+
const _cache = { current: undefined };
|
|
3
|
+
export function getAnthropicClient(apiConfig) {
|
|
4
|
+
if (!_cache.current) {
|
|
5
|
+
_cache.current = new Anthropic({
|
|
6
|
+
apiKey: apiConfig.apiKey,
|
|
7
|
+
defaultHeaders: {
|
|
8
|
+
'anthropic-beta': 'tools-2024-04-04',
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
return _cache.current;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/_/anthropic/_/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/_/anthropic/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAA;AACzC,cAAc,0BAA0B,CAAA;AACxC,cAAc,yBAAyB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeAnthropicQuickie.d.ts","sourceRoot":"","sources":["../../../src/_/anthropic/makeAnthropicQuickie.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,53 @@
|
|
|
1
|
+
import { AIEventType, defaultAIConfig } from '@sjts/ai-shared';
|
|
2
|
+
import { Logger } from '@sjts/lib-log';
|
|
3
|
+
import { callAI } from '../callAI.js';
|
|
4
|
+
import { getAnthropicClient } from './_/getAnthropicClient.js';
|
|
5
|
+
import { aiEvent2AnthropicMessage } from './_/index.js';
|
|
6
|
+
export function makeAnthropicQuickie() {
|
|
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 = getAnthropicClient(ai.api);
|
|
18
|
+
const c = { ...defaultAIConfig, ...ai };
|
|
19
|
+
// Handling JSON mode
|
|
20
|
+
if (ai.jsonMode) {
|
|
21
|
+
content =
|
|
22
|
+
`# IMPORTANT
|
|
23
|
+
- Please respond in JSON
|
|
24
|
+
- Do not wrap the JSON in Markdown
|
|
25
|
+
- Raw JSON, starting with { and ending with }
|
|
26
|
+
- Thank you
|
|
27
|
+
|
|
28
|
+
` + content;
|
|
29
|
+
}
|
|
30
|
+
const request = {
|
|
31
|
+
messages: [aiEvent2AnthropicMessage({ type: AIEventType.USER_CHAT, content, images })],
|
|
32
|
+
model: c.llm.model,
|
|
33
|
+
max_tokens: c.max_tokens,
|
|
34
|
+
};
|
|
35
|
+
return callAI(r => client.messages.create(r), request, c.timeout);
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
Logger.error('Problem processing AI Request', { message: e.message });
|
|
39
|
+
throw e;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// ---------------
|
|
43
|
+
// HANDLE RESPONSE
|
|
44
|
+
// ---------------
|
|
45
|
+
async function handleResponse(response) {
|
|
46
|
+
try {
|
|
47
|
+
return response.content[0].text;
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
Logger.error('Problem processing AI response', { message: e.message });
|
|
51
|
+
throw e;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeAnthropicStream.d.ts","sourceRoot":"","sources":["../../../src/_/anthropic/makeAnthropicStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAe,OAAO,EAAmB,MAAM,iBAAiB,CAAA;AAIjF,wBAAgB,mBAAmB,KAEjC,IAAI,QAAQ,EACZ,SAAS,MAAM,EACf,QAAQ,OAAO,EAAE,YAAK,EACtB,SAAS,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,KAC9B,OAAO,CAAC,IAAI,CAAC,CA+BhB"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AIEventType, defaultAIConfig } from '@sjts/ai-shared';
|
|
2
|
+
import { getAnthropicClient } from './_/getAnthropicClient.js';
|
|
3
|
+
import { aiEvent2AnthropicMessage } from './_/index.js';
|
|
4
|
+
export function makeAnthropicStream() {
|
|
5
|
+
return async (ai, content, images = [], onChunk) => {
|
|
6
|
+
await new Promise(async (resolve, reject) => {
|
|
7
|
+
const client = getAnthropicClient(ai.api);
|
|
8
|
+
const c = { ...defaultAIConfig, ...ai };
|
|
9
|
+
await client.messages
|
|
10
|
+
.stream({
|
|
11
|
+
messages: [
|
|
12
|
+
aiEvent2AnthropicMessage({
|
|
13
|
+
type: AIEventType.USER_CHAT,
|
|
14
|
+
content,
|
|
15
|
+
images,
|
|
16
|
+
}),
|
|
17
|
+
],
|
|
18
|
+
model: c.llm.model,
|
|
19
|
+
max_tokens: c.max_tokens,
|
|
20
|
+
})
|
|
21
|
+
.on('text', text => {
|
|
22
|
+
console.log('Stream text', text);
|
|
23
|
+
onChunk(text);
|
|
24
|
+
})
|
|
25
|
+
.on('error', error => {
|
|
26
|
+
console.log('Stream error', error);
|
|
27
|
+
reject(error);
|
|
28
|
+
})
|
|
29
|
+
.on('end', () => {
|
|
30
|
+
console.log('Stream end');
|
|
31
|
+
resolve();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeAnthropicThink.d.ts","sourceRoot":"","sources":["../../../src/_/anthropic/makeAnthropicThink.ts"],"names":[],"mappings":"AACA,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,84 @@
|
|
|
1
|
+
import { AIEventType, AIToolChoice, defaultAIConfig, } from '@sjts/ai-shared';
|
|
2
|
+
import { Logger } from '@sjts/lib-log';
|
|
3
|
+
import { callAI } from '../callAI.js';
|
|
4
|
+
import { compileAnthropicThought } from './_/compileAnthropicThought.js';
|
|
5
|
+
import { getAnthropicClient } from './_/getAnthropicClient.js';
|
|
6
|
+
export function makeAnthropicThink() {
|
|
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 client = getAnthropicClient(ai.api);
|
|
18
|
+
let { system, messages, tools } = compileAnthropicThought(input);
|
|
19
|
+
const c = { ...defaultAIConfig, ...ai };
|
|
20
|
+
// Handling JSON mode
|
|
21
|
+
if (ai.jsonMode) {
|
|
22
|
+
system += `
|
|
23
|
+
# IMPORTANT
|
|
24
|
+
- Please respond in JSON
|
|
25
|
+
- Do not wrap the JSON in Markdown
|
|
26
|
+
- Raw JSON, starting with { and ending with }
|
|
27
|
+
- Thank you
|
|
28
|
+
`;
|
|
29
|
+
}
|
|
30
|
+
const request = {
|
|
31
|
+
system,
|
|
32
|
+
messages,
|
|
33
|
+
tools,
|
|
34
|
+
model: c.llm.model,
|
|
35
|
+
max_tokens: c.max_tokens,
|
|
36
|
+
};
|
|
37
|
+
// Handling REQUIRED tool choice
|
|
38
|
+
if (c.tool_choice === AIToolChoice.REQUIRED) {
|
|
39
|
+
request.tool_choice = { type: 'any' };
|
|
40
|
+
}
|
|
41
|
+
return callAI(r => client.messages.create(r), request, c.timeout);
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
Logger.error('Problem processing AI Request', { message: e.message });
|
|
45
|
+
throw e;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// ---------------
|
|
49
|
+
// HANDLE RESPONSE
|
|
50
|
+
// ---------------
|
|
51
|
+
async function handleResponse(response) {
|
|
52
|
+
try {
|
|
53
|
+
const textBlock = response.content.find(c => c.type === 'text');
|
|
54
|
+
const toolBlocks = response.content.filter(c => c.type === 'tool_use');
|
|
55
|
+
//----------------
|
|
56
|
+
// AI TOOL CALL MESSAGE
|
|
57
|
+
//----------------
|
|
58
|
+
if (!!toolBlocks?.length) {
|
|
59
|
+
return {
|
|
60
|
+
type: AIEventType.TOOL_REQUEST,
|
|
61
|
+
content: textBlock?.text ?? undefined,
|
|
62
|
+
toolCalls: toolBlocks.map(tc => ({
|
|
63
|
+
callId: tc.id,
|
|
64
|
+
tool: tc.name,
|
|
65
|
+
params: tc.input,
|
|
66
|
+
})),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
//----------------
|
|
70
|
+
// AI CHAT MESSAGE
|
|
71
|
+
//----------------
|
|
72
|
+
else {
|
|
73
|
+
return {
|
|
74
|
+
type: AIEventType.AI_CHAT,
|
|
75
|
+
content: textBlock.text ?? '',
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
Logger.error('Problem processing AI response', { message: e.message });
|
|
81
|
+
Logger.error(JSON.stringify(response, null, 2));
|
|
82
|
+
throw e;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callAI.d.ts","sourceRoot":"","sources":["../../src/_/callAI.ts"],"names":[],"mappings":"AAEA,wBAAsB,MAAM,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,gBAO9F"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AITimeoutError } from './types/errors.js';
|
|
2
|
+
export async function callAI(fnc, request, timeout) {
|
|
3
|
+
const response = await Promise.race([
|
|
4
|
+
fnc(request),
|
|
5
|
+
new Promise(r => setTimeout(() => r('timeout'), timeout)),
|
|
6
|
+
]);
|
|
7
|
+
if (response === 'timeout')
|
|
8
|
+
throw new AITimeoutError();
|
|
9
|
+
return response;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aiEvent2CerebrasMessage.d.ts","sourceRoot":"","sources":["../../../../src/_/cerebras/_/aiEvent2CerebrasMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,iBAAiB,CAAA;AAEtD,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,GAAG,GAAG,CAmD3D"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { AIEventType } from '@sjts/ai-shared';
|
|
2
|
+
export function aiEvent2CerebrasMessage(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":"getCerebrasClient.d.ts","sourceRoot":"","sources":["../../../../src/_/cerebras/_/getCerebrasClient.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,8BAA8B,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAI3C,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,SAAS,GAAG,QAAQ,CAK1D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/_/cerebras/_/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAA;AAC5C,cAAc,wBAAwB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/_/cerebras/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAA;AACxC,cAAc,wBAAwB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"makeCerebrasQuickie.d.ts","sourceRoot":"","sources":["../../../src/_/cerebras/makeCerebrasQuickie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAe,OAAO,EAAmB,MAAM,iBAAiB,CAAA;AAKjF,wBAAgB,mBAAmB,KACpB,IAAI,QAAQ,EAAE,SAAS,MAAM,EAAE,SAAQ,OAAO,EAAO,KAAG,OAAO,CAAC,MAAM,CAAC,CAIrF"}
|