@crewdle/mist-connector-openai 1.0.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/README.md ADDED
@@ -0,0 +1 @@
1
+ # Crewdle Mist Open AI Connector
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { OpenAIGenerativeAIWorkerConnector } from './models/OpenAIGenerativeAIWorkerConnector';
@@ -0,0 +1,95 @@
1
+ import OpenAI from 'openai';
2
+ export class OpenAIGenerativeAIWorkerConnector {
3
+ models = new Map();
4
+ apiKey;
5
+ client;
6
+ constructor(apiKey) {
7
+ this.apiKey = apiKey;
8
+ }
9
+ async initialize(_, models) {
10
+ this.client = new OpenAI({
11
+ apiKey: this.apiKey,
12
+ });
13
+ for (const [modelId, model] of models) {
14
+ if (model.engineType !== 'openai') {
15
+ continue;
16
+ }
17
+ this.models.set(modelId, model);
18
+ }
19
+ }
20
+ async close() {
21
+ this.client = undefined;
22
+ this.models.clear();
23
+ }
24
+ getEngineType() {
25
+ return 'openai';
26
+ }
27
+ async processJob(parameters, options) {
28
+ if (!this.client) {
29
+ throw new Error('Client not initialized');
30
+ }
31
+ if (!options || !this.models.has(options.model.id)) {
32
+ throw new Error('Model not initialized');
33
+ }
34
+ const response = await this.client.chat.completions.create({
35
+ model: options.model.id,
36
+ messages: this.getMessages(parameters),
37
+ max_completion_tokens: parameters.maxTokens,
38
+ temperature: parameters.temperature,
39
+ });
40
+ return {
41
+ type: 'prompt',
42
+ output: response.choices[0].message.content ?? '',
43
+ inputTokens: response.usage?.prompt_tokens ?? 0,
44
+ outputTokens: response.usage?.completion_tokens ?? 0,
45
+ };
46
+ }
47
+ async *processJobStream(parameters, options) {
48
+ if (!this.client) {
49
+ throw new Error('Client not initialized');
50
+ }
51
+ if (!options || !this.models.has(options.model.id)) {
52
+ throw new Error('Model not initialized');
53
+ }
54
+ const stream = await this.client.chat.completions.create({
55
+ model: options.model.id,
56
+ messages: this.getMessages(parameters),
57
+ max_completion_tokens: parameters.maxTokens,
58
+ temperature: parameters.temperature,
59
+ stream: true,
60
+ stream_options: {
61
+ include_usage: true,
62
+ },
63
+ });
64
+ for await (const chunk of stream) {
65
+ yield {
66
+ type: 'prompt',
67
+ output: chunk.choices[0].delta.content ?? '',
68
+ inputTokens: chunk.usage?.prompt_tokens ?? 0,
69
+ outputTokens: chunk.usage?.completion_tokens ?? 0,
70
+ };
71
+ }
72
+ }
73
+ getMessages(parameters) {
74
+ const messages = [];
75
+ if (parameters.instructions) {
76
+ messages.push({
77
+ role: 'system',
78
+ content: parameters.instructions,
79
+ });
80
+ }
81
+ if (parameters.history) {
82
+ for (const message of parameters.history) {
83
+ messages.push({
84
+ role: message.source === 'human' ? 'user' : 'assistant',
85
+ content: message.message,
86
+ });
87
+ }
88
+ }
89
+ messages.push({
90
+ role: 'user',
91
+ content: parameters.prompt,
92
+ });
93
+ return messages;
94
+ }
95
+ }
@@ -0,0 +1 @@
1
+ export { OpenAIGenerativeAIWorkerConnector } from './models/OpenAIGenerativeAIWorkerConnector';
@@ -0,0 +1,13 @@
1
+ import { GenerativeAIEngineType, GenerativeAIWorkerConnectorParameters, IGenerativeAIModel, IGenerativeAIWorkerConnector, IGenerativeAIWorkerConnectorPromptResult, IGenerativeAIWorkerOptions } from '@crewdle/web-sdk-types';
2
+ export declare class OpenAIGenerativeAIWorkerConnector implements IGenerativeAIWorkerConnector {
3
+ private models;
4
+ private apiKey;
5
+ private client?;
6
+ constructor(apiKey: string);
7
+ initialize(_: string, models: Map<string, IGenerativeAIModel>): Promise<void>;
8
+ close(): Promise<void>;
9
+ getEngineType(): GenerativeAIEngineType;
10
+ processJob(parameters: GenerativeAIWorkerConnectorParameters, options?: IGenerativeAIWorkerOptions): Promise<IGenerativeAIWorkerConnectorPromptResult>;
11
+ processJobStream(parameters: GenerativeAIWorkerConnectorParameters, options?: IGenerativeAIWorkerOptions): AsyncGenerator<IGenerativeAIWorkerConnectorPromptResult>;
12
+ private getMessages;
13
+ }
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@crewdle/mist-connector-openai",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/types/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "copy-sdk-types": "cp -r ../web-sdk-types/dist ./node_modules/@crewdle/web-sdk-types"
10
+ },
11
+ "keywords": [],
12
+ "author": "",
13
+ "license": "ISC",
14
+ "files": [
15
+ "dist/"
16
+ ],
17
+ "devDependencies": {
18
+ "@crewdle/web-sdk-types": "^1.0.33",
19
+ "@types/node": "^22.13.9",
20
+ "typescript": "^5.8.2"
21
+ },
22
+ "dependencies": {
23
+ "openai": "^4.86.1"
24
+ }
25
+ }