Skip to content

Commit

Permalink
feat: create synopsis entity (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
634750802 authored Jun 7, 2024
1 parent 13e06c7 commit 996900d
Show file tree
Hide file tree
Showing 14 changed files with 610 additions and 27 deletions.
22 changes: 19 additions & 3 deletions src/app/(main)/(admin)/indexes/[id]/[part]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import dynamic from 'next/dynamic';
import { type ReactElement, Suspense } from 'react';

const GraphEditor = dynamic(() => import('@/components/graph/GraphEditor').then(m => m.GraphEditor), { ssr: false });
const GraphEntitiesTable = dynamic(() => import('@/components/graph/GraphEntitiesTable').then(m => m.GraphEntitiesTable), { ssr: false });

export default function Page ({ params }: PageProps<{ part: string }>) {
const index = useIndex();
Expand Down Expand Up @@ -39,13 +40,28 @@ export default function Page ({ params }: PageProps<{ part: string }>) {
</div>
);
} else if (index.config.provider === 'knowledge-graph') {
switch (params.part as 'graph-editor') {
let el: ReactElement | undefined;
switch (params.part as 'graph-editor' | 'graph-entities') {
case 'graph-editor':
return (
el = (
<Suspense fallback={<Skeleton className="block w-4 h-20 rounded" />}>
<GraphEditor index={index} />
</Suspense>
);
break;
case 'graph-entities':
el = (
<Suspense fallback={<Skeleton className="block w-4 h-20 rounded" />}>
<GraphEditor />
<GraphEntitiesTable index={index} />
</Suspense>
);
break;
}

return (
<div className="p-4">
{el}
</div>
);
}
}
8 changes: 6 additions & 2 deletions src/app/(main)/(admin)/indexes/[id]/nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const items: NavItem<keyof IndexConfig>[] = [
},
];

const graphItems: NavItem<'graph-editor'>[] = [
const graphItems: NavItem<'graph-editor' | 'graph-entities'>[] = [
{
title: 'General',
part: null,
Expand All @@ -73,4 +73,8 @@ const graphItems: NavItem<'graph-editor'>[] = [
title: 'Graph Editor',
part: 'graph-editor',
},
];
{
title: 'Graph Entities',
part: 'graph-entities',
},
];
31 changes: 31 additions & 0 deletions src/app/api/v1/indexes/[name]/entities/search/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getIndexByName } from '@/core/repositories/index_';
import { KnowledgeGraphClient } from '@/lib/knowledge-graph/client';
import { defineHandler } from '@/lib/next/handler';
import { notFound } from 'next/navigation';
import z from 'zod';

export const GET = defineHandler({
params: z.object({
name: z.string(),
}),
searchParams: z.object({
name: z.string(),
description: z.string(),
top_k: z.coerce.number().optional(),
}),
auth: 'admin',
}, async ({ params, searchParams }) => {
const index = await getIndexByName(params.name);

if (!index) {
notFound();
}

if (index.config.provider !== 'knowledge-graph') {
notFound();
}

return new KnowledgeGraphClient().searchEntity(searchParams);
});

export const dynamic = 'force-dynamic';
33 changes: 33 additions & 0 deletions src/app/api/v1/indexes/[name]/entities/synopsis/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getIndexByName } from '@/core/repositories/index_';
import { KnowledgeGraphClient } from '@/lib/knowledge-graph/client';
import { defineHandler } from '@/lib/next/handler';
import { notFound } from 'next/navigation';
import z from 'zod';

export const POST = defineHandler({
params: z.object({
name: z.string(),
}),
body: z.object({
name: z.string(),
description: z.string(),
topic: z.string(),
meta: z.object({}).passthrough(),
entities: z.number().array(),
}),
auth: 'admin',
}, async ({ params, body }) => {
const index = await getIndexByName(params.name);

if (!index) {
notFound();
}

if (index.config.provider !== 'knowledge-graph') {
notFound();
}

return new KnowledgeGraphClient().createSynopsisEntity(body);
});

export const dynamic = 'force-dynamic';
13 changes: 7 additions & 6 deletions src/components/graph/GraphEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Input } from '@/components/ui/input';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { useSearchParam } from '@/components/use-search-param';
import type { Index } from '@/core/repositories/index_';
import { getErrorMessage } from '@/lib/errors';
import { fetcher } from '@/lib/fetch';
import isHotkey from 'is-hotkey';
import { useEffect, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import useSWR from 'swr';

export function GraphEditor ({}: {}) {
export function GraphEditor ({ index }: { index: Index }) {
const [query, setQuery] = useSearchParam('query', 'sample-question:What is TiDB?');

const { data: span, isLoading, error } = useSWR(getFetchUrl(query), fetcher<ServerGraphData | { output: ServerGraphData }>, { revalidateOnFocus: false });
const { data: span, isLoading, error } = useSWR(getFetchUrl(index.name, query), fetcher<ServerGraphData | { output: ServerGraphData }>, { revalidateOnFocus: false });

const network = useNetwork(span);

Expand Down Expand Up @@ -122,7 +123,7 @@ function Editor ({ network, target, onTargetChange, onEnterSubgraph }: NetworkVi
</div>;
}

function getFetchUrl (query: string | null): ['get', string] | null {
function getFetchUrl (indexName: string, query: string | null): ['get', string] | null {
if (!query) {
return null;
}
Expand All @@ -136,11 +137,11 @@ function getFetchUrl (query: string | null): ['get', string] | null {
case 'trace':
return ['get', `/api/v1/traces/${parsedQuery[1]}/knowledge-graph-retrieval`];
case 'document':
return ['get', `/api/v1/indexes/graph/chunks/${encodeURIComponent(parsedQuery[1])}/subgraph`];
return ['get', `/api/v1/indexes/${indexName}/chunks/${encodeURIComponent(parsedQuery[1])}/subgraph`];
case 'entity':
return ['get', `/api/v1/indexes/graph/entities/${parsedQuery[1]}/subgraph`];
return ['get', `/api/v1/indexes/${indexName}/entities/${parsedQuery[1]}/subgraph`];
case 'sample-question':
return ['get', `/api/v1/indexes/graph/search/?query=${encodeURIComponent(parsedQuery[1])}`];
return ['get', `/api/v1/indexes/${indexName}/search/?query=${encodeURIComponent(parsedQuery[1])}`];
}

return null;
Expand Down
Loading

0 comments on commit 996900d

Please sign in to comment.