Skip to content

feat: add chat interaction UI#152

Merged
sergioramos merged 9 commits into
mainfrom
148-include-chat-in-ai-interaction-component
Jun 15, 2023
Merged

feat: add chat interaction UI#152
sergioramos merged 9 commits into
mainfrom
148-include-chat-in-ai-interaction-component

Conversation

@cguedes

@cguedes cguedes commented Jun 13, 2023

Copy link
Copy Markdown
Collaborator

This PR closes #148 by adding a UI for chat interaction.

Note that currently the backend don't support threads and history (will be fixed by #144) so this PR adds that as a UI state in order to experiment the interaction pattern.

image

@cguedes cguedes linked an issue Jun 13, 2023 that may be closed by this pull request
Comment thread src/api/rewrite.ts
Comment thread src/index.css
Comment thread tailwind.config.js
Comment thread src/panels/ai/ChatPanelSection.tsx
Comment thread src/panels/ai/SelectionPanelSection.tsx
@cguedes cguedes marked this pull request as ready for review June 13, 2023 15:36
@cguedes cguedes requested a review from sehyod June 13, 2023 15:36
@cguedes

cguedes commented Jun 13, 2023

Copy link
Copy Markdown
Collaborator Author

This is ready for review @sehyod

@sehyod sehyod left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR and for the work to add custom tailwind components!

Comment thread src/panels/ai/ChatPanelSection.tsx Outdated
Comment thread src/panels/ai/ChatPanelSection.tsx Outdated
@cguedes cguedes requested a review from sehyod June 14, 2023 08:30
Comment thread src/panels/ai/ChatPanelSection.tsx Outdated
@hammer

hammer commented Jun 14, 2023

Copy link
Copy Markdown
Contributor

Very exciting! We're getting close to the MLP.

I hit an error when I tried to chat before uploading any references:

Chat error: Error: Traceback (most recent call last): File "main.py", line 13, in <module> File "sidecar/chat.py", line 30, in ask_question File "sidecar/storage.py", line 14, in load FileNotFoundError: [Errno 2] No such file or directory: '/Users/gium/Library/Application Support/com.tauri.dev/project-x/.storage/references.json' [50699] Failed to execute script 'main' due to unhandled exception!

I was able to get this working with a PDF I uploaded though, very cool!
Screenshot 2023-06-14 at 10 53 32 AM

@cguedes

cguedes commented Jun 14, 2023

Copy link
Copy Markdown
Collaborator Author

That's the expected behaviour if you didn't uploaded PDFs. I forgot to mention that in the PR description.

@cguedes cguedes requested a review from sehyod June 14, 2023 09:42
sehyod
sehyod previously approved these changes Jun 14, 2023
@hammer

hammer commented Jun 14, 2023

Copy link
Copy Markdown
Contributor

That's the expected behaviour if you didn't uploaded PDFs.

Rather than the unfriendly error message can we say something more useful about needing to upload PDFs to chat? It's not clear to me that's even the right behavior. We should probably just mention to the user they can upload PDFs to improve the chat experience and pass through their chat to get a response.

@cguedes

cguedes commented Jun 14, 2023

Copy link
Copy Markdown
Collaborator Author

That's a good point Jeff. In the frontend we can't distinguish between errors in the backend. You can have this one but also an API Rate limit, or invalid API Key, ..

I think that we should add (reply error codes) to the #144 and then improve the UI on top of that.

@hammer

hammer commented Jun 14, 2023

Copy link
Copy Markdown
Contributor

In the frontend we can't distinguish between errors in the backend.

Ah, perhaps we should file a broader issue for better sharing of error state between the frontend and backend?

@cguedes cguedes requested a review from sehyod June 15, 2023 09:31
@sergioramos sergioramos changed the title feat: Adds chat interaction UI feat: add chat interaction UI Jun 15, 2023
@sergioramos sergioramos merged commit cd15dc7 into main Jun 15, 2023
@sergioramos sergioramos deleted the 148-include-chat-in-ai-interaction-component branch June 15, 2023 09:54

@danvk danvk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more comments. I got this to work in my local client.

Comment thread src/api/rewrite.ts

export async function askForRewrite(selection: string): Promise<string> {
try {
const response = await callSidecar('rewrite', ['--text', String(selection)]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The String() wrapper here isn't necessary (if the TS types are to be believed!).

// 'Hidden feedback loops in machine learning refer to situations where two systems indirectly influence each other through the world, leading to changes in behavior that may not be immediately visible. These loops may exist between completely disjoint systems and can make analyzing the effect of proposed changes extremely difficult, adding cost to even simple improvements. It is recommended to look carefully for hidden feedback loops and remove them whenever feasible.',
// },
]);
const [currentChatThreadItem, setCurrentChatThreadItem] = useState<ChatThreadItem | null>(null);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to move the thread state into atoms? Presumably the interactions will grow more elaborate in the future and we'll want it to persist even if you close the component. Alternatively the thread state could live in the backend.

]);
const [currentChatThreadItem, setCurrentChatThreadItem] = useState<ChatThreadItem | null>(null);

function handleKeyDown(evt: React.KeyboardEvent<HTMLTextAreaElement>): void {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: more concisely written as:

const handleKeydown: React.KeyboardEventHandler = (evt) => {

}
setCurrentChatThreadItem({ id: String(chatThread.length + 1), question: text });
setText('');
chatWithAI(question)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest using async / await.

setText('');
chatWithAI(question)
.then((reply) => {
setChatThread([...chatThread, { id: String(chatThread.length + 1), question: text, answer: reply[0] }]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be a stale closure issue here if you send two chats before the first one comes back. Safer to use the function form of setState:

setChatThread(prevChatThread => [...prevChatThread, { id: String(prevChatThread.length + 1), question: text, answer: reply[0] }]);

(also below)

expect(screen.getByText('Will this test pass?')).toBeInTheDocument();
expect(screen.getByText('...')).toBeInTheDocument();

resolveFn();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test!


describe('PrimarySideBar', () => {
it('should display Explorer and References icons', () => {
test('should display Explorer and References icons', () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I left a comment about this elsewhere, but when we write "should" in a test description we should use it rather than test to make it read more clearly (I pretty much always use it) https://stackoverflow.com/questions/45778192/what-is-the-difference-between-it-and-test-in-jest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Include chat in AI interaction component

5 participants