diff --git a/.env.local.template b/.env.local.template deleted file mode 100644 index ab2504c..0000000 --- a/.env.local.template +++ /dev/null @@ -1,6 +0,0 @@ -NEXT_PUBLIC_MODE=UNLIMITED # Optional values LIMITED/UNLIMITED - -FALKORDB_URL=falkordb://localhost:6379 # FALKORDB_URL - -OPENAI_API_KEY=[API_KEY] # Set you openai api key here - diff --git a/app/api/chat/[graph]/route.ts b/app/api/chat/[graph]/route.ts new file mode 100644 index 0000000..d6ef73a --- /dev/null +++ b/app/api/chat/[graph]/route.ts @@ -0,0 +1,29 @@ +import { NextRequest, NextResponse } from "next/server" + + +export async function POST(request: NextRequest, { params }: { params: { graph: string } }) { + + const graphName = params.graph + const msg = request.nextUrl.searchParams.get('msg') + + try { + const result = await fetch(`${process.env.BEAKEND_URL}/chat`, { + method: 'POST', + body: JSON.stringify({ repo: graphName, msg }), + headers: { + "Authorization": process.env.SECRET_TOKEN!, + "Content-Type": 'application/json' + } + }) + + if (!result.ok) { + throw new Error(await result.text()) + } + + const json = await result.json() + + return NextResponse.json({ result: json }, { status: 200 }) + } catch (err) { + return NextResponse.json({ message: (err as Error).message }, { status: 400 }) + } +} \ No newline at end of file diff --git a/app/api/repo/[graph]/[node]/route.ts b/app/api/repo/[graph]/[node]/route.ts index 277e394..4b4f014 100644 --- a/app/api/repo/[graph]/[node]/route.ts +++ b/app/api/repo/[graph]/[node]/route.ts @@ -1,24 +1,55 @@ -import { FalkorDB, Graph } from "falkordb"; import { NextRequest, NextResponse } from "next/server"; export async function GET(request: NextRequest, { params }: { params: { graph: string, node: string } }) { - - const nodeId = parseInt(params.node); + + const nodeId = parseInt(params.node); + const graphId = params.graph; + try { + + const result = await fetch(`${process.env.BEAKEND_URL}/get_neighbors?repo=${graphId}&node_id=${nodeId}`, { + method: 'GET', + headers: { + "Authorization": process.env.SECRET_TOKEN!, + } + }) + + const json = await result.json() + + return NextResponse.json({ result: json }, { status: 200 }) + } catch (err) { + return NextResponse.json({ massage: (err as Error).message }, { status: 400 }) + } +} + +export async function POST(request: NextRequest, { params }: { params: { graph: string, node: string } }) { + + const nodeId = params.node; const graphId = params.graph; + const targetId = request.nextUrl.searchParams.get('targetId') + + try { - const db = await FalkorDB.connect({url: process.env.FALKORDB_URL || 'falkor://localhost:6379',}); - const graph = db.selectGraph(graphId); + const result = await fetch(`${process.env.BEAKEND_URL}/find_paths`, { + method: 'POST', + headers: { + "Authorization": process.env.SECRET_TOKEN!, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + repo: graphId, + src: Number(nodeId), + dest: Number(targetId!) + }) + }) - // Get node's neighbors - const q_params = {nodeId: nodeId}; - const query = `MATCH (src)-[e]-(n) - WHERE ID(src) = $nodeId - RETURN collect(distinct { label:labels(n)[0], id:ID(n), name: n.name } ) as nodes, - collect( { src: ID(startNode(e)), id: ID(e), dest: ID(endNode(e)), type: type(e) } ) as edges`; + if (!result.ok) { + throw new Error(await result.text()) + } - let res: any = await graph.query(query, { params: q_params }); - let nodes = res.data[0]['nodes']; - let edges = res.data[0]['edges']; + const json = await result.json() - return NextResponse.json({ id: graphId, nodes: nodes, edges: edges }, { status: 200 }) + return NextResponse.json({ result: json }, { status: 200 }) + } catch (err) { + return NextResponse.json({ massage: (err as Error).message }, { status: 200 }) + } } \ No newline at end of file diff --git a/app/api/repo/[graph]/route.ts b/app/api/repo/[graph]/route.ts index 1fe88b8..a4a2dd7 100644 --- a/app/api/repo/[graph]/route.ts +++ b/app/api/repo/[graph]/route.ts @@ -1,296 +1,256 @@ -import OpenAI from "openai"; -import { QUESTIONS } from '../questions'; -import { graphSchema } from "../graph_ops"; -import { FalkorDB, Graph } from 'falkordb'; -import { NextRequest, NextResponse } from "next/server"; -import { ChatCompletionCreateParams, ChatCompletionMessageParam, ChatCompletionMessageToolCall, ChatCompletionTool } from 'openai/resources/chat/completions.mjs'; +import { NextRequest, NextResponse } from "next/server" -// convert a structured graph schema into a string representation -// used in a model prompt -async function GraphSchemaToPrompt( - graph: Graph, - graphId: string, - db: FalkorDB -) { - // Retrieve graph schema - let schema: any = await graphSchema(graphId, db); - - // Build a string description of graph schema - let desc: string = "The knowladge graph schema is as follows:\n"; - - //------------------------------------------------------------------------- - // Describe labels - //------------------------------------------------------------------------- - - // list labels - desc = desc + "The graph contains the following node labels:\n"; - for (const lbl in schema["labels"]) { - desc = desc + `${lbl}\n`; - } - - // specify attributes associated with each label - for (const lbl in schema["labels"]) { - let node_count = schema["labels"][lbl]['node_count']; - let attributes = schema["labels"][lbl]['attributes']; - let attr_count = Object.keys(attributes).length; - - if (attr_count == 0) { - desc = desc + `the ${lbl} label has ${node_count} nodes and has no attributes\n`; - } else { - desc = desc + `the ${lbl} label has ${node_count} nodes and is associated with the following attribute(s):\n`; - for (const attr in attributes) { - let type = attributes[attr]['type']; - desc = desc + `'${attr}' which is of type ${type}\n`; - } - } - } - - desc = desc + "The graph contains the following relationship types:\n" - - //------------------------------------------------------------------------- - // Describe relationships - //------------------------------------------------------------------------- - - // list relations - for (const relation in schema["relations"]) { - desc = desc + `${relation}\n`; - } - - // specify attributes associated with each relationship - for (const relation in schema["relations"]) { - let connect = schema["relations"][relation]['connect']; - let edge_count = schema["relations"][relation]['edge_count']; - let attributes = schema["relations"][relation]['attributes']; - let attr_count = Object.keys(attributes).length; +export async function GET(request: NextRequest, { params }: { params: { graph: string } }) { - if (attr_count == 0) { - desc = desc + `the ${relation} relationship has ${edge_count} edges and has no attributes\n`; - } else { - desc = desc + `the ${relation} relationship has ${edge_count} edges and is associated with the following attribute(s):\n`; - for (const attr in attributes) { - let type = attributes[attr]['type']; - desc = desc + `'${attr}' which is of type ${type}\n`; - } - } + const graphName = params.graph - if (connect.length > 0) { - desc = desc + `the ${relation} relationship connects the following labels:\n` - for (let i = 0; i < connect.length; i += 2) { - let src = connect[i]; - let dest = connect[i + 1]; - desc = desc + `${src} is connected via ${relation} to ${dest}\n`; + try { + const result = await fetch(`${process.env.BEAKEND_URL}/graph_entities?repo=${graphName}`, { + method: 'GET', + headers: { + "Authorization": process.env.SECRET_TOKEN!, } - } - } - - desc = desc + `This is the end of the knowladge graph schema description.\n` - - //------------------------------------------------------------------------- - // include graph indices - //------------------------------------------------------------------------- - - // vector indices - let query = `CALL db.indexes() YIELD label, properties, types, entitytype`; - let res = await graph.query(query); - - // process indexes - let indexes: any = res.data; - if (indexes.length > 0) { - let index_prompt = "The knowladge graph contains the following indexes:\n" - for (let i = 0; i < indexes.length; i++) { - const index = indexes[i]; - const label: string = index['label']; - const entityType: string = index['entitytype']; - const props = index['properties']; - const types = index['types']; + }) - for (const prop of props) { - const propTypes: string[] = types[prop]; - for (let j = 0; j < propTypes.length; j++) { - const idxType: string = propTypes[j]; - index_prompt += `${entityType} of type ${label} have a ${idxType} index indexing its ${prop} attribute\n`; - } - } + if (!result.ok) { + throw new Error(await result.text()) } - index_prompt += `This is the end of our indexes list - To use a Vector index use the following procedure: - CALL db.idx.vector.queryNodes(