| 899 | if (request.body.use_fallback) { | 899 | if (request.body.use_fallback) { |
| 900 | bodyParams['route'] = 'fallback'; | 900 | bodyParams['route'] = 'fallback'; |
| 901 | } | 901 | } |
| | 902 | |
| | 903 | let cachingAtDepth = getConfigValue('claude.cachingAtDepth', -1); |
| | 904 | if (Number.isInteger(cachingAtDepth) && cachingAtDepth >= 0 && request.body.model.startsWith('anthropic/claude-3')) { |
| | 905 | //caching the prefill is a terrible idea in general |
| | 906 | let passedThePrefill = false; |
| | 907 | //depth here is the number of message role switches |
| | 908 | let depth = 0; |
| | 909 | let previousRoleName = ""; |
| | 910 | for (let i = request.body.messages.length - 1; i >= 0; i--) { |
| | 911 | if (!passedThePrefill && request.body.messages[i].role === 'assistant') { |
| | 912 | continue; |
| | 913 | } |
| | 914 | |
| | 915 | passedThePrefill = true; |
| | 916 | |
| | 917 | if (request.body.messages[i].role !== previousRoleName) { |
| | 918 | if (depth === cachingAtDepth || depth === cachingAtDepth + 2) { |
| | 919 | const content = request.body.messages[i].content; |
| | 920 | if (typeof content === 'string') { |
| | 921 | request.body.messages[i].content = [{ |
| | 922 | type: 'text', |
| | 923 | text: content, |
| | 924 | cache_control: { type: "ephemeral"}, |
| | 925 | }]; |
| | 926 | } else { |
| | 927 | const contentPartCount = content.length; |
| | 928 | content[contentPartCount - 1].cache_control = { |
| | 929 | type: "ephemeral" |
| | 930 | } |
| | 931 | } |
| | 932 | } |
| | 933 | |
| | 934 | if (depth === cachingAtDepth + 2) { |
| | 935 | break |
| | 936 | } |
| | 937 | |
| | 938 | depth += 1; |
| | 939 | previousRoleName = request.body.messages[i].role; |
| | 940 | } |
| | 941 | } |
| | 942 | } |
| 902 | } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.CUSTOM) { | 943 | } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.CUSTOM) { |
| 903 | apiUrl = request.body.custom_url; | 944 | apiUrl = request.body.custom_url; |
| 904 | apiKey = readSecret(request.user.directories, SECRET_KEYS.CUSTOM); | 945 | apiKey = readSecret(request.user.directories, SECRET_KEYS.CUSTOM); |