mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-04-08 20:08:36 +02:00
Fix newlines in answers
This commit is contained in:
parent
fa879f7d7f
commit
3a8d89afd3
@ -261,7 +261,7 @@ export function PersonaEditor({
|
||||
|
||||
{finalPrompt ? (
|
||||
<pre className="text-sm mt-2 whitespace-pre-wrap">
|
||||
{finalPrompt.replaceAll("\\n", "\n")}
|
||||
{finalPrompt}
|
||||
</pre>
|
||||
) : (
|
||||
"-"
|
||||
|
@ -113,7 +113,7 @@ export const AIMessage = ({
|
||||
),
|
||||
}}
|
||||
>
|
||||
{content.replaceAll("\\n", "\n")}
|
||||
{content}
|
||||
</ReactMarkdown>
|
||||
) : (
|
||||
content
|
||||
@ -236,7 +236,7 @@ export const HumanMessage = ({
|
||||
),
|
||||
}}
|
||||
>
|
||||
{content.replaceAll("\\n", "\n")}
|
||||
{content}
|
||||
</ReactMarkdown>
|
||||
) : (
|
||||
content
|
||||
|
@ -2,6 +2,25 @@ import { Quote } from "@/lib/search/interfaces";
|
||||
import { ResponseSection, StatusOptions } from "./ResponseSection";
|
||||
import ReactMarkdown from "react-markdown";
|
||||
|
||||
const TEMP_STRING = "__$%^TEMP$%^__";
|
||||
|
||||
function replaceNewlines(answer: string) {
|
||||
// Since the one-shot answer is a JSON, GPT adds extra backslashes to the
|
||||
// newlines. This function replaces the extra backslashes with the correct
|
||||
// number of backslashes so that ReactMarkdown can render the newlines
|
||||
|
||||
// Step 1: Replace '\\\\n' with a temporary placeholder
|
||||
answer = answer.replace(/\\\\n/g, TEMP_STRING);
|
||||
|
||||
// Step 2: Replace '\\n' with '\n'
|
||||
answer = answer.replace(/\\n/g, "\n");
|
||||
|
||||
// Step 3: Replace the temporary placeholder with '\\n'
|
||||
answer = answer.replace(TEMP_STRING, "\\n");
|
||||
|
||||
return answer;
|
||||
}
|
||||
|
||||
interface AnswerSectionProps {
|
||||
answer: string | null;
|
||||
quotes: Quote[] | null;
|
||||
@ -41,7 +60,7 @@ const AnswerBody = ({ answer, error, isFetching }: AnswerSectionProps) => {
|
||||
} else if (answer) {
|
||||
return (
|
||||
<ReactMarkdown className="prose text-sm max-w-full">
|
||||
{answer.replaceAll("\\n", "\n")}
|
||||
{replaceNewlines(answer)}
|
||||
</ReactMarkdown>
|
||||
);
|
||||
} else if (!isFetching) {
|
||||
|
@ -10,7 +10,7 @@ import {
|
||||
SearchRequestArgs,
|
||||
} from "./interfaces";
|
||||
import { processRawChunkString } from "./streamingUtils";
|
||||
import { buildFilters } from "./utils";
|
||||
import { buildFilters, endsWithLetterOrNumber } from "./utils";
|
||||
|
||||
export const searchRequestStreamed = async ({
|
||||
query,
|
||||
@ -99,7 +99,8 @@ export const searchRequestStreamed = async ({
|
||||
answer &&
|
||||
!answer.endsWith(".") &&
|
||||
!answer.endsWith("?") &&
|
||||
!answer.endsWith("!")
|
||||
!answer.endsWith("!") &&
|
||||
endsWithLetterOrNumber(answer)
|
||||
) {
|
||||
answer += ".";
|
||||
updateCurrentAnswer(answer);
|
||||
|
@ -18,3 +18,7 @@ export const buildFilters = (
|
||||
|
||||
return filters;
|
||||
};
|
||||
|
||||
export function endsWithLetterOrNumber(str: string) {
|
||||
return /[a-zA-Z0-9]$/.test(str);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user