From 6d41fb596b9f836b381b91882b16000b6184ea57 Mon Sep 17 00:00:00 2001 From: Joseph J Helfenbein Date: Sun, 26 Jan 2025 07:39:44 -0500 Subject: [PATCH] add friendli call --- src/app/(panels)/suite/patient/chat/page.jsx | 2 ++ src/app/api/chat/route.js | 26 +++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/app/(panels)/suite/patient/chat/page.jsx b/src/app/(panels)/suite/patient/chat/page.jsx index 2a2d3d0..c7d8d67 100644 --- a/src/app/(panels)/suite/patient/chat/page.jsx +++ b/src/app/(panels)/suite/patient/chat/page.jsx @@ -44,6 +44,8 @@ export default function Chat() { setLoading(true); try { + console.log("Query being sent:", userQuery); + const response = await axios.post("/api/chat", { query: userQuery }); const botResponse = response.data?.answer || "No response from the bot."; diff --git a/src/app/api/chat/route.js b/src/app/api/chat/route.js index bc6ff2d..21cbd7b 100644 --- a/src/app/api/chat/route.js +++ b/src/app/api/chat/route.js @@ -1,20 +1,28 @@ -import { HuggingFaceInference } from "@langchain/community/llms/hf"; - export async function POST(req) { try { const body = await req.json(); const { query } = body; - const model = new HuggingFaceInference({ - model: "m42-health/Llama3-Med42-8B", - apiKey: process.env.HUGGING_FACE_API_KEY, + const response = await fetch("https://api.friendli.ai/dedicated", { + method: "POST", + 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 }), { - status: 200, - }); + const data = await response.json(); + return new Response(JSON.stringify({ answer: data.answer }), { status: 200 }); } catch (error) { console.error("Backend error:", error); return new Response(JSON.stringify({ error: "Error generating response" }), {