Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📊 refactor: use Parameters from Side Panel for OpenAI, Anthropic, and Custom endpoints #4092

Merged
merged 17 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions api/app/clients/AnthropicClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const {
parseParamFromPrompt,
createContextHandlers,
} = require('./prompts');
const { getModelMaxTokens, getModelMaxOutputTokens, matchModelName } = require('~/utils');
const { spendTokens, spendStructuredTokens } = require('~/models/spendTokens');
const { getModelMaxTokens, matchModelName } = require('~/utils');
const { sleep } = require('~/server/utils');
const BaseClient = require('./BaseClient');
const { logger } = require('~/config');
Expand Down Expand Up @@ -120,7 +120,14 @@ class AnthropicClient extends BaseClient {
this.options.maxContextTokens ??
getModelMaxTokens(this.modelOptions.model, EModelEndpoint.anthropic) ??
100000;
this.maxResponseTokens = this.modelOptions.maxOutputTokens || 1500;
this.maxResponseTokens =
this.modelOptions.maxOutputTokens ??
getModelMaxOutputTokens(
this.modelOptions.model,
this.options.endpointType ?? this.options.endpoint,
this.options.endpointTokenConfig,
) ??
1500;
this.maxPromptTokens =
this.options.maxPromptTokens || this.maxContextTokens - this.maxResponseTokens;

Expand All @@ -144,17 +151,6 @@ class AnthropicClient extends BaseClient {
this.endToken = '';
this.gptEncoder = this.constructor.getTokenizer('cl100k_base');

if (!this.modelOptions.stop) {
const stopTokens = [this.startToken];
if (this.endToken && this.endToken !== this.startToken) {
stopTokens.push(this.endToken);
}
stopTokens.push(`${this.userLabel}`);
stopTokens.push('<|diff_marker|>');

this.modelOptions.stop = stopTokens;
}

return this;
}

Expand Down
7 changes: 5 additions & 2 deletions api/app/clients/BaseClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fetch = require('node-fetch');
const {
supportsBalanceCheck,
isAgentsEndpoint,
paramEndpoints,
isParamEndpoint,
ErrorTypes,
Constants,
CacheKeys,
Expand Down Expand Up @@ -588,7 +588,10 @@ class BaseClient {

if (typeof completion === 'string') {
responseMessage.text = addSpaceIfNeeded(generation) + completion;
} else if (Array.isArray(completion) && paramEndpoints.has(this.options.endpoint)) {
} else if (
Array.isArray(completion) &&
isParamEndpoint(this.options.endpoint, this.options.endpointType)
) {
responseMessage.text = '';
responseMessage.content = completion;
} else if (Array.isArray(completion)) {
Expand Down
1 change: 1 addition & 0 deletions api/app/clients/OpenAIClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@ ${convo}

if (modelOptions.stream && /\bo1\b/i.test(modelOptions.model)) {
delete modelOptions.stream;
delete modelOptions.stop;
}

if (modelOptions.stream) {
Expand Down
7 changes: 6 additions & 1 deletion api/server/controllers/AskController.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ const AskController = async (req, res, next, initializeClient, addTitle) => {
overrideParentMessageId = null,
} = req.body;

logger.debug('[AskController]', { text, conversationId, ...endpointOption });
logger.debug('[AskController]', {
text,
conversationId,
...endpointOption,
modelsConfig: endpointOption.modelsConfig ? 'exists' : '',
});

let userMessage;
let userMessagePromise;
Expand Down
1 change: 1 addition & 0 deletions api/server/controllers/EditController.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const EditController = async (req, res, next, initializeClient) => {
isContinued,
conversationId,
...endpointOption,
modelsConfig: endpointOption.modelsConfig ? 'exists' : '',
});

let userMessage;
Expand Down
9 changes: 9 additions & 0 deletions api/utils/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,16 @@ const modelMaxOutputs = {
system_default: 1024,
};

const anthropicMaxOutputs = {
'claude-3-haiku': 4096,
'claude-3-sonnet': 4096,
'claude-3-opus': 4096,
'claude-3.5-sonnet': 8192,
'claude-3-5-sonnet': 8192,
};

const maxOutputTokensMap = {
[EModelEndpoint.anthropic]: anthropicMaxOutputs,
[EModelEndpoint.azureOpenAI]: modelMaxOutputs,
[EModelEndpoint.openAI]: modelMaxOutputs,
[EModelEndpoint.custom]: modelMaxOutputs,
Expand Down
10 changes: 6 additions & 4 deletions client/src/components/Chat/Input/HeaderOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRecoilState } from 'recoil';
import { Settings2 } from 'lucide-react';
import { Root, Anchor } from '@radix-ui/react-popover';
import { useState, useEffect, useMemo } from 'react';
import { tPresetUpdateSchema, EModelEndpoint, paramEndpoints } from 'librechat-data-provider';
import { tPresetUpdateSchema, EModelEndpoint, isParamEndpoint } from 'librechat-data-provider';
import type { TPreset, TInterfaceConfig } from 'librechat-data-provider';
import { EndpointSettings, SaveAsPresetDialog, AlternativeSettings } from '~/components/Endpoints';
import { PluginStoreDialog, TooltipAnchor } from '~/components';
Expand All @@ -28,7 +28,7 @@ export default function HeaderOptions({
useChatContext();
const { setOption } = useSetIndexOptions();

const { endpoint, conversationId, jailbreak = false } = conversation ?? {};
const { endpoint, endpointType, conversationId, jailbreak = false } = conversation ?? {};

const altConditions: { [key: string]: boolean } = {
bingAI: !!(latestMessage && jailbreak && endpoint === 'bingAI'),
Expand Down Expand Up @@ -64,6 +64,8 @@ export default function HeaderOptions({
const triggerAdvancedMode = altConditions[endpoint]
? altSettings[endpoint]
: () => setShowPopover((prev) => !prev);

const paramEndpoint = isParamEndpoint(endpoint, endpointType);
return (
<Root
open={showPopover}
Expand All @@ -83,7 +85,7 @@ export default function HeaderOptions({
)}
{!noSettings[endpoint] &&
interfaceConfig?.parameters === true &&
!paramEndpoints.has(endpoint) && (
paramEndpoint === false && (
<TooltipAnchor
id="parameters-button"
aria-label={localize('com_ui_model_parameters')}
Expand All @@ -98,7 +100,7 @@ export default function HeaderOptions({
</TooltipAnchor>
)}
</div>
{interfaceConfig?.parameters === true && !paramEndpoints.has(endpoint) && (
{interfaceConfig?.parameters === true && paramEndpoint === false && (
<OptionsPopover
visible={showPopover}
saveAsPreset={saveAsPreset}
Expand Down
Loading
Loading