Skip to content

Commit

Permalink
Clearly mark frontend fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoros committed Feb 17, 2024
1 parent 5bb84f8 commit 8299b4c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/common/util/clientFetchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const frontendSideFetch = fetch;
4 changes: 3 additions & 1 deletion src/modules/aifn/react/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { callApiSearchGoogle } from '~/modules/google/search.client';
import { callBrowseFetchPage } from '~/modules/browse/browse.client';
import { llmChatGenerateOrThrow, VChatMessageIn } from '~/modules/llms/llm.client';

import { frontendSideFetch } from '~/common/util/clientFetchers';


// prompt to implement the ReAct paradigm: https://arxiv.org/abs/2210.03629
const reActPrompt = (enableBrowse: boolean): string =>
Expand Down Expand Up @@ -172,7 +174,7 @@ export class Agent {
type ActionFunction = (input: string) => Promise<string>;

async function wikipedia(q: string): Promise<string> {
const response = await fetch(
const response = await frontendSideFetch(
`https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(q)}&format=json&origin=*`,
);
const data = await response.json();
Expand Down
3 changes: 2 additions & 1 deletion src/modules/blocks/code/RenderCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SchemaIcon from '@mui/icons-material/Schema';
import ShapeLineOutlinedIcon from '@mui/icons-material/ShapeLineOutlined';

import { copyToClipboard } from '~/common/util/clipboardUtils';
import { frontendSideFetch } from '~/common/util/clientFetchers';

import type { CodeBlock } from '../blocks';
import { ButtonCodePen, isCodePenSupported } from './ButtonCodePen';
Expand All @@ -28,7 +29,7 @@ async function fetchPlantUmlSvg(plantUmlCode: string): Promise<string | null> {

// retrieve and manually adapt the SVG, to remove the background
const encodedPlantUML: string = plantUmlEncode(plantUmlCode);
const response = await fetch(`https://www.plantuml.com/plantuml/svg/${encodedPlantUML}`);
const response = await frontendSideFetch(`https://www.plantuml.com/plantuml/svg/${encodedPlantUML}`);
text = await response.text();
} catch (e) {
return null;
Expand Down
9 changes: 5 additions & 4 deletions src/modules/elevenlabs/elevenlabs.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { backendCaps } from '~/modules/backend/state-backend';

import { AudioLivePlayer } from '~/common/util/AudioLivePlayer';
import { CapabilityElevenLabsSpeechSynthesis } from '~/common/components/useCapabilities';
import { frontendSideFetch } from '~/common/util/clientFetchers';
import { playSoundBuffer } from '~/common/util/audioUtils';
import { useUIPreferencesStore } from '~/common/state/store-ui';

Expand Down Expand Up @@ -35,7 +36,7 @@ export async function speakText(text: string, voiceId?: string) {
const nonEnglish = !(preferredLanguage?.toLowerCase()?.startsWith('en'));

try {
const edgeResponse = await fetchApiElevenlabsSpeech(text, elevenLabsApiKey, voiceId || elevenLabsVoiceId, nonEnglish, false);
const edgeResponse = await frontendFetchAPIElevenLabsSpeech(text, elevenLabsApiKey, voiceId || elevenLabsVoiceId, nonEnglish, false);
const audioBuffer = await edgeResponse.arrayBuffer();
await playSoundBuffer(audioBuffer);
} catch (error) {
Expand All @@ -55,7 +56,7 @@ export async function EXPERIMENTAL_speakTextStream(text: string, voiceId?: strin
const nonEnglish = !(preferredLanguage?.toLowerCase()?.startsWith('en'));

try {
const edgeResponse = await fetchApiElevenlabsSpeech(text, elevenLabsApiKey, voiceId || elevenLabsVoiceId, nonEnglish, true);
const edgeResponse = await frontendFetchAPIElevenLabsSpeech(text, elevenLabsApiKey, voiceId || elevenLabsVoiceId, nonEnglish, true);

// if (!liveAudioPlayer)
const liveAudioPlayer = new AudioLivePlayer();
Expand All @@ -72,7 +73,7 @@ export async function EXPERIMENTAL_speakTextStream(text: string, voiceId?: strin
/**
* Note: we have to use this client-side API instead of TRPC because of ArrayBuffers..
*/
async function fetchApiElevenlabsSpeech(text: string, elevenLabsApiKey: string, elevenLabsVoiceId: string, nonEnglish: boolean, streaming: boolean): Promise<Response> {
async function frontendFetchAPIElevenLabsSpeech(text: string, elevenLabsApiKey: string, elevenLabsVoiceId: string, nonEnglish: boolean, streaming: boolean): Promise<Response> {
// NOTE: hardcoded 1000 as a failsafe, since the API will take very long and consume lots of credits for longer texts
const speechInput: SpeechInputSchema = {
elevenKey: elevenLabsApiKey,
Expand All @@ -82,7 +83,7 @@ async function fetchApiElevenlabsSpeech(text: string, elevenLabsApiKey: string,
...(streaming && { streaming: true, streamOptimization: 4 }),
};

const response = await fetch('/api/elevenlabs/speech', {
const response = await frontendSideFetch('/api/elevenlabs/speech', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(speechInput),
Expand Down
3 changes: 2 additions & 1 deletion src/modules/llms/vendors/unifiedStreamingClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { apiAsync } from '~/common/util/trpc.client';
import { frontendSideFetch } from '~/common/util/clientFetchers';

import type { ChatStreamingFirstOutputPacketSchema, ChatStreamingInputSchema } from '../server/llm.server.streaming';
import type { DLLMId } from '../store-llms';
Expand Down Expand Up @@ -57,7 +58,7 @@ export async function unifiedStreamingClient<TSourceSetup = unknown, TLLMOptions
};

// connect to the server-side streaming endpoint
const response = await fetch('/api/llms/stream', {
const response = await frontendSideFetch('/api/llms/stream', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(input),
Expand Down

0 comments on commit 8299b4c

Please sign in to comment.