Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/config/chatAPI-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const guidelines = {
UNRELATED_QUESTION: `If the question is not related to the context, say this phrase EXACTLY '${ERROR_MESSAGES.NO_ANSWER}'`,
LINKING: `DO NOT explicity mention the existence of the context provided, however, references can and should be made to the links provided in the context e.g '[0]'.`,
FOLLOW_UP_QUESTIONS: 'If you have an answer, generate four relevant follow up questions, do not include introductory text about follow-up questions. Each question must be in this format: `--{{ what problems did segwit solve }}--` in a new line.',
USED_SOURCES: `Lastly, list all sources relevant in generating the answer in a list in this format '__sources__: [LINK_INDICES_HERE]'`
USED_SOURCES: `Lastly, list all sources relevant in generating the answer in a list in this format '__sources__: [LINK_INDICES_HERE]'`,
FALLBACK_INSTRUCTION: `You are an AI assistant providing helpful answers to user queries. Answer the user's question directly and concisely without using any external context or search results. You must rely on your own internal knowledge to generate the response.`,
};

export const CONTEXT_WINDOW_MESSAGES = 6
Expand Down
28 changes: 15 additions & 13 deletions src/pages/api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export default async function handler(req: Request) {
const requesturl = req.url;
const reqBody = await req.json();

let esResults: any[] | null;
let esResults: any[] | null = null;
let userQuery;

const inputs = reqBody?.inputs;
const { query, author }: { query: string; author: string } = inputs;
const { query, author, fallback }: { query: string; author: string; fallback?: boolean } = inputs;

if (!query) {
return new Response(
Expand All @@ -66,20 +66,22 @@ export default async function handler(req: Request) {
const chatHistory = reqBody?.chatHistory ?? ([] as ChatHistory[]);

try {
const fetchUrl = getNewUrl(requesturl, "/search");
if (!fallback) {
const fetchUrl = getNewUrl(requesturl, "/search");

const gptKeywords = await GPTKeywordExtractor([...chatHistory]);
const gptKeywords = await GPTKeywordExtractor([...chatHistory]);

esResults = await internalFetch({url: fetchUrl, query, author, keywords: gptKeywords});
esResults = await internalFetch({ url: fetchUrl, query, author, keywords: gptKeywords });

// FOR logging purposes
const loggedResultsURLs = esResults?.map(result => result?._source.url)
console.log(`query: ${query}\n gptKeywords: ${gptKeywords} \n results: ${loggedResultsURLs}`)
// FOR logging purposes
const loggedResultsURLs = esResults?.map(result => result?._source.url)
console.log(`query: ${query}\n gptKeywords: ${gptKeywords} \n results: ${loggedResultsURLs}`)

if (!esResults || !esResults.length) {
const error = createReadableStream(ERROR_MESSAGES.NO_ANSWER);
console.log(error);
return new Response(error);
if (!esResults || !esResults.length) {
const error = createReadableStream(ERROR_MESSAGES.NO_ANSWER);
console.log(error);
return new Response(error);
}
}
} catch (error) {
console.log(error);
Expand All @@ -90,7 +92,7 @@ export default async function handler(req: Request) {
}

try {
const result = await processInput(esResults, query, chatHistory);
const result = await processInput(esResults ?? undefined, query, chatHistory, fallback);
return new Response(result);
} catch (error: any) {
const errMessage = error?.message
Expand Down
12 changes: 10 additions & 2 deletions src/service/chat/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import { separateLinksFromApiMessage } from "@/utils/links";
import { CONTEXT_WINDOW_MESSAGES, guidelines } from "@/config/chatAPI-config";
import { ChatHistory } from "@/types";

const buildSystemMessage = (question: string, context: string) => {
const buildSystemMessage = (question: string, context: string, fallback?: boolean) => {
const {
BASE_INSTRUCTION,
NO_ANSWER,
UNRELATED_QUESTION,
FOLLOW_UP_QUESTIONS,
LINKING,
FALLBACK_INSTRUCTION,
} = guidelines;

if (fallback) {
return `${FALLBACK_INSTRUCTION}`;
}

return `${BASE_INSTRUCTION}\n${NO_ANSWER}\n${UNRELATED_QUESTION}\n${context}\n${LINKING}\n${FOLLOW_UP_QUESTIONS}`;
};

Expand All @@ -19,13 +25,15 @@ export const buildChatMessages = ({
context,
oldContext,
messages,
fallback,
}: {
question: string;
context: string;
oldContext?: string;
messages: ChatHistory[];
fallback?: boolean;
}) => {
const systemMessage = buildSystemMessage(question, context);
const systemMessage = buildSystemMessage(question, context, fallback);
return [
{
role: "system",
Expand Down
Loading