Skip to content

Commit

Permalink
Merge branch 'main' into feature/set-balance
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis531 authored Oct 23, 2024
2 parents f9a1481 + 4da35b9 commit 44c46a1
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ PROXY=
#============#

ANTHROPIC_API_KEY=user_provided
# ANTHROPIC_MODELS=claude-3-5-sonnet-20240620,claude-3-opus-20240229,claude-3-sonnet-20240229,claude-3-haiku-20240307,claude-2.1,claude-2,claude-1.2,claude-1,claude-1-100k,claude-instant-1,claude-instant-1-100k
# ANTHROPIC_MODELS=claude-3-5-sonnet-20241022,claude-3-5-sonnet-latest,claude-3-5-sonnet-20240620,claude-3-opus-20240229,claude-3-sonnet-20240229,claude-3-haiku-20240307,claude-2.1,claude-2,claude-1.2,claude-1,claude-1-100k,claude-instant-1,claude-instant-1-100k
# ANTHROPIC_REVERSE_PROXY=

#============#
Expand Down
3 changes: 3 additions & 0 deletions api/app/clients/AnthropicClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,9 @@ class AnthropicClient extends BaseClient {
*/
checkPromptCacheSupport(modelName) {
const modelMatch = matchModelName(modelName, EModelEndpoint.anthropic);
if (modelMatch.includes('claude-3-5-sonnet-latest')) {
return false;
}
if (
modelMatch === 'claude-3-5-sonnet' ||
modelMatch === 'claude-3-haiku' ||
Expand Down
30 changes: 27 additions & 3 deletions api/app/clients/specs/AnthropicClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ describe('AnthropicClient', () => {
);
});

it('should add beta header for claude-3-5-sonnet model', () => {
it('should add "max-tokens" & "prompt-caching" beta header for claude-3-5-sonnet model', () => {
const client = new AnthropicClient('test-api-key');
const modelOptions = {
model: 'claude-3-5-sonnet-20240307',
model: 'claude-3-5-sonnet-20241022',
};
client.setOptions({ modelOptions, promptCache: true });
const anthropicClient = client.getClient(modelOptions);
Expand All @@ -215,7 +215,7 @@ describe('AnthropicClient', () => {
);
});

it('should add beta header for claude-3-haiku model', () => {
it('should add "prompt-caching" beta header for claude-3-haiku model', () => {
const client = new AnthropicClient('test-api-key');
const modelOptions = {
model: 'claude-3-haiku-2028',
Expand All @@ -229,6 +229,30 @@ describe('AnthropicClient', () => {
);
});

it('should add "prompt-caching" beta header for claude-3-opus model', () => {
const client = new AnthropicClient('test-api-key');
const modelOptions = {
model: 'claude-3-opus-2028',
};
client.setOptions({ modelOptions, promptCache: true });
const anthropicClient = client.getClient(modelOptions);
expect(anthropicClient._options.defaultHeaders).toBeDefined();
expect(anthropicClient._options.defaultHeaders).toHaveProperty('anthropic-beta');
expect(anthropicClient._options.defaultHeaders['anthropic-beta']).toBe(
'prompt-caching-2024-07-31',
);
});

it('should not add beta header for claude-3-5-sonnet-latest model', () => {
const client = new AnthropicClient('test-api-key');
const modelOptions = {
model: 'anthropic/claude-3-5-sonnet-latest',
};
client.setOptions({ modelOptions, promptCache: true });
const anthropicClient = client.getClient(modelOptions);
expect(anthropicClient.defaultHeaders).not.toHaveProperty('anthropic-beta');
});

it('should not add beta header for other models', () => {
const client = new AnthropicClient('test-api-key');
client.setOptions({
Expand Down
10 changes: 5 additions & 5 deletions api/server/controllers/agents/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,6 @@ const uploadAgentAvatarHandler = async (req, res) => {
return res.status(400).json({ message: 'Agent ID is required' });
}

let { avatar: _avatar = '{}' } = req.body;

const image = await uploadImageBuffer({
req,
context: FileContext.avatar,
Expand All @@ -236,18 +234,20 @@ const uploadAgentAvatarHandler = async (req, res) => {
},
});

let _avatar;
try {
_avatar = JSON.parse(_avatar);
const agent = await getAgent({ id: agent_id });
_avatar = agent.avatar;
} catch (error) {
logger.error('[/avatar/:agent_id] Error parsing avatar', error);
logger.error('[/avatar/:agent_id] Error fetching agent', error);
_avatar = {};
}

if (_avatar && _avatar.source) {
const { deleteFile } = getStrategyFunctions(_avatar.source);
try {
await deleteFile(req, { filepath: _avatar.filepath });
await deleteFileByFilter({ filepath: _avatar.filepath });
await deleteFileByFilter({ user: req.user.id, filepath: _avatar.filepath });
} catch (error) {
logger.error('[/avatar/:agent_id] Error deleting old avatar', error);
}
Expand Down
13 changes: 8 additions & 5 deletions api/server/controllers/assistants/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ const getAssistantDocuments = async (req, res) => {
* @param {string} req.params.assistant_id - The ID of the assistant.
* @param {Express.Multer.File} req.file - The avatar image file.
* @param {object} req.body - Request body
* @param {string} [req.body.metadata] - Optional metadata for the assistant's avatar.
* @returns {Object} 200 - success response - application/json
*/
const uploadAssistantAvatar = async (req, res) => {
Expand All @@ -251,7 +250,6 @@ const uploadAssistantAvatar = async (req, res) => {
return res.status(400).json({ message: 'Assistant ID is required' });
}

let { metadata: _metadata = '{}' } = req.body;
const { openai } = await getOpenAIClient({ req, res });
await validateAuthor({ req, openai });

Expand All @@ -263,18 +261,23 @@ const uploadAssistantAvatar = async (req, res) => {
},
});

let _metadata;

try {
_metadata = JSON.parse(_metadata);
const assistant = await openai.beta.assistants.retrieve(assistant_id);
if (assistant) {
_metadata = assistant.metadata;
}
} catch (error) {
logger.error('[/avatar/:assistant_id] Error parsing metadata', error);
logger.error('[/avatar/:assistant_id] Error fetching assistant', error);
_metadata = {};
}

if (_metadata.avatar && _metadata.avatar_source) {
const { deleteFile } = getStrategyFunctions(_metadata.avatar_source);
try {
await deleteFile(req, { filepath: _metadata.avatar });
await deleteFileByFilter({ filepath: _metadata.avatar });
await deleteFileByFilter({ user: req.user.id, filepath: _metadata.avatar });
} catch (error) {
logger.error('[/avatar/:assistant_id] Error deleting old avatar', error);
}
Expand Down
16 changes: 14 additions & 2 deletions api/server/services/Files/Local/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,20 @@ const deleteLocalFile = async (req, file) => {
}

if (file.filepath.startsWith(`/uploads/${req.user.id}`)) {
const basePath = file.filepath.split('/uploads/')[1];
const filepath = path.join(uploads, basePath);
const userUploadDir = path.join(uploads, req.user.id);
const basePath = file.filepath.split(`/uploads/${req.user.id}/`)[1];

if (!basePath) {
throw new Error(`Invalid file path: ${file.filepath}`);
}

const filepath = path.join(userUploadDir, basePath);

const rel = path.relative(userUploadDir, filepath);
if (rel.startsWith('..') || path.isAbsolute(rel) || rel.includes(`..${path.sep}`)) {
throw new Error(`Invalid file path: ${file.filepath}`);
}

await fs.promises.unlink(filepath);
return;
}
Expand Down
2 changes: 2 additions & 0 deletions api/utils/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const anthropicModels = {
'claude-3-opus': 200000,
'claude-3-5-sonnet': 200000,
'claude-3.5-sonnet': 200000,
'claude-3-5-sonnet-latest': 200000,
'claude-3.5-sonnet-latest': 200000,
};

const metaModels = {
Expand Down
4 changes: 0 additions & 4 deletions client/src/components/SidePanel/Agents/AgentAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ function Avatar({
formData.append('file', input, input.name);
formData.append('agent_id', createMutation.data.id);

if (typeof createMutation.data.avatar === 'object') {
formData.append('avatar', JSON.stringify(createMutation.data.avatar));
}

uploadAvatar({
agent_id: createMutation.data.id,
postCreation: true,
Expand Down
29 changes: 10 additions & 19 deletions client/src/components/SidePanel/Builder/AssistantAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function Avatar({
setInput(null);
setPreviewUrl(data.metadata?.avatar as string | null);

const res = queryClient.getQueryData<AssistantListResponse>([
const res = queryClient.getQueryData<AssistantListResponse | undefined>([
QueryKeys.assistants,
endpoint,
defaultOrderQuery,
Expand All @@ -78,16 +78,15 @@ function Avatar({
return;
}

const assistants =
res.data.map((assistant) => {
if (assistant.id === assistant_id) {
return {
...assistant,
...data,
};
}
return assistant;
}) ?? [];
const assistants = res.data.map((assistant) => {
if (assistant.id === assistant_id) {
return {
...assistant,
...data,
};
}
return assistant;
});

queryClient.setQueryData<AssistantListResponse>(
[QueryKeys.assistants, endpoint, defaultOrderQuery],
Expand Down Expand Up @@ -149,10 +148,6 @@ function Avatar({
formData.append('file', input, input.name);
formData.append('assistant_id', createMutation.data.id);

if (typeof createMutation.data.metadata === 'object') {
formData.append('metadata', JSON.stringify(createMutation.data.metadata));
}

uploadAvatar({
assistant_id: createMutation.data.id,
model: activeModel,
Expand Down Expand Up @@ -195,10 +190,6 @@ function Avatar({
formData.append('file', file, file.name);
formData.append('assistant_id', assistant_id);

if (typeof metadata === 'object') {
formData.append('metadata', JSON.stringify(metadata));
}

uploadAvatar({
assistant_id,
model: activeModel,
Expand Down
2 changes: 2 additions & 0 deletions packages/data-provider/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,9 @@ const sharedOpenAIModels = [
];

const sharedAnthropicModels = [
'claude-3-5-sonnet-20241022',
'claude-3-5-sonnet-20240620',
'claude-3-5-sonnet-latest',
'claude-3-opus-20240229',
'claude-3-sonnet-20240229',
'claude-3-haiku-20240307',
Expand Down
2 changes: 1 addition & 1 deletion packages/data-provider/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ const ANTHROPIC_MAX_OUTPUT = 8192;
const LEGACY_ANTHROPIC_MAX_OUTPUT = 4096;
export const anthropicSettings = {
model: {
default: 'claude-3-5-sonnet-20240620',
default: 'claude-3-5-sonnet-20241022',
},
temperature: {
min: 0,
Expand Down

0 comments on commit 44c46a1

Please sign in to comment.