实现了文本内嵌XML调用的识别
This commit is contained in:
parent
9fe13fa719
commit
af43d0d774
200
host/Gateway.cs
200
host/Gateway.cs
@ -229,6 +229,27 @@ public class Gateway
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static (string Matched, string Remaining) ExtractMatchedPart(string input, string toolPattern)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(toolPattern))
|
||||||
|
return (string.Empty, input);
|
||||||
|
|
||||||
|
Regex regex = new Regex(toolPattern);
|
||||||
|
Match match = regex.Match(input);
|
||||||
|
|
||||||
|
if (!match.Success)
|
||||||
|
return (string.Empty, input);
|
||||||
|
|
||||||
|
string matched = match.Value;
|
||||||
|
int startIndex = match.Index;
|
||||||
|
int length = match.Length;
|
||||||
|
|
||||||
|
// 构造剩余字符串
|
||||||
|
string remaining = input.Substring(0, startIndex) + input.Substring(startIndex + length);
|
||||||
|
|
||||||
|
return (matched, remaining);
|
||||||
|
}
|
||||||
|
|
||||||
public static async void SendMessageStream(string message, string model, string gdbPath, Action<MessageListItem> callback)
|
public static async void SendMessageStream(string message, string model, string gdbPath, Action<MessageListItem> callback)
|
||||||
{
|
{
|
||||||
Llm bailian = new Bailian
|
Llm bailian = new Bailian
|
||||||
@ -249,8 +270,8 @@ public class Gateway
|
|||||||
Content = message
|
Content = message
|
||||||
});
|
});
|
||||||
goOn = true;
|
goOn = true;
|
||||||
string toolPattern = "^<tool_use>[\\s\\S]*?<\\/tool_use>$";
|
string toolPattern = "<tool_use>([\\s\\S]*?)<name>([\\s\\S]*?)<\\/name>([\\s\\S]*?)<arguments>([\\s\\S]*?)<\\/arguments>([\\s\\S]*?)<\\/tool_use>";
|
||||||
string promptPattern = "^<prompt>[\\s\\S]*?<\\/prompt>$";
|
string promptPattern = "<prompt>([\\s\\S]*?)<name>([\\s\\S]*?)<\\/name>([\\s\\S]*?)<\\/prompt>";
|
||||||
McpServerList mcpServerList = new McpServerList();
|
McpServerList mcpServerList = new McpServerList();
|
||||||
int loop = 0;
|
int loop = 0;
|
||||||
while (goOn)
|
while (goOn)
|
||||||
@ -270,23 +291,80 @@ public class Gateway
|
|||||||
MaxTokens = 1000,
|
MaxTokens = 1000,
|
||||||
};
|
};
|
||||||
long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||||
string messageContent = "";
|
string messageContent = ""; //一次请求下完整的response
|
||||||
|
var (toolMatched, toolRemaining) = ExtractMatchedPart(messageContent, toolPattern);
|
||||||
|
var (promptMatched, promptRemaining) = ExtractMatchedPart(messageContent, promptPattern);
|
||||||
|
if (toolMatched == "" && promptMatched == "" && messageContent != "")
|
||||||
|
{
|
||||||
|
//如果本次回复不包含任何工具的调用或提示词的调用,则不再请求
|
||||||
|
break;
|
||||||
|
}
|
||||||
await foreach(var chunk in bailian.SendChatStreamAsync(jsonContent))
|
await foreach(var chunk in bailian.SendChatStreamAsync(jsonContent))
|
||||||
{
|
{
|
||||||
if (chunk == "[DONE]")
|
if (!goOn)
|
||||||
{
|
{
|
||||||
goOn = false;
|
break;
|
||||||
}else if (chunk.StartsWith("<tool_use>"))
|
}
|
||||||
|
var (matched, remaining) = ExtractMatchedPart(chunk, toolPattern);
|
||||||
|
if (matched == "")
|
||||||
{
|
{
|
||||||
if (Regex.IsMatch(chunk, toolPattern))
|
var (matchedPrompt, remainingPrompt) = ExtractMatchedPart(chunk, promptPattern);
|
||||||
|
if (matchedPrompt == "")
|
||||||
{
|
{
|
||||||
//返回工具卡片
|
//普通消息文本
|
||||||
|
MessageListItem chatMessageListItem = new ChatMessageItem()
|
||||||
|
{
|
||||||
|
content = remainingPrompt,
|
||||||
|
role = "assistant",
|
||||||
|
type = MessageType.CHAT_MESSAGE,
|
||||||
|
id = timestamp.ToString()
|
||||||
|
};
|
||||||
|
messageContent = remainingPrompt;
|
||||||
|
callback?.Invoke(chatMessageListItem);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//包含Prompt调用请求的消息
|
||||||
|
MessageListItem chatMessageListItem = new ChatMessageItem()
|
||||||
|
{
|
||||||
|
content = remainingPrompt,
|
||||||
|
role = "assistant",
|
||||||
|
type = MessageType.CHAT_MESSAGE,
|
||||||
|
id = timestamp.ToString()
|
||||||
|
};
|
||||||
|
callback?.Invoke(chatMessageListItem);
|
||||||
|
XElement promptUse = XElement.Parse(matchedPrompt);
|
||||||
|
string promptKey = promptUse.Element("name")?.Value;
|
||||||
|
string promptContent = DynamicPrompt.GetPrompt(promptKey,null);
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
Role = "assistant",
|
Role = "user",
|
||||||
Content = chunk
|
Content = JsonConvert.SerializeObject(promptContent)
|
||||||
});
|
});
|
||||||
XElement toolUse = XElement.Parse(chunk);
|
MessageListItem toolMessageItem = new ToolMessageItem
|
||||||
|
{
|
||||||
|
toolName = "调用提示词",
|
||||||
|
toolParams = null,
|
||||||
|
type = MessageType.TOOL_MESSAGE,
|
||||||
|
status = "success",
|
||||||
|
content = "成功调用提示词:"+promptKey,
|
||||||
|
id = (timestamp+1).ToString()
|
||||||
|
};
|
||||||
|
callback?.Invoke(toolMessageItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//包含工具调用请求的消息
|
||||||
|
MessageListItem chatMessageListItem = new ChatMessageItem()
|
||||||
|
{
|
||||||
|
content = remaining,
|
||||||
|
role = "assistant",
|
||||||
|
type = MessageType.CHAT_MESSAGE,
|
||||||
|
id = timestamp.ToString()
|
||||||
|
};
|
||||||
|
callback?.Invoke(chatMessageListItem);
|
||||||
|
XElement toolUse = XElement.Parse(matched);
|
||||||
string fullToolName = toolUse.Element("name")?.Value;
|
string fullToolName = toolUse.Element("name")?.Value;
|
||||||
string toolArgs = toolUse.Element("arguments")?.Value;
|
string toolArgs = toolUse.Element("arguments")?.Value;
|
||||||
Dictionary<string, object> toolParams = JsonConvert.DeserializeObject<Dictionary<string, object>>(toolArgs);
|
Dictionary<string, object> toolParams = JsonConvert.DeserializeObject<Dictionary<string, object>>(toolArgs);
|
||||||
@ -305,19 +383,13 @@ public class Gateway
|
|||||||
type = MessageType.TOOL_MESSAGE,
|
type = MessageType.TOOL_MESSAGE,
|
||||||
status = toolResponse.IsError ? "fail" : "success",
|
status = toolResponse.IsError ? "fail" : "success",
|
||||||
content = JsonConvert.SerializeObject(toolResponse),
|
content = JsonConvert.SerializeObject(toolResponse),
|
||||||
id = timestamp.ToString()
|
id = (timestamp+1).ToString()
|
||||||
};
|
};
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
Role = "user",
|
Role = "user",
|
||||||
// Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePromptTemplate
|
|
||||||
Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(toolResponse))
|
Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(toolResponse))
|
||||||
});
|
});
|
||||||
// messages.Add(new Message
|
|
||||||
// {
|
|
||||||
// Role = "user",
|
|
||||||
// Content = JsonConvert.SerializeObject(toolResponse)
|
|
||||||
// });
|
|
||||||
callback?.Invoke(toolMessageItem);
|
callback?.Invoke(toolMessageItem);
|
||||||
}else if (mcpServer is StdioMcpServer)
|
}else if (mcpServer is StdioMcpServer)
|
||||||
{
|
{
|
||||||
@ -331,19 +403,13 @@ public class Gateway
|
|||||||
type = MessageType.TOOL_MESSAGE,
|
type = MessageType.TOOL_MESSAGE,
|
||||||
status = toolResponse.IsError ? "fail" : "success",
|
status = toolResponse.IsError ? "fail" : "success",
|
||||||
content = JsonConvert.SerializeObject(toolResponse),
|
content = JsonConvert.SerializeObject(toolResponse),
|
||||||
id = timestamp.ToString()
|
id = (timestamp+1).ToString()
|
||||||
};
|
};
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
Role = "user",
|
Role = "user",
|
||||||
// Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePromptTemplate
|
|
||||||
Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(toolResponse))
|
Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(toolResponse))
|
||||||
});
|
});
|
||||||
// messages.Add(new Message
|
|
||||||
// {
|
|
||||||
// Role = "user",
|
|
||||||
// Content = JsonConvert.SerializeObject(toolResponse)
|
|
||||||
// });
|
|
||||||
callback?.Invoke(toolMessageItem);
|
callback?.Invoke(toolMessageItem);
|
||||||
}else if (mcpServer is InnerMcpServer)
|
}else if (mcpServer is InnerMcpServer)
|
||||||
{
|
{
|
||||||
@ -375,7 +441,7 @@ public class Gateway
|
|||||||
type = MessageType.TOOL_MESSAGE,
|
type = MessageType.TOOL_MESSAGE,
|
||||||
status = "fail",
|
status = "fail",
|
||||||
content = JsonConvert.SerializeObject(innerResult),
|
content = JsonConvert.SerializeObject(innerResult),
|
||||||
id = timestamp.ToString()
|
id = (timestamp+1).ToString()
|
||||||
};
|
};
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
@ -397,98 +463,18 @@ public class Gateway
|
|||||||
type = MessageType.TOOL_MESSAGE,
|
type = MessageType.TOOL_MESSAGE,
|
||||||
status = "success",
|
status = "success",
|
||||||
content = JsonConvert.SerializeObject(innerResult),
|
content = JsonConvert.SerializeObject(innerResult),
|
||||||
id = timestamp.ToString()
|
id = (timestamp+1).ToString()
|
||||||
};
|
};
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
Role = "user",
|
Role = "user",
|
||||||
// Content = SystemPrompt.ContinuePromptTemplate
|
|
||||||
Content = SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(innerResult))
|
Content = SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(innerResult))
|
||||||
});
|
});
|
||||||
// messages.Add(new Message
|
|
||||||
// {
|
|
||||||
// Role = "user",
|
|
||||||
// Content = JsonConvert.SerializeObject(innerResult)
|
|
||||||
// });
|
|
||||||
callback?.Invoke(toolMessageItem);
|
callback?.Invoke(toolMessageItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
MessageListItem toolMessageItem = new ToolMessageItem
|
|
||||||
{
|
|
||||||
toolName = "",
|
|
||||||
toolParams = new Dictionary<string, object>(),
|
|
||||||
type = MessageType.TOOL_MESSAGE,
|
|
||||||
status = "loading",
|
|
||||||
content = "正在生成工具调用参数",
|
|
||||||
id = timestamp.ToString()
|
|
||||||
};
|
|
||||||
callback?.Invoke(toolMessageItem);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}else if (chunk.StartsWith("<prompt>"))
|
|
||||||
{
|
|
||||||
if (Regex.IsMatch(chunk, promptPattern))
|
|
||||||
{
|
|
||||||
XElement promptUse = XElement.Parse(chunk);
|
|
||||||
string promptKey = promptUse.Element("name")?.Value;
|
|
||||||
string promptContent = DynamicPrompt.GetPrompt(promptKey,null);
|
|
||||||
messages.Add(new Message
|
|
||||||
{
|
|
||||||
Role = "user",
|
|
||||||
Content = JsonConvert.SerializeObject(promptContent)
|
|
||||||
});
|
|
||||||
MessageListItem toolMessageItem = new ToolMessageItem
|
|
||||||
{
|
|
||||||
toolName = "调用提示词",
|
|
||||||
toolParams = null,
|
|
||||||
type = MessageType.TOOL_MESSAGE,
|
|
||||||
status = "success",
|
|
||||||
content = promptKey,
|
|
||||||
id = timestamp.ToString()
|
|
||||||
};
|
|
||||||
callback?.Invoke(toolMessageItem);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MessageListItem toolMessageItem = new ToolMessageItem
|
|
||||||
{
|
|
||||||
toolName = "调用提示词",
|
|
||||||
toolParams = null,
|
|
||||||
type = MessageType.TOOL_MESSAGE,
|
|
||||||
status = "loading",
|
|
||||||
content = "正在调用提示词",
|
|
||||||
id = timestamp.ToString()
|
|
||||||
};
|
|
||||||
callback?.Invoke(toolMessageItem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
string content = chunk;
|
|
||||||
if (content.EndsWith("[DONE]"))
|
|
||||||
{
|
|
||||||
content = content.Substring(0, content.Length - 6);
|
|
||||||
}
|
|
||||||
//普通流式消息卡片
|
|
||||||
MessageListItem chatMessageListItem = new ChatMessageItem()
|
|
||||||
{
|
|
||||||
content = content,
|
|
||||||
role = "assistant",
|
|
||||||
type = MessageType.CHAT_MESSAGE,
|
|
||||||
id = timestamp.ToString()
|
|
||||||
};
|
|
||||||
messageContent = content;
|
|
||||||
callback?.Invoke(chatMessageListItem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
messages.Add(new Message
|
|
||||||
{
|
|
||||||
Role = "assistant",
|
|
||||||
Content = messageContent
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user