@nu-art/ts-openai-backend 0.400.2
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.d.ts +1 -0
- package/index.js +2 -0
- package/index.js.map +1 -0
- package/modules/ModuleBE_OpenAI.d.ts +33 -0
- package/modules/ModuleBE_OpenAI.js +49 -0
- package/modules/ModuleBE_OpenAI.js.map +1 -0
- package/modules/ModuleBE_OpenAIMCP.d.ts +32 -0
- package/modules/ModuleBE_OpenAIMCP.js +74 -0
- package/modules/ModuleBE_OpenAIMCP.js.map +1 -0
- package/modules/ModuleBE_OpenAIV2/ModuleBE_OpenAIV2.d.ts +19 -0
- package/modules/ModuleBE_OpenAIV2/ModuleBE_OpenAIV2.js +41 -0
- package/modules/ModuleBE_OpenAIV2/ModuleBE_OpenAIV2.js.map +1 -0
- package/modules/ModuleBE_OpenAIV2/clients/OpenAIClient_Assistant.d.ts +65 -0
- package/modules/ModuleBE_OpenAIV2/clients/OpenAIClient_Assistant.js +134 -0
- package/modules/ModuleBE_OpenAIV2/clients/OpenAIClient_Assistant.js.map +1 -0
- package/modules/ModuleBE_OpenAIV2/clients/OpenAIClient_Chat.d.ts +17 -0
- package/modules/ModuleBE_OpenAIV2/clients/OpenAIClient_Chat.js +27 -0
- package/modules/ModuleBE_OpenAIV2/clients/OpenAIClient_Chat.js.map +1 -0
- package/package.json +65 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './modules/ModuleBE_OpenAI.js';
|
package/index.js
ADDED
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/Users/tacb0ss/dev/nu-art/beamz/_thunderstorm/ts-agents/openai/backend/src/main/","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Module, TypedMap } from '@nu-art/ts-common';
|
|
2
|
+
import { GPT_Model } from '@nu-art/ts-openai-shared/types';
|
|
3
|
+
import { Request_ChatGPT } from '@nu-art/ts-openai-shared/api-def';
|
|
4
|
+
type Config = {
|
|
5
|
+
directives: TypedMap<{
|
|
6
|
+
agent?: GPT_Model;
|
|
7
|
+
directive: string;
|
|
8
|
+
}>;
|
|
9
|
+
defaultModel: GPT_Model;
|
|
10
|
+
apiKey: string;
|
|
11
|
+
orgId?: string;
|
|
12
|
+
};
|
|
13
|
+
export type Request_PredefinedDirective = {
|
|
14
|
+
directiveKey: string;
|
|
15
|
+
message: string;
|
|
16
|
+
model?: GPT_Model;
|
|
17
|
+
};
|
|
18
|
+
export declare class ModuleBE_OpenAI_Class extends Module<Config> {
|
|
19
|
+
private openai;
|
|
20
|
+
constructor(tag?: string);
|
|
21
|
+
init(): void;
|
|
22
|
+
test: (query: Request_ChatGPT) => Promise<{
|
|
23
|
+
response: string;
|
|
24
|
+
}>;
|
|
25
|
+
predefinedQuery: (query: Request_PredefinedDirective) => Promise<{
|
|
26
|
+
response: string;
|
|
27
|
+
}>;
|
|
28
|
+
simpleQuery: (query: Request_ChatGPT) => Promise<{
|
|
29
|
+
response: string;
|
|
30
|
+
}>;
|
|
31
|
+
}
|
|
32
|
+
export declare const ModuleBE_OpenAI: ModuleBE_OpenAI_Class;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { __stringify, BadImplementationException, Module, ThisShouldNotHappenException } from '@nu-art/ts-common';
|
|
2
|
+
import { OpenAI } from 'openai';
|
|
3
|
+
import { addRoutes, createBodyServerApi } from '@nu-art/thunderstorm/backend/index';
|
|
4
|
+
import { ApiDef_OpenAI } from '@nu-art/ts-openai-shared/api-def';
|
|
5
|
+
export class ModuleBE_OpenAI_Class extends Module {
|
|
6
|
+
openai;
|
|
7
|
+
constructor(tag) {
|
|
8
|
+
super(tag);
|
|
9
|
+
}
|
|
10
|
+
init() {
|
|
11
|
+
const apiKey = this.config.apiKey;
|
|
12
|
+
const organization = this.config.orgId;
|
|
13
|
+
const opts = { apiKey, organization };
|
|
14
|
+
this.logDebug(opts);
|
|
15
|
+
this.openai = new OpenAI(opts);
|
|
16
|
+
addRoutes([
|
|
17
|
+
createBodyServerApi(ApiDef_OpenAI.v1.test, this.test),
|
|
18
|
+
]);
|
|
19
|
+
}
|
|
20
|
+
test = async (query) => this.simpleQuery(query);
|
|
21
|
+
predefinedQuery = async (query) => {
|
|
22
|
+
const directive = this.config.directives[query.directiveKey];
|
|
23
|
+
if (!directive)
|
|
24
|
+
throw new BadImplementationException(`Missing instruction for directive: ${query.directiveKey}`);
|
|
25
|
+
return this.simpleQuery({ model: directive.agent ?? query.model, message: query.message, directive: directive.directive });
|
|
26
|
+
};
|
|
27
|
+
simpleQuery = async (query) => {
|
|
28
|
+
const completion = await this.openai.chat.completions.create({
|
|
29
|
+
messages: [
|
|
30
|
+
{
|
|
31
|
+
role: 'system',
|
|
32
|
+
content: query.directive
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
role: 'user',
|
|
36
|
+
content: query.message
|
|
37
|
+
}
|
|
38
|
+
],
|
|
39
|
+
model: query.model || this.config.defaultModel || 'gpt-3.5-turbo',
|
|
40
|
+
});
|
|
41
|
+
const content = completion.choices[0].message.content;
|
|
42
|
+
this.logInfo(completion);
|
|
43
|
+
if (!content)
|
|
44
|
+
throw new ThisShouldNotHappenException(`Didn't receive a response from GPT, got: ${__stringify(completion, true)}`);
|
|
45
|
+
return { response: content };
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export const ModuleBE_OpenAI = new ModuleBE_OpenAI_Class();
|
|
49
|
+
//# sourceMappingURL=ModuleBE_OpenAI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ModuleBE_OpenAI.js","sourceRoot":"/Users/tacb0ss/dev/nu-art/beamz/_thunderstorm/ts-agents/openai/backend/src/main/","sources":["modules/ModuleBE_OpenAI.ts"],"names":[],"mappings":"AAAA,OAAO,E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AC,KAAK,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,SAAS,EAAE,SAAS,CAAC,SAAS,EAAC,CAAC,CAAC;IAC1H,CAAC,CAAC;IAEF,WAAW,GAAG,KAAK,EAAE,KAAsB,EAAE,EAAE;QAC9C,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;YAC5D,QAAQ,EAAE;gBACT;oBACC,IAAI,EAAE,QAAQ;oBACd,OAAO,EAAE,KAAK,CAAC,SAAS;iBACxB;gBACD;oBACC,IAAI,EAAE,MAAM;oBACZ,OAAO,EAAE,KAAK,CAAC,OAAO;iBACtB;aACD;YACD,KAAK,EAAE,KAAK,CAAC,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,YAAY,IAAI,eAAe;SACjE,CAAC,CAAC;QAEH,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC;QACtD,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QACzB,IAAI,CAAC,OAAO;YACX,MAAM,IAAI,4BAA4B,CAAC,4CAA4C,WAAW,CAAC,UAAU,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;QAErH,OAAO,EAAC,QAAQ,EAAE,OAAO,EAAC,CAAC;IAC5B,CAAC,CAAC;CACF;AAED,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,qBAAqB,EAAE,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Module } from '@nu-art/ts-common';
|
|
2
|
+
import { ChatCompletionDeveloperMessageParam, ChatCompletionSystemMessageParam, ChatCompletionUserMessageParam } from 'openai/resources';
|
|
3
|
+
type AgentInputMessage = ChatCompletionSystemMessageParam | ChatCompletionUserMessageParam | ChatCompletionDeveloperMessageParam;
|
|
4
|
+
type Config = {
|
|
5
|
+
openaiApiKey: string;
|
|
6
|
+
maxChatMessages: number;
|
|
7
|
+
};
|
|
8
|
+
type Prompt = {
|
|
9
|
+
messages: {
|
|
10
|
+
role: AgentInputMessage['role'];
|
|
11
|
+
content: string;
|
|
12
|
+
}[];
|
|
13
|
+
agent: string;
|
|
14
|
+
tools?: string[];
|
|
15
|
+
};
|
|
16
|
+
export type PromptResponse = {
|
|
17
|
+
message: string;
|
|
18
|
+
conversationId?: string;
|
|
19
|
+
toolsUsed?: {
|
|
20
|
+
toolName: string;
|
|
21
|
+
arguments: any;
|
|
22
|
+
result: any;
|
|
23
|
+
}[];
|
|
24
|
+
};
|
|
25
|
+
export declare class ModuleBE_OpenAIMCP_Class extends Module<Config> {
|
|
26
|
+
private client;
|
|
27
|
+
constructor();
|
|
28
|
+
protected init(): void;
|
|
29
|
+
chat: (prompts: Prompt[]) => Promise<PromptResponse>;
|
|
30
|
+
}
|
|
31
|
+
export declare const ModuleBE_OpenAIMCP: ModuleBE_OpenAIMCP_Class;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { ImplementationMissingException, lastElement, Module } from '@nu-art/ts-common';
|
|
2
|
+
import { OpenAI } from 'openai';
|
|
3
|
+
import { ModuleBE_AgentTools } from '@nu-art/ts-agent-tools-backend';
|
|
4
|
+
export class ModuleBE_OpenAIMCP_Class extends Module {
|
|
5
|
+
client;
|
|
6
|
+
constructor() {
|
|
7
|
+
super();
|
|
8
|
+
this.setDefaultConfig({ maxChatMessages: 10 });
|
|
9
|
+
}
|
|
10
|
+
init() {
|
|
11
|
+
if (!this.config.openaiApiKey)
|
|
12
|
+
throw new ImplementationMissingException('Missing \'openaiApiKey\' in config. Please provide a valid OpenAI API key.');
|
|
13
|
+
this.client = new OpenAI({ apiKey: this.config.openaiApiKey });
|
|
14
|
+
}
|
|
15
|
+
chat = async (prompts) => {
|
|
16
|
+
let messages = [];
|
|
17
|
+
for (const prompt of prompts) {
|
|
18
|
+
messages.push(...prompt.messages);
|
|
19
|
+
const toolsInput = ModuleBE_AgentTools.getTools(prompt.tools)?.map(tool => ({
|
|
20
|
+
type: 'function',
|
|
21
|
+
function: {
|
|
22
|
+
name: tool.name,
|
|
23
|
+
parameters: tool.inputSchema
|
|
24
|
+
}
|
|
25
|
+
}));
|
|
26
|
+
const request = {
|
|
27
|
+
model: prompt.agent,
|
|
28
|
+
messages,
|
|
29
|
+
tools: toolsInput?.length ? toolsInput : undefined,
|
|
30
|
+
};
|
|
31
|
+
this.logVerbose('Prompt: ', prompt);
|
|
32
|
+
const completion = await this.client.chat.completions.create(request);
|
|
33
|
+
const choice = completion.choices[0];
|
|
34
|
+
let assistantMessage = choice.message;
|
|
35
|
+
this.logVerbose('Response: ', assistantMessage);
|
|
36
|
+
messages.push(assistantMessage);
|
|
37
|
+
while (assistantMessage.tool_calls && messages.length < this.config.maxChatMessages) {
|
|
38
|
+
const toolCallsResponses = await Promise.all(assistantMessage.tool_calls.map(async (toolCall) => {
|
|
39
|
+
let toolName;
|
|
40
|
+
let args;
|
|
41
|
+
if (toolCall.type === 'function') {
|
|
42
|
+
toolName = toolCall.function.name;
|
|
43
|
+
args = JSON.parse(toolCall.function.arguments);
|
|
44
|
+
}
|
|
45
|
+
else
|
|
46
|
+
toolName = toolCall.custom.name;
|
|
47
|
+
const result = await ModuleBE_AgentTools.getTool(toolName)?.execute(args);
|
|
48
|
+
return {
|
|
49
|
+
id: toolCall.id,
|
|
50
|
+
toolName,
|
|
51
|
+
arguments: args,
|
|
52
|
+
result: { result }
|
|
53
|
+
};
|
|
54
|
+
}));
|
|
55
|
+
messages.push(...toolCallsResponses.map(result => ({
|
|
56
|
+
role: 'tool',
|
|
57
|
+
tool_call_id: result.id,
|
|
58
|
+
content: JSON.stringify(result.result)
|
|
59
|
+
})));
|
|
60
|
+
const finalCompletion = await this.client.chat.completions.create({
|
|
61
|
+
model: prompt.agent,
|
|
62
|
+
messages
|
|
63
|
+
});
|
|
64
|
+
const choice = finalCompletion.choices[0];
|
|
65
|
+
assistantMessage = choice.message;
|
|
66
|
+
}
|
|
67
|
+
messages.push(assistantMessage);
|
|
68
|
+
}
|
|
69
|
+
const message = lastElement(messages).content;
|
|
70
|
+
return { message };
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export const ModuleBE_OpenAIMCP = new ModuleBE_OpenAIMCP_Class();
|
|
74
|
+
//# sourceMappingURL=ModuleBE_OpenAIMCP.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ModuleBE_OpenAIMCP.js","sourceRoot":"/Users/tacb0ss/dev/nu-art/beamz/_thunderstorm/ts-agents/openai/backend/src/main/","sources":["modules/ModuleBE_OpenAIMCP.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,8BAA8B,EAAE,WAAW,EAAE,MAAM,EAAC,MAAM,mBAAmB,CAAC;AAStF,OAAO,EAAC,MAAM,EAAC,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AA8BrE,MAAM,OAAO,wBACZ,SAAQ,MAAc;IAEd,MAAM,CAAU;IAExB;QACC,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,gBAAgB,CAAC,EAAC,eAAe,EAAE,EAAE,EAAC,CAAC,CAAC;IAC9C,CAAC;IAES,IAAI;QACb,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY;YAC5B,MAAM,IAAI,8BAA8B,CAAC,4EAA4E,CAAC,CAAC;QAExH,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC,EAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,YAAY,EAAC,CAAC,CAAC;IAC9D,CAAC;IAEM,IAAI,GAAG,KAAK,EAAE,OAAiB,EAA2B,EAAE;QAClE,IAAI,QAAQ,GAAmB,EAAE,CAAC;QAClC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC9B,QAAQ,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;YAElC,MAAM,UAAU,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"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Module } from "@nu-art/ts-common";
|
|
2
|
+
import { OpenAI } from "openai";
|
|
3
|
+
import { OpenAIClient_Assistant_Class } from "./clients/OpenAIClient_Assistant.js";
|
|
4
|
+
import { OpenAIClient_Chat_Class } from "./clients/OpenAIClient_Chat.js";
|
|
5
|
+
import { GPT_Model } from '@nu-art/ts-openai-shared/types';
|
|
6
|
+
type Config = {
|
|
7
|
+
apiKey: string;
|
|
8
|
+
orgId: string;
|
|
9
|
+
openAIModel: GPT_Model;
|
|
10
|
+
};
|
|
11
|
+
declare class ModuleBE_OpenAIV2_Class extends Module<Config> {
|
|
12
|
+
private OpenAIClient;
|
|
13
|
+
constructor();
|
|
14
|
+
protected init(): void;
|
|
15
|
+
getAssistant: (assistantConfig: OpenAI.Beta.Assistants.AssistantCreateParams) => Promise<OpenAIClient_Assistant_Class>;
|
|
16
|
+
getChatClient: () => OpenAIClient_Chat_Class;
|
|
17
|
+
}
|
|
18
|
+
export declare const ModuleBE_OpenAIV2: ModuleBE_OpenAIV2_Class;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { LogLevel, Module, tsValidate, tsValidateString, tsValidateValue } from "@nu-art/ts-common";
|
|
2
|
+
import { OpenAI } from "openai";
|
|
3
|
+
import { OpenAIClient_Assistant_Class } from "./clients/OpenAIClient_Assistant.js";
|
|
4
|
+
import { OpenAIClient_Chat_Class } from "./clients/OpenAIClient_Chat.js";
|
|
5
|
+
import { GPT_Model } from '@nu-art/ts-openai-shared/types';
|
|
6
|
+
class ModuleBE_OpenAIV2_Class extends Module {
|
|
7
|
+
OpenAIClient;
|
|
8
|
+
constructor() {
|
|
9
|
+
super();
|
|
10
|
+
// set the config validator in the base module
|
|
11
|
+
this.setConfigValidator({
|
|
12
|
+
apiKey: tsValidateString(),
|
|
13
|
+
orgId: tsValidateString(),
|
|
14
|
+
openAIModel: tsValidateValue([...GPT_Model]),
|
|
15
|
+
minLogLevel: tsValidateValue([LogLevel.Debug, LogLevel.Info, LogLevel.Warning, LogLevel.Error])
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
init() {
|
|
19
|
+
super.init();
|
|
20
|
+
try {
|
|
21
|
+
// validate the config - The config is being set in the constructor so I can assume it exists
|
|
22
|
+
tsValidate(this.config, this.configValidator);
|
|
23
|
+
// create the basic OpenAI client
|
|
24
|
+
this.OpenAIClient = new OpenAI({
|
|
25
|
+
apiKey: this.config.apiKey,
|
|
26
|
+
organization: this.config.orgId
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
this.logError('Failed initializing modules', err);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
getAssistant = (assistantConfig) => {
|
|
34
|
+
return OpenAIClient_Assistant_Class.createInstance(this.OpenAIClient, assistantConfig);
|
|
35
|
+
};
|
|
36
|
+
getChatClient = () => {
|
|
37
|
+
return new OpenAIClient_Chat_Class(this.OpenAIClient, this.config.openAIModel ?? 'gpt-3.5-turbo');
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export const ModuleBE_OpenAIV2 = new ModuleBE_OpenAIV2_Class();
|
|
41
|
+
//# sourceMappingURL=ModuleBE_OpenAIV2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ModuleBE_OpenAIV2.js","sourceRoot":"/Users/tacb0ss/dev/nu-art/beamz/_thunderstorm/ts-agents/openai/backend/src/main/","sources":["modules/ModuleBE_OpenAIV2/ModuleBE_OpenAIV2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,gBAAgB,EAAE,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAClG,OAAO,EAAC,MAAM,EAAC,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAC,4BAA4B,EAAC,MAAM,qCAAqC,CAAC;AACjF,OAAO,EAAC,uBAAuB,EAAC,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e,CAAC,CAAA;IACrG,CAAC,CAAA;CAEJ;AAGD,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,uBAAuB,EAAE,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Module, UniqueId } from '@nu-art/ts-common';
|
|
2
|
+
import { OpenAI } from 'openai';
|
|
3
|
+
import { Uploadable } from 'openai/uploads';
|
|
4
|
+
export declare class OpenAIClient_Assistant_Class extends Module {
|
|
5
|
+
private client;
|
|
6
|
+
private readonly assistant;
|
|
7
|
+
static createInstance: (client: OpenAI, config: OpenAI.Beta.Assistants.AssistantCreateParams) => Promise<OpenAIClient_Assistant_Class>;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new assistant with the given parameters.
|
|
10
|
+
* @param client - The OpenAI client to use for the request.
|
|
11
|
+
* @param params - The parameters for the assistant creation.
|
|
12
|
+
* @private
|
|
13
|
+
*/
|
|
14
|
+
private static createAssistant;
|
|
15
|
+
constructor(client: OpenAI, assistant: OpenAI.Beta.Assistants.Assistant);
|
|
16
|
+
/**
|
|
17
|
+
* Uploads a file to the OpenAI API
|
|
18
|
+
* @param fileReadStream - The file to upload - this should be a readable stream
|
|
19
|
+
* @param filePurpose - The purpose of the file - this should be one of the OpenAI.Files.FilePurpose enums
|
|
20
|
+
*/
|
|
21
|
+
uploadFile: (fileReadStream: Uploadable, filePurpose: OpenAI.Files.FilePurpose) => import("openai").APIPromise<OpenAI.Files.FileObject>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new message for the assistant to handle.
|
|
24
|
+
* @param prompt
|
|
25
|
+
* @param attachments
|
|
26
|
+
*/
|
|
27
|
+
createMessage: (prompt: string, attachments: OpenAI.Beta.Threads.MessageCreateParams.Attachment[]) => Promise<{
|
|
28
|
+
threadId: string;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* Run the assistant on the thread to get a proper response to the added message
|
|
32
|
+
* @param threadId - The thread id to run the assistant on
|
|
33
|
+
*/
|
|
34
|
+
runAssistantOnThread: (threadId: UniqueId) => Promise<OpenAI.Beta.Threads.Messages.Message>;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new message for the assistant to handle and runs the assistant on the created thread.
|
|
37
|
+
* @param prompt - The prompt to send to the assistant.
|
|
38
|
+
* @param attachments - Attachments to include in the message.
|
|
39
|
+
* @returns The result of the assistant's response to the message.
|
|
40
|
+
*/
|
|
41
|
+
createMessageAndRun: (prompt: string, attachments: OpenAI.Beta.Threads.MessageCreateParams.Attachment[]) => Promise<OpenAI.Beta.Threads.Messages.Message>;
|
|
42
|
+
/**
|
|
43
|
+
* Utility methods for managing the assistant.
|
|
44
|
+
* - `getAssistant`: Returns a deep copy of the current assistant instance.
|
|
45
|
+
* - `deleteAssistant`: Deletes an assistant by its unique ID.
|
|
46
|
+
* - `updateAssistant`: Updates an assistant's configuration using the provided parameters.
|
|
47
|
+
*/
|
|
48
|
+
assistantUtils: {
|
|
49
|
+
getAssistant: () => OpenAI.Beta.Assistants.Assistant;
|
|
50
|
+
deleteAssistant: (assistantId: UniqueId) => Promise<OpenAI.Beta.Assistants.AssistantDeleted & {
|
|
51
|
+
_request_id?: string | null;
|
|
52
|
+
}>;
|
|
53
|
+
updateAssistant: (assistantId: UniqueId, params: OpenAI.Beta.Assistants.AssistantUpdateParams) => Promise<OpenAI.Beta.Assistants.Assistant & {
|
|
54
|
+
_request_id?: string | null;
|
|
55
|
+
}>;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* A set of utils to help with the message creation process.
|
|
59
|
+
* @method createThread - Creates a new thread for the assistant to handle.
|
|
60
|
+
* @method pollRunStatus - Polls the status of a run until it is completed.
|
|
61
|
+
* @method fetchRunResult - Fetches the latest message sent by the assistant in the thread.
|
|
62
|
+
* @private
|
|
63
|
+
*/
|
|
64
|
+
private messageUtils;
|
|
65
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { Module, Second } from '@nu-art/ts-common';
|
|
2
|
+
import { HttpCodes } from '@nu-art/ts-common/core/exceptions/http-codes';
|
|
3
|
+
export class OpenAIClient_Assistant_Class extends Module {
|
|
4
|
+
client;
|
|
5
|
+
assistant;
|
|
6
|
+
//###################### Static ######################
|
|
7
|
+
static createInstance = async (client, config) => {
|
|
8
|
+
const assistant = await this.createAssistant(client, config);
|
|
9
|
+
return new OpenAIClient_Assistant_Class(client, assistant);
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new assistant with the given parameters.
|
|
13
|
+
* @param client - The OpenAI client to use for the request.
|
|
14
|
+
* @param params - The parameters for the assistant creation.
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
static async createAssistant(client, params) {
|
|
18
|
+
return client.beta.assistants.create(params);
|
|
19
|
+
}
|
|
20
|
+
//###################### LifeCycle ######################
|
|
21
|
+
constructor(client, assistant) {
|
|
22
|
+
super();
|
|
23
|
+
this.client = client;
|
|
24
|
+
this.assistant = assistant;
|
|
25
|
+
}
|
|
26
|
+
//###################### Files ######################
|
|
27
|
+
/**
|
|
28
|
+
* Uploads a file to the OpenAI API
|
|
29
|
+
* @param fileReadStream - The file to upload - this should be a readable stream
|
|
30
|
+
* @param filePurpose - The purpose of the file - this should be one of the OpenAI.Files.FilePurpose enums
|
|
31
|
+
*/
|
|
32
|
+
uploadFile = (fileReadStream, filePurpose) => {
|
|
33
|
+
return this.client.files.create({
|
|
34
|
+
file: fileReadStream,
|
|
35
|
+
purpose: filePurpose
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
//###################### Messages & Run ######################
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new message for the assistant to handle.
|
|
41
|
+
* @param prompt
|
|
42
|
+
* @param attachments
|
|
43
|
+
*/
|
|
44
|
+
createMessage = async (prompt, attachments) => {
|
|
45
|
+
try {
|
|
46
|
+
const thread = await this.messageUtils.createThread();
|
|
47
|
+
// create the message in the thread
|
|
48
|
+
await this.client.beta.threads.messages.create(thread.id, {
|
|
49
|
+
role: 'user',
|
|
50
|
+
content: prompt,
|
|
51
|
+
attachments: attachments
|
|
52
|
+
});
|
|
53
|
+
// return the thread id to run the message
|
|
54
|
+
return { threadId: thread.id };
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
this.logError('Failed creating message', err);
|
|
58
|
+
throw HttpCodes._5XX.INTERNAL_SERVER_ERROR;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Run the assistant on the thread to get a proper response to the added message
|
|
63
|
+
* @param threadId - The thread id to run the assistant on
|
|
64
|
+
*/
|
|
65
|
+
runAssistantOnThread = async (threadId) => {
|
|
66
|
+
try {
|
|
67
|
+
// create the run
|
|
68
|
+
const run = await this.client.beta.threads.runs.create(threadId, {
|
|
69
|
+
assistant_id: this.assistant.id
|
|
70
|
+
});
|
|
71
|
+
// poll the status of the run until it is completed
|
|
72
|
+
await this.messageUtils.pollRunStatus(threadId, run.id);
|
|
73
|
+
// return the latest message sent by the assistant in the thread
|
|
74
|
+
return this.messageUtils.fetchRunResult(threadId);
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
this.logError('Failed running assistant on thread', err);
|
|
78
|
+
throw HttpCodes._5XX.INTERNAL_SERVER_ERROR;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Creates a new message for the assistant to handle and runs the assistant on the created thread.
|
|
83
|
+
* @param prompt - The prompt to send to the assistant.
|
|
84
|
+
* @param attachments - Attachments to include in the message.
|
|
85
|
+
* @returns The result of the assistant's response to the message.
|
|
86
|
+
*/
|
|
87
|
+
createMessageAndRun = async (prompt, attachments) => {
|
|
88
|
+
try {
|
|
89
|
+
// Create a new message
|
|
90
|
+
const { threadId } = await this.createMessage(prompt, attachments);
|
|
91
|
+
// Run the assistant on the created thread
|
|
92
|
+
return await this.runAssistantOnThread(threadId);
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
this.logError('Failed to create message and run assistant', err);
|
|
96
|
+
throw HttpCodes._5XX.INTERNAL_SERVER_ERROR;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
//###################### Public Utils ######################
|
|
100
|
+
/**
|
|
101
|
+
* Utility methods for managing the assistant.
|
|
102
|
+
* - `getAssistant`: Returns a deep copy of the current assistant instance.
|
|
103
|
+
* - `deleteAssistant`: Deletes an assistant by its unique ID.
|
|
104
|
+
* - `updateAssistant`: Updates an assistant's configuration using the provided parameters.
|
|
105
|
+
*/
|
|
106
|
+
assistantUtils = {
|
|
107
|
+
getAssistant: () => structuredClone(this.assistant),
|
|
108
|
+
deleteAssistant: async (assistantId) => this.client.beta.assistants.delete(assistantId),
|
|
109
|
+
updateAssistant: async (assistantId, params) => this.client.beta.assistants.update(assistantId, params)
|
|
110
|
+
};
|
|
111
|
+
//###################### Private Utils ######################
|
|
112
|
+
/**
|
|
113
|
+
* A set of utils to help with the message creation process.
|
|
114
|
+
* @method createThread - Creates a new thread for the assistant to handle.
|
|
115
|
+
* @method pollRunStatus - Polls the status of a run until it is completed.
|
|
116
|
+
* @method fetchRunResult - Fetches the latest message sent by the assistant in the thread.
|
|
117
|
+
* @private
|
|
118
|
+
*/
|
|
119
|
+
messageUtils = {
|
|
120
|
+
createThread: () => this.client.beta.threads.create(),
|
|
121
|
+
pollRunStatus: async (threadId, runId) => {
|
|
122
|
+
let runStatus;
|
|
123
|
+
do {
|
|
124
|
+
await new Promise(resolve => setTimeout(resolve, Second));
|
|
125
|
+
runStatus = await this.client.beta.threads.runs.retrieve(runId, { thread_id: threadId });
|
|
126
|
+
} while (runStatus.status !== 'completed');
|
|
127
|
+
},
|
|
128
|
+
fetchRunResult: async (threadId) => {
|
|
129
|
+
const messages = await this.client.beta.threads.messages.list(threadId);
|
|
130
|
+
return messages.data[0];
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=OpenAIClient_Assistant.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OpenAIClient_Assistant.js","sourceRoot":"/Users/tacb0ss/dev/nu-art/beamz/_thunderstorm/ts-agents/openai/backend/src/main/","sources":["modules/ModuleBE_OpenAIV2/clients/OpenAIClient_Assistant.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAE,MAAM,EAAW,MAAM,mBAAmB,CAAC;AAG3D,OAAO,EAAC,SAAS,EAAC,MAAM,8CAA8C,CAAC;AAGvE,MAAM,OAAO,4BACZ,SAAQ,MAAM;IAEN,MAAM,CAAS;IACN,SAAS,CAAwB;IAElD,sDAAsD;IAEtD,MAAM,CAAC,cAAc,GAAG,KAAK,EAAE,MAAc,EAAE,MAAoD,EAAyC,EAAE;QAC7I,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC7D,OAAO,IAAI,4BAA4B,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAC5D,CAAC,CAAC;IAEF;;;;;OAKG;IACK,MAAM,CAAC,KAAK,CAAC,eAAe,CAAC,MAAc,EAAE,MAAoD;QACxG,OAAO,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC9C,CAAC;IAED,yDAAyD;IAEzD,YAAY,MAAc,EAAE,SAA2C;QACtE,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC5B,CAAC;IAED,qDAAqD;IAErD;;;;OAIG;IACI,UAAU,GAAG,CAAC,cAA0B,EAAE,WAAqC,EAAE,EAAE;QACzF,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC;YAC/B,IAAI,EAAE,cAAc;YACpB,OAAO,EAAE,WAAW;SACpB,CAAC,CAAC;IACJ,CAAC,CAAC;IAEF,8DAA8D;IAE9D;;;;OAIG;IACI,aAAa,GAAG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EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;gBAC1D,SAAS,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAC,SAAS,EAAE,QAAQ,EAAC,CAAC,CAAC;YACxF,CAAC,QAAQ,SAAS,CAAC,MAAM,KAAK,WAAW,EAAE;QAC5C,CAAC;QACD,cAAc,EAAE,KAAK,EAAE,QAAkB,EAAE,EAAE;YAC5C,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACxE,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACzB,CAAC;KACD,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Module } from '@nu-art/ts-common';
|
|
2
|
+
import { GPT_Model } from '@nu-art/ts-openai-shared/types';
|
|
3
|
+
import { OpenAI } from 'openai';
|
|
4
|
+
type Request_Query = {
|
|
5
|
+
message: string;
|
|
6
|
+
model?: GPT_Model;
|
|
7
|
+
directive?: string;
|
|
8
|
+
};
|
|
9
|
+
export declare class OpenAIClient_Chat_Class extends Module {
|
|
10
|
+
private readonly client;
|
|
11
|
+
private readonly defaultModel;
|
|
12
|
+
constructor(client: OpenAI, defaultModel: GPT_Model);
|
|
13
|
+
query({ message, model, directive }: Request_Query): Promise<{
|
|
14
|
+
response: string;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { __stringify, BadImplementationException, Module, ThisShouldNotHappenException } from '@nu-art/ts-common';
|
|
2
|
+
export class OpenAIClient_Chat_Class extends Module {
|
|
3
|
+
client;
|
|
4
|
+
defaultModel;
|
|
5
|
+
constructor(client, defaultModel) {
|
|
6
|
+
super();
|
|
7
|
+
this.client = client;
|
|
8
|
+
this.defaultModel = defaultModel;
|
|
9
|
+
}
|
|
10
|
+
async query({ message, model, directive }) {
|
|
11
|
+
if (!message)
|
|
12
|
+
throw new BadImplementationException('Missing message for query');
|
|
13
|
+
const messages = [
|
|
14
|
+
...(directive ? [{ role: 'system', content: directive }] : []),
|
|
15
|
+
{ role: 'user', content: message }
|
|
16
|
+
];
|
|
17
|
+
const response = await this.client.chat.completions.create({
|
|
18
|
+
model: model ?? this.defaultModel,
|
|
19
|
+
messages: messages
|
|
20
|
+
});
|
|
21
|
+
const content = response.choices?.[0]?.message?.content;
|
|
22
|
+
if (!content)
|
|
23
|
+
throw new ThisShouldNotHappenException(`Empty GPT response: ${__stringify(response, true)}`);
|
|
24
|
+
return { response: content };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=OpenAIClient_Chat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OpenAIClient_Chat.js","sourceRoot":"/Users/tacb0ss/dev/nu-art/beamz/_thunderstorm/ts-agents/openai/backend/src/main/","sources":["modules/ModuleBE_OpenAIV2/clients/OpenAIClient_Chat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAE,0BAA0B,EAAE,MAAM,EAAE,4BAA4B,EAAC,MAAM,mBAAmB,CAAC;AAWhH,MAAM,OAAO,uBACZ,SAAQ,MAAM;IACG,MAAM,CAAS;IACf,YAAY,CAAY;IAEzC,YAAY,MAAc,EAAE,YAAuB;QAClD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IAClC,CAAC;IAEM,KAAK,CAAC,KAAK,CAAC,EAAC,OAAO,EAAE,KAAK,EAAE,SAAS,EAAgB;QAC5D,IAAI,CAAC,OAAO;YACX,MAAM,IAAI,0BAA0B,CAAC,2BAA2B,CAAC,CAAC;QAEnE,MAAM,QAAQ,GAAiC;YAC9C,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAAC,IAAI,EAAE,QAAiB,EAAE,OAAO,EAAE,SAAU,EAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YACtE,EAAC,IAAI,EAAE,MAAe,EAAE,OAAO,EAAE,OAAO,EAAC;SACzC,CAAC;QAEF,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;YAC1D,KAAK,EAAE,KAAK,IAAI,IAAI,CAAC,YAAY;YACjC,QAAQ,EAAE,QAAQ;SAClB,CAAC,CAAC;QAEH,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC;QACxD,IAAI,CAAC,OAAO;YACX,MAAM,IAAI,4BAA4B,CAAC,uBAAuB,WAAW,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;QAE9F,OAAO,EAAC,QAAQ,EAAE,OAAO,EAAC,CAAC;IAC5B,CAAC;CACD"}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nu-art/ts-openai-backend",
|
|
3
|
+
"version": "0.400.2",
|
|
4
|
+
"description": "ts-openai - Express & Typescript based backend framework",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"TacB0sS",
|
|
7
|
+
"infra",
|
|
8
|
+
"nu-art",
|
|
9
|
+
"thunderstorm",
|
|
10
|
+
"typescript",
|
|
11
|
+
"ts-openai"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/nu-art-js/thunderstorm",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/nu-art-js/thunderstorm/issues"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"directory": "dist",
|
|
19
|
+
"linkDirectory": true
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+ssh://git@github.com:nu-art-js/thunderstorm.git"
|
|
24
|
+
},
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"author": "TacB0sS",
|
|
27
|
+
"files": [
|
|
28
|
+
"**/*"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsc"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@nu-art/ts-common": "0.400.2",
|
|
35
|
+
"@nu-art/thunderstorm": "0.400.2",
|
|
36
|
+
"@nu-art/ts-agent-tools-backend": "0.400.2",
|
|
37
|
+
"@nu-art/ts-openai-shared": "0.400.2",
|
|
38
|
+
"openai": "^6.7.0",
|
|
39
|
+
"firebase": "^11.9.0",
|
|
40
|
+
"firebase-admin": "13.4.0",
|
|
41
|
+
"firebase-functions": "6.3.2",
|
|
42
|
+
"react": "^18.0.0",
|
|
43
|
+
"sinon": "^20.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/react": "^18.0.0",
|
|
47
|
+
"@types/chai": "^4.3.4",
|
|
48
|
+
"@types/mocha": "^10.0.1",
|
|
49
|
+
"@types/sinon": "^17.0.0"
|
|
50
|
+
},
|
|
51
|
+
"unitConfig": {
|
|
52
|
+
"type": "typescript-lib"
|
|
53
|
+
},
|
|
54
|
+
"type": "module",
|
|
55
|
+
"exports": {
|
|
56
|
+
".": {
|
|
57
|
+
"types": "./index.d.ts",
|
|
58
|
+
"import": "./index.js"
|
|
59
|
+
},
|
|
60
|
+
"./*": {
|
|
61
|
+
"types": "./*.d.ts",
|
|
62
|
+
"import": "./*.js"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|