mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-11 20:05:19 +00:00
fix formatting
This commit is contained in:
parent
d10a71c298
commit
0422b5e29d
4 changed files with 20 additions and 16 deletions
|
|
@ -374,7 +374,9 @@ ENABLE_REALTIME_CHAT_SAVE = (
|
||||||
|
|
||||||
ENABLE_QUERIES_CACHE = os.environ.get("ENABLE_QUERIES_CACHE", "False").lower() == "true"
|
ENABLE_QUERIES_CACHE = os.environ.get("ENABLE_QUERIES_CACHE", "False").lower() == "true"
|
||||||
|
|
||||||
ENABLE_WRAP_TOOL_RESULT = os.environ.get("ENABLE_WRAP_TOOL_RESULT", "True").lower() == "true"
|
ENABLE_WRAP_TOOL_RESULT = (
|
||||||
|
os.environ.get("ENABLE_WRAP_TOOL_RESULT", "True").lower() == "true"
|
||||||
|
)
|
||||||
|
|
||||||
TOOL_RESULT_INDENT_SIZE = os.environ.get("TOOL_RESULT_INDENT_SIZE", 2)
|
TOOL_RESULT_INDENT_SIZE = os.environ.get("TOOL_RESULT_INDENT_SIZE", 2)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -286,10 +286,14 @@ def process_tool_result(
|
||||||
|
|
||||||
return tool_result, tool_result_files, tool_result_embeds
|
return tool_result, tool_result_files, tool_result_embeds
|
||||||
|
|
||||||
|
|
||||||
def dump_tool_result_to_json(model, ensure_ascii=True):
|
def dump_tool_result_to_json(model, ensure_ascii=True):
|
||||||
indent_size = None if TOOL_RESULT_INDENT_SIZE == 0 else TOOL_RESULT_INDENT_SIZE
|
indent_size = None if TOOL_RESULT_INDENT_SIZE == 0 else TOOL_RESULT_INDENT_SIZE
|
||||||
separators = None if indent_size and indent_size > 0 else (',', ':')
|
separators = None if indent_size and indent_size > 0 else (",", ":")
|
||||||
return json.dumps(model, indent=indent_size, separators=separators, ensure_ascii=ensure_ascii)
|
return json.dumps(
|
||||||
|
model, indent=indent_size, separators=separators, ensure_ascii=ensure_ascii
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def chat_completion_tools_handler(
|
async def chat_completion_tools_handler(
|
||||||
request: Request, body: dict, extra_params: dict, user: UserModel, models, tools
|
request: Request, body: dict, extra_params: dict, user: UserModel, models, tools
|
||||||
|
|
|
||||||
|
|
@ -1877,6 +1877,7 @@
|
||||||
for (const message of _messages) {
|
for (const message of _messages) {
|
||||||
let content = message?.merged?.content ?? message?.content;
|
let content = message?.merged?.content ?? message?.content;
|
||||||
let processedMessages = processDetailsAndExtractToolCalls(content ?? '');
|
let processedMessages = processDetailsAndExtractToolCalls(content ?? '');
|
||||||
|
|
||||||
let nonToolMesssage = null;
|
let nonToolMesssage = null;
|
||||||
let toolCallIndex = 0;
|
let toolCallIndex = 0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -862,14 +862,14 @@ const detailsAttributesRegex = /(\w+)="([^"]*)"/g;
|
||||||
|
|
||||||
export const processDetailsAndExtractToolCalls = (content) => {
|
export const processDetailsAndExtractToolCalls = (content) => {
|
||||||
content = removeDetails(content, ['reasoning', 'code_interpreter']);
|
content = removeDetails(content, ['reasoning', 'code_interpreter']);
|
||||||
|
|
||||||
// Split text and tool calls into messages array
|
// Split text and tool calls into messages array
|
||||||
let messages = [];
|
let messages = [];
|
||||||
const matches = content.match(toolCallsDetailsRegex);
|
const matches = content.match(toolCallsDetailsRegex);
|
||||||
if (matches && matches.length > 0) {
|
if (matches && matches.length > 0) {
|
||||||
let previousDetailsEndIndex = 0;
|
let previousDetailsEndIndex = 0;
|
||||||
for (const match of matches) {
|
for (const match of matches) {
|
||||||
|
|
||||||
let detailsStartIndex = content.indexOf(match, previousDetailsEndIndex);
|
let detailsStartIndex = content.indexOf(match, previousDetailsEndIndex);
|
||||||
let assistantMessage = content.substr(previousDetailsEndIndex, detailsStartIndex - previousDetailsEndIndex);
|
let assistantMessage = content.substr(previousDetailsEndIndex, detailsStartIndex - previousDetailsEndIndex);
|
||||||
previousDetailsEndIndex = detailsStartIndex + match.length;
|
previousDetailsEndIndex = detailsStartIndex + match.length;
|
||||||
|
|
@ -884,46 +884,43 @@ export const processDetailsAndExtractToolCalls = (content) => {
|
||||||
while ((attributeMatch = detailsAttributesRegex.exec(match)) !== null) {
|
while ((attributeMatch = detailsAttributesRegex.exec(match)) !== null) {
|
||||||
attributes[attributeMatch[1]] = attributeMatch[2];
|
attributes[attributeMatch[1]] = attributeMatch[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!attributes.id) {
|
if (!attributes.id) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let toolCall = {
|
let toolCall = {
|
||||||
id: attributes.id,
|
id: attributes.id,
|
||||||
name: attributes.name,
|
name: attributes.name,
|
||||||
arguments: unescapeHtml(attributes.arguments ?? ''),
|
arguments: unescapeHtml(attributes.arguments ?? ''),
|
||||||
result: unescapeHtml(attributes.result ?? '')
|
result: unescapeHtml(attributes.result ?? '')
|
||||||
}
|
}
|
||||||
|
|
||||||
toolCall.arguments = parseDoubleEncodedString(toolCall.arguments);
|
toolCall.arguments = parseDoubleEncodedString(toolCall.arguments);
|
||||||
toolCall.result = parseDoubleEncodedString(toolCall.result);
|
toolCall.result = parseDoubleEncodedString(toolCall.result);
|
||||||
|
|
||||||
messages.push(toolCall);
|
messages.push(toolCall);
|
||||||
}
|
}
|
||||||
|
|
||||||
let finalAssistantMessage = content.substr(previousDetailsEndIndex);
|
let finalAssistantMessage = content.substr(previousDetailsEndIndex);
|
||||||
finalAssistantMessage = finalAssistantMessage.trim('\n');
|
finalAssistantMessage = finalAssistantMessage.trim('\n');
|
||||||
if (finalAssistantMessage.length > 0) {
|
if (finalAssistantMessage.length > 0) {
|
||||||
messages.push(finalAssistantMessage);
|
messages.push(finalAssistantMessage);
|
||||||
}
|
}
|
||||||
}
|
} else if (content.length > 0) {
|
||||||
else if (content.length > 0) {
|
|
||||||
messages.push(content);
|
messages.push(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
return messages;
|
return messages;
|
||||||
};
|
};
|
||||||
|
|
||||||
function parseDoubleEncodedString(value) {
|
function parseDoubleEncodedString(value) {
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
let parsedValue = JSON.parse(value);
|
let parsedValue = JSON.parse(value);
|
||||||
if (typeof parsedValue == "string") {
|
if (typeof parsedValue == "string") {
|
||||||
return parsedValue;
|
return parsedValue;
|
||||||
}
|
}
|
||||||
}
|
} catch {}
|
||||||
catch {}
|
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue