Skip to content

Commit

Permalink
fail gracefully on provider fetch (#2981)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablodanswer authored Oct 29, 2024
1 parent 1201ed5 commit aafcf7a
Showing 1 changed file with 64 additions and 46 deletions.
110 changes: 64 additions & 46 deletions web/src/lib/chat/fetchAssistantdata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { fetchSS } from "@/lib/utilsSS";
import { CCPairBasicInfo } from "@/lib/types";
import { Persona } from "@/app/admin/assistants/interfaces";
import { fetchLLMProvidersSS } from "@/lib/llm/fetchLLMs";
import { personaComparator } from "@/app/admin/assistants/lib";
Expand All @@ -12,58 +11,77 @@ interface AssistantData {
hasImageCompatibleModel: boolean;
}
export async function fetchAssistantData(): Promise<AssistantData> {
const [assistants, assistantsFetchError] = await fetchAssistantsSS();
const ccPairsResponse = await fetchSS("/manage/indexing-status");

let ccPairs: CCPairBasicInfo[] = [];
if (ccPairsResponse?.ok) {
ccPairs = await ccPairsResponse.json();
} else {
console.log(`Failed to fetch connectors - ${ccPairsResponse?.status}`);
}

const hasAnyConnectors = ccPairs.length > 0;
// Default state if anything fails
const defaultState: AssistantData = {
assistants: [],
hasAnyConnectors: false,
hasImageCompatibleModel: false,
};

// if no connectors are setup, only show personas that are pure
// passthrough and don't do any retrieval
let filteredAssistants = assistants;
if (assistantsFetchError) {
console.log(`Failed to fetch assistants - ${assistantsFetchError}`);
}
try {
// Fetch core assistants data first
const [assistants, assistantsFetchError] = await fetchAssistantsSS();
if (assistantsFetchError) {
console.error(`Failed to fetch assistants - ${assistantsFetchError}`);
return defaultState;
}

// remove those marked as hidden by an admin
filteredAssistants = filteredAssistants.filter(
(assistant) => assistant.is_visible
);
// Parallel fetch of additional data
const [ccPairsResponse, llmProviders] = await Promise.all([
fetchSS("/manage/indexing-status").catch((error) => {
console.error("Failed to fetch connectors:", error);
return null;
}),
fetchLLMProvidersSS().catch((error) => {
console.error("Failed to fetch LLM providers:", error);
return [];
}),
]);

if (!hasAnyConnectors) {
filteredAssistants = filteredAssistants.filter(
(assistant) => assistant.num_chunks === 0
// Process visible assistants
let filteredAssistants = assistants.filter(
(assistant) => assistant.is_visible
);
}

// sort them in priority order
filteredAssistants.sort(personaComparator);
// Process connector status
const hasAnyConnectors = ccPairsResponse?.ok
? (await ccPairsResponse.json()).length > 0
: false;

const llmProviders = await fetchLLMProvidersSS();
const hasImageCompatibleModel = llmProviders.some(
(provider) =>
provider.provider === "openai" ||
provider.model_names.some((model) => checkLLMSupportsImageInput(model))
);
// Filter assistants based on connector status
if (!hasAnyConnectors) {
filteredAssistants = filteredAssistants.filter(
(assistant) => assistant.num_chunks === 0
);
}

if (!hasImageCompatibleModel) {
filteredAssistants = filteredAssistants.filter(
(assistant) =>
!assistant.tools.some(
(tool) => tool.in_code_tool_id === "ImageGenerationTool"
)
// Sort assistants
filteredAssistants.sort(personaComparator);

// Check for image-compatible models
const hasImageCompatibleModel = llmProviders.some(
(provider) =>
provider.provider === "openai" ||
provider.model_names.some((model) => checkLLMSupportsImageInput(model))
);
}

return {
assistants: filteredAssistants,
hasAnyConnectors,
hasImageCompatibleModel,
};
// Filter out image generation tools if no compatible model
if (!hasImageCompatibleModel) {
filteredAssistants = filteredAssistants.filter(
(assistant) =>
!assistant.tools.some(
(tool) => tool.in_code_tool_id === "ImageGenerationTool"
)
);
}

return {
assistants: filteredAssistants,
hasAnyConnectors,
hasImageCompatibleModel,
};
} catch (error) {
console.error("Unexpected error in fetchAssistantData:", error);
return defaultState;
}
}

0 comments on commit aafcf7a

Please sign in to comment.