Skip to content

Commit

Permalink
refactoring component to allow new custom roles
Browse files Browse the repository at this point in the history
  • Loading branch information
OvidijusParsiunas committed Nov 13, 2023
1 parent 88b69da commit 9bb5b26
Show file tree
Hide file tree
Showing 43 changed files with 236 additions and 227 deletions.
4 changes: 2 additions & 2 deletions component/src/services/assemblyAI/assemblyAIAudioIO.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {AssemblyAIResult} from '../../types/assemblyAIResult';
import {MessageContentI} from '../../types/messagesInternal';
import {Messages} from '../../views/chat/messages/messages';
import {DirectServiceIO} from '../utils/directServiceIO';
import {HTTPRequest} from '../../utils/HTTP/HTTPRequest';
import {AssemblyAIUtils} from './utils/assemblyAIUtils';
import {MessageContent} from '../../types/messages';
import {Response} from '../../types/response';
import {DeepChat} from '../../deepChat';

Expand Down Expand Up @@ -32,7 +32,7 @@ export class AssemblyAIAudioIO extends DirectServiceIO {
return !!files?.[0];
}

override async callServiceAPI(messages: Messages, _: MessageContent[], files?: File[]) {
override async callServiceAPI(messages: Messages, _: MessageContentI[], files?: File[]) {
if (!this.requestSettings?.headers) throw new Error('Request settings have not been set up');
if (!files?.[0]) throw new Error('No file was added');
HTTPRequest.request(this, files[0], messages, false);
Expand Down
4 changes: 2 additions & 2 deletions component/src/services/azure/azureSpeechToTextIO.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {AzureSpeechToTextResult} from '../../types/azureResult';
import {MessageContentI} from '../../types/messagesInternal';
import {Messages} from '../../views/chat/messages/messages';
import {HTTPRequest} from '../../utils/HTTP/HTTPRequest';
import {MessageContent} from '../../types/messages';
import {AzureUtils} from './utils/azureUtils';
import {AzureSpeechIO} from './azureSpeechIO';
import {Response} from '../../types/response';
Expand Down Expand Up @@ -39,7 +39,7 @@ export class AzureSpeechToTextIO extends AzureSpeechIO {
return !!files?.[0];
}

override async callServiceAPI(messages: Messages, _: MessageContent[], files?: File[]) {
override async callServiceAPI(messages: Messages, _: MessageContentI[], files?: File[]) {
if (!this.requestSettings?.headers) throw new Error('Request settings have not been set up');
if (!files?.[0]) throw new Error('No file was added');
if (this.requestSettings?.headers) {
Expand Down
6 changes: 3 additions & 3 deletions component/src/services/azure/azureSummarizationIO.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {AzureSummarizationResult, AzureAuthenticationError} from '../../types/azureResult';
import {Azure, AzureSummarizationConfig} from '../../types/azure';
import {MessageContentI} from '../../types/messagesInternal';
import {Messages} from '../../views/chat/messages/messages';
import {HTTPRequest} from '../../utils/HTTP/HTTPRequest';
import {MessageContent} from '../../types/messages';
import {AzureLanguageIO} from './azureLanguageIO';
import {GenericObject} from '../../types/object';
import {AzureUtils} from './utils/azureUtils';
Expand All @@ -26,7 +26,7 @@ export class AzureSummarizationIO extends AzureLanguageIO {
this.url = `${config.endpoint}/language/analyze-text/jobs?api-version=2022-10-01-preview`;
}

preprocessBody(body: RawBody, messages: MessageContent[]) {
preprocessBody(body: RawBody, messages: MessageContentI[]) {
const mostRecentMessageText = messages[messages.length - 1].text;
if (!mostRecentMessageText) return;
return {
Expand All @@ -47,7 +47,7 @@ export class AzureSummarizationIO extends AzureLanguageIO {
};
}

override async callServiceAPI(messages: Messages, pMessages: MessageContent[]) {
override async callServiceAPI(messages: Messages, pMessages: MessageContentI[]) {
if (!this.requestSettings) throw new Error('Request settings have not been set up');
const body = this.preprocessBody(this.rawBody, pMessages);
HTTPRequest.request(this, body as object, messages);
Expand Down
6 changes: 3 additions & 3 deletions component/src/services/azure/azureTextToSpeechIO.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Azure, AzureTextToSpeechConfig} from '../../types/azure';
import {AzureTextToSpeechResult} from '../../types/azureResult';
import {MessageContentI} from '../../types/messagesInternal';
import {Messages} from '../../views/chat/messages/messages';
import {HTTPRequest} from '../../utils/HTTP/HTTPRequest';
import {MessageContent} from '../../types/messages';
import {AzureUtils} from './utils/azureUtils';
import {AzureSpeechIO} from './azureSpeechIO';
import {Response} from '../../types/response';
Expand Down Expand Up @@ -35,7 +35,7 @@ export class AzureTextToSpeechIO extends AzureSpeechIO {
this.url = `https://${config.region}.tts.speech.microsoft.com/cognitiveservices/v1`;
}

preprocessBody(body: AzureTextToSpeechConfig, messages: MessageContent[]) {
preprocessBody(body: AzureTextToSpeechConfig, messages: MessageContentI[]) {
const mostRecentMessageText = messages[messages.length - 1].text;
if (!mostRecentMessageText) return;
return `<speak version='1.0' xml:lang='${body.lang}'>
Expand All @@ -45,7 +45,7 @@ export class AzureTextToSpeechIO extends AzureSpeechIO {
</speak>`;
}

override async callServiceAPI(messages: Messages, pMessages: MessageContent[]) {
override async callServiceAPI(messages: Messages, pMessages: MessageContentI[]) {
if (!this.requestSettings) throw new Error('Request settings have not been set up');
const body = this.preprocessBody(this.rawBody, pMessages);
HTTPRequest.request(this, body as unknown as object, messages, false);
Expand Down
6 changes: 3 additions & 3 deletions component/src/services/azure/azureTranslationIO.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {AzureTranslationResult} from '../../types/azureResult';
import {MessageContentI} from '../../types/messagesInternal';
import {Messages} from '../../views/chat/messages/messages';
import {DirectServiceIO} from '../utils/directServiceIO';
import {HTTPRequest} from '../../utils/HTTP/HTTPRequest';
import {MessageContent} from '../../types/messages';
import {AzureUtils} from './utils/azureUtils';
import {Response} from '../../types/response';
import {DeepChat} from '../../deepChat';
Expand All @@ -26,13 +26,13 @@ export class AzureTranslationIO extends DirectServiceIO {
this.url = `https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=${config.language || 'es'}`;
}

preprocessBody(messages: MessageContent[]) {
preprocessBody(messages: MessageContentI[]) {
const mostRecentMessageText = messages[messages.length - 1].text;
if (!mostRecentMessageText) return;
return [{Text: mostRecentMessageText}];
}

override async callServiceAPI(messages: Messages, pMessages: MessageContent[]) {
override async callServiceAPI(messages: Messages, pMessages: MessageContentI[]) {
if (!this.requestSettings) throw new Error('Request settings have not been set up');
const body = this.preprocessBody(pMessages);
HTTPRequest.request(this, body as unknown as object, messages);
Expand Down
6 changes: 3 additions & 3 deletions component/src/services/cohere/cohereChatIO.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {MessageContentI} from '../../types/messagesInternal';
import {Messages} from '../../views/chat/messages/messages';
import {Cohere, CohereChatConfig} from '../../types/cohere';
import {CohereChatResult} from '../../types/cohereResult';
import {HTTPRequest} from '../../utils/HTTP/HTTPRequest';
import {MessageContent} from '../../types/messages';
import {Response} from '../../types/response';
import {DeepChat} from '../../deepChat';
import {CohereIO} from './cohereIO';
Expand All @@ -27,7 +27,7 @@ export class CohereChatIO extends CohereIO {
delete config.user_name;
}

private preprocessBody(body: CohereChatConfig, pMessages: MessageContent[]) {
private preprocessBody(body: CohereChatConfig, pMessages: MessageContentI[]) {
const bodyCopy = JSON.parse(JSON.stringify(body));
const textMessages = pMessages.filter((message) => message.text);
bodyCopy.query = textMessages[textMessages.length - 1].text;
Expand All @@ -37,7 +37,7 @@ export class CohereChatIO extends CohereIO {
return bodyCopy;
}

override async callServiceAPI(messages: Messages, pMessages: MessageContent[]) {
override async callServiceAPI(messages: Messages, pMessages: MessageContentI[]) {
if (!this.requestSettings) throw new Error('Request settings have not been set up');
const body = this.preprocessBody(this.rawBody, pMessages);
HTTPRequest.request(this, body, messages);
Expand Down
6 changes: 3 additions & 3 deletions component/src/services/cohere/cohereSummarizationIO.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Cohere, CohereSummarizationConfig} from '../../types/cohere';
import {CohereSummarizationResult} from '../../types/cohereResult';
import {MessageContentI} from '../../types/messagesInternal';
import {Messages} from '../../views/chat/messages/messages';
import {HTTPRequest} from '../../utils/HTTP/HTTPRequest';
import {MessageContent} from '../../types/messages';
import {Response} from '../../types/response';
import {DeepChat} from '../../deepChat';
import {CohereIO} from './cohereIO';
Expand All @@ -14,14 +14,14 @@ export class CohereSummarizationIO extends CohereIO {
super(deepChat, 'https://api.cohere.ai/v1/summarize', 'Insert text to summarize', config, apiKey);
}

preprocessBody(body: CohereSummarizationConfig, messages: MessageContent[]) {
preprocessBody(body: CohereSummarizationConfig, messages: MessageContentI[]) {
const bodyCopy = JSON.parse(JSON.stringify(body));
const mostRecentMessageText = messages[messages.length - 1].text;
if (!mostRecentMessageText) return;
return {text: mostRecentMessageText, ...bodyCopy};
}

override async callServiceAPI(messages: Messages, pMessages: MessageContent[]) {
override async callServiceAPI(messages: Messages, pMessages: MessageContentI[]) {
if (!this.requestSettings) throw new Error('Request settings have not been set up');
const body = this.preprocessBody(this.rawBody, pMessages);
HTTPRequest.request(this, body, messages);
Expand Down
6 changes: 3 additions & 3 deletions component/src/services/cohere/cohereTextGenerationIO.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {CohereCompletionsResult} from '../../types/cohereResult';
import {Cohere, CohereGenerateConfig} from '../../types/cohere';
import {MessageContentI} from '../../types/messagesInternal';
import {Messages} from '../../views/chat/messages/messages';
import {HTTPRequest} from '../../utils/HTTP/HTTPRequest';
import {MessageContent} from '../../types/messages';
import {Response} from '../../types/response';
import {DeepChat} from '../../deepChat';
import {CohereIO} from './cohereIO';
Expand All @@ -15,14 +15,14 @@ export class CohereTextGenerationIO extends CohereIO {
super(deepChat, 'https://api.cohere.ai/v1/generate', 'Once upon a time', config, apiKey);
}

preprocessBody(body: CohereGenerateConfig, messages: MessageContent[]) {
preprocessBody(body: CohereGenerateConfig, messages: MessageContentI[]) {
const bodyCopy = JSON.parse(JSON.stringify(body));
const mostRecentMessageText = messages[messages.length - 1].text;
if (!mostRecentMessageText) return;
return {prompt: mostRecentMessageText, ...bodyCopy};
}

override async callServiceAPI(messages: Messages, pMessages: MessageContent[]) {
override async callServiceAPI(messages: Messages, pMessages: MessageContentI[]) {
if (!this.requestSettings) throw new Error('Request settings have not been set up');
const body = this.preprocessBody(this.rawBody, pMessages);
HTTPRequest.request(this, body, messages);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {HuggingFaceConversationResult} from '../../types/huggingFaceResult';
import {HuggingFaceQuestionAnswerConfig} from '../../types/huggingFace';
import {MessageContent} from '../../types/messages';
import {MessageContentI} from '../../types/messagesInternal';
import {HuggingFaceIO} from './huggingFaceIO';
import {Response} from '../../types/response';
import {DeepChat} from '../../deepChat';
Expand All @@ -15,7 +15,7 @@ export class HuggingFaceConversationIO extends HuggingFaceIO {
}

// prettier-ignore
private processMessages(messages: MessageContent[]) {
private processMessages(messages: MessageContentI[]) {
const textMessages = messages.filter((message) => message.text);
const mostRecentMessageText = textMessages[textMessages.length - 1].text;
const previousMessages = textMessages.slice(0, textMessages.length - 1);
Expand All @@ -28,7 +28,7 @@ export class HuggingFaceConversationIO extends HuggingFaceIO {
}

// prettier-ignore
override preprocessBody(body: HuggingFaceQuestionAnswerConfig, messages: MessageContent[]) {
override preprocessBody(body: HuggingFaceQuestionAnswerConfig, messages: MessageContentI[]) {
const bodyCopy = JSON.parse(JSON.stringify(body)) as HuggingFaceQuestionAnswerConfig & {
options?: {wait_for_model?: boolean};
};
Expand Down
6 changes: 3 additions & 3 deletions component/src/services/huggingFace/huggingFaceFileIO.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {MessageContentI} from '../../types/messagesInternal';
import {Messages} from '../../views/chat/messages/messages';
import {HTTPRequest} from '../../utils/HTTP/HTTPRequest';
import {HuggingFaceModel} from '../../types/huggingFace';
import {MessageContent} from '../../types/messages';
import {ServiceFileTypes} from '../serviceIO';
import {HuggingFaceIO} from './huggingFaceIO';
import {APIKey} from '../../types/APIKey';
Expand All @@ -22,12 +22,12 @@ export class HuggingFaceFileIO extends HuggingFaceIO {
return !!files?.[0];
}

override preprocessBody(_: {}, __: MessageContent[], files: File[]) {
override preprocessBody(_: {}, __: MessageContentI[], files: File[]) {
return files[0] as unknown as {inputs: string};
}

// prettier-ignore
override async callServiceAPI(messages: Messages, _: MessageContent[], files?: File[]) {
override async callServiceAPI(messages: Messages, _: MessageContentI[], files?: File[]) {
if (!this.requestSettings) throw new Error('Request settings have not been set up');
if (!files?.[0]) throw new Error('No file was added');
HTTPRequest.poll(this, files[0], messages, false);
Expand Down
6 changes: 3 additions & 3 deletions component/src/services/huggingFace/huggingFaceIO.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {MessageContentI} from '../../types/messagesInternal';
import {Messages} from '../../views/chat/messages/messages';
import {HuggingFaceUtils} from './utils/huggingFaceUtils';
import {DirectServiceIO} from '../utils/directServiceIO';
import {HuggingFaceModel} from '../../types/huggingFace';
import {HTTPRequest} from '../../utils/HTTP/HTTPRequest';
import {MessageContent} from '../../types/messages';
import {ServiceFileTypes} from '../serviceIO';
import {APIKey} from '../../types/APIKey';
import {DeepChat} from '../../deepChat';
Expand Down Expand Up @@ -39,7 +39,7 @@ export class HuggingFaceIO extends DirectServiceIO {
}

// prettier-ignore
preprocessBody(body: HuggingFaceServiceConfigObj, messages: MessageContent[], _?: File[]) {
preprocessBody(body: HuggingFaceServiceConfigObj, messages: MessageContentI[], _?: File[]) {
const bodyCopy = JSON.parse(JSON.stringify(body)) as (HuggingFaceServiceConfigObj
& {options?: {wait_for_model?: boolean}});
const mostRecentMessageText = messages[messages.length - 1].text;
Expand All @@ -49,7 +49,7 @@ export class HuggingFaceIO extends DirectServiceIO {
return {inputs: mostRecentMessageText, ...bodyCopy};
}

override async callServiceAPI(messages: Messages, pMessages: MessageContent[], files?: File[]) {
override async callServiceAPI(messages: Messages, pMessages: MessageContentI[], files?: File[]) {
if (!this.requestSettings) throw new Error('Request settings have not been set up');
const body = this.preprocessBody(this.rawBody, pMessages, files) as object;
HTTPRequest.request(this, body, messages);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {HuggingFace, HuggingFaceQuestionAnswerConfig} from '../../types/huggingFace';
import {HuggingFaceQuestionAnswerResult} from '../../types/huggingFaceResult';
import {MessageContent} from '../../types/messages';
import {MessageContentI} from '../../types/messagesInternal';
import {HuggingFaceIO} from './huggingFaceIO';
import {Response} from '../../types/response';
import {DeepChat} from '../../deepChat';
Expand All @@ -17,7 +17,7 @@ export class HuggingFaceQuestionAnswerIO extends HuggingFaceIO {
this.context = config.context;
}

override preprocessBody(_: HuggingFaceQuestionAnswerConfig, messages: MessageContent[]) {
override preprocessBody(_: HuggingFaceQuestionAnswerConfig, messages: MessageContentI[]) {
const mostRecentMessageText = messages[messages.length - 1].text;
if (!mostRecentMessageText) return;
return {
Expand Down
22 changes: 11 additions & 11 deletions component/src/services/openAI/openAIAssistantIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {OpenAIConverseBodyInternal} from '../../types/openAIInternal';
import {MessageUtils} from '../../views/chat/messages/messageUtils';
import {DirectConnection} from '../../types/directConnection';
import {MessageLimitUtils} from '../utils/messageLimitUtils';
import {MessageContentI} from '../../types/messagesInternal';
import {Messages} from '../../views/chat/messages/messages';
import {HTTPRequest} from '../../utils/HTTP/HTTPRequest';
import {DirectServiceIO} from '../utils/directServiceIO';
import {MessageContent} from '../../types/messages';
import {OpenAIUtils} from './utils/openAIUtils';
import {DeepChat} from '../../deepChat';
import {PollResult} from '../serviceIO';
Expand Down Expand Up @@ -44,21 +44,21 @@ export class OpenAIAssistantIO extends DirectServiceIO {
this.maxMessages = 1; // messages are stored in OpenAI threads and can't create new thread with 'assistant' messages
}

private processMessages(pMessages: MessageContent[], file_ids?: string[]) {
private processMessages(pMessages: MessageContentI[], file_ids?: string[]) {
const totalMessagesMaxCharLength = this.totalMessagesMaxCharLength || -1;
return MessageLimitUtils.getCharacterLimitMessages(pMessages, totalMessagesMaxCharLength).map((message) => {
return {content: message.text || '', role: message.role === MessageUtils.AI_ROLE ? 'assistant' : 'user', file_ids};
return {content: message.text || '', role: message.role === MessageUtils.USER_ROLE ? 'user' : 'assistant', file_ids};
});
}

private createNewThreadMessages(body: OpenAIConverseBodyInternal, pMessages: MessageContent[], file_ids?: string[]) {
private createNewThreadMessages(body: OpenAIConverseBodyInternal, pMessages: MessageContentI[], file_ids?: string[]) {
const bodyCopy = JSON.parse(JSON.stringify(body));
const processedMessages = this.processMessages(pMessages, file_ids);
bodyCopy.thread = {messages: processedMessages};
return bodyCopy;
}

private callService(messages: Messages, pMessages: MessageContent[], file_ids?: string[]) {
private callService(messages: Messages, pMessages: MessageContentI[], file_ids?: string[]) {
if (this.sessionId) {
// https://platform.openai.com/docs/api-reference/messages/createMessage
this.url = `${OpenAIAssistantIO.THREAD_PREFIX}/${this.sessionId}/messages`;
Expand All @@ -73,7 +73,7 @@ export class OpenAIAssistantIO extends DirectServiceIO {
this.messages = messages;
}

override async callServiceAPI(messages: Messages, pMessages: MessageContent[], files?: File[]) {
override async callServiceAPI(messages: Messages, pMessages: MessageContentI[], files?: File[]) {
if (!this.requestSettings) throw new Error('Request settings have not been set up');
// here instead of constructor as messages may be loaded later
if (!this.searchedForThreadId) this.searchPreviousMessagesForThreadId(messages.messages);
Expand All @@ -82,9 +82,9 @@ export class OpenAIAssistantIO extends DirectServiceIO {
this.callService(messages, pMessages, file_ids);
}

private searchPreviousMessagesForThreadId(messages: MessageContent[]) {
const messageWithSession = messages.find((message) => message.sessionId);
if (messageWithSession) this.sessionId = messageWithSession.sessionId;
private searchPreviousMessagesForThreadId(messages: MessageContentI[]) {
const messageWithSession = messages.find((message) => message._sessionId);
if (messageWithSession) this.sessionId = messageWithSession._sessionId;
this.searchedForThreadId = true;
}

Expand All @@ -108,7 +108,7 @@ export class OpenAIAssistantIO extends DirectServiceIO {
this.sessionId = result.thread_id;
this.run_id = result.id;
// updates the user sent message with the session id (the message event sent did not have this id)
if (this.messages) this.messages.messages[this.messages.messages.length - 1].sessionId = this.sessionId;
if (this.messages) this.messages.messages[this.messages.messages.length - 1]._sessionId = this.sessionId;
}
}

Expand All @@ -119,7 +119,7 @@ export class OpenAIAssistantIO extends DirectServiceIO {
this.url = `${OpenAIAssistantIO.THREAD_PREFIX}/${result.thread_id}/messages`;
const threadMessages = (await OpenAIUtils.directFetch(this, {}, 'GET')) as OpenAIAssistantMessagesResult;
const lastMessage = threadMessages.data[0];
return {text: lastMessage.content[0].text.value, sessionId: this.sessionId};
return {text: lastMessage.content[0].text.value, _sessionId: this.sessionId};
}
const toolCalls = required_action?.submit_tool_outputs?.tool_calls;
if (status === 'requires_action' && toolCalls) return await this.handleTools(toolCalls);
Expand Down
Loading

0 comments on commit 9bb5b26

Please sign in to comment.