浏览代码

refac: title generation

Timothy J. Baek 6 月之前
父节点
当前提交
e2d4a69750
共有 4 个文件被更改,包括 36 次插入26 次删除
  1. 5 3
      backend/open_webui/main.py
  2. 22 16
      backend/open_webui/utils/task.py
  3. 2 2
      src/lib/apis/index.ts
  4. 7 5
      src/lib/components/chat/Chat.svelte

+ 5 - 3
backend/open_webui/main.py

@@ -1472,7 +1472,7 @@ async def generate_title(form_data: dict, user=Depends(get_verified_user)):
     if app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE != "":
     if app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE != "":
         template = app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE
         template = app.state.config.TITLE_GENERATION_PROMPT_TEMPLATE
     else:
     else:
-        template = """Create a concise, 3-5 word title with an emoji as a title for the prompt in the given language. Suitable Emojis for the summary can be used to enhance understanding but avoid quotation marks or special formatting. RESPOND ONLY WITH THE TITLE TEXT.
+        template = """Create a concise, 3-5 word title with an emoji as a title for the chat history, in the given language. Suitable Emojis for the summary can be used to enhance understanding but avoid quotation marks or special formatting. RESPOND ONLY WITH THE TITLE TEXT.
 
 
 Examples of titles:
 Examples of titles:
 📉 Stock Market Trends
 📉 Stock Market Trends
@@ -1482,11 +1482,13 @@ Remote Work Productivity Tips
 Artificial Intelligence in Healthcare
 Artificial Intelligence in Healthcare
 🎮 Video Game Development Insights
 🎮 Video Game Development Insights
 
 
-Prompt: {{prompt:middletruncate:8000}}"""
+<chat_history>
+{{MESSAGES:END:2}}
+</chat_history>"""
 
 
     content = title_generation_template(
     content = title_generation_template(
         template,
         template,
-        form_data["prompt"],
+        form_data["messages"],
         {
         {
             "name": user.name,
             "name": user.name,
             "location": user.info.get("location") if user.info else None,
             "location": user.info.get("location") if user.info else None,

+ 22 - 16
backend/open_webui/utils/task.py

@@ -70,22 +70,6 @@ def replace_prompt_variable(template: str, prompt: str) -> str:
     return template
     return template
 
 
 
 
-def title_generation_template(
-    template: str, prompt: str, user: Optional[dict] = None
-) -> str:
-    template = replace_prompt_variable(template, prompt)
-    template = prompt_template(
-        template,
-        **(
-            {"user_name": user.get("name"), "user_location": user.get("location")}
-            if user
-            else {}
-        ),
-    )
-
-    return template
-
-
 def replace_messages_variable(template: str, messages: list[str]) -> str:
 def replace_messages_variable(template: str, messages: list[str]) -> str:
     def replacement_function(match):
     def replacement_function(match):
         full_match = match.group(0)
         full_match = match.group(0)
@@ -123,6 +107,28 @@ def replace_messages_variable(template: str, messages: list[str]) -> str:
     return template
     return template
 
 
 
 
+# {{prompt:middletruncate:8000}}
+
+
+def title_generation_template(
+    template: str, messages: list[dict], user: Optional[dict] = None
+) -> str:
+    prompt = get_last_user_message(messages)
+    template = replace_prompt_variable(template, prompt)
+    template = replace_messages_variable(template, messages)
+
+    template = prompt_template(
+        template,
+        **(
+            {"user_name": user.get("name"), "user_location": user.get("location")}
+            if user
+            else {}
+        ),
+    )
+
+    return template
+
+
 def tags_generation_template(
 def tags_generation_template(
     template: str, messages: list[dict], user: Optional[dict] = None
     template: str, messages: list[dict], user: Optional[dict] = None
 ) -> str:
 ) -> str:

+ 2 - 2
src/lib/apis/index.ts

@@ -208,7 +208,7 @@ export const updateTaskConfig = async (token: string, config: object) => {
 export const generateTitle = async (
 export const generateTitle = async (
 	token: string = '',
 	token: string = '',
 	model: string,
 	model: string,
-	prompt: string,
+	messages: string[],
 	chat_id?: string
 	chat_id?: string
 ) => {
 ) => {
 	let error = null;
 	let error = null;
@@ -222,7 +222,7 @@ export const generateTitle = async (
 		},
 		},
 		body: JSON.stringify({
 		body: JSON.stringify({
 			model: model,
 			model: model,
-			prompt: prompt,
+			messages: messages,
 			...(chat_id && { chat_id: chat_id })
 			...(chat_id && { chat_id: chat_id })
 		})
 		})
 	})
 	})

+ 7 - 5
src/lib/components/chat/Chat.svelte

@@ -1408,7 +1408,8 @@
 		const messages = createMessagesList(responseMessageId);
 		const messages = createMessagesList(responseMessageId);
 		if (messages.length == 2 && messages.at(-1).content !== '' && selectedModels[0] === model.id) {
 		if (messages.length == 2 && messages.at(-1).content !== '' && selectedModels[0] === model.id) {
 			window.history.replaceState(history.state, '', `/c/${_chatId}`);
 			window.history.replaceState(history.state, '', `/c/${_chatId}`);
-			const title = await generateChatTitle(userPrompt);
+
+			const title = await generateChatTitle(messages);
 			await setChatTitle(_chatId, title);
 			await setChatTitle(_chatId, title);
 
 
 			if ($settings?.autoTags ?? true) {
 			if ($settings?.autoTags ?? true) {
@@ -1726,7 +1727,8 @@
 		const messages = createMessagesList(responseMessageId);
 		const messages = createMessagesList(responseMessageId);
 		if (messages.length == 2 && selectedModels[0] === model.id) {
 		if (messages.length == 2 && selectedModels[0] === model.id) {
 			window.history.replaceState(history.state, '', `/c/${_chatId}`);
 			window.history.replaceState(history.state, '', `/c/${_chatId}`);
-			const title = await generateChatTitle(userPrompt);
+
+			const title = await generateChatTitle(messages);
 			await setChatTitle(_chatId, title);
 			await setChatTitle(_chatId, title);
 
 
 			if ($settings?.autoTags ?? true) {
 			if ($settings?.autoTags ?? true) {
@@ -1887,12 +1889,12 @@
 		}
 		}
 	};
 	};
 
 
-	const generateChatTitle = async (userPrompt) => {
+	const generateChatTitle = async (messages) => {
 		if ($settings?.title?.auto ?? true) {
 		if ($settings?.title?.auto ?? true) {
 			const title = await generateTitle(
 			const title = await generateTitle(
 				localStorage.token,
 				localStorage.token,
 				selectedModels[0],
 				selectedModels[0],
-				userPrompt,
+				messages,
 				$chatId
 				$chatId
 			).catch((error) => {
 			).catch((error) => {
 				console.error(error);
 				console.error(error);
@@ -1901,7 +1903,7 @@
 
 
 			return title;
 			return title;
 		} else {
 		} else {
-			return `${userPrompt}`;
+			return 'New Chat';
 		}
 		}
 	};
 	};