mirror of
https://github.com/open-webui/open-webui
synced 2025-06-26 18:26:48 +00:00
feat: autocompletion
This commit is contained in:
@@ -1037,6 +1037,12 @@ Only output a continuation. If you are unsure how to proceed, output nothing.
|
||||
<context>Search</context>
|
||||
<text>Best destinations for hiking in</text>
|
||||
**Output**: Europe, such as the Alps or the Scottish Highlands.
|
||||
|
||||
### Input:
|
||||
<context>{{CONTEXT}}</context>
|
||||
<text>
|
||||
{{PROMPT}}
|
||||
</text>
|
||||
"""
|
||||
|
||||
|
||||
|
||||
@@ -1991,7 +1991,6 @@ async def generate_queries(form_data: dict, user=Depends(get_verified_user)):
|
||||
|
||||
@app.post("/api/task/auto/completions")
|
||||
async def generate_autocompletion(form_data: dict, user=Depends(get_verified_user)):
|
||||
context = form_data.get("context")
|
||||
|
||||
model_list = await get_all_models()
|
||||
models = {model["id"]: model for model in model_list}
|
||||
@@ -2021,8 +2020,11 @@ async def generate_autocompletion(form_data: dict, user=Depends(get_verified_use
|
||||
else:
|
||||
template = DEFAULT_AUTOCOMPLETE_GENERATION_PROMPT_TEMPLATE
|
||||
|
||||
context = form_data.get("context")
|
||||
prompt = form_data.get("prompt")
|
||||
|
||||
content = autocomplete_generation_template(
|
||||
template, form_data["messages"], context, {"name": user.name}
|
||||
template, prompt, context, {"name": user.name}
|
||||
)
|
||||
|
||||
payload = {
|
||||
@@ -2036,6 +2038,8 @@ async def generate_autocompletion(form_data: dict, user=Depends(get_verified_use
|
||||
},
|
||||
}
|
||||
|
||||
print(payload)
|
||||
|
||||
# Handle pipeline filters
|
||||
try:
|
||||
payload = filter_pipeline(payload, user, models)
|
||||
|
||||
@@ -53,7 +53,9 @@ def prompt_template(
|
||||
|
||||
def replace_prompt_variable(template: str, prompt: str) -> str:
|
||||
def replacement_function(match):
|
||||
full_match = match.group(0)
|
||||
full_match = match.group(
|
||||
0
|
||||
).lower() # Normalize to lowercase for consistent handling
|
||||
start_length = match.group(1)
|
||||
end_length = match.group(2)
|
||||
middle_length = match.group(3)
|
||||
@@ -73,11 +75,9 @@ def replace_prompt_variable(template: str, prompt: str) -> str:
|
||||
return f"{start}...{end}"
|
||||
return ""
|
||||
|
||||
template = re.sub(
|
||||
r"{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}",
|
||||
replacement_function,
|
||||
template,
|
||||
)
|
||||
# Updated regex pattern to make it case-insensitive with the `(?i)` flag
|
||||
pattern = r"(?i){{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}"
|
||||
template = re.sub(pattern, replacement_function, template)
|
||||
return template
|
||||
|
||||
|
||||
@@ -214,15 +214,12 @@ def emoji_generation_template(
|
||||
|
||||
def autocomplete_generation_template(
|
||||
template: str,
|
||||
messages: list[dict],
|
||||
prompt: Optional[str] = None,
|
||||
context: Optional[str] = None,
|
||||
user: Optional[dict] = None,
|
||||
) -> str:
|
||||
prompt = get_last_user_message(messages)
|
||||
template = template.replace("{{CONTEXT}}", context if context else "")
|
||||
|
||||
template = replace_prompt_variable(template, prompt)
|
||||
template = replace_messages_variable(template, messages)
|
||||
|
||||
template = prompt_template(
|
||||
template,
|
||||
|
||||
Reference in New Issue
Block a user