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 embedding model setting #100

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
25 changes: 25 additions & 0 deletions src/components/Settings/Sections/ChatSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,31 @@ const ChatSettings = () => {
))}
</select>
</FieldWrapper>
<FieldWrapper
title="Embedding Model"
description="Choose between available embedding models"
row={true}
>
<select
value={chatSettings.embeddingModel || ''}
className="input cdx-w-44"
onChange={(e) => {
setSettings({
...settings,
chat: {
...chatSettings,
embeddingModel: e.target.value,
},
})
}}
>
{models.map((model) => (
<option key={model.id} value={model.id}>
{model.id}
</option>
))}
</select>
</FieldWrapper>
Comment on lines +164 to +188
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical: Incorrect reuse of chat models for embeddings

The implementation has several issues:

  1. Chat models (like GPT-3.5, GPT-4) are different from embedding models (like text-embedding-ada-002)
  2. No validation to ensure selected model supports embeddings
  3. UI doesn't distinguish between chat and embedding models

This could lead to runtime errors if incompatible models are selected.

Consider maintaining a separate list of embedding models:

+const EMBEDDING_MODELS = [
+  { id: 'text-embedding-ada-002', name: 'Ada 002' },
+  // Add other supported embedding models
+]

 <FieldWrapper
   title="Embedding Model"
   description="Choose between available embedding models"
   row={true}
 >
   <select
     value={chatSettings.embeddingModel || ''}
     className="input cdx-w-44"
     onChange={(e) => {
       setSettings({
         ...settings,
         chat: {
           ...chatSettings,
           embeddingModel: e.target.value,
         },
       })
     }}
   >
-    {models.map((model) => (
+    {EMBEDDING_MODELS.map((model) => (
       <option key={model.id} value={model.id}>
-        {model.id}
+        {model.name}
       </option>
     ))}
   </select>
 </FieldWrapper>

Committable suggestion skipped: line range outside the PR's diff.

</div>
)
}
Expand Down
2 changes: 2 additions & 0 deletions src/config/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Settings = {
model: string | null
mode: Mode
openAiBaseUrl: string | null
embeddingModel: string | null
}
general: {
theme: ThemeOptions
Expand All @@ -42,6 +43,7 @@ export const defaultSettings: Settings = {
model: null,
mode: Mode.BALANCED,
openAiBaseUrl: null,
embeddingModel: null,
},
general: {
theme: ThemeOptions.SYSTEM,
Expand Down
4 changes: 4 additions & 0 deletions src/lib/getMatchedContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'
import { MemoryVectorStore } from 'langchain/vectorstores/memory'
import { createSHA256Hash } from './createSHA256Hash'
import { readStorage, setStorage } from '../hooks/useStorage'
import { useSettings } from '../hooks/useSettings'
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical: Invalid React hooks usage in non-React context

The useSettings hook is being used incorrectly:

  1. React hooks can only be used inside React components
  2. Hooks cannot be called inside regular functions like getContextVectorStore

This will cause runtime errors.

Consider refactoring to pass the embedding model as a parameter:

-import { useSettings } from '../hooks/useSettings'

 export const getMatchedContent = async (
   query: string,
   context: string,
   apiKey: string,
   baseURL: string,
+  embeddingModel?: string,
 ) => {
-  const vectorStore = await getContextVectorStore(context, apiKey, baseURL)
+  const vectorStore = await getContextVectorStore(context, apiKey, baseURL, embeddingModel)
   const retriever = vectorStore.asRetriever()
   const relevantDocs = await retriever.getRelevantDocuments(query)
   return relevantDocs.map((doc) => doc.pageContent).join('\n')
 }

 const getContextVectorStore = async (
   context: string,
   apiKey: string,
   baseURL: string,
+  embeddingModel?: string,
 ) => {
-  const [settings] = useSettings()
-  const embeddingModel = settings.chat.embeddingModel || 'text-embedding-ada-002'
+  const modelName = embeddingModel || 'text-embedding-ada-002'
   const embeddings = new OpenAIEmbeddings({
     openAIApiKey: apiKey,
-    modelName: embeddingModel,
+    modelName,
     configuration: {
       baseURL: baseURL,
     },
   })

Also applies to: 25-26, 29-29


export const getMatchedContent = async (
query: string,
Expand All @@ -21,8 +22,11 @@ const getContextVectorStore = async (
apiKey: string,
baseURL: string,
) => {
const [settings] = useSettings()
const embeddingModel = settings.chat.embeddingModel || 'text-embedding-ada-002'
const embeddings = new OpenAIEmbeddings({
openAIApiKey: apiKey,
modelName: embeddingModel,
configuration: {
baseURL: baseURL,
},
Expand Down
Loading