Skip to content

Commit

Permalink
Merge branch 'main' into feat/upgrade-langchain
Browse files Browse the repository at this point in the history
  • Loading branch information
mayooear authored Mar 27, 2023
2 parents 10c66b0 + 55f58da commit 53f5ae6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ PINECONE_INDEX_NAME=

2. In `scripts/ingest-data.ts` replace `filePath` with `docs/{yourdocname}.pdf`

3. Run the script `npm run ingest` to 'ingest' and embed your docs. If you run into errors troubleshoot below.
3. Run the script `pnpm run ingest` to 'ingest' and embed your docs

4. Check Pinecone dashboard to verify your namespace and vectors have been added.

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "gpt4-langchain-pdf-chatbot",
"version": "0.1.0",
"private": true,
"engines": {
"node": ">=18"
},
"license": "MIT",
"author": "Mayooear<twitter:@mayowaoshin>",
"type": "module",
Expand Down
39 changes: 29 additions & 10 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState, useEffect, useMemo } from 'react';
import { useRef, useState, useEffect, useMemo, useCallback } from 'react';
import Layout from '@/components/layout';
import styles from '@/styles/Home.module.css';
import { Message } from '@/types/chat';
Expand All @@ -18,6 +18,7 @@ export default function Home() {
const [query, setQuery] = useState<string>('');
const [loading, setLoading] = useState<boolean>(false);
const [sourceDocs, setSourceDocs] = useState<Document[]>([]);
const [error, setError] = useState<string | null>(null);
const [messageState, setMessageState] = useState<{
messages: Message[];
pending?: string;
Expand Down Expand Up @@ -47,6 +48,8 @@ export default function Home() {
async function handleSubmit(e: any) {
e.preventDefault();

setError(null);

if (!query) {
alert('Please input a question');
return;
Expand Down Expand Up @@ -118,18 +121,22 @@ export default function Home() {
});
} catch (error) {
setLoading(false);
setError('An error occurred while fetching the data. Please try again.');
console.log('error', error);
}
}

//prevent empty submissions
const handleEnter = (e: any) => {
if (e.key === 'Enter' && query) {
handleSubmit(e);
} else if (e.key == 'Enter') {
e.preventDefault();
}
};
const handleEnter = useCallback(
(e: any) => {
if (e.key === 'Enter' && query) {
handleSubmit(e);
} else if (e.key == 'Enter') {
e.preventDefault();
}
},
[query],
);

const chatMessages = useMemo(() => {
return [
Expand All @@ -146,6 +153,13 @@ export default function Home() {
];
}, [messages, pending, pendingSourceDocs]);

//scroll to bottom of chat
useEffect(() => {
if (messageListRef.current) {
messageListRef.current.scrollTop = messageListRef.current.scrollHeight;
}
}, [chatMessages]);

return (
<>
<Layout>
Expand Down Expand Up @@ -209,7 +223,7 @@ export default function Home() {
className="flex-col"
>
{message.sourceDocs.map((doc, index) => (
<div key={`sourceDoc-${index}`}>
<div key={`messageSourceDocs-${index}`}>
<AccordionItem value={`item-${index}`}>
<AccordionTrigger>
<h3>Source {index + 1}</h3>
Expand Down Expand Up @@ -297,9 +311,14 @@ export default function Home() {
</form>
</div>
</div>
{error && (
<div className="border border-red-400 rounded-md p-4">
<p className="text-red-500">{error}</p>
</div>
)}
</main>
</div>
<footer className="m-auto">
<footer className="m-auto p-4">
<a href="https://twitter.com/mayowaoshin">
Powered by LangChainAI. Demo built by Mayo (Twitter: @mayowaoshin).
</a>
Expand Down
4 changes: 2 additions & 2 deletions styles/Home.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
flex-direction: column;
justify-content: space-between;
align-items: center;
padding: 2rem;
padding: 1rem;
}

.header {
Expand Down Expand Up @@ -184,7 +184,7 @@
justify-content: center;
align-items: center;
position: relative;
padding: 2rem 0;
padding: 1rem 0;
flex-direction: column;
}

Expand Down

0 comments on commit 53f5ae6

Please sign in to comment.