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

enhancement: create new thread with last chosen model #4524

Merged
merged 2 commits into from
Jan 27, 2025
Merged
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
14 changes: 8 additions & 6 deletions web/hooks/useCreateNewThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@

const { recommendedModel } = useRecommendedModel()

const selectedModel = useAtomValue(selectedModelAtom)

const requestCreateNewThread = async (
assistant: (ThreadAssistantInfo & { id: string; name: string }) | Assistant,
model?: Model | undefined
) => {
const defaultModel = model || recommendedModel
const defaultModel = model || selectedModel || recommendedModel

if (!model) {
// if we have model, which means user wants to create new thread from Model hub. Allow them.
Expand All @@ -82,23 +84,23 @@
// Default context length is 8192
const defaultContextLength = Math.min(
8192,
defaultModel?.settings.ctx_len ?? 8192
defaultModel?.settings?.ctx_len ?? 8192
)

const overriddenSettings = {
ctx_len: defaultModel?.settings.ctx_len
? Math.min(8192, defaultModel.settings.ctx_len)
ctx_len: defaultModel?.settings?.ctx_len
? Math.min(8192, defaultModel?.settings?.ctx_len)
: undefined,
}

// Use ctx length by default
const overriddenParameters = {
max_tokens: defaultContextLength
? Math.min(
defaultModel?.parameters.max_tokens ?? 8192,
defaultModel?.parameters?.max_tokens ?? 8192,
defaultContextLength
)
: defaultModel?.parameters.max_tokens,
: defaultModel?.parameters?.max_tokens,
}

const createdAt = Date.now()
Expand Down Expand Up @@ -134,19 +136,19 @@
// add the new thread on top of the thread list to the state
try {
const createdThread = await persistNewThread(thread, assistantInfo)
if (!createdThread) throw 'Thread created failed.'
createNewThread(createdThread)

Check warning on line 140 in web/hooks/useCreateNewThread.ts

View workflow job for this annotation

GitHub Actions / coverage-check

139-140 lines are not covered with tests

setSelectedModel(defaultModel)
setThreadModelParams(createdThread.id, {

Check warning on line 143 in web/hooks/useCreateNewThread.ts

View workflow job for this annotation

GitHub Actions / coverage-check

142-143 lines are not covered with tests
...defaultModel?.settings,
...defaultModel?.parameters,
...overriddenSettings,
})

// Delete the file upload state
setFileUpload(undefined)
setActiveThread(createdThread)

Check warning on line 151 in web/hooks/useCreateNewThread.ts

View workflow job for this annotation

GitHub Actions / coverage-check

150-151 lines are not covered with tests
} catch (ex) {
return toaster({
title: 'Thread created failed.',
Expand All @@ -157,7 +159,7 @@
}

const updateThreadExtension = (thread: Thread) => {
return extensionManager

Check warning on line 162 in web/hooks/useCreateNewThread.ts

View workflow job for this annotation

GitHub Actions / coverage-check

162 line is not covered with tests
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
?.modifyThread(thread)
}
Expand All @@ -166,7 +168,7 @@
threadId: string,
assistant: ThreadAssistantInfo
) => {
return extensionManager

Check warning on line 171 in web/hooks/useCreateNewThread.ts

View workflow job for this annotation

GitHub Actions / coverage-check

171 line is not covered with tests
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
?.modifyThreadAssistant(threadId, assistant)
}
Expand Down Expand Up @@ -203,13 +205,13 @@
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
?.createThread(thread)
.then(async (thread) => {
await extensionManager

Check warning on line 208 in web/hooks/useCreateNewThread.ts

View workflow job for this annotation

GitHub Actions / coverage-check

208 line is not covered with tests
.get<ConversationalExtension>(ExtensionTypeEnum.Conversational)
?.createThreadAssistant(thread.id, assistantInfo)
.catch(console.error)
return thread

Check warning on line 212 in web/hooks/useCreateNewThread.ts

View workflow job for this annotation

GitHub Actions / coverage-check

212 line is not covered with tests
})
.catch(() => undefined)

Check warning on line 214 in web/hooks/useCreateNewThread.ts

View workflow job for this annotation

GitHub Actions / coverage-check

214 line is not covered with tests
}

return {
Expand Down
Loading