Skip to content

Commit

Permalink
Merge main into sweep/docstrings-addition
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Dec 14, 2023
2 parents 826e4da + a93da83 commit fb873fd
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/pages/api/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ const handler = async (req: Request): Promise<NextResponse> => {
const contexts_arr = messages[messages.length - 1]
?.contexts as ContextWithMetadata[]

console.log('CONTEXTS ARRAY: ', contexts_arr)
console.log('Type of contexts array: ', typeof contexts_arr);
console.log('Contexts array is derrived from: ', messages[messages.length - 1]?.contexts)

if (course_name == 'extreme' || course_name == 'zotero-extreme') {
console.log('CONTEXT STUFFING FOR /extreme and /zotero-extreme slugs')
promptToSend = await getExtremePrompt(course_name, search_query).catch(
Expand All @@ -83,29 +79,28 @@ const handler = async (req: Request): Promise<NextResponse> => {
// else if (course_name == 'global' || course_name == 'search-all') {
// todo
// }
else if (stream) {
// regular context stuffing
const stuffedPrompt = (await getStuffedPrompt(
course_name,
search_query,
contexts_arr,
token_limit,
)) as string
if (typeof messages[messages.length - 1]?.content === 'string') {
messages[messages.length - 1]!.content = stuffedPrompt;
} else if (Array.isArray(messages[messages.length - 1]?.content) &&
(messages[messages.length - 1]!.content as Content[]).every(item => 'type' in item)) {

const contentArray = messages[messages.length - 1]!.content as Content[];
const textContentIndex = contentArray.findIndex(item => item.type === 'text') || 0;

if (textContentIndex !== -1 && contentArray[textContentIndex]) {
// Replace existing text content with the new stuffed prompt
contentArray[textContentIndex] = { ...contentArray[textContentIndex], text: stuffedPrompt, type: 'text' };
} else {
// Add new stuffed prompt if no text content exists
contentArray.push({ type: 'text', text: stuffedPrompt });
}

// regular context stuffing
const stuffedPrompt = (await getStuffedPrompt(
course_name,
search_query,
contexts_arr,
token_limit,
)) as string
if (typeof messages[messages.length - 1]?.content === 'string') {
messages[messages.length - 1]!.content = stuffedPrompt;
} else if (Array.isArray(messages[messages.length - 1]?.content) &&
(messages[messages.length - 1]!.content as Content[]).every(item => 'type' in item)) {

const contentArray = messages[messages.length - 1]!.content as Content[];
const textContentIndex = contentArray.findIndex(item => item.type === 'text') || 0;

if (textContentIndex !== -1 && contentArray[textContentIndex]) {
// Replace existing text content with the new stuffed prompt
contentArray[textContentIndex] = { ...contentArray[textContentIndex], text: stuffedPrompt, type: 'text' };
} else {
// Add new stuffed prompt if no text content exists
contentArray.push({ type: 'text', text: stuffedPrompt });
}
}

Expand Down

0 comments on commit fb873fd

Please sign in to comment.