forked from openchatai/OpenChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openchatai#95 from openchatai/chore/apply-js-linters
chore: apply linters
- Loading branch information
Showing
12 changed files
with
112 additions
and
149 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
declare module 'pdf-parse/lib/pdf-parse.js' { | ||
import pdf from 'pdf-parse'; | ||
import pdf from 'pdf-parse'; | ||
|
||
export default pdf; | ||
export default pdf; | ||
} |
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 |
---|---|---|
@@ -1,55 +1,45 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { OpenAIEmbeddings } from 'langchain/embeddings/openai'; | ||
import { PineconeStore } from 'langchain/vectorstores/pinecone'; | ||
import { makeChain } from '@/utils/makechain'; | ||
import { pinecone } from '@/utils/pinecone-client'; | ||
import { PINECONE_INDEX_NAME, PINECONE_NAME_SPACE } from '@/config/pinecone'; | ||
import type {NextApiRequest, NextApiResponse} from 'next'; | ||
import {OpenAIEmbeddings} from 'langchain/embeddings/openai'; | ||
import {PineconeStore} from 'langchain/vectorstores/pinecone'; | ||
import {makeChain} from '@/utils/makechain'; | ||
import {pinecone} from '@/utils/pinecone-client'; | ||
import {PINECONE_INDEX_NAME, PINECONE_NAME_SPACE} from '@/config/pinecone'; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
) { | ||
const { question, history, namespace, mode, initial_prompt } = req.body; | ||
export default async function handler(req: NextApiRequest, res: NextApiResponse,) { | ||
const {question, history, namespace, mode, initial_prompt} = req.body; | ||
|
||
console.log('req.body', req.body); | ||
console.log({ question, history, namespace, mode, initial_prompt }); | ||
//only accept post requests | ||
if (req.method !== 'POST') { | ||
res.status(405).json({ error: 'Method not allowed' }); | ||
return; | ||
} | ||
console.log('req.body', req.body); | ||
console.log({question, history, namespace, mode, initial_prompt}); | ||
//only accept post requests | ||
if (req.method !== 'POST') { | ||
return res.status(405).json({error: 'Method not allowed'}); | ||
} | ||
|
||
if (!question) { | ||
return res.status(400).json({ message: 'No question in the request' }); | ||
} | ||
// OpenAI recommends replacing newlines with spaces for best results | ||
const sanitizedQuestion = question.trim().replaceAll('\n', ' '); | ||
if (!question) { | ||
return res.status(400).json({message: 'No question in the request'}); | ||
} | ||
// OpenAI recommends replacing newlines with spaces for best results | ||
const sanitizedQuestion = question.trim().replaceAll('\n', ' '); | ||
|
||
try { | ||
const index = pinecone.Index(PINECONE_INDEX_NAME); | ||
try { | ||
const index = pinecone.Index(PINECONE_INDEX_NAME); | ||
|
||
/* create vectorstore*/ | ||
const vectorStore = await PineconeStore.fromExistingIndex( | ||
new OpenAIEmbeddings({}), | ||
{ | ||
pineconeIndex: index, | ||
textKey: 'text', | ||
namespace: namespace, //namespace comes from your config folder | ||
}, | ||
); | ||
/* create vectorstore*/ | ||
const vectorStore = await PineconeStore.fromExistingIndex(new OpenAIEmbeddings({}), { | ||
pineconeIndex: index, textKey: 'text', namespace: namespace, //namespace comes from your config folder | ||
},); | ||
|
||
//create chain | ||
const chain = makeChain(vectorStore, mode, initial_prompt); | ||
//Ask a question using chat history | ||
const response = await chain.call({ | ||
question: sanitizedQuestion, | ||
chat_history: history || [], | ||
}); | ||
//create chain | ||
const chain = makeChain(vectorStore, mode, initial_prompt); | ||
//Ask a question using chat history | ||
const response = await chain.call({ | ||
question: sanitizedQuestion, chat_history: history || [], | ||
}); | ||
|
||
console.log('response', response); | ||
res.status(200).json(response); | ||
} catch (error: any) { | ||
console.log('error', error); | ||
res.status(500).json({ error: error.message || 'Something went wrong' }); | ||
} | ||
console.log('response', response); | ||
return res.status(200).json(response); | ||
} catch (error: any) { | ||
console.log('error', error); | ||
return res.status(500).json({error: error.message || 'Something went wrong'}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,5 @@ | ||
import { Document } from 'langchain/document'; | ||
import {Document} from 'langchain/document'; | ||
|
||
export type Message = { | ||
type: 'apiMessage' | 'userMessage'; | ||
message: string; | ||
isStreaming?: boolean; | ||
sourceDocs?: Document[]; | ||
type: 'apiMessage' | 'userMessage'; message: string; isStreaming?: boolean; sourceDocs?: Document[]; | ||
}; |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { ClassValue, clsx } from 'clsx'; | ||
import { twMerge } from 'tailwind-merge'; | ||
import {ClassValue, clsx} from 'clsx'; | ||
import {twMerge} from 'tailwind-merge'; | ||
|
||
export function cn(...inputs: ClassValue[]) { | ||
return twMerge(clsx(inputs)); | ||
return twMerge(clsx(inputs)); | ||
} |
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 |
---|---|---|
@@ -1,61 +1,49 @@ | ||
import { Document } from 'langchain/document'; | ||
import { readFile } from 'fs/promises'; | ||
import { BaseDocumentLoader } from 'langchain/document_loaders'; | ||
import {Document} from 'langchain/document'; | ||
import {readFile} from 'fs/promises'; | ||
import {BaseDocumentLoader} from 'langchain/document_loaders'; | ||
|
||
export abstract class BufferLoader extends BaseDocumentLoader { | ||
constructor(public filePathOrBlob: string | Blob) { | ||
super(); | ||
} | ||
|
||
protected abstract parse( | ||
raw: Buffer, | ||
metadata: Document['metadata'], | ||
): Promise<Document[]>; | ||
constructor(public filePathOrBlob: string | Blob) { | ||
super(); | ||
} | ||
|
||
public async load(): Promise<Document[]> { | ||
let buffer: Buffer; | ||
let metadata: Record<string, string>; | ||
if (typeof this.filePathOrBlob === 'string') { | ||
buffer = await readFile(this.filePathOrBlob); | ||
metadata = { source: this.filePathOrBlob }; | ||
} else { | ||
buffer = await this.filePathOrBlob | ||
.arrayBuffer() | ||
.then((ab) => Buffer.from(ab)); | ||
metadata = { source: 'blob', blobType: this.filePathOrBlob.type }; | ||
public async load(): Promise<Document[]> { | ||
let buffer: Buffer; | ||
let metadata: Record<string, string>; | ||
if (typeof this.filePathOrBlob === 'string') { | ||
buffer = await readFile(this.filePathOrBlob); | ||
metadata = {source: this.filePathOrBlob}; | ||
} else { | ||
buffer = await this.filePathOrBlob | ||
.arrayBuffer() | ||
.then((ab) => Buffer.from(ab)); | ||
metadata = {source: 'blob', blobType: this.filePathOrBlob.type}; | ||
} | ||
return this.parse(buffer, metadata); | ||
} | ||
return this.parse(buffer, metadata); | ||
} | ||
|
||
protected abstract parse(raw: Buffer, metadata: Document['metadata'],): Promise<Document[]>; | ||
} | ||
|
||
export class CustomPDFLoader extends BufferLoader { | ||
public async parse( | ||
raw: Buffer, | ||
metadata: Document['metadata'], | ||
): Promise<Document[]> { | ||
const { pdf } = await PDFLoaderImports(); | ||
const parsed = await pdf(raw); | ||
return [ | ||
new Document({ | ||
pageContent: parsed.text, | ||
metadata: { | ||
...metadata, | ||
pdf_numpages: parsed.numpages, | ||
}, | ||
}), | ||
]; | ||
} | ||
public async parse(raw: Buffer, metadata: Document['metadata'],): Promise<Document[]> { | ||
const {pdf} = await PDFLoaderImports(); | ||
const parsed = await pdf(raw); | ||
return [new Document({ | ||
pageContent: parsed.text, metadata: { | ||
...metadata, pdf_numpages: parsed.numpages, | ||
}, | ||
}),]; | ||
} | ||
} | ||
|
||
async function PDFLoaderImports() { | ||
try { | ||
// the main entrypoint has some debug code that we don't want to import | ||
const { default: pdf } = await import('pdf-parse/lib/pdf-parse.js'); | ||
return { pdf }; | ||
} catch (e) { | ||
console.error(e); | ||
throw new Error( | ||
'Failed to load pdf-parse. Please install it with eg. `npm install pdf-parse`.', | ||
); | ||
} | ||
try { | ||
// the main entrypoint has some debug code that we don't want to import | ||
const {default: pdf} = await import('pdf-parse/lib/pdf-parse.js'); | ||
return {pdf}; | ||
} catch (e) { | ||
console.error(e); | ||
throw new Error('Failed to load pdf-parse. Please install it with eg. `npm install pdf-parse`.',); | ||
} | ||
} |
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
Oops, something went wrong.