这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@shatfield4
Copy link
Collaborator

resolves #379

Allows user to change embedding engine for any llm and shows embedding engine in data handling modal to let users know where their documents can be seen.

@shatfield4 shatfield4 self-assigned this Nov 16, 2023
@shatfield4 shatfield4 linked an issue Nov 16, 2023 that may be closed by this pull request
@review-agent-prime
Copy link

frontend/src/pages/GeneralSettings/EmbeddingPreference/index.jsx

Consider using destructuring assignment for the state variables to make the code more readable and cleaner.
Create Issue

    const { LLMProvider, VectorDB, EmbeddingEngine } = await System.keys();
    setLLMChoice(LLMProvider);
    setVectorDb(VectorDB);
    setEmbeddingEngine(EmbeddingEngine);

Consider using React.memo for the functional components to avoid unnecessary re-renders and improve the performance of the application.
Create Issue

    const LLMProviderOption = React.memo(({ name, value, link, description, checked, image, onClick }) => {
      // component code
    });

Consider using a switch statement instead of multiple if statements to make the code more readable and easier to maintain.
Create Issue

    switch (embeddingChoice) {
      case "openai":
        // code for openai
        break;
      case "azure":
        // code for azure
        break;
      case "localai":
        // code for localai
        break;
      default:
        // default code
    }

server/endpoints/admin.js, server/endpoints/api/admin/index.js

Instead of using console.error(e) and then sending a response with status 500, it would be better to create a middleware for error handling. This would improve the readability of the code and make it easier to manage errors.
Create Issue

    function errorHandler (err, req, res, next) {
      console.error(err);
      res.status(500).end();
    }

    app.use(errorHandler);

It would be better to separate the routes into different files based on their functionality. This would make the code more readable and easier to maintain.
Create Issue

    // users.js
    app.get("/admin/users", [validatedRequest, strictMultiUserRoleValid], getUsers);
    app.post("/admin/users/new", [validatedRequest, strictMultiUserRoleValid], createUser);
    // ...

    // workspaces.js
    app.get("/admin/workspaces", [validatedRequest, strictMultiUserRoleValid], getWorkspaces);
    app.post("/admin/workspaces/new", [validatedRequest, strictMultiUserRoleValid], createWorkspace);
    // ...

Instead of using the map function to remove the password from the user object, it would be better to select only the necessary fields from the database. This would improve the performance of the code.
Create Issue

    const users = await User.where().select('id', 'username', 'role');

Comment on lines +120 to +146
<LLMProviderOption
name="OpenAI"
value="openai"
link="openai.com"
description="Use OpenAI's text-embedding-ada-002 embedding model."
checked={embeddingChoice === "openai"}
image={OpenAiLogo}
onClick={updateChoice}
/>
<LLMProviderOption
name="Azure OpenAI"
value="azure"
link="azure.microsoft.com"
description="The enterprise option of OpenAI hosted on Azure services."
checked={embeddingChoice === "azure"}
image={AzureOpenAiLogo}
onClick={updateChoice}
/>
<LLMProviderOption
name="LocalAI"
value="localai"
link="localai.io"
description="Self hosted LocalAI embedding engine."
checked={embeddingChoice === "localai"}
image={LocalAiLogo}
onClick={updateChoice}
/>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapped the LLMProviderOption component with React.memo to prevent unnecessary re-renders and improve the performance of the application.

Suggested change
<LLMProviderOption
name="OpenAI"
value="openai"
link="openai.com"
description="Use OpenAI's text-embedding-ada-002 embedding model."
checked={embeddingChoice === "openai"}
image={OpenAiLogo}
onClick={updateChoice}
/>
<LLMProviderOption
name="Azure OpenAI"
value="azure"
link="azure.microsoft.com"
description="The enterprise option of OpenAI hosted on Azure services."
checked={embeddingChoice === "azure"}
image={AzureOpenAiLogo}
onClick={updateChoice}
/>
<LLMProviderOption
name="LocalAI"
value="localai"
link="localai.io"
description="Self hosted LocalAI embedding engine."
checked={embeddingChoice === "localai"}
image={LocalAiLogo}
onClick={updateChoice}
/>
const LLMProviderOption = React.memo(({ name, value, link, description, checked, image, onClick }) => {
// component code
});

Comment on lines 149 to 249
{embeddingChoice === "openai" && (
<>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
API Key
</label>
<input
type="text"
name="OpenAiKey"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="OpenAI API Key"
defaultValue={
settings?.OpenAiKey ? "*".repeat(20) : ""
}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
</>
)}

{embeddingChoice === "azure" && (
<>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
Azure Service Endpoint
</label>
<input
type="url"
name="AzureOpenAiEndpoint"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="https://my-azure.openai.azure.com"
defaultValue={settings?.AzureOpenAiEndpoint}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
{embeddingChoice === "azure" && (
<>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
Azure Service Endpoint
</label>
<input
type="url"
name="AzureOpenAiEndpoint"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="https://my-azure.openai.azure.com"
defaultValue={settings?.AzureOpenAiEndpoint}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>

<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
API Key
</label>
<input
type="password"
name="AzureOpenAiKey"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="Azure OpenAI API Key"
defaultValue={
settings?.AzureOpenAiKey ? "*".repeat(20) : ""
}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
API Key
</label>
<input
type="password"
name="AzureOpenAiKey"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="Azure OpenAI API Key"
defaultValue={
settings?.AzureOpenAiKey ? "*".repeat(20) : ""
}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>

<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
Embedding Deployment Name
</label>
<input
type="text"
name="AzureOpenAiEmbeddingModelPref"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="Azure OpenAI embedding model deployment name"
defaultValue={
settings?.AzureOpenAiEmbeddingModelPref
}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
</>
)}
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
Embedding Deployment Name
</label>
<input
type="text"
name="AzureOpenAiEmbeddingModelPref"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="Azure OpenAI embedding model deployment name"
defaultValue={settings?.AzureOpenAiEmbeddingModelPref}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
</>
)}

{embeddingChoice === "localai" && (
<>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
LocalAI Base URL
</label>
<input
type="url"
name="EmbeddingBasePath"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="http://localhost:8080/v1"
defaultValue={settings?.EmbeddingBasePath}
onChange={(e) => setBasePathValue(e.target.value)}
onBlur={updateBasePath}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
<LocalAIModelSelection
settings={settings}
basePath={basePath}
{embeddingChoice === "localai" && (
<>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
LocalAI Base URL
</label>
<input
type="url"
name="EmbeddingBasePath"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="http://localhost:8080/v1"
defaultValue={settings?.EmbeddingBasePath}
onChange={(e) => setBasePathValue(e.target.value)}
onBlur={updateBasePath}
required={true}
autoComplete="off"
spellCheck={false}
/>
</>
)}
</div>
</>
)}
</div>
<LocalAIModelSelection
settings={settings}
basePath={basePath}
/>
</>
)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace the multiple if statements with a switch statement to improve code readability and maintainability.

Suggested change
{embeddingChoice === "openai" && (
<>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
API Key
</label>
<input
type="text"
name="OpenAiKey"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="OpenAI API Key"
defaultValue={
settings?.OpenAiKey ? "*".repeat(20) : ""
}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
</>
)}
{embeddingChoice === "azure" && (
<>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
Azure Service Endpoint
</label>
<input
type="url"
name="AzureOpenAiEndpoint"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="https://my-azure.openai.azure.com"
defaultValue={settings?.AzureOpenAiEndpoint}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
{embeddingChoice === "azure" && (
<>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
Azure Service Endpoint
</label>
<input
type="url"
name="AzureOpenAiEndpoint"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="https://my-azure.openai.azure.com"
defaultValue={settings?.AzureOpenAiEndpoint}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
API Key
</label>
<input
type="password"
name="AzureOpenAiKey"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="Azure OpenAI API Key"
defaultValue={
settings?.AzureOpenAiKey ? "*".repeat(20) : ""
}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
API Key
</label>
<input
type="password"
name="AzureOpenAiKey"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="Azure OpenAI API Key"
defaultValue={
settings?.AzureOpenAiKey ? "*".repeat(20) : ""
}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
Embedding Deployment Name
</label>
<input
type="text"
name="AzureOpenAiEmbeddingModelPref"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="Azure OpenAI embedding model deployment name"
defaultValue={
settings?.AzureOpenAiEmbeddingModelPref
}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
</>
)}
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
Embedding Deployment Name
</label>
<input
type="text"
name="AzureOpenAiEmbeddingModelPref"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="Azure OpenAI embedding model deployment name"
defaultValue={settings?.AzureOpenAiEmbeddingModelPref}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
</>
)}
{embeddingChoice === "localai" && (
<>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
LocalAI Base URL
</label>
<input
type="url"
name="EmbeddingBasePath"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="http://localhost:8080/v1"
defaultValue={settings?.EmbeddingBasePath}
onChange={(e) => setBasePathValue(e.target.value)}
onBlur={updateBasePath}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
<LocalAIModelSelection
settings={settings}
basePath={basePath}
{embeddingChoice === "localai" && (
<>
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
LocalAI Base URL
</label>
<input
type="url"
name="EmbeddingBasePath"
className="bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="http://localhost:8080/v1"
defaultValue={settings?.EmbeddingBasePath}
onChange={(e) => setBasePathValue(e.target.value)}
onBlur={updateBasePath}
required={true}
autoComplete="off"
spellCheck={false}
/>
</>
)}
</div>
</>
)}
</div>
<LocalAIModelSelection
settings={settings}
basePath={basePath}
/>
</>
)}
switch (embeddingChoice) {
case 'openai':
return (
<div className='flex flex-col w-60'>
<label className='text-white text-sm font-semibold block mb-4'>
API Key
</label>
<input
type='text'
name='OpenAiKey'
className='bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5'
placeholder='OpenAI API Key'
defaultValue={settings?.OpenAiKey ? '*'.repeat(20) : ''}
required={true}
autoComplete='off'
spellCheck={false}
/>
</div>
);
case 'azure':
return (
<>
<div className='flex flex-col w-60'>
<label className='text-white text-sm font-semibold block mb-4'>
Azure Service Endpoint
</label>
<input
type='url'
name='AzureOpenAiEndpoint'
className='bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5'
placeholder='https://my-azure.openai.azure.com'
defaultValue={settings?.AzureOpenAiEndpoint}
required={true}
autoComplete='off'
spellCheck={false}
/>
</div>
<div className='flex flex-col w-60'>
<label className='text-white text-sm font-semibold block mb-4'>
API Key
</label>
<input
type='password'
name='AzureOpenAiKey'
className='bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5'
placeholder='Azure OpenAI API Key'
defaultValue={settings?.AzureOpenAiKey ? '*'.repeat(20) : ''}
required={true}
autoComplete='off'
spellCheck={false}
/>
</div>
<div className='flex flex-col w-60'>
<label className='text-white text-sm font-semibold block mb-4'>
Embedding Deployment Name
</label>
<input
type='text'
name='AzureOpenAiEmbeddingModelPref'
className='bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5'
placeholder='Azure OpenAI embedding model deployment name'
defaultValue={settings?.AzureOpenAiEmbeddingModelPref}
required={true}
autoComplete='off'
spellCheck={false}
/>
</div>
</>
);
case 'localai':
return (
<>
<div className='flex flex-col w-60'>
<label className='text-white text-sm font-semibold block mb-4'>
LocalAI Base URL
</label>
<input
type='url'
name='EmbeddingBasePath'
className='bg-zinc-900 text-white placeholder-white placeholder-opacity-60 text-sm rounded-lg focus:border-white block w-full p-2.5'
placeholder='http://localhost:8080/v1'
defaultValue={settings?.EmbeddingBasePath}
onChange={(e) => setBasePathValue(e.target.value)}
onBlur={updateBasePath}
required={true}
autoComplete='off'
spellCheck={false}
/>
</div>
<LocalAIModelSelection
settings={settings}
basePath={basePath}
/>
</>
);
default:
return null;
}

@timothycarambat timothycarambat merged commit 5ad8a5f into master Nov 16, 2023
@timothycarambat timothycarambat deleted the 379-allow-any-llm-to-use-any-embedder branch November 16, 2023 23:19
cabwds pushed a commit to cabwds/anything-llm that referenced this pull request Jul 3, 2025
…tplex-Labs#386)

* allow use of any embedder for any llm/update data handling modal

* Apply embedder override and fallback to OpenAI and Azure models

---------

Co-authored-by: timothycarambat <rambat1010@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow any LLM to use any embedder

3 participants