From 37d9dfaed53ff227f41b52d9daca40264fdd9a03 Mon Sep 17 00:00:00 2001 From: Brandon Ruffridge Date: Fri, 1 Nov 2024 12:51:40 -0400 Subject: [PATCH 1/2] Fixed an issue where listMessagesPostfix was generating an invalid url with two question marks `messages??api-version`. Fixed an issue where Azure API Headers were not being used (OpenAI headers were instead). --- component/src/services/azure/azureOpenAIAssistantIO.ts | 6 +++--- .../src/services/openAI/assistant/openAIAssistantIO.ts | 3 ++- .../src/services/openAI/assistant/openAIAssistantIOI.ts | 7 ++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/component/src/services/azure/azureOpenAIAssistantIO.ts b/component/src/services/azure/azureOpenAIAssistantIO.ts index 5d0413f71..6d9742045 100644 --- a/component/src/services/azure/azureOpenAIAssistantIO.ts +++ b/component/src/services/azure/azureOpenAIAssistantIO.ts @@ -21,14 +21,14 @@ export class AzureOpenAIAssistantIO extends OpenAIAssistantIOI { threadsPrefix: `${commonPrefix}${AzureOpenAIAssistantIO.THREAD_RESOURCE}`, threadsPosfix: commonPostfix, newAssistantUrl: `${commonPrefix}${AzureOpenAIAssistantIO.NEW_ASSISTANT_RESOURCE}${commonPostfix}`, - createMessagePostfix: `?order=desc&api-version=${config?.urlDetails?.version}`, - listMessagesPostfix: commonPostfix, + createMessagePostfix: commonPostfix, + listMessagesPostfix: `order=desc&api-version=${config?.urlDetails?.version}`, storeFiles: `${commonPrefix}files${commonPostfix}`, getFilesPrefix: `${commonPrefix}files/`, getFilesPostfix: `/content${commonPostfix}`, }; - super(deepChat, config?.assistant, urlSegments, apiKey); + super(deepChat, config?.assistant, urlSegments, AzureOpenAIUtils.buildHeaders, apiKey); if (!AzureOpenAIUtils.validateURLDetails(urlDetails)) { this.isTextInputDisabled = true; diff --git a/component/src/services/openAI/assistant/openAIAssistantIO.ts b/component/src/services/openAI/assistant/openAIAssistantIO.ts index 6e842a9b1..c11f80efc 100644 --- a/component/src/services/openAI/assistant/openAIAssistantIO.ts +++ b/component/src/services/openAI/assistant/openAIAssistantIO.ts @@ -2,6 +2,7 @@ import {DirectConnection} from '../../../types/directConnection'; import {Response as ResponseI} from '../../../types/response'; import {OpenAIAssistantIOI} from './openAIAssistantIOI'; import {DeepChat} from '../../../deepChat'; +import {OpenAIUtils} from '../utils/openAIUtils'; export class OpenAIAssistantIO extends OpenAIAssistantIOI { fetchHistory?: () => Promise; @@ -20,7 +21,7 @@ export class OpenAIAssistantIO extends OpenAIAssistantIOI { const directConnectionCopy = JSON.parse(JSON.stringify(deepChat.directConnection)) as DirectConnection; const apiKey = directConnectionCopy.openAI; const config = directConnectionCopy.openAI?.assistant; - super(deepChat, config, OpenAIAssistantIO.URL_SEGMENTS, apiKey); + super(deepChat, config, OpenAIAssistantIO.URL_SEGMENTS, OpenAIUtils.buildHeaders, apiKey); this.connectSettings.headers ??= {}; this.connectSettings.headers['OpenAI-Beta'] ??= 'assistants=v2'; // runs keep failing but keep trying if (this.shouldFetchHistory && this.sessionId) this.fetchHistory = this.fetchHistoryFunc.bind(this); diff --git a/component/src/services/openAI/assistant/openAIAssistantIOI.ts b/component/src/services/openAI/assistant/openAIAssistantIOI.ts index 730d7b448..4bbbe8a38 100644 --- a/component/src/services/openAI/assistant/openAIAssistantIOI.ts +++ b/component/src/services/openAI/assistant/openAIAssistantIOI.ts @@ -22,6 +22,7 @@ import { ToolCalls, } from '../../../types/openAIResult'; import {APIKey} from '../../../types/APIKey'; +import {BuildHeadersFunc} from '../../../types/headers'; // https://platform.openai.com/docs/api-reference/messages/createMessage type MessageContentArr = { @@ -66,9 +67,9 @@ export class OpenAIAssistantIOI extends DirectServiceIO { private readonly urlSegments: URLSegments; private messageStream: MessageStream | undefined; private readonly filesToolType: OpenAIAssistant['files_tool_type']; - - constructor(deepChat: DeepChat, config: OpenAI['assistant'], urlSegments: URLSegments, apiKey?: APIKey) { - super(deepChat, OpenAIUtils.buildKeyVerificationDetails(), OpenAIUtils.buildHeaders, apiKey); + + constructor(deepChat: DeepChat, config: OpenAI['assistant'], urlSegments: URLSegments, buildHeadersFunc: BuildHeadersFunc, apiKey?: APIKey) { + super(deepChat, OpenAIUtils.buildKeyVerificationDetails(), buildHeadersFunc, apiKey); this.urlSegments = urlSegments; if (typeof config === 'object') { this.config = config; // stored that assistant_id could be added From dbe6f3a9f149f0f1940e779298fb00a89205bc77 Mon Sep 17 00:00:00 2001 From: Brandon Ruffridge Date: Fri, 1 Nov 2024 13:30:32 -0400 Subject: [PATCH 2/2] Fixes key verification for Azure OpenAI API, and customizes placeholder text and helpURL. --- component/src/services/azure/azureOpenAIAssistantIO.ts | 4 +++- component/src/services/openAI/assistant/openAIAssistantIO.ts | 2 +- .../src/services/openAI/assistant/openAIAssistantIOI.ts | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/component/src/services/azure/azureOpenAIAssistantIO.ts b/component/src/services/azure/azureOpenAIAssistantIO.ts index 6d9742045..89b737061 100644 --- a/component/src/services/azure/azureOpenAIAssistantIO.ts +++ b/component/src/services/azure/azureOpenAIAssistantIO.ts @@ -8,6 +8,8 @@ export class AzureOpenAIAssistantIO extends OpenAIAssistantIOI { private static readonly THREAD_RESOURCE = `threads`; private static readonly NEW_ASSISTANT_RESOURCE = 'assistants'; override permittedErrorPrefixes: string[] = [AzureOpenAIUtils.URL_DETAILS_ERROR_MESSAGE]; + override insertKeyPlaceholderText = 'Azure OpenAI API Key'; + override keyHelpUrl = 'https://learn.microsoft.com/en-us/answers/questions/1193991/how-to-get-the-value-of-openai-api-key'; isTextInputDisabled = false; constructor(deepChat: DeepChat) { @@ -28,7 +30,7 @@ export class AzureOpenAIAssistantIO extends OpenAIAssistantIOI { getFilesPostfix: `/content${commonPostfix}`, }; - super(deepChat, config?.assistant, urlSegments, AzureOpenAIUtils.buildHeaders, apiKey); + super(deepChat, config?.assistant, urlSegments, AzureOpenAIUtils.buildKeyVerificationDetails(urlDetails), AzureOpenAIUtils.buildHeaders, apiKey); if (!AzureOpenAIUtils.validateURLDetails(urlDetails)) { this.isTextInputDisabled = true; diff --git a/component/src/services/openAI/assistant/openAIAssistantIO.ts b/component/src/services/openAI/assistant/openAIAssistantIO.ts index c11f80efc..0edd5b6de 100644 --- a/component/src/services/openAI/assistant/openAIAssistantIO.ts +++ b/component/src/services/openAI/assistant/openAIAssistantIO.ts @@ -21,7 +21,7 @@ export class OpenAIAssistantIO extends OpenAIAssistantIOI { const directConnectionCopy = JSON.parse(JSON.stringify(deepChat.directConnection)) as DirectConnection; const apiKey = directConnectionCopy.openAI; const config = directConnectionCopy.openAI?.assistant; - super(deepChat, config, OpenAIAssistantIO.URL_SEGMENTS, OpenAIUtils.buildHeaders, apiKey); + super(deepChat, config, OpenAIAssistantIO.URL_SEGMENTS, OpenAIUtils.buildKeyVerificationDetails(), OpenAIUtils.buildHeaders, apiKey); this.connectSettings.headers ??= {}; this.connectSettings.headers['OpenAI-Beta'] ??= 'assistants=v2'; // runs keep failing but keep trying if (this.shouldFetchHistory && this.sessionId) this.fetchHistory = this.fetchHistoryFunc.bind(this); diff --git a/component/src/services/openAI/assistant/openAIAssistantIOI.ts b/component/src/services/openAI/assistant/openAIAssistantIOI.ts index 4bbbe8a38..4b0c125b2 100644 --- a/component/src/services/openAI/assistant/openAIAssistantIOI.ts +++ b/component/src/services/openAI/assistant/openAIAssistantIOI.ts @@ -23,6 +23,7 @@ import { } from '../../../types/openAIResult'; import {APIKey} from '../../../types/APIKey'; import {BuildHeadersFunc} from '../../../types/headers'; +import { KeyVerificationDetails } from '../../../types/keyVerificationDetails'; // https://platform.openai.com/docs/api-reference/messages/createMessage type MessageContentArr = { @@ -68,8 +69,8 @@ export class OpenAIAssistantIOI extends DirectServiceIO { private messageStream: MessageStream | undefined; private readonly filesToolType: OpenAIAssistant['files_tool_type']; - constructor(deepChat: DeepChat, config: OpenAI['assistant'], urlSegments: URLSegments, buildHeadersFunc: BuildHeadersFunc, apiKey?: APIKey) { - super(deepChat, OpenAIUtils.buildKeyVerificationDetails(), buildHeadersFunc, apiKey); + constructor(deepChat: DeepChat, config: OpenAI['assistant'], urlSegments: URLSegments, keyVerificationDetails: KeyVerificationDetails, buildHeadersFunc: BuildHeadersFunc, apiKey?: APIKey) { + super(deepChat, keyVerificationDetails, buildHeadersFunc, apiKey); this.urlSegments = urlSegments; if (typeof config === 'object') { this.config = config; // stored that assistant_id could be added