Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { z } from "zod";
import { exec } from "child_process";
import { promisify } from "util";
import { commandKeywords } from "@/data/keywords";
import { Message } from "@/app/components/ChatInterface";

const execAsync = promisify(exec);
const MESSAGE_CONTEXT_WINDOW = 4;
let ctx: number[] | undefined;

// Initialize Ollama client
const ollama = new Ollama({
Expand Down Expand Up @@ -117,7 +120,7 @@ const TextResponseSchema = {

export async function POST(request: NextRequest) {
try {
const { message, context } = await request.json();
const { message, context, history } = await request.json();

// Determine if this is a command request or general query
const isCommandRequest = commandKeywords.some((keyword) =>
Expand Down Expand Up @@ -178,11 +181,12 @@ Example response format:
options: {
temperature: 0.05,
},
context: ctx,
});

try {
const parsedResponse = JSON.parse(response.response);

ctx = response.context;
return NextResponse.json({
success: true,
data: parsedResponse,
Expand All @@ -208,13 +212,19 @@ Example response format:
Provide a helpful, detailed explanation about network security, firewall rules, or topology analysis. Be specific and technical when appropriate. Keep responses concise but informative.
Be friendly but still professional.
`;

const messagesToPass = [
{ role: "system", content: prompt },
...history
.slice(-MESSAGE_CONTEXT_WINDOW)
.map((msg: Message) => ({
role: msg.type,
content: msg.content,
})),
{ role: "user", content: message },
];
const response = await ollama.chat({
model: "qwen3:4b",
messages: [
{ role: "system", content: prompt },
{ role: "user", content: message },
],
messages: messagesToPass,
options: {
temperature: 0.3,
},
Expand Down
3 changes: 2 additions & 1 deletion app/components/ChatInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Bot, Loader2, Play, Send, Terminal, Trash2, User } from "lucide-react";
import React, { useRef, useState } from "react";
import ReactMarkdown from "react-markdown";

interface Message {
export interface Message {
id: string;
type: "user" | "assistant";
content: string;
Expand Down Expand Up @@ -72,6 +72,7 @@ export const ChatInterface: React.FC<ChatInterfaceProps> = ({
body: JSON.stringify({
message: currentInput,
context: networkContext,
history: messages,
}),
});

Expand Down