Skip to content

Commit

Permalink
Merge pull request #1 from ehsanghaffar/feature/langchain
Browse files Browse the repository at this point in the history
Merge Request: `feature/langchain` to `main`
  • Loading branch information
ehsanghaffar authored May 26, 2024
2 parents d23d862 + 389ab2c commit 4399522
Show file tree
Hide file tree
Showing 23 changed files with 826 additions and 172 deletions.
6 changes: 3 additions & 3 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getOpenAIApiInstance } from "@/lib/OpenAiCompletaions"
import { errorHandler } from "@/lib/utils";
import { getOpenAIApiInstance } from "@/libs/OpenAiCompletaions"
import { errorHandler } from "@/libs/utils";

// export const dynamic = 'force-dynamic'

const apikey = process.env.NEXT_OPENAI_API_KEY
const apikey = process.env.NEXT_PUBLIC_OPENAI_API_KEY

export async function POST(requset: Request) {
const messages = await requset.json()
Expand Down
69 changes: 69 additions & 0 deletions app/api/langchain/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { NextRequest, NextResponse } from "next/server";

import { z } from "zod";

import { createStructuredOutputChainFromZod } from "langchain/chains/openai_functions";
import { PromptTemplate } from "@langchain/core/prompts";
import { createOpenAIModel } from "@/libs/Langchain";
import { getIP } from "@/libs/utils";
import { checkRateLimit } from "@/store/rateLimitStore";

export const runtime = "edge";

const TEMPLATE = `Generate a compelling social media bio for user centered around context which them provide you.
The bio should be concise (150-200 characters) and capture the essence of user in a way that resonates with context.
Include elements that showcase personality, passion, and any relevant hashtags or keywords.
Feel free to add a touch of creativity to make it engaging.
{input}`;


const apikey = process.env.NEXT_PUBLIC_OPENAI_API_KEY


export async function POST(req: NextRequest) {
try {

const ip = getIP(req);
if (!checkRateLimit(ip as string)) {
return NextResponse.json({ error: "شما بیش از حد مجاز از سرویس استفاده کرده اید. چند ساعت بعد امتحان کنید" }, { status: 429 });
}

const messages = await req.json()

const prompt = PromptTemplate.fromTemplate<{ input: string }>(TEMPLATE);

const model = createOpenAIModel(apikey)

const schema = z.object({
output: z.array(
z.object({
id: z.string(),
content: z.string(),
})
),

});


const chain = createStructuredOutputChainFromZod(schema, {
llm: model,
prompt,
outputKey: "output",
});
console.log(chain)

const result = await chain.call({
input: messages,
});
console.log(result)

return NextResponse.json(result.output, { status: 200 });
} catch (e: any) {
console.log(e)
if (e.message.includes("API key")){
return NextResponse.json({ error: "سرویس فعلا در دسترس نیست" }, { status: 500 });
}
return NextResponse.json({ error: e.message }, { status: 500 });
}
}
25 changes: 25 additions & 0 deletions app/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use client' // Error components must be Client Components

import { useEffect } from 'react'

export default function Error({
error,
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
useEffect(() => {
// Log the error to an error reporting service
console.error(error)
}, [error])

return (
<section className="w-full flex text-center md:text-left justify-center">
<div className=" flex flex-col py-10 md:py-44 items-center gap-8 md:gap-24 w-full max-w-screen-xl mx-5 md:mx-20">
<h2 className='md:text-xl text-primaryColor font-bold'>یه مشکلی به وجود اومده ⚠️</h2>
<span className='md:text-xl text-black'>{error.message}</span>
</div>
</section >
)
}
36 changes: 36 additions & 0 deletions app/global-error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use client";

import { Button } from "@/components/ui/button";
import { useRouter } from "next/router";

export default function GlobalError({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
const router = useRouter();
console.error(error);

return (
<html>
<body>
<div className="container flex justify-center text-center">
<h2>Something went wrong!</h2>
<Button
onClick={() => reset()}
variant={"outline"}
>
Try again
</Button>
<Button
variant={"default"}
onClick={() => router.push("/")}>
Go home
</Button>
</div>
</body>
</html>
);
}
76 changes: 0 additions & 76 deletions app/globals.css

This file was deleted.

6 changes: 3 additions & 3 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Header from '@/components/Header';
import './globals.css';
import '@/styles/globals.css';
import Footer from '@/components/Footer';
import { Toaster } from '@/components/ui/sonner';
import { Metadata } from 'next';
Expand Down Expand Up @@ -36,14 +36,14 @@ export default function RootLayout({
}) {
return (
<html lang="fa" dir="rtl">
<body className=" min-h-full flex flex-col">
<body className="min-h-screen flex flex-col">
<header>
<Header />
</header>
<main className="flex flex-1">
{children}
</main>
<footer className='flex flex-col w-full absolute bottom-0'>
<footer className='flex flex-col w-full bottom-0'>
<Footer />
</footer>
<Toaster richColors />
Expand Down
Loading

0 comments on commit 4399522

Please sign in to comment.