-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introducing OpenRouter, supporting models such as Claude, PaLM2…
…, Llama 2.
- Loading branch information
1 parent
8ae7dcb
commit 6c4b020
Showing
37 changed files
with
743 additions
and
133 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
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
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,84 @@ | ||
import { ResErr, isUndefined } from "@/lib"; | ||
import { stream } from "@/lib/stream"; | ||
import type { supportModelType } from "@/lib/calcTokens/gpt-tokens"; | ||
import type { IFetchOpenRouter } from "./types"; | ||
|
||
interface IRegular extends IFetchOpenRouter { | ||
prompt?: string; | ||
modelLabel: supportModelType; | ||
userId?: string; | ||
headerApiKey?: string; | ||
} | ||
|
||
const fetchOpenRouter = async ({ | ||
fetchURL, | ||
Authorization, | ||
model, | ||
temperature, | ||
max_tokens, | ||
messages, | ||
}: IFetchOpenRouter) => { | ||
return await fetch(fetchURL, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${Authorization}`, | ||
"HTTP-Referer": "https://chat.ltopx.com", | ||
"X-Title": "L-GPT", | ||
}, | ||
method: "POST", | ||
body: JSON.stringify({ | ||
stream: true, | ||
model, | ||
temperature: isUndefined(temperature) ? 1 : temperature, | ||
max_tokens: isUndefined(max_tokens) ? 1000 : max_tokens, | ||
messages, | ||
}), | ||
}); | ||
}; | ||
|
||
export const regular = async ({ | ||
prompt, | ||
messages, | ||
fetchURL, | ||
Authorization, | ||
model, | ||
modelLabel, | ||
temperature, | ||
max_tokens, | ||
userId, | ||
headerApiKey, | ||
}: IRegular) => { | ||
if (prompt) messages.unshift({ role: "system", content: prompt }); | ||
|
||
try { | ||
const response = await fetchOpenRouter({ | ||
fetchURL, | ||
Authorization, | ||
model, | ||
temperature, | ||
max_tokens, | ||
messages, | ||
}); | ||
|
||
if (response.status !== 200) { | ||
return new Response(response.body, { status: 500 }); | ||
} | ||
|
||
const { readable, writable } = new TransformStream(); | ||
|
||
stream({ | ||
readable: response.body as ReadableStream, | ||
writable, | ||
userId, | ||
headerApiKey, | ||
messages, | ||
model, | ||
modelLabel, | ||
}); | ||
|
||
return new Response(readable, response); | ||
} catch (error: any) { | ||
console.log(error, "openrouter regular error"); | ||
return ResErr({ msg: error?.message || "Error" }); | ||
} | ||
}; |
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,75 @@ | ||
import { headers } from "next/headers"; | ||
import { getServerSession } from "next-auth/next"; | ||
import { authOptions } from "@/utils/plugin/auth"; | ||
import { prisma } from "@/lib/prisma"; | ||
import { ResErr } from "@/lib"; | ||
import { PREMIUM_MODELS } from "@/hooks/useLLM"; | ||
import { regular } from "./regular"; | ||
|
||
export async function POST(request: Request) { | ||
const session = await getServerSession(authOptions); | ||
const headersList = headers(); | ||
const headerApiKey = headersList.get("Authorization") || ""; | ||
const API_KEY = process.env.NEXT_PUBLIC_OPENROUTER_API_KEY; | ||
|
||
const { | ||
// model 用于接口发送给 OpenRouter 的请求参数 | ||
model, | ||
// modelLabel 用于 Token 计算 | ||
modelLabel, | ||
temperature, | ||
max_tokens, | ||
prompt, | ||
chat_list, | ||
} = await request.json(); | ||
|
||
/** | ||
* If not logged in, only the locally configured API Key can be used. | ||
*/ | ||
if (!session && !headerApiKey) return ResErr({ error: 10001 }); | ||
|
||
if (!headerApiKey) { | ||
const user = await prisma.user.findUnique({ | ||
where: { id: session?.user.id }, | ||
}); | ||
if (!user) return ResErr({ error: 20002 }); | ||
|
||
// audit user license | ||
if ( | ||
user.license_type !== "premium" && | ||
user.license_type !== "team" && | ||
PREMIUM_MODELS.includes(modelLabel) | ||
) { | ||
return ResErr({ error: 20009 }); | ||
} | ||
|
||
const { availableTokens } = user; | ||
if (availableTokens <= 0) return ResErr({ error: 10005 }); | ||
} | ||
|
||
// first use local | ||
// then use env configuration | ||
// or empty | ||
const Authorization = headerApiKey || API_KEY || ""; | ||
|
||
if (!Authorization) return ResErr({ error: 10002 }); | ||
|
||
const fetchURL = "https://openrouter.ai/api/v1/chat/completions"; | ||
|
||
const messages = [...chat_list]; | ||
|
||
const userId = session?.user.id; | ||
|
||
return await regular({ | ||
prompt, | ||
messages, | ||
fetchURL, | ||
Authorization, | ||
model, | ||
modelLabel, | ||
temperature, | ||
max_tokens, | ||
userId, | ||
headerApiKey, | ||
}); | ||
} |
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,8 @@ | ||
export interface IFetchOpenRouter { | ||
messages: any[]; | ||
fetchURL: string; | ||
Authorization: string; | ||
model: string; | ||
temperature?: number; | ||
max_tokens?: number; | ||
} |
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
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
Oops, something went wrong.
6c4b020
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
l-gpt – ./
l-gpt-ethan-pro.vercel.app
l-gpt.vercel.app
gpt.ltopx.com
l-gpt-git-main-ethan-pro.vercel.app
chat.ltopx.com