Skip to content

Commit

Permalink
Show tool calls
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Dec 17, 2024
1 parent 933b068 commit 9e7fb83
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
33 changes: 28 additions & 5 deletions src/ChatBlock/ChatMessageBubble.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import React from 'react';
import loadable from '@loadable/component';
import React from 'react';

import { Citation } from './Citation';
import { SourceDetails } from './Source';
import { transformEmailsToLinks, SVGIcon } from './utils';
import { SVGIcon, transformEmailsToLinks } from './utils';

import BotIcon from './../icons/bot.svg';
import UserIcon from './../icons/user.svg';

const CITATION_MATCH = /\[\d+\](?![[(\])])/gm;

const Markdown = loadable(() => import('react-markdown'));

const components = (message) => {
Expand Down Expand Up @@ -45,17 +48,33 @@ const components = (message) => {
};
};

const CITATION_MATCH = /\[\d+\](?![[(\])])/gm;

function addCitations(text) {
return text.replaceAll(CITATION_MATCH, (match) => {
const number = match.match(/\d+/)[0];
return `${match}(${number})`;
});
}

function ToolCall({ tool_args, tool_name, tool_result }) {
if (tool_name === 'run_search') {
return (
<div className="tool_info">
Searched for: <em>{tool_args?.query || ''}</em>
</div>
);
}
return null;
}

export function ChatMessageBubble(props) {
const { message, isLoading, isMostRecent, libs, onChoice } = props;
const {
message,
isLoading,
isMostRecent,
libs,
onChoice,
showToolCalls,
} = props;
const { remarkGfm } = libs; // , rehypePrism
const { citations = {}, documents, type } = message;
const isUser = type === 'user';
Expand Down Expand Up @@ -95,6 +114,10 @@ export function ChatMessageBubble(props) {
{icon}

<div>
{showToolCalls &&
message.toolCalls?.map((info, index) => (
<ToolCall key={index} {...info} />
))}
<Markdown
components={components(message)}
remarkPlugins={[remarkGfm]}
Expand Down
20 changes: 18 additions & 2 deletions src/ChatBlock/ChatWindow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ function ChatWindow({
isEditMode,
...data
}) {
const { height, qgenAsistantId, enableQgen, scrollToInput } = data;
const {
height,
qgenAsistantId,
enableQgen,
scrollToInput,
showToolCalls,
} = data;
const libs = { rehypePrism, remarkGfm }; // rehypePrism, remarkGfm
const { onSubmit, messages, isStreaming, clearChat } = useBackendChat({
persona,
Expand Down Expand Up @@ -62,7 +68,7 @@ function ChatWindow({
}, [messages]);

//eslint-disable-next-line
console.log(messages);
// console.log(messages);

useScrollonStream({
isStreaming,
Expand All @@ -73,6 +79,15 @@ function ChatWindow({
debounce: 100, // time for debouncing
});

// const timeoutRef = React.useRef();

// const debouncedSetInput = React.useCallback((e) => {
// if (timeoutRef.current) clearTimeout(timeoutRef.current);
// timeoutRef.current = setTimeout(() => {
// setInput(e.target.value);
// }, 1000);
// }, []);

return (
<div className="chat-window">
<div className="messages">
Expand Down Expand Up @@ -107,6 +122,7 @@ function ChatWindow({
onChoice={(message) => {
onSubmit({ message });
}}
showToolCalls={showToolCalls}
/>
))}
<div ref={endDivRef} /> {/* End div to mark the bottom */}
Expand Down
6 changes: 6 additions & 0 deletions src/ChatBlock/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function ChatBlockSchema({ assistants }) {
'height',
'enableQgen',
'scrollToInput',
'showToolCalls',
'showAssistantTitle',
'showAssistantDescription',
'showAssistantPrompts',
Expand All @@ -39,6 +40,11 @@ export function ChatBlockSchema({ assistants }) {
type: 'boolean',
default: false,
},
showToolCalls: {
title: 'Show query used in retriever',
type: 'boolean',
default: true,
},
placeholderPrompt: {
default: 'Ask a question',
title: 'Prompt',
Expand Down
5 changes: 5 additions & 0 deletions src/ChatBlock/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
i.icons {
font-size: 1em;
}

.tool_info {
font-size: small;
padding-bottom: 0.2em;
}
}

.sources {
Expand Down
2 changes: 2 additions & 0 deletions src/ChatBlock/useBackendChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ export function useBackendChat({ persona, qgenAsistantId, enableQgen }) {
setCurrChatSessionId(null);
};

// console.log('history', messageHistory);

return {
messages: messageHistory,
onSubmit: submitHandler.onSubmit,
Expand Down

0 comments on commit 9e7fb83

Please sign in to comment.