Skip to content

Commit bbe8820

Browse files
Merge pull request #45 from CivicTechFredericton/build-fix-branch
Ollama API call - timeout changes added
2 parents c50ede2 + e8ce118 commit bbe8820

1 file changed

Lines changed: 43 additions & 18 deletions

File tree

src/app/api/llama/route.ts

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,54 @@
11
import { NextRequest, NextResponse } from "next/server";
2+
3+
// Set maximum duration for this API route (in seconds)
4+
export const maxDuration = 300; // 5 minutes
5+
26
export async function POST(req: NextRequest) {
37
try {
48
const { input } = await req.json();
59
const json_format = '{"flagged_words": ["word1", "word2"],"explanations": {"word1": "Explanation for why word1 is problematic","word2": "Explanation for why word2 is problematic"},"suggestions": {"word1": "New suggested word without description","word2": "New suggested word without description"},"revisedjobposting":"New fully revised job posting with good language and should have format of job posting(new lines) and at least length'+input.length+'"}';
610
const prompt =
711
'You are tasked with reviewing a job posting for potential biases based on protected classes under Canadian law. Analyze the document and identify any flagged words or phrases that may disadvantage individuals from the following protected classes: Citizenship, Race, Place of origin, Ethnic origin, Colour, Ancestry, Disability, Age, Creed (Religion), Sex/Pregnancy, Family status, Marital status, Sexual orientation, Gender identity, Gender expression, Receipt of public assistance, and Record of offences (criminal charges). For each instance of potential bias, provide a JSON response strictly in the format below. Ensure no text, characters, or formatting marks are included before or after the JSON output. The output should be valid JSON that follows this format precisely mandatorily:' + json_format + 'Ensure that your response is completely and strictly in the above JSON format (no text or characters before and after JSON). Promote diversity, equity, and inclusion, and consider any implicit biases or unintentional exclusionary language that might exist.';
812

9-
// Call Ollama API running on localhost
10-
const ollamaResponse = await fetch("http://localhost:11434/api/generate", {
11-
method: "POST",
12-
headers: {
13-
"Content-Type": "application/json",
14-
},
15-
body: JSON.stringify({
16-
model: "llama3.1",
17-
// model: "gemma2",
18-
prompt: prompt + input,
19-
temperature: 1,
20-
stream: false,
21-
maxtokens: 1024,
22-
response_format: {
23-
type: "json_object"
24-
},
25-
}),
26-
});
13+
// Call Ollama API running on localhost with timeout
14+
const controller = new AbortController();
15+
const timeoutId = setTimeout(() => controller.abort(), 240000); // 4 minute timeout
16+
17+
// Get Ollama URL from environment variable, fallback to default
18+
const ollamaUrl = process.env.OLLAMA_URL || "http://localhost:11434";
19+
const ollamaApiUrl = `${ollamaUrl}/api/generate`;
20+
21+
let ollamaResponse;
22+
try {
23+
ollamaResponse = await fetch(ollamaApiUrl, {
24+
method: "POST",
25+
headers: {
26+
"Content-Type": "application/json",
27+
},
28+
body: JSON.stringify({
29+
model: "llama3.1",
30+
// model: "gemma2",
31+
prompt: prompt + input,
32+
temperature: 1,
33+
stream: false,
34+
maxtokens: 1024,
35+
response_format: {
36+
type: "json_object"
37+
},
38+
}),
39+
signal: controller.signal,
40+
});
41+
clearTimeout(timeoutId);
42+
} catch (fetchError: any) {
43+
clearTimeout(timeoutId);
44+
if (fetchError.name === 'AbortError') {
45+
return NextResponse.json(
46+
{ error: "Request timeout: The model is taking too long to respond. Please try with a shorter input or check if Ollama is running." },
47+
{ status: 504 }
48+
);
49+
}
50+
throw fetchError;
51+
}
2752

2853
// AWM 20250208 - New interface to maintain data structure in responses
2954
interface ParsedResponse {

0 commit comments

Comments
 (0)