Skip to content

Commit

Permalink
Init chat with journal feature
Browse files Browse the repository at this point in the history
  • Loading branch information
UdaraJay committed Feb 3, 2024
1 parent ff2616c commit 1e3f09f
Show file tree
Hide file tree
Showing 35 changed files with 1,664 additions and 255 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@
"follow-redirects": "^1.15.5",
"framer-motion": "^10.12.18",
"gray-matter": "^4.0.3",
"llamaindex": "^0.0.35",
"llamaindex": "0.1.9",
"luxon": "^3.3.0",
"million": "^2.6.4",
"openai": "4.0.0-beta.6",
"openai": "^4.26.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.11.2",
Expand Down
2 changes: 1 addition & 1 deletion src/main/handlers/vectorIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ipcMain.handle('vectorindex-query', (event, text) =>
);

ipcMain.handle('vectorindex-reset-chat', (event) =>
pileVectorIndex.chat()
pileVectorIndex.resetChatEngine()
);

ipcMain.handle('vectorindex-chat', (event, text) =>
Expand Down
53 changes: 45 additions & 8 deletions src/main/utils/pileVectorIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ class PileVectorIndex {
async setServiceContext() {
this.serviceContext = serviceContextFromDefaults({
llm: new OpenAI({
model: 'gpt-4-0613',
model: 'gpt-4-0125-preview',
temperature: 0.85,
prompt: 'As a wise librarian of this humans journals, how would you respond to this inquiry',
prompt:
'As a wise librarian of this humans journals, how would you respond to this inquiry',
}),
});
}
Expand Down Expand Up @@ -114,7 +115,7 @@ class PileVectorIndex {

async initQueryEngine() {
const retriever = this.vectorIndex.asRetriever();
retriever.similarityTopK = 20;
retriever.similarityTopK = 30;

const nodePostprocessor = new SimilarityPostprocessor({
similarityCutoff: 0.7,
Expand All @@ -128,10 +129,29 @@ class PileVectorIndex {
);
}

customContextSystemPrompt({ context = '' }) {
// You can customize the prompt based on the context or other logic
const systemPrompt = `System: You are an AI within my personal journal. Answer you questions primarily based on the context provided, only stray away from it when you think it's helpful. You have access to the user's journal entries as context, the user is aware of this so you don't need to preface that to the user. Don't answer in lists all the time. Make your outputs aesthetically pleasing. You are like a wise librarian of my thoughts, providing advice and counsel. You try to keep responses consise and get to the point quickly. You address the user as 'you', you don't need to know their name. You should engage with the user like you're a human \nCurrent date and time: ${new Date().toISOString()} \nSome relevant past journal entries for context: ${context}\nResponse:`;

return systemPrompt;
}

async initChatEngine() {
const retriever = this.vectorIndex.asRetriever();
retriever.similarityTopK = 20;
this.chatEngine = new ContextChatEngine({ retriever });
retriever.similarityTopK = 30;
this.chatEngine = new ContextChatEngine({
retriever,
contextSystemPrompt: this.customContextSystemPrompt,
});
}

async resetChatEngine() {
const retriever = this.vectorIndex.asRetriever();
retriever.similarityTopK = 30;
this.chatEngine = new ContextChatEngine({
retriever,
contextSystemPrompt: this.customContextSystemPrompt,
});
}

// This takes a new entry and its parent entry if available as
Expand Down Expand Up @@ -243,10 +263,18 @@ class PileVectorIndex {
return;
}

const response = await this.queryEngine.query(text);
const response = await this.queryEngine.query({ query: text });
return response;
}

sanitize(text) {
if (!text) {
return " ";
}
return text;
}


async chat(text) {
if (!this.chatEngine) {
console.warn(
Expand All @@ -255,8 +283,17 @@ class PileVectorIndex {
return;
}

const response = await this.chatEngine.chat({message: text});
return response;
let message = '';
const stream = await this.chatEngine.chat({ message: this.sanitize(text), stream: true });

for await (const chunk of stream) {
const part = chunk?.response ?? ''
message = message.concat(part);
await this.sendMessageToRenderer('streamed_chat', part)
}

await this.sendMessageToRenderer('streamed_chat', '@@END@@')
return message;
}

async addDocument(thread) {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/context/AIContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const AIContextProvider = ({ children }) => {
const openaiInstance = new OpenAI({
baseURL: getBaseUrl(),
apiKey: key,
dangerouslyAllowBrowser: true
});

setAi(openaiInstance);
Expand Down
6 changes: 6 additions & 0 deletions src/renderer/context/IndexContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export const IndexContextProvider = ({ children }) => {
async (text) => window.electron.ipc.invoke('vectorindex-chat', text),
[currentPile]
);

const resetChat = useCallback(
async (text) => window.electron.ipc.invoke('vectorindex-reset-chat'),
[currentPile]
);

const getVectorIndex = useCallback(async () => {
const pilePath = getCurrentPilePath();
Expand All @@ -112,6 +117,7 @@ export const IndexContextProvider = ({ children }) => {
updateIndex,
query,
chat,
resetChat,
};

return (
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/context/PilesContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
import { useLocation } from 'react-router-dom';

export const availableThemes = {
light: { primary: '#4d80e6', secondary: '#fff' },
light: { primary: '#ddd', secondary: '#fff' },
blue: { primary: '#a4d5ff', secondary: '#fff' },
purple: { primary: '#d014e1', secondary: '#fff' },
yellow: { primary: '#ff9634', secondary: '#fff' },
green: { primary: '#22ff00', secondary: '#fff' },
Expand Down
19 changes: 19 additions & 0 deletions src/renderer/icons/img/ChatIcon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const ChatIcon = (props) => {
return (
<svg
{...props}
width="1600"
height="1600"
viewBox="0 0 1600 1600"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M440 182.4H1160C1318.4 182.4 1448 312 1448 470.4V934.4C1448 1092.8 1318.4 1222.4 1160 1222.4H1072L1019.2 1411.2C1017.6 1417.6 1014.4 1424 1011.2 1428.8C996.804 1451.2 971.199 1465.6 944.001 1465.6C920.001 1465.6 896.001 1454.4 881.6 1435.2L676.8 1222.4H440C281.599 1222.4 152 1092.8 152 934.4V470.4C152 311.999 281.6 182.4 440 182.4ZM833.004 430.969C872.831 607.344 897.604 632.109 1074 671.943C1089.17 675.405 1099.97 688.875 1099.97 704.473C1099.97 720.072 1089.17 733.535 1074 737.004C897.603 776.831 872.837 801.604 833.004 978.004C829.541 993.171 816.072 1003.97 800.473 1003.97C784.875 1003.97 771.411 993.171 767.943 978.004C728.103 801.603 703.329 776.837 526.969 737.004C511.766 733.541 501 720.072 501 704.473C501 688.875 511.772 675.411 526.969 671.943C703.344 632.103 728.109 607.329 767.943 430.969C771.405 415.766 784.875 405 800.473 405C816.072 405 829.535 415.772 833.004 430.969Z"
fill="currentcolor"
/>
</svg>
);
};
37 changes: 37 additions & 0 deletions src/renderer/icons/img/OpenBookIcon.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/renderer/icons/img/PersonIcon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const PersonIcon = (props) => {
return (
<svg
{...props}
viewBox="0 0 1110 1302"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M946.4 391.88C946.4 608.04 771.16 783.267 555 783.267C338.84 783.267 163.6 608.04 163.6 391.88C163.6 175.72 338.84 0.480469 555 0.480469C771.16 0.480469 946.4 175.72 946.4 391.88Z"
fill="currentcolor"
/>
<path
d="M555 1301.53C861.12 1301.53 1109.24 1196.25 1109.24 1124.45C1109.24 1008.46 966.64 841.547 764.333 767.467C702.339 802.18 630.96 822.051 554.987 822.051C479.028 822.051 407.667 802.176 345.64 767.467C143.373 841.519 0.773438 1008.45 0.773438 1124.45C0.773438 1196.25 248.92 1301.53 555.013 1301.53H555Z"
fill="currentcolor"
/>
</svg>
);
};
18 changes: 11 additions & 7 deletions src/renderer/icons/img/SearchIcon.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
export const SearchIcon = (props) => {
return (
<svg {...props} width="24" height="24" fill="none" viewBox="0 0 24 24">
<svg
{...props}
width="1600"
height="1600"
viewBox="0 0 1600 1600"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="1.5"
d="M19.25 19.25L15.5 15.5M4.75 11C4.75 7.54822 7.54822 4.75 11 4.75C14.4518 4.75 17.25 7.54822 17.25 11C17.25 14.4518 14.4518 17.25 11 17.25C7.54822 17.25 4.75 14.4518 4.75 11Z"
></path>
d="M1494.27 1305.73L1186.72 998.187C1248.17 904.041 1284.25 791.88 1284.25 671.307C1284.25 340.48 1015.1 71.3066 684.246 71.3066C353.393 71.3066 84.2461 340.48 84.2461 671.307C84.2461 1002.16 353.393 1271.31 684.246 1271.31C798.095 1271.31 904.246 1238.86 995.033 1183.58L1305.74 1494.29C1331.78 1520.33 1365.9 1533.35 1400.01 1533.35C1434.12 1533.35 1468.24 1520.33 1494.28 1494.29C1546.36 1442.21 1546.36 1357.84 1494.28 1305.74L1494.27 1305.73ZM684.239 1004.63C500.453 1004.63 350.906 855.08 350.906 671.293C350.906 487.507 500.453 337.96 684.239 337.96C868.026 337.96 1017.57 487.507 1017.57 671.293C1017.57 855.08 868.026 1004.63 684.239 1004.63Z"
fill="currentcolor"
/>
</svg>
);
};
3 changes: 3 additions & 0 deletions src/renderer/icons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ export * from './img/PileIcon';
export * from './img/ChainIcon';
export * from './img/WarningIcon';
export * from './img/ReflectIcon';
export * from './img/ChatIcon';
export * from './img/OpenBookIcon';
export * from './img/PersonIcon';
Loading

0 comments on commit 1e3f09f

Please sign in to comment.