-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Markdown formatter for chatbot responses (#12)
* Feat: adding showdownjs markdown formatter * fix: prettier * feat: moving formatter to composable * refactor: method to get expected response format * fix: use responseFormat * Fix: lists not formatting as they should * feat: markdown recognizes lists now * fix: prettier * Feat: using regex formatter instead of showdownjs * fix: prettier + linter * fix: removing unused packages * fix: adding headers style * Feat: '\n' now become '<br />' and history style fix * fix: prettier * fix: changing break-spaces to break-words * chore: downsize titles sizes * chore: changing text-md to text-base * chore: removing unused dependencies --------- Co-authored-by: stefanorosanelli <[email protected]>
- Loading branch information
1 parent
454cf47
commit 52c6bc1
Showing
6 changed files
with
103 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
export const useResponseFormat = () => { | ||
const formatResponse = (textToFormat: string, format: string = 'text') => { | ||
if (format === 'markdown') { | ||
const formattedText = formatText(textToFormat); | ||
return formattedText; | ||
} | ||
|
||
return textToFormat; | ||
}; | ||
|
||
const llmResponseFormat = (llmConf: any) => { | ||
if (!llmConf || llmConf._type === 'openai-chat') { | ||
return 'markdown'; | ||
} | ||
|
||
return 'text'; | ||
}; | ||
|
||
const formatText = (textToFormat: string) => { | ||
//Regex for recognizing bold text ( **...** e __...__ ) | ||
const boldRegex = /\*\*(.+?)\*\*|__(.+?)__/g; | ||
|
||
//Regex for recognizing titles ( ###.... ) | ||
const titleRegex = /^(#{1,6})\s+(.+)$/gm; | ||
|
||
//Regex for code (`...`) | ||
const codeRegex = /`([^`]*)`/g; | ||
|
||
const lines = textToFormat.split('\n'); | ||
let fortmattedLines; | ||
fortmattedLines = lines.map((line) => { | ||
return line.replace(boldRegex, (_, boldType1, boldType2) => { | ||
const boldText = boldType1 || boldType2; | ||
return `<strong>${boldText}</strong>`; | ||
}); | ||
}); | ||
fortmattedLines = fortmattedLines.map((line) => { | ||
return line.replace(titleRegex, (_, hashes, titleText) => { | ||
const titleLevel = hashes.length; | ||
return `<h${titleLevel}>${titleText}</h${titleLevel}>`; | ||
}); | ||
}); | ||
fortmattedLines = fortmattedLines.map((line) => { | ||
return line.replace(codeRegex, (_, code) => { | ||
return `<code>${code}</code>`; | ||
}); | ||
}); | ||
return fortmattedLines.join('<br />'); | ||
}; | ||
|
||
return { | ||
llmResponseFormat, | ||
formatResponse, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters