Compare commits
1 Commits
refactor/A
...
azure-v2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78283e1686 |
@@ -19,9 +19,9 @@ const {
|
|||||||
saveAssistantMessage,
|
saveAssistantMessage,
|
||||||
} = require('~/server/services/Threads');
|
} = require('~/server/services/Threads');
|
||||||
const { sendResponse, sendMessage, sleep, isEnabled, countTokens } = require('~/server/utils');
|
const { sendResponse, sendMessage, sleep, isEnabled, countTokens } = require('~/server/utils');
|
||||||
const { runAssistant, createOnTextProgress } = require('~/server/services/AssistantService');
|
const { createOnTextProgress } = require('~/server/services/AssistantService');
|
||||||
const { createRun, StreamRunManager } = require('~/server/services/Runs');
|
|
||||||
const { addTitle } = require('~/server/services/Endpoints/assistants');
|
const { addTitle } = require('~/server/services/Endpoints/assistants');
|
||||||
|
const { StreamRunManager } = require('~/server/services/Runs');
|
||||||
const { getTransactions } = require('~/models/Transaction');
|
const { getTransactions } = require('~/models/Transaction');
|
||||||
const checkBalance = require('~/models/checkBalance');
|
const checkBalance = require('~/models/checkBalance');
|
||||||
const { getConvo } = require('~/models/Conversation');
|
const { getConvo } = require('~/models/Conversation');
|
||||||
@@ -471,39 +471,7 @@ const chatV2 = async (req, res) => {
|
|||||||
/** @type {RunResponse | typeof StreamRunManager | undefined} */
|
/** @type {RunResponse | typeof StreamRunManager | undefined} */
|
||||||
let response;
|
let response;
|
||||||
|
|
||||||
const processRun = async (retry = false) => {
|
const processRun = async () => {
|
||||||
if (endpoint === EModelEndpoint.azureAssistants) {
|
|
||||||
body.model = openai._options.model;
|
|
||||||
openai.attachedFileIds = attachedFileIds;
|
|
||||||
if (retry) {
|
|
||||||
response = await runAssistant({
|
|
||||||
openai,
|
|
||||||
thread_id,
|
|
||||||
run_id,
|
|
||||||
in_progress: openai.in_progress,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* NOTE:
|
|
||||||
* By default, a Run will use the model and tools configuration specified in Assistant object,
|
|
||||||
* but you can override most of these when creating the Run for added flexibility:
|
|
||||||
*/
|
|
||||||
const run = await createRun({
|
|
||||||
openai,
|
|
||||||
thread_id,
|
|
||||||
body,
|
|
||||||
});
|
|
||||||
|
|
||||||
run_id = run.id;
|
|
||||||
await cache.set(cacheKey, `${thread_id}:${run_id}`, ten_minutes);
|
|
||||||
sendInitialResponse();
|
|
||||||
|
|
||||||
// todo: retry logic
|
|
||||||
response = await runAssistant({ openai, thread_id, run_id });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @type {{[AssistantStreamEvents.ThreadRunCreated]: (event: ThreadRunCreated) => Promise<void>}} */
|
/** @type {{[AssistantStreamEvents.ThreadRunCreated]: (event: ThreadRunCreated) => Promise<void>}} */
|
||||||
const handlers = {
|
const handlers = {
|
||||||
[AssistantStreamEvents.ThreadRunCreated]: async (event) => {
|
[AssistantStreamEvents.ThreadRunCreated]: async (event) => {
|
||||||
|
|||||||
@@ -15,44 +15,6 @@ const OpenAIClient = require('~/app/clients/OpenAIClient');
|
|||||||
const { isUserProvided } = require('~/server/utils');
|
const { isUserProvided } = require('~/server/utils');
|
||||||
const { constructAzureURL } = require('~/utils');
|
const { constructAzureURL } = require('~/utils');
|
||||||
|
|
||||||
class Files {
|
|
||||||
constructor(client) {
|
|
||||||
this._client = client;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Create an assistant file by attaching a
|
|
||||||
* [File](https://platform.openai.com/docs/api-reference/files) to an
|
|
||||||
* [assistant](https://platform.openai.com/docs/api-reference/assistants).
|
|
||||||
*/
|
|
||||||
create(assistantId, body, options) {
|
|
||||||
return this._client.post(`/assistants/${assistantId}/files`, {
|
|
||||||
body,
|
|
||||||
...options,
|
|
||||||
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves an AssistantFile.
|
|
||||||
*/
|
|
||||||
retrieve(assistantId, fileId, options) {
|
|
||||||
return this._client.get(`/assistants/${assistantId}/files/${fileId}`, {
|
|
||||||
...options,
|
|
||||||
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete an assistant file.
|
|
||||||
*/
|
|
||||||
del(assistantId, fileId, options) {
|
|
||||||
return this._client.delete(`/assistants/${assistantId}/files/${fileId}`, {
|
|
||||||
...options,
|
|
||||||
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const initializeClient = async ({ req, res, version, endpointOption, initAppClient = false }) => {
|
const initializeClient = async ({ req, res, version, endpointOption, initAppClient = false }) => {
|
||||||
const { PROXY, OPENAI_ORGANIZATION, AZURE_ASSISTANTS_API_KEY, AZURE_ASSISTANTS_BASE_URL } =
|
const { PROXY, OPENAI_ORGANIZATION, AZURE_ASSISTANTS_API_KEY, AZURE_ASSISTANTS_BASE_URL } =
|
||||||
process.env;
|
process.env;
|
||||||
@@ -168,8 +130,6 @@ const initializeClient = async ({ req, res, version, endpointOption, initAppClie
|
|||||||
...opts,
|
...opts,
|
||||||
});
|
});
|
||||||
|
|
||||||
openai.beta.assistants.files = new Files(openai);
|
|
||||||
|
|
||||||
openai.req = req;
|
openai.req = req;
|
||||||
openai.res = res;
|
openai.res = res;
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,12 @@ const { logger } = require('~/config');
|
|||||||
*/
|
*/
|
||||||
function azureAssistantsDefaults() {
|
function azureAssistantsDefaults() {
|
||||||
return {
|
return {
|
||||||
capabilities: [Capabilities.tools, Capabilities.actions, Capabilities.code_interpreter],
|
capabilities: [
|
||||||
|
Capabilities.tools,
|
||||||
|
Capabilities.actions,
|
||||||
|
Capabilities.code_interpreter,
|
||||||
|
Capabilities.retrieval,
|
||||||
|
],
|
||||||
version: defaultAssistantsVersion.azureAssistants,
|
version: defaultAssistantsVersion.azureAssistants,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -181,7 +181,6 @@ function generateConfig(key, baseURL, endpoint) {
|
|||||||
config.retrievalModels = defaultRetrievalModels;
|
config.retrievalModels = defaultRetrievalModels;
|
||||||
config.capabilities = [
|
config.capabilities = [
|
||||||
Capabilities.code_interpreter,
|
Capabilities.code_interpreter,
|
||||||
Capabilities.image_vision,
|
|
||||||
Capabilities.retrieval,
|
Capabilities.retrieval,
|
||||||
Capabilities.actions,
|
Capabilities.actions,
|
||||||
Capabilities.tools,
|
Capabilities.tools,
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ export enum Capabilities {
|
|||||||
|
|
||||||
export const defaultAssistantsVersion = {
|
export const defaultAssistantsVersion = {
|
||||||
[EModelEndpoint.assistants]: 2,
|
[EModelEndpoint.assistants]: 2,
|
||||||
[EModelEndpoint.azureAssistants]: 1,
|
[EModelEndpoint.azureAssistants]: 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const assistantEndpointSchema = z.object({
|
export const assistantEndpointSchema = z.object({
|
||||||
@@ -441,7 +441,7 @@ export const EndpointURLs: { [key in EModelEndpoint]: string } = {
|
|||||||
[EModelEndpoint.gptPlugins]: `/api/ask/${EModelEndpoint.gptPlugins}`,
|
[EModelEndpoint.gptPlugins]: `/api/ask/${EModelEndpoint.gptPlugins}`,
|
||||||
[EModelEndpoint.azureOpenAI]: `/api/ask/${EModelEndpoint.azureOpenAI}`,
|
[EModelEndpoint.azureOpenAI]: `/api/ask/${EModelEndpoint.azureOpenAI}`,
|
||||||
[EModelEndpoint.chatGPTBrowser]: `/api/ask/${EModelEndpoint.chatGPTBrowser}`,
|
[EModelEndpoint.chatGPTBrowser]: `/api/ask/${EModelEndpoint.chatGPTBrowser}`,
|
||||||
[EModelEndpoint.azureAssistants]: '/api/assistants/v1/chat',
|
[EModelEndpoint.azureAssistants]: '/api/assistants/v2/chat',
|
||||||
[EModelEndpoint.assistants]: '/api/assistants/v2/chat',
|
[EModelEndpoint.assistants]: '/api/assistants/v2/chat',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user