Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nx-dev): get the correct query for ai feedback #18961

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 9 additions & 20 deletions nx-dev/feature-ai/src/lib/feed-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { ErrorMessage } from './error-message';
import { Feed } from './feed/feed';
import { LoadingState } from './loading-state';
import { Prompt } from './prompt';
import { ChatItem, extractLinksFromSourcesSection } from '@nx/nx-dev/util-ai';
import { getQueryFromUid, storeQueryForUid } from '@nx/nx-dev/util-ai';
import { Message, useChat } from 'ai/react';

const assistantWelcome: ChatItem = {
const assistantWelcome: Message = {
id: 'first-custom-message',
role: 'assistant',
content:
"馃憢 Hi, I'm your Nx Assistant. With my ocean of knowledge about Nx, I can answer your questions and guide you to the relevant documentation. What would you like to know?",
Expand All @@ -16,7 +17,6 @@ const assistantWelcome: ChatItem = {
export function FeedContainer(): JSX.Element {
const [error, setError] = useState<Error | null>(null);
const [startedReply, setStartedReply] = useState(false);
const [sources, setSources] = useState<string[]>([]);

const feedContainer: RefObject<HTMLDivElement> | undefined = useRef(null);
const { messages, input, handleInputChange, handleSubmit, isLoading } =
Expand All @@ -34,8 +34,7 @@ export function FeedContainer(): JSX.Element {
},
onFinish: (response: Message) => {
setStartedReply(false);
setSources(extractLinksFromSourcesSection(response.content));
// Here we have the message id and the timestamp, so we can create a linked list
storeQueryForUid(response.id, input);
},
});

Expand All @@ -47,20 +46,10 @@ export function FeedContainer(): JSX.Element {
}
}, [messages, isLoading]);

const handleFeedback = (statement: 'good' | 'bad', chatItemIndex: number) => {
// TODO(katerina): Fix this - Read on
// This is wrong
// We have to make sure to send the query for the actual message that was clicked
// Here we are just sending the last one
const question = messages[chatItemIndex - 1];
const answer = messages[chatItemIndex];

const handleFeedback = (statement: 'good' | 'bad', chatItemUid: string) => {
const query = getQueryFromUid(chatItemUid);
sendCustomEvent('ai_feedback', 'ai', statement, undefined, {
query: question ? question.content : 'Could not retrieve the question',
result: answer ? answer.content : 'Could not retrieve the answer',
sources: sources
? JSON.stringify(sources)
: 'Could not retrieve last answer sources',
query: query ?? 'Could not retrieve the question',
});
};

Expand All @@ -86,8 +75,8 @@ export function FeedContainer(): JSX.Element {
>
<Feed
activity={!!messages.length ? messages : [assistantWelcome]}
handleFeedback={(statement, chatItemIndex) =>
handleFeedback(statement, chatItemIndex)
handleFeedback={(statement, chatItemUid) =>
handleFeedback(statement, chatItemUid)
}
/>

Expand Down
10 changes: 5 additions & 5 deletions nx-dev/feature-ai/src/lib/feed/feed.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { ChatItem } from '@nx/nx-dev/util-ai';
import { FeedAnswer } from './feed-answer';
import { FeedQuestion } from './feed-question';
import { Message } from 'ai/react';

export function Feed({
activity,
handleFeedback,
}: {
activity: ChatItem[];
handleFeedback: (statement: 'bad' | 'good', chatItemIndex: number) => void;
activity: Message[];
handleFeedback: (statement: 'bad' | 'good', chatItemUid: string) => void;
}) {
return (
<div className="flow-root my-12">
<ul role="list" className="-mb-8 space-y-12">
{activity.map((activityItem, activityItemIdx) => (
<li
key={[activityItem.role, activityItemIdx].join('-')}
key={[activityItem.role, activityItem.id].join('-')}
className="pt-12 relative flex items-start space-x-3 feed-item"
>
{activityItem.role === 'assistant' ? (
<FeedAnswer
content={activityItem.content}
feedbackButtonCallback={(statement) =>
handleFeedback(statement, activityItemIdx)
handleFeedback(statement, activityItem.id)
}
isFirst={activityItemIdx === 0}
/>
Expand Down
1 change: 1 addition & 0 deletions nx-dev/util-ai/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './lib/utils';
export * from './lib/history';
export * from './lib/constants';
export * from './lib/moderation';
export * from './lib/chat-utils';
19 changes: 0 additions & 19 deletions nx-dev/util-ai/src/lib/chat-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,6 @@ export function toMarkdownList(
return finalSections;
}

export function extractLinksFromSourcesSection(markdown: string): string[] {
const sectionRegex = /### Sources\n\n([\s\S]*?)(?:\n##|$)/;
const sectionMatch = sectionRegex.exec(markdown);

if (!sectionMatch) return [];

const sourcesSection = sectionMatch[1];

const linkRegex = /\]\((.*?)\)/g;
const links: string[] = [];
let match;

while ((match = linkRegex.exec(sourcesSection)) !== null) {
links.push(match[1]);
}

return links;
}

export function removeSourcesSection(markdown: string): string {
const sectionRegex = /### Sources\n\n([\s\S]*?)(?:\n###|$)/;
return markdown.replace(sectionRegex, '').trim();
Expand Down
13 changes: 13 additions & 0 deletions nx-dev/util-ai/src/lib/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const history: { [key: string]: string } = {};

export function storeQueryForUid(uid: string, query: string) {
history[uid] = query;
}

export function getQueryFromUid(uid: string) {
return history[uid];
}

export function getHistory() {
return history;
}