Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

Add ollama integration and streaming response

Khuram Niaz635f27cb92ce
diff --git a/.env.example b/.env.example
index 8444df8..9d5eac2 100644
--- a/.env.example
+++ b/.env.example
@@ -9,4 +9,5 @@ AUTH_SECRET=your_generated_secret
NEXTAUTH_URL='http://localhost:3000'
AUTH_TRUST_HOST=http://localhost:3000
-OPENAI_API_KEY=your-api-key
\ No newline at end of file
+OPENAI_API_KEY=your-api-key
+OLLAMA_BASE_URL=http://localhost:11434
diff --git a/.vscode/launch.json b/.vscode/launch.json
index 7003b54..d096a34 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -1,28 +1,29 @@
{
- "version": "0.2.0",
- "configurations": [
- {
- "name": "Next.js: debug server-side",
- "type": "node-terminal",
- "request": "launch",
- "command": "npm run dev"
+ "version": "0.2.0",
+ "configurations": [
+ {
+ "name": "Next.js: debug server-side",
+ "type": "node-terminal",
+ "request": "launch",
+ "command": "npm run dev"
+ },
+ {
+ "name": "Next.js: debug client-side",
+ "type": "chrome",
+ "request": "launch",
+ "url": "http://localhost:3000/dashboard"
+ },
+ {
+ "name": "Next.js: debug full stack",
+ "type": "node-terminal",
+ "request": "launch",
+ "command": "npm run dev",
+ "serverReadyAction": {
+ "pattern": "- Local:.+(https?://.+)",
+ "uriFormat": "%s",
+ "action": "debugWithChrome"
},
- {
- "name": "Next.js: debug client-side",
- "type": "chrome",
- "request": "launch",
- "url": "http://localhost:3000/dashboard"
- },
- {
- "name": "Next.js: debug full stack",
- "type": "node-terminal",
- "request": "launch",
- "command": "npm run dev",
- "serverReadyAction": {
- "pattern": "- Local:.+(https?://.+)",
- "uriFormat": "%s",
- "action": "debugWithChrome"
- }
- }
- ]
- }
\ No newline at end of file
+ "resolveSourceMapLocations": ["${workspaceFolder}/", "!/node_modules/**"]
+ }
+ ]
+}
diff --git a/src/actions/ai.actions.ts b/src/actions/ai.actions.ts
index a7ebd0e..f593196 100644
--- a/src/actions/ai.actions.ts
+++ b/src/actions/ai.actions.ts
@@ -6,8 +6,6 @@ import { ChatOllama } from "@langchain/community/chat_models/ollama";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts";
-import { handleError } from "@/lib/utils";
-
export const getResumeReviewByAi = async (
modelProvider?: string
): Promise<any | undefined> => {
@@ -21,32 +19,34 @@ export const getResumeReviewByAi = async (
// new HumanMessage("hi!"),
// ];
- const prompt = ChatPromptTemplate.fromMessages([
- [
- "system",
- `You are an expert translator. Format all responses as JSON objects with two keys: "original" and "translated".`,
- ],
- ["human", `Translate "{input}" into {language}.`],
- ]);
+ // const prompt = ChatPromptTemplate.fromMessages([
+ // [
+ // "system",
+ // `You are an expert resume writer. Format all responses as JSON objects.`,
+ // ],
+ // ["human", `Translate "{input}" into {language}.`],
+ // ]);
const model = new ChatOllama({
- baseUrl: "http://localhost:11434",
+ baseUrl: process.env.OLLAMA_BASE_URL || "http://localhost:11434",
model: "llama3",
- format: "json",
+ // format: "json",
});
- try {
- // const response = await model.invoke(messages);
-
- const chain = prompt.pipe(model);
-
- const response = await chain.invoke({
- input: "I love programming",
- language: "German",
- });
- return { response, success: true };
- } catch (error) {
- const msg = "Failed to review resume.";
- return handleError(error, msg);
- }
+ const stream = await model
+ .pipe(new StringOutputParser())
+ .stream(
+ `Write a resume summary of 100 words at most for a software engineer, focusing on experience with Python and JavaScript.`
+ );
+
+ const encoder = new TextEncoder();
+
+ return new ReadableStream({
+ async start(controller) {
+ for await (const chunk of stream) {
+ controller.enqueue(encoder.encode(chunk));
+ }
+ controller.close();
+ },
+ });
};
diff --git a/src/app/api/ai/route.ts b/src/app/api/ai/route.ts
new file mode 100644
index 0000000..d386f99
--- /dev/null
+++ b/src/app/api/ai/route.ts
@@ -0,0 +1,53 @@
+import "server-only";
+
+import { auth } from "@/auth";
+import { NextApiRequest, NextApiResponse } from "next";
+import { getResumeReviewByAi } from "@/actions/ai.actions";
+import { NextResponse } from "next/server";
+import { ChatOllama } from "@langchain/community/chat_models/ollama";
+import { StringOutputParser } from "@langchain/core/output_parsers";
+import { ChatPromptTemplate } from "@langchain/core/prompts";
+
+export const GET = async (req: NextApiRequest, res: NextApiResponse) => {
+ const session = await auth();
+ console.log("SESSION USER: ", session?.user);
+ const userId = session?.accessToken.sub;
+
+ if (!session || !session.user) {
+ return res.status(401).json({ message: "Not Authenticated" });
+ }
+
+ // const { label, value } = await req.body();
+ // if (!value) {
+ // return res.status(400).json({ message: "Input text is required" });
+ // }
+
+ try {
+ const readableStream = await getResumeReviewByAi();
+
+ return new NextResponse(readableStream, {
+ headers: {
+ "Content-Type": "text/event-stream",
+ "Cache-Control": "no-cache",
+ Connection: "keep-alive",
+ },
+ });
+ } catch (error) {
+ const message = "Error getting AI response.";
+ console.error(message, error);
+ if (error instanceof Error) {
+ if (error.message === "fetch failed") {
+ error.message =
+ "Fetch failed, please make sure selected AI service is running.";
+ }
+ return NextResponse.json(
+ { error: error.message },
+ { status: 500, statusText: error.message }
+ );
+ }
+ return NextResponse.json(
+ { error: message },
+ { status: 500, statusText: message }
+ );
+ }
+};
diff --git a/src/components/AiSection.tsx b/src/components/AiSection.tsx
index ca05c12..5d47ce4 100644
--- a/src/components/AiSection.tsx
+++ b/src/components/AiSection.tsx
@@ -1,4 +1,5 @@
-import { PlusCircle } from "lucide-react";
+"use client";
+import { Sparkles } from "lucide-react";
import { Button } from "./ui/button";
import {
Sheet,
@@ -10,43 +11,101 @@ import {
SheetTitle,
} from "./ui/sheet";
import Loading from "./Loading";
+import { useCallback, useEffect, useState } from "react";
+import { toast } from "./ui/use-toast";
interface AiSectionProps {
- aISectionOpen: boolean;
- setAiSectionOpen: (o: boolean) => void;
getAiResponse: () => void;
- aIContent: string;
- isPending: boolean;
}
-const AiSection = ({
- aISectionOpen,
- setAiSectionOpen,
- getAiResponse,
- aIContent,
- isPending,
-}: AiSectionProps) => {
+const AiSection = () => {
+ const [aIContent, setAIContent] = useState("");
+ const [aISectionOpen, setAiSectionOpen] = useState(false);
+ const [loading, setLoading] = useState(false);
+ let reader: ReadableStreamDefaultReader<Uint8Array> | null = null;
+ let isStreaming = true;
+ const terminateStream = useCallback(() => {
+ if (reader) {
+ console.log("Stream terminated");
+ isStreaming = false;
+ reader
+ .cancel()
+ .catch((error) => console.error("Stream cancel error:", error));
+ }
+ }, [reader]);
+ useEffect(() => {
+ if (!aISectionOpen) {
+ setAIContent("");
+ terminateStream();
+ }
+ }, [aISectionOpen, terminateStream]);
+
+ const getResumeReview = async () => {
+ try {
+ setAiSectionOpen(true);
+ setLoading(true);
+
+ const response = await fetch("/api/ai", {
+ method: "GET",
+ headers: { "Content-Type": "application/json" },
+ // body: JSON.stringify({ text: resume?.title }),
+ });
+
+ if (!response.body) {
+ setLoading(false);
+ throw new Error("No response body");
+ }
+
+ if (!response.ok) {
+ setLoading(false);
+ throw new Error(response.statusText);
+ }
+
+ reader = response.body.getReader();
+ const decoder = new TextDecoder();
+ let done = false;
+ setLoading(false);
+
+ while (!done && isStreaming) {
+ const { value, done: doneReading } = await reader.read();
+ done = doneReading;
+ const chunk = decoder.decode(value, { stream: !done });
+ setAIContent((prev) => prev + chunk);
+ }
+ } catch (error) {
+ const message = "Error fetching resume review";
+ console.error(message, error);
+ const description = error instanceof Error ? error.message : message;
+ setLoading(false);
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description,
+ });
+ }
+ };
+
return (
<Sheet open={aISectionOpen} onOpenChange={setAiSectionOpen}>
<div className="ml-2">
<Button
size="sm"
className="h-8 gap-1 cursor-pointer"
- onClick={getAiResponse}
- disabled={isPending}
+ onClick={getResumeReview}
+ disabled={loading}
>
- <PlusCircle className="h-3.5 w-3.5" />
+ <Sparkles className="h-3.5 w-3.5" />
<span className="sr-only sm:not-sr-only sm:whitespace-nowrap">
Review
</span>
</Button>
</div>
<SheetPortal>
- <SheetContent>
+ <SheetContent className="overflow-y-scroll">
<SheetHeader>
<SheetTitle>AI Review</SheetTitle>
<SheetDescription>
- {isPending ? (
+ {loading ? (
<div className="flex items-center flex-col">
<Loading />
<div>Loading...</div>
diff --git a/src/components/profile/ResumeContainer.tsx b/src/components/profile/ResumeContainer.tsx
index 9730344..b1b6f28 100644
--- a/src/components/profile/ResumeContainer.tsx
+++ b/src/components/profile/ResumeContainer.tsx
@@ -3,19 +3,13 @@ import { Resume, ResumeSection, SectionType } from "@/models/profile.model";
import { Card, CardDescription, CardHeader, CardTitle } from "../ui/card";
import AddResumeSection, { AddResumeSectionRef } from "./AddResumeSection";
import ContactInfoCard from "./ContactInfoCard";
-import { useRef, useState, useTransition } from "react";
+import { useRef, useState } from "react";
import SummarySectionCard from "./SummarySectionCard";
import ExperienceCard from "./ExperienceCard";
import EducationCard from "./EducationCard";
import AiSection from "../AiSection";
-import { getResumeReviewByAi } from "@/actions/ai.actions";
-import { toast } from "../ui/use-toast";
function ResumeContainer({ resume }: { resume: Resume }) {
- const [aISectionOpen, setAiSectionOpen] = useState(false);
- const [aIContent, setAIContent] = useState("");
- const [isPending, startTransition] = useTransition();
-
const resumeSectionRef = useRef<AddResumeSectionRef>(null);
const summarySection = resume?.ResumeSections?.find(
(section) => section.sectionType === SectionType.SUMMARY
@@ -50,23 +44,7 @@ function ResumeContainer({ resume }: { resume: Resume }) {
};
resumeSectionRef.current?.openEducationDialog(section);
};
- const getResumeReview = () => {
- setAiSectionOpen(true);
- startTransition(async () => {
- const { response, success, message } = await getResumeReviewByAi();
- if (success && response && response.kwargs.content) {
- setAIContent(response.kwargs.content);
- }
- if (!success) {
- toast({
- variant: "destructive",
- title: "Error!",
- description: message,
- });
- return;
- }
- });
- };
+
return (
<>
<Card>
@@ -75,13 +53,7 @@ function ResumeContainer({ resume }: { resume: Resume }) {
<CardDescription>{resume?.title}</CardDescription>
<div className="flex items-center">
<AddResumeSection resume={resume} ref={resumeSectionRef} />
- <AiSection
- aISectionOpen={aISectionOpen}
- setAiSectionOpen={setAiSectionOpen}
- getAiResponse={getResumeReview}
- aIContent={aIContent}
- isPending={isPending}
- />
+ <AiSection />
</div>
</CardHeader>
</Card>
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index 6d3f013..f10b5a9 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -37,6 +37,10 @@ export function formatUrl(url: string) {
export function handleError(error: unknown, msg = "Server Error.") {
console.error(error, msg);
if (error instanceof Error) {
+ if (error.message === "fetch failed") {
+ error.message =
+ "Fetch failed, please make sure selected AI service is running.";
+ }
return { success: false, message: error.message || msg };
}
}