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

add custom_openai embedding Provider #347

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions src/lib/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ export const getAvailableEmbeddingModelProviders = async () => {
}
}

models['custom_openai'] = {};

return models;
};
13 changes: 11 additions & 2 deletions src/websocket/connectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import type { Embeddings } from '@langchain/core/embeddings';
import type { IncomingMessage } from 'http';
import logger from '../utils/logger';
import { ChatOpenAI } from '@langchain/openai';
import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai';

export const handleConnection = async (
ws: WebSocket,
Expand Down Expand Up @@ -61,11 +61,20 @@ export const handleConnection = async (

if (
embeddingModelProviders[embeddingModelProvider] &&
embeddingModelProviders[embeddingModelProvider][embeddingModel]
embeddingModelProviders[embeddingModelProvider][embeddingModel] &&
embeddingModelProvider != 'custom_openai'
) {
embeddings = embeddingModelProviders[embeddingModelProvider][
embeddingModel
] as Embeddings | undefined;
} else if (embeddingModelProvider == 'custom_openai') {
embeddings = new OpenAIEmbeddings({
modelName: embeddingModel,
openAIApiKey: searchParams.get('openAIApiKey'),
configuration: {
baseURL: searchParams.get('openAIBaseURL'),
},
}) as unknown as Embeddings
}

if (!llm || !embeddings) {
Expand Down
12 changes: 12 additions & 0 deletions ui/components/ChatWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const useSocket = (

if (
embeddingModelProvider &&
embeddingModelProvider != 'custom_openai' &&
!embeddingModelProviders[embeddingModelProvider][embeddingModel]
) {
embeddingModel = Object.keys(
Expand Down Expand Up @@ -159,6 +160,17 @@ const useSocket = (
searchParams.append('embeddingModel', embeddingModel!);
searchParams.append('embeddingModelProvider', embeddingModelProvider);

if (embeddingModelProvider === 'custom_openai') {
searchParams.append(
'openAIApiKey',
localStorage.getItem('openAIApiKey')!,
);
searchParams.append(
'openAIBaseURL',
localStorage.getItem('openAIBaseURL')!,
);
}

wsURL.search = searchParams.toString();

const ws = new WebSocket(wsURL.toString());
Expand Down
85 changes: 52 additions & 33 deletions ui/components/SettingsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, {
} from 'react';
import ThemeSwitcher from './theme/Switcher';

interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {}
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> { }

const Input = ({ className, ...restProps }: InputProps) => {
return (
Expand Down Expand Up @@ -258,30 +258,30 @@ const SettingsDialog = ({
options={(() => {
const chatModelProvider =
config.chatModelProviders[
selectedChatModelProvider
selectedChatModelProvider
];

return chatModelProvider
? chatModelProvider.length > 0
? chatModelProvider.map((model) => ({
value: model,
label: model,
}))
value: model,
label: model,
}))
: [
{
value: '',
label: 'No models available',
disabled: true,
},
]
: [
{
value: '',
label:
'Invalid provider, please check backend logs',
label: 'No models available',
disabled: true,
},
];
]
: [
{
value: '',
label:
'Invalid provider, please check backend logs',
disabled: true,
},
];
})()}
/>
</div>
Expand Down Expand Up @@ -340,9 +340,13 @@ const SettingsDialog = ({
value={selectedEmbeddingModelProvider ?? undefined}
onChange={(e) => {
setSelectedEmbeddingModelProvider(e.target.value);
setSelectedEmbeddingModel(
config.embeddingModelProviders[e.target.value][0],
);
if (e.target.value === 'custom_openai') {
setSelectedEmbeddingModel('');
} else {
setSelectedEmbeddingModel(
config.embeddingModelProviders[e.target.value][0],
);
}
}}
options={Object.keys(
config.embeddingModelProviders,
Expand All @@ -355,7 +359,7 @@ const SettingsDialog = ({
/>
</div>
)}
{selectedEmbeddingModelProvider && (
{selectedEmbeddingModelProvider && selectedEmbeddingModelProvider != 'custom_openai' && (
<div className="flex flex-col space-y-1">
<p className="text-black/70 dark:text-white/70 text-sm">
Embedding Model
Expand All @@ -368,34 +372,49 @@ const SettingsDialog = ({
options={(() => {
const embeddingModelProvider =
config.embeddingModelProviders[
selectedEmbeddingModelProvider
selectedEmbeddingModelProvider
];

return embeddingModelProvider
? embeddingModelProvider.length > 0
? embeddingModelProvider.map((model) => ({
label: model,
value: model,
}))
label: model,
value: model,
}))
: [
{
label: 'No embedding models available',
value: '',
disabled: true,
},
]
: [
{
label:
'Invalid provider, please check backend logs',
label: 'No embedding models available',
value: '',
disabled: true,
},
];
]
: [
{
label:
'Invalid provider, please check backend logs',
value: '',
disabled: true,
},
];
})()}
/>
</div>
)}
{selectedEmbeddingModelProvider && selectedEmbeddingModelProvider === 'custom_openai' && (
<div className="flex flex-col space-y-1">
<p className="text-black/70 dark:text-white/70 text-sm">
Embedding Model
</p>
<Input
type="text"
placeholder="Embedding Model name"
defaultValue={selectedEmbeddingModel!}
onChange={(e) =>
setSelectedEmbeddingModel(e.target.value)
}
/>
</div>
)}
<div className="flex flex-col space-y-1">
<p className="text-black/70 dark:text-white/70 text-sm">
OpenAI API Key
Expand Down