@smythos/sre 1.5.60 → 1.5.63

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.
@@ -29,6 +29,9 @@ import { SystemEvents } from '@sre/Core/SystemEvents';
29
29
  import { ConnectorService } from '@sre/Core/ConnectorsService';
30
30
  import { HandlerDependencies, TToolType } from './types';
31
31
  import { OpenAIApiInterfaceFactory, OpenAIApiInterface } from './apiInterfaces';
32
+ import { Logger } from '@sre/helpers/Log.helper';
33
+
34
+ const logger = Logger('OpenAIConnector');
32
35
 
33
36
  export class OpenAIConnector extends LLMConnector {
34
37
  public name = 'LLM:OpenAI';
@@ -80,86 +83,98 @@ export class OpenAIConnector extends LLMConnector {
80
83
  }
81
84
 
82
85
  protected async request({ acRequest, body, context }: ILLMRequestFuncParams): Promise<TLLMChatResponse> {
83
- const _body = body as OpenAI.ChatCompletionCreateParams;
86
+ try {
87
+ logger.debug(`request ${this.name}`, acRequest.candidate);
88
+ const _body = body as OpenAI.ChatCompletionCreateParams;
89
+
90
+ // #region Validate token limit
91
+ const messages = _body?.messages || [];
92
+ const lastMessage = messages[messages.length - 1];
93
+ const promptTokens = await this.computePromptTokens(messages, context);
94
+
95
+ await this.validateTokenLimit({
96
+ acRequest,
97
+ promptTokens,
98
+ context,
99
+ maxTokens: _body.max_completion_tokens,
100
+ });
101
+ // #endregion Validate token limit
84
102
 
85
- // #region Validate token limit
86
- const messages = _body?.messages || [];
87
- const lastMessage = messages[messages.length - 1];
88
- const promptTokens = await this.computePromptTokens(messages, context);
103
+ const responseInterface = this.getInterfaceType(context);
104
+ const apiInterface = this.getApiInterface(responseInterface, context);
89
105
 
90
- await this.validateTokenLimit({
91
- acRequest,
92
- promptTokens,
93
- context,
94
- maxTokens: _body.max_completion_tokens,
95
- });
96
- // #endregion Validate token limit
106
+ const result = await apiInterface.createRequest(body, context);
97
107
 
98
- const responseInterface = this.getInterfaceType(context);
99
- const apiInterface = this.getApiInterface(responseInterface, context);
108
+ const message = result?.choices?.[0]?.message || { content: result?.output_text };
109
+ const finishReason = result?.choices?.[0]?.finish_reason || result?.incomplete_details || 'stop';
100
110
 
101
- const result = await apiInterface.createRequest(body, context);
111
+ let toolsData: ToolData[] = [];
112
+ let useTool = false;
102
113
 
103
- const message = result?.choices?.[0]?.message;
104
- const finishReason = result?.choices?.[0]?.finish_reason;
114
+ if (finishReason === 'tool_calls') {
115
+ toolsData =
116
+ message?.tool_calls?.map((tool, index) => ({
117
+ index,
118
+ id: tool?.id,
119
+ type: tool?.type,
120
+ name: tool?.function?.name,
121
+ arguments: tool?.function?.arguments,
122
+ role: 'tool',
123
+ })) || [];
105
124
 
106
- let toolsData: ToolData[] = [];
107
- let useTool = false;
125
+ useTool = true;
126
+ }
108
127
 
109
- if (finishReason === 'tool_calls') {
110
- toolsData =
111
- message?.tool_calls?.map((tool, index) => ({
112
- index,
113
- id: tool?.id,
114
- type: tool?.type,
115
- name: tool?.function?.name,
116
- arguments: tool?.function?.arguments,
117
- role: 'tool',
118
- })) || [];
128
+ const usage = result?.usage;
129
+ this.reportUsage(usage, {
130
+ modelEntryName: context.modelEntryName,
131
+ keySource: context.isUserKey ? APIKeySource.User : APIKeySource.Smyth,
132
+ agentId: context.agentId,
133
+ teamId: context.teamId,
134
+ });
119
135
 
120
- useTool = true;
136
+ return {
137
+ content: message?.content ?? '',
138
+ finishReason,
139
+ useTool,
140
+ toolsData,
141
+ message,
142
+ usage,
143
+ };
144
+ } catch (error) {
145
+ logger.error(`request ${this.name}`, error, acRequest.candidate);
146
+ throw error;
121
147
  }
122
-
123
- const usage = result?.usage;
124
- this.reportUsage(usage, {
125
- modelEntryName: context.modelEntryName,
126
- keySource: context.isUserKey ? APIKeySource.User : APIKeySource.Smyth,
127
- agentId: context.agentId,
128
- teamId: context.teamId,
129
- });
130
-
131
- return {
132
- content: message?.content ?? '',
133
- finishReason,
134
- useTool,
135
- toolsData,
136
- message,
137
- usage,
138
- };
139
148
  }
140
149
 
141
150
  protected async streamRequest({ acRequest, body, context }: ILLMRequestFuncParams): Promise<EventEmitter> {
142
- // #region Validate token limit
143
- const messages = body?.messages || body?.input || [];
144
- const lastMessage = messages[messages.length - 1];
145
- const promptTokens = await this.computePromptTokens(messages, context);
146
-
147
- await this.validateTokenLimit({
148
- acRequest,
149
- promptTokens,
150
- context,
151
- maxTokens: body.max_completion_tokens,
152
- });
153
- // #endregion Validate token limit
151
+ try {
152
+ logger.debug(`streamRequest ${this.name}`, acRequest.candidate);
153
+ // #region Validate token limit
154
+ const messages = body?.messages || body?.input || [];
155
+ const lastMessage = messages[messages.length - 1];
156
+ const promptTokens = await this.computePromptTokens(messages, context);
157
+
158
+ await this.validateTokenLimit({
159
+ acRequest,
160
+ promptTokens,
161
+ context,
162
+ maxTokens: body.max_completion_tokens,
163
+ });
164
+ // #endregion Validate token limit
154
165
 
155
- const responseInterface = this.getInterfaceType(context);
156
- const apiInterface = this.getApiInterface(responseInterface, context);
166
+ const responseInterface = this.getInterfaceType(context);
167
+ const apiInterface = this.getApiInterface(responseInterface, context);
157
168
 
158
- const stream = await apiInterface.createStream(body, context);
169
+ const stream = await apiInterface.createStream(body, context);
159
170
 
160
- const emitter = apiInterface.handleStream(stream, context);
171
+ const emitter = apiInterface.handleStream(stream, context);
161
172
 
162
- return emitter;
173
+ return emitter;
174
+ } catch (error) {
175
+ logger.error(`streamRequest ${this.name}`, error, acRequest.candidate);
176
+ throw error;
177
+ }
163
178
  }
164
179
 
165
180
  // #region Image Generation, will be moved to a different subsystem
@@ -19,6 +19,9 @@ import { LLMHelper } from '@sre/LLMManager/LLM.helper';
19
19
 
20
20
  import { LLMConnector } from '../LLMConnector';
21
21
  import { SystemEvents } from '@sre/Core/SystemEvents';
22
+ import { Logger } from '@sre/helpers/Log.helper';
23
+
24
+ const logger = Logger('xAIConnector');
22
25
 
23
26
  type ChatCompletionParams = {
24
27
  model: string;
@@ -96,6 +99,7 @@ export class xAIConnector extends LLMConnector {
96
99
 
97
100
  protected async request({ acRequest, body, context }: ILLMRequestFuncParams): Promise<TLLMChatResponse> {
98
101
  try {
102
+ logger.debug(`request ${this.name}`, acRequest.candidate);
99
103
  const grok = await this.getClient(context);
100
104
  const response = await grok.post('/chat/completions', body);
101
105
 
@@ -144,6 +148,7 @@ export class xAIConnector extends LLMConnector {
144
148
  usage,
145
149
  };
146
150
  } catch (error) {
151
+ logger.error(`request ${this.name}`, error, acRequest.candidate);
147
152
  throw error;
148
153
  }
149
154
  }
@@ -152,6 +157,7 @@ export class xAIConnector extends LLMConnector {
152
157
  const emitter = new EventEmitter();
153
158
 
154
159
  try {
160
+ logger.debug(`streamRequest ${this.name}`, acRequest.candidate);
155
161
  const grok = await this.getClient(context);
156
162
  const response = await grok.post(
157
163
  '/chat/completions',
@@ -260,6 +266,7 @@ export class xAIConnector extends LLMConnector {
260
266
  emitter.emit('error', error);
261
267
  });
262
268
  } catch (error) {
269
+ logger.error(`streamRequest ${this.name}`, error, acRequest.candidate);
263
270
  emitter.emit('error', error);
264
271
  }
265
272