route.ts 708 B

12345678910111213141516171819202122232425262728293031
  1. import { OpenAIApi, Configuration } from "openai";
  2. import { apiKey } from "./config";
  3. import { ChatRequest } from "./typing";
  4. // set up openai api client
  5. const config = new Configuration({
  6. apiKey,
  7. });
  8. const openai = new OpenAIApi(config);
  9. export async function POST(req: Request) {
  10. try {
  11. const requestBody = (await req.json()) as ChatRequest;
  12. const completion = await openai.createChatCompletion(
  13. {
  14. ...requestBody,
  15. },
  16. {
  17. proxy: {
  18. protocol: "socks",
  19. host: "127.0.0.1",
  20. port: 7890,
  21. },
  22. }
  23. );
  24. return new Response(JSON.stringify(completion.data));
  25. } catch (e) {
  26. return new Response(JSON.stringify(e));
  27. }
  28. }