add friendli call
This commit is contained in:
@@ -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.";
|
||||||
|
|||||||
@@ -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" }), {
|
||||||
|
|||||||
Reference in New Issue
Block a user