Skip to content

Commit

Permalink
Fixes formatting and linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Royal-lobster committed Oct 21, 2024
1 parent 31168c4 commit 7b0a75f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 35 deletions.
15 changes: 9 additions & 6 deletions src/components/Settings/Sections/ChatSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@ const ChatSettings = () => {
) => {
event.preventDefault()
const target = event.target as HTMLFormElement

const apiKeyValue = target.openAiApiKey.value
const baseurlValue = target.openAiBaseUrl.value

if (OpenAiApiKeyInputRef.current) {
const isOpenAiKeyValid: boolean = await validateApiKey(apiKeyValue, baseurlValue)
const isOpenAiKeyValid: boolean = await validateApiKey(
apiKeyValue,
baseurlValue,
)
if (isOpenAiKeyValid) {
setSettings({
...settings,
chat: {
...chatSettings,
openAIKey: apiKeyValue,
openAiBaseUrl: baseurlValue ,
openAiBaseUrl: baseurlValue,
},
})
}
Expand Down Expand Up @@ -83,11 +86,11 @@ const ChatSettings = () => {
type={showPassword ? 'text' : 'password'}
className="input"
/>
<input
<input
ref={OpenAiBaseUrlInputRef}
name="openAiBaseUrl"
name="openAiBaseUrl"
defaultValue={chatSettings.openAiBaseUrl || ''}
placeholder="Enter your OpenAI Base URL"
placeholder="Enter your OpenAI Base URL"
className="cdx-mt-4 cdx-text-center cdx-p-2 cdx-w-full cdx-rounded-md cdx-border dark:cdx-border-neutral-600 cdx-border-neutral-200 dark:cdx-bg-neutral-800/90 cdx-bg-neutral-200/90 focus:cdx-outline-none focus:cdx-ring-2 focus:cdx-ring-blue-900 focus:cdx-ring-opacity-50 data-[error]:cdx-text-red-500"
/>

Expand Down
12 changes: 7 additions & 5 deletions src/components/Sidebar/auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ const Auth = () => {
e.preventDefault()
const data = new FormData(e.currentTarget)
const key = data.get('openAiKey') as string | null
const openAiBaseUrl = data.get('openAiBaseUrl') as string | "https://api.openai.com/v1"
const openAiBaseUrl = data.get('openAiBaseUrl') as
| string
| 'https://api.openai.com/v1'

if (key && (await validateApiKey(key,openAiBaseUrl))) {
if (key && (await validateApiKey(key, openAiBaseUrl))) {
setSettings((prev) => ({
...prev,
chat: {
Expand Down Expand Up @@ -62,9 +64,9 @@ const Auth = () => {
data-error={error ? 'true' : undefined}
className="cdx-mt-4 cdx-text-center cdx-p-2 cdx-w-full cdx-rounded-md cdx-border dark:cdx-border-neutral-600 cdx-border-neutral-200 dark:cdx-bg-neutral-800/90 cdx-bg-neutral-200/90 focus:cdx-outline-none focus:cdx-ring-2 focus:cdx-ring-blue-900 focus:cdx-ring-opacity-50 data-[error]:cdx-text-red-500"
/>
<input
name="openAiBaseUrl"
placeholder="Enter your OpenAI Base URL"
<input
name="openAiBaseUrl"
placeholder="Enter your OpenAI Base URL"
className="cdx-mt-4 cdx-text-center cdx-p-2 cdx-w-full cdx-rounded-md cdx-border dark:cdx-border-neutral-600 cdx-border-neutral-200 dark:cdx-bg-neutral-800/90 cdx-bg-neutral-200/90 focus:cdx-outline-none focus:cdx-ring-2 focus:cdx-ring-blue-900 focus:cdx-ring-opacity-50 data-[error]:cdx-text-red-500"
/>

Expand Down
1 change: 0 additions & 1 deletion src/components/Sidebar/chat/ChatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,3 @@ const ChatList = ({
}

export default ChatList

2 changes: 1 addition & 1 deletion src/components/Sidebar/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Chat = ({ settings }: ChatProps) => {
apiKey: settings.chat.openAIKey!,
mode: settings.chat.mode,
systemPrompt: SYSTEM_PROMPT,
baseURL: settings.chat.openAiBaseUrl || "",
baseURL: settings.chat.openAiBaseUrl || '',
})

useEffect(() => {
Expand Down
11 changes: 8 additions & 3 deletions src/hooks/useChatCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const useChatCompletion = ({
apiKey,
mode,
systemPrompt,
baseURL
baseURL,
}: UseChatCompletionProps) => {
const {
messages,
Expand All @@ -58,7 +58,7 @@ export const useChatCompletion = ({
openAIApiKey: apiKey,
modelName: model,
configuration: {
baseURL: baseURL,
baseURL: baseURL,
},
temperature: Number(mode),
maxTokens: 4_096,
Expand Down Expand Up @@ -97,7 +97,12 @@ export const useChatCompletion = ({
*/
let matchedContext: string | undefined
if (context) {
matchedContext = await getMatchedContent(message.text, context, apiKey, baseURL)
matchedContext = await getMatchedContent(
message.text,
context,
apiKey,
baseURL,
)
}

const expandedQuery = matchedContext
Expand Down
24 changes: 13 additions & 11 deletions src/lib/getMatchedContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export const getMatchedContent = async (
query: string,
context: string,
apiKey: string,
baseURL: string
baseURL: string,
) => {
const vectorStore = await getContextVectorStore(context, apiKey,baseURL)
const vectorStore = await getContextVectorStore(context, apiKey, baseURL)
const retriever = vectorStore.asRetriever()
const relevantDocs = await retriever.getRelevantDocuments(query)
return relevantDocs.map((doc) => doc.pageContent).join('\n')
Expand All @@ -24,15 +24,17 @@ export const getMatchedContent = async (
* from the context. It caches the vector store in the local storage
* for faster retrieval
*/
const getContextVectorStore = async (context: string, apiKey: string, baseURL: string) => {
const embeddings = new OpenAIEmbeddings(
{
openAIApiKey: apiKey ,
configuration: {
baseURL: baseURL,
},
}
)
const getContextVectorStore = async (
context: string,
apiKey: string,
baseURL: string,
) => {
const embeddings = new OpenAIEmbeddings({
openAIApiKey: apiKey,
configuration: {
baseURL: baseURL,
},
})
const hashKey = `SYNCIA_STORE_EMBEDDINGS_${await createSHA256Hash(context)}`
const memoryVectors: [] | null = JSON.parse(
localStorage.getItem(hashKey) || 'null',
Expand Down
8 changes: 4 additions & 4 deletions src/lib/getScreenshotImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ export const getScreenshotImage = async (): Promise<Blob> => {
})

// Crop the screenshot to the user's selection
let finalStartX = startX < endX ? startX : endX
let finalStaryY = startY < endY ? startY : endY
const finalStartX = startX < endX ? startX : endX
const finalStaryY = startY < endY ? startY : endY

ctx.drawImage(
image,
finalStartX * window.devicePixelRatio,
finalStaryY * window.devicePixelRatio,
(canvas.width) * window.devicePixelRatio,
(canvas.height) * window.devicePixelRatio,
canvas.width * window.devicePixelRatio,
canvas.height * window.devicePixelRatio,
0,
0,
canvas.width,
Expand Down
11 changes: 7 additions & 4 deletions src/lib/validApiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { ChatOpenAI } from '@langchain/openai'

export const validateApiKey = async (
openAIApiKey: string,
baseURL: string
baseURL: string,
): Promise<boolean> => {
const model = new ChatOpenAI({ openAIApiKey:openAIApiKey,configuration: {
baseURL: baseURL || "https://api.openai.com/v1",
},})
const model = new ChatOpenAI({
openAIApiKey: openAIApiKey,
configuration: {
baseURL: baseURL || 'https://api.openai.com/v1',
},
})
try {
await model.invoke([new HumanMessage('Say Ok')])
return true
Expand Down

0 comments on commit 7b0a75f

Please sign in to comment.