Fix newlines in answers

This commit is contained in:
Weves
2024-01-17 02:05:26 -08:00
committed by Chris Weaver
parent fa879f7d7f
commit 3a8d89afd3
5 changed files with 30 additions and 6 deletions

View File

@@ -261,7 +261,7 @@ export function PersonaEditor({
{finalPrompt ? ( {finalPrompt ? (
<pre className="text-sm mt-2 whitespace-pre-wrap"> <pre className="text-sm mt-2 whitespace-pre-wrap">
{finalPrompt.replaceAll("\\n", "\n")} {finalPrompt}
</pre> </pre>
) : ( ) : (
"-" "-"

View File

@@ -113,7 +113,7 @@ export const AIMessage = ({
), ),
}} }}
> >
{content.replaceAll("\\n", "\n")} {content}
</ReactMarkdown> </ReactMarkdown>
) : ( ) : (
content content
@@ -236,7 +236,7 @@ export const HumanMessage = ({
), ),
}} }}
> >
{content.replaceAll("\\n", "\n")} {content}
</ReactMarkdown> </ReactMarkdown>
) : ( ) : (
content content

View File

@@ -2,6 +2,25 @@ import { Quote } from "@/lib/search/interfaces";
import { ResponseSection, StatusOptions } from "./ResponseSection"; import { ResponseSection, StatusOptions } from "./ResponseSection";
import ReactMarkdown from "react-markdown"; 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 { interface AnswerSectionProps {
answer: string | null; answer: string | null;
quotes: Quote[] | null; quotes: Quote[] | null;
@@ -41,7 +60,7 @@ const AnswerBody = ({ answer, error, isFetching }: AnswerSectionProps) => {
} else if (answer) { } else if (answer) {
return ( return (
<ReactMarkdown className="prose text-sm max-w-full"> <ReactMarkdown className="prose text-sm max-w-full">
{answer.replaceAll("\\n", "\n")} {replaceNewlines(answer)}
</ReactMarkdown> </ReactMarkdown>
); );
} else if (!isFetching) { } else if (!isFetching) {

View File

@@ -10,7 +10,7 @@ import {
SearchRequestArgs, SearchRequestArgs,
} from "./interfaces"; } from "./interfaces";
import { processRawChunkString } from "./streamingUtils"; import { processRawChunkString } from "./streamingUtils";
import { buildFilters } from "./utils"; import { buildFilters, endsWithLetterOrNumber } from "./utils";
export const searchRequestStreamed = async ({ export const searchRequestStreamed = async ({
query, query,
@@ -99,7 +99,8 @@ export const searchRequestStreamed = async ({
answer && answer &&
!answer.endsWith(".") && !answer.endsWith(".") &&
!answer.endsWith("?") && !answer.endsWith("?") &&
!answer.endsWith("!") !answer.endsWith("!") &&
endsWithLetterOrNumber(answer)
) { ) {
answer += "."; answer += ".";
updateCurrentAnswer(answer); updateCurrentAnswer(answer);

View File

@@ -18,3 +18,7 @@ export const buildFilters = (
return filters; return filters;
}; };
export function endsWithLetterOrNumber(str: string) {
return /[a-zA-Z0-9]$/.test(str);
}