refac
This commit is contained in:
1 parent
40c45ffe1f
commit
119fc21257
2 files changed
+56
-11
No files matched your search
@@ -2,6 +2,8 @@
|
|||||||
Built-in tools for Open WebUI.
|
Built-in tools for Open WebUI.
|
||||||
|
|
||||||
These tools are automatically available when native function calling is enabled.
|
These tools are automatically available when native function calling is enabled.
|
||||||
|
|
||||||
|
IMPORTANT: DO NOT IMPORT THIS MODULE DIRECTLY IN OTHER PARTS OF THE CODEBASE.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
@@ -22,9 +24,11 @@ from open_webui.routers.images import (
|
|||||||
)
|
)
|
||||||
from open_webui.routers.memories import (
|
from open_webui.routers.memories import (
|
||||||
query_memory,
|
query_memory,
|
||||||
add_memory,
|
add_memory as _add_memory,
|
||||||
|
update_memory_by_id,
|
||||||
QueryMemoryForm,
|
QueryMemoryForm,
|
||||||
AddMemoryForm,
|
AddMemoryForm,
|
||||||
|
MemoryUpdateModel,
|
||||||
)
|
)
|
||||||
from open_webui.models.notes import Notes
|
from open_webui.models.notes import Notes
|
||||||
from open_webui.models.chats import Chats
|
from open_webui.models.chats import Chats
|
||||||
@@ -297,7 +301,7 @@ async def edit_image(
|
|||||||
# =============================================================================
|
# =============================================================================
|
||||||
|
|
||||||
|
|
||||||
async def memory_query(
|
async def search_memories(
|
||||||
query: str,
|
query: str,
|
||||||
__request__: Request = None,
|
__request__: Request = None,
|
||||||
__user__: dict = None,
|
__user__: dict = None,
|
||||||
@@ -323,6 +327,9 @@ async def memory_query(
|
|||||||
if results and hasattr(results, "documents") and results.documents:
|
if results and hasattr(results, "documents") and results.documents:
|
||||||
memories = []
|
memories = []
|
||||||
for doc_idx, doc in enumerate(results.documents[0]):
|
for doc_idx, doc in enumerate(results.documents[0]):
|
||||||
|
memory_id = None
|
||||||
|
if results.ids and results.ids[0]:
|
||||||
|
memory_id = results.ids[0][doc_idx]
|
||||||
created_at = "Unknown"
|
created_at = "Unknown"
|
||||||
if results.metadatas and results.metadatas[0][doc_idx].get(
|
if results.metadatas and results.metadatas[0][doc_idx].get(
|
||||||
"created_at"
|
"created_at"
|
||||||
@@ -331,16 +338,16 @@ async def memory_query(
|
|||||||
"%Y-%m-%d",
|
"%Y-%m-%d",
|
||||||
time.localtime(results.metadatas[0][doc_idx]["created_at"]),
|
time.localtime(results.metadatas[0][doc_idx]["created_at"]),
|
||||||
)
|
)
|
||||||
memories.append({"date": created_at, "content": doc})
|
memories.append({"id": memory_id, "date": created_at, "content": doc})
|
||||||
return json.dumps(memories, ensure_ascii=False)
|
return json.dumps(memories, ensure_ascii=False)
|
||||||
else:
|
else:
|
||||||
return json.dumps([])
|
return json.dumps([])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception(f"memory_query error: {e}")
|
log.exception(f"search_memories error: {e}")
|
||||||
return json.dumps({"error": str(e)})
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
|
||||||
async def memory_add(
|
async def add_memory(
|
||||||
content: str,
|
content: str,
|
||||||
__request__: Request = None,
|
__request__: Request = None,
|
||||||
__user__: dict = None,
|
__user__: dict = None,
|
||||||
@@ -357,7 +364,7 @@ async def memory_add(
|
|||||||
try:
|
try:
|
||||||
user = UserModel(**__user__) if __user__ else None
|
user = UserModel(**__user__) if __user__ else None
|
||||||
|
|
||||||
memory = await add_memory(
|
memory = await _add_memory(
|
||||||
__request__,
|
__request__,
|
||||||
AddMemoryForm(content=content),
|
AddMemoryForm(content=content),
|
||||||
user,
|
user,
|
||||||
@@ -365,7 +372,42 @@ async def memory_add(
|
|||||||
|
|
||||||
return json.dumps({"status": "success", "id": memory.id}, ensure_ascii=False)
|
return json.dumps({"status": "success", "id": memory.id}, ensure_ascii=False)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception(f"memory_add error: {e}")
|
log.exception(f"add_memory error: {e}")
|
||||||
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
|
||||||
|
async def replace_memory_content(
|
||||||
|
memory_id: str,
|
||||||
|
content: str,
|
||||||
|
__request__: Request = None,
|
||||||
|
__user__: dict = None,
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
Update the content of an existing memory by its ID.
|
||||||
|
|
||||||
|
:param memory_id: The ID of the memory to update
|
||||||
|
:param content: The new content for the memory
|
||||||
|
:return: Confirmation that the memory was updated
|
||||||
|
"""
|
||||||
|
if __request__ is None:
|
||||||
|
return json.dumps({"error": "Request context not available"})
|
||||||
|
|
||||||
|
try:
|
||||||
|
user = UserModel(**__user__) if __user__ else None
|
||||||
|
|
||||||
|
memory = await update_memory_by_id(
|
||||||
|
memory_id=memory_id,
|
||||||
|
request=__request__,
|
||||||
|
form_data=MemoryUpdateModel(content=content),
|
||||||
|
user=user,
|
||||||
|
)
|
||||||
|
|
||||||
|
return json.dumps(
|
||||||
|
{"status": "success", "id": memory.id, "content": memory.content},
|
||||||
|
ensure_ascii=False,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
log.exception(f"replace_memory_content error: {e}")
|
||||||
return json.dumps({"error": str(e)})
|
return json.dumps({"error": str(e)})
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -48,8 +48,9 @@ from open_webui.tools.builtin import (
|
|||||||
fetch_url,
|
fetch_url,
|
||||||
generate_image,
|
generate_image,
|
||||||
edit_image,
|
edit_image,
|
||||||
memory_query,
|
search_memories,
|
||||||
memory_add,
|
add_memory,
|
||||||
|
replace_memory_content,
|
||||||
get_current_timestamp,
|
get_current_timestamp,
|
||||||
calculate_timestamp,
|
calculate_timestamp,
|
||||||
search_notes,
|
search_notes,
|
||||||
@@ -359,7 +360,7 @@ def get_builtin_tools(
|
|||||||
|
|
||||||
# Add memory tools if enabled for this chat
|
# Add memory tools if enabled for this chat
|
||||||
if features.get("memory"):
|
if features.get("memory"):
|
||||||
builtin_functions.extend([memory_query, memory_add])
|
builtin_functions.extend([search_memories, add_memory, replace_memory_content])
|
||||||
|
|
||||||
# Add web search tools if enabled globally AND for this chat
|
# Add web search tools if enabled globally AND for this chat
|
||||||
if getattr(request.app.state.config, "ENABLE_WEB_SEARCH", False) and features.get(
|
if getattr(request.app.state.config, "ENABLE_WEB_SEARCH", False) and features.get(
|
||||||
@@ -379,7 +380,9 @@ def get_builtin_tools(
|
|||||||
|
|
||||||
# Notes tools - search, view, create, and update user's notes (if notes enabled globally)
|
# Notes tools - search, view, create, and update user's notes (if notes enabled globally)
|
||||||
if getattr(request.app.state.config, "ENABLE_NOTES", False):
|
if getattr(request.app.state.config, "ENABLE_NOTES", False):
|
||||||
builtin_functions.extend([search_notes, view_note, write_note, replace_note_content])
|
builtin_functions.extend(
|
||||||
|
[search_notes, view_note, write_note, replace_note_content]
|
||||||
|
)
|
||||||
|
|
||||||
# Channels tools - search channels and messages (if channels enabled globally)
|
# Channels tools - search channels and messages (if channels enabled globally)
|
||||||
if getattr(request.app.state.config, "ENABLE_CHANNELS", False):
|
if getattr(request.app.state.config, "ENABLE_CHANNELS", False):
|
||||||
|
|||||||
Reference in new issue
Block a user