add friendli call

This commit is contained in:
Joseph J Helfenbein
2025-01-26 07:39:44 -05:00
parent 5b464ae15d
commit 6d41fb596b
2 changed files with 19 additions and 9 deletions

View File

@@ -44,6 +44,8 @@ export default function Chat() {
setLoading(true); setLoading(true);
try { try {
console.log("Query being sent:", userQuery);
const response = await axios.post("/api/chat", { query: userQuery }); const response = await axios.post("/api/chat", { query: userQuery });
const botResponse = response.data?.answer || "No response from the bot."; const botResponse = response.data?.answer || "No response from the bot.";

View File

@@ -1,20 +1,28 @@
import { HuggingFaceInference } from "@langchain/community/llms/hf";
export async function POST(req) { export async function POST(req) {
try { try {
const body = await req.json(); const body = await req.json();
const { query } = body; const { query } = body;
const model = new HuggingFaceInference({ const response = await fetch("https://api.friendli.ai/dedicated", {
model: "m42-health/Llama3-Med42-8B", method: "POST",
apiKey: process.env.HUGGING_FACE_API_KEY, headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.FRIENDLI_API_KEY}`,
},
body: JSON.stringify({ id: "idv6w0upi158", query }),
}); });
const response = await model.invoke(query); if (!response.ok) {
const errorDetails = await response.text();
console.error("Friendli API error:", errorDetails);
return new Response(
JSON.stringify({ error: "Failed to fetch data from Friendli API" }),
{ status: 500 }
);
}
return new Response(JSON.stringify({ answer: response }), { const data = await response.json();
status: 200, return new Response(JSON.stringify({ answer: data.answer }), { status: 200 });
});
} catch (error) { } catch (error) {
console.error("Backend error:", error); console.error("Backend error:", error);
return new Response(JSON.stringify({ error: "Error generating response" }), { return new Response(JSON.stringify({ error: "Error generating response" }), {