-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ehsanghaffar/feature/langchain
Merge Request: `feature/langchain` to `main`
- Loading branch information
Showing
23 changed files
with
826 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 > | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.