route.ts 728 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { OpenAIApi, Configuration } from "openai";
  2. import { apiKey } from "./config";
  3. // set up openai api client
  4. const config = new Configuration({
  5. apiKey,
  6. });
  7. const openai = new OpenAIApi(config);
  8. export async function GET(req: Request) {
  9. try {
  10. const completion = await openai.createChatCompletion(
  11. {
  12. messages: [
  13. {
  14. role: "user",
  15. content: "hello",
  16. },
  17. ],
  18. model: "gpt-3.5-turbo",
  19. },
  20. {
  21. proxy: {
  22. protocol: "socks",
  23. host: "127.0.0.1",
  24. port: 7890,
  25. },
  26. }
  27. );
  28. return new Response(JSON.stringify(completion.data));
  29. } catch (e) {
  30. return new Response(JSON.stringify(e));
  31. }
  32. }