update ui

This commit is contained in:
Andriy 2023-05-22 15:03:51 -06:00
parent 775fe27e29
commit e2c8d5524d
3 changed files with 31 additions and 26 deletions

View File

@ -55,26 +55,27 @@ async function askOpenAI({
console.log("messages req: ", messages);
// updated the message content to include context snippets
// if (messages?.length > 0) {
// const lastMsgContent = messages[messages.length - 1].content;
if (messages?.length > 0) {
const lastMsgContent = messages[messages.length - 1].content;
// const data = await pinecone.similaritySearch(lastMsgContent, 2);
const data = await pinecone.similaritySearch(lastMsgContent, 3);
// console.log("pinecone data.length: ", data.length);
console.log("pinecone data.length: ", data.length);
// const updatedMsgContent = `
// user question/statement: ${lastMsgContent}
// context snippets:
// ---
// 1) ${data?.[0]?.pageContent}
// ---
// 2) ${data?.[1]?.pageContent}
// `;
const updatedMsgContent = `
user question/statement: ${lastMsgContent}
context snippets:
---
1) ${data?.[0]?.pageContent}
---
2) ${data?.[1]?.pageContent}
---
3) ${data?.[2]?.pageContent}
`;
// messages[messages.length - 1].content = updatedMsgContent;
// }
messages[messages.length - 1].content = updatedMsgContent;
}
console.log("messages length after pinecone: ", messages?.length);
try {
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0301",
@ -96,8 +97,6 @@ async function askOpenAI({
],
});
console.log("response: ", response);
return response?.data?.choices?.[0]?.message?.content;
} catch (e: any) {
console.log("error in response: ", e.message);

View File

@ -21,8 +21,10 @@ import NameInput from "@/components/NameInput";
import { Message } from "@/types";
function Home() {
// ref need to play audio
const audioRef = useRef<HTMLAudioElement | null>(null);
// storing array messages in state
const [messages, setMessages] = useState<Message[]>([]);
const addMessage = (message: Message) => {
@ -30,12 +32,13 @@ function Home() {
};
const toast = useToast();
const [text, setText] = useState("");
const [loading, setLoading] = useState(false);
// on the first translate, we need to get the user's name
// on subsequent translates, we can use the name from state
const translate = async (props?: { name?: string }) => {
// tracking user input
const [text, setText] = useState("");
// function to execute api request and communicate with open ai, pinecone & eleven labs
const askAi = async (props?: { name?: string }) => {
if (!text && !props?.name)
return toast({
title: "Enter text to translate first!",
@ -148,7 +151,7 @@ function Home() {
onEnter={(name) => {
startAudioForPermission();
setUserName(name);
translate({ name });
askAi({ name });
}}
/>
) : (
@ -186,7 +189,7 @@ function Home() {
value={text}
onChange={(e) => setText(e.target.value)}
onKeyDown={handleEnterKeyPress(() => {
translate();
askAi();
})}
/>
</VStack>
@ -196,7 +199,7 @@ function Home() {
h={9}
variant="outline"
onClick={() => {
translate();
askAi();
window.scrollTo({
left: 0,

View File

@ -27,22 +27,25 @@ dotenv.config();
apiKey: process.env.PINECONE_API_KEY as string,
environment: process.env.PINECONE_ENVIRONMENT as string,
});
// referencing index we want to upload to
const pineconeIndex = client.Index(process.env.PINECONE_INDEX as string);
// loading pdf
const loader = new PDFLoader("./scripts/navalPdf.pdf", {
splitPages: false,
});
const docs = await loader.load();
// splitting pdf into chunks
const splitter = new CharacterTextSplitter({
separator: "\n",
chunkSize: 2000,
chunkOverlap: 200,
});
const splitDocs = await splitter.splitDocuments(docs);
// uploading chunks to pinecone
await PineconeStore.fromDocuments(splitDocs, new OpenAIEmbeddings(), {
pineconeIndex,
});