Skip to content

Commit e75915d

Browse files
authored
ChatMessage: Accept invalid JSON source references and process them properly (#23)
Some models generate invalid JSON without quotes around the values of the JSON string: - Make quotes optional in the RegEx to properly recognize those source refs as well. - Accept normal braces instead of curly braces.
1 parent f0daea9 commit e75915d

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

src/views/chatbot/ChatMessage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ function KnowledgeSourceRenderer({
188188
}: Readonly<KnowledgeSourceRendererProps>) {
189189
const renderContent = (content: string) => {
190190
// split by source references
191-
const parts = content.split(/(\{[^}]*})/g);
191+
const parts = content.split(/(\{[^}]*})|(\([^}]*\))/g);
192192

193193
return parts.map((part, index) => {
194194
// extract knowledge_id and embedding_id from source reference using named RegEx groups
195-
// the following RegEx allows optional spaces and optional quotes around keys
195+
// the following RegEx allows optional spaces and optional quotes around keys and values, as well as normal brackets instead of {}
196196
const match =
197-
/{ ?"?knowledge_id"?: ?"(?<kid>[a-fA-F0-9-]{36})", ?"?embedding_id"?: ?"(?<eid>[a-fA-F0-9-]{36})" ?}/.exec(
197+
/[{(] ?"?knowledge_id"?: ?"?(?<kid>[a-fA-F0-9-]{36})"?, ?"?embedding_id"?: ?"?(?<eid>[a-fA-F0-9-]{36})"? ?[)}]/.exec(
198198
part,
199199
);
200200
if (match?.groups?.kid && match.groups.eid) {

0 commit comments

Comments
 (0)