File tree 2 files changed +47
-5
lines changed
2 files changed +47
-5
lines changed Original file line number Diff line number Diff line change 1
1
import { NextResponse } from "next/server" ;
2
2
3
+ const config = {
4
+ apiKey : process . env . OPENAI_API_KEY as string ,
5
+ apiHost : "https://api.openai.com/v1/chat/completions" ,
6
+ systemContent :
7
+ "You are a knowlegeable assistant that provides quality information." ,
8
+ userContent : ( question : string ) => `Tell me ${ question } ` ,
9
+ } ;
10
+
3
11
export const POST = async ( request : Request ) => {
4
12
const { question } = await request . json ( ) ;
5
13
6
14
try {
7
- const response = await fetch ( "https://api.openai.com/v1/chat/completions" , {
15
+ const response = await fetch ( config . apiHost , {
8
16
method : "POST" ,
9
17
headers : {
10
18
"Content-Type" : "application/json" ,
11
- Authorization : `Bearer ${ process . env . OPENAI_API_KEY } ` ,
19
+ Authorization : `Bearer ${ config . apiKey } }` ,
12
20
} ,
13
21
body : JSON . stringify ( {
14
22
model : "gpt-3.5-turbo" ,
15
23
messages : [
16
24
{
17
25
role : "system" ,
18
- content :
19
- "You are a knowlegeable assistant that provides quality information." ,
26
+ content : config . systemContent ,
20
27
} ,
21
28
{
22
29
role : "user" ,
23
- content : `Tell me ${ question } ` ,
30
+ content : config . userContent ( question ) ,
24
31
} ,
25
32
] ,
26
33
} ) ,
Original file line number Diff line number Diff line change
1
+ import { NextResponse } from "next/server" ;
2
+
3
+ const config = {
4
+ apiKey : process . env . RAPIDAPI_API_KEY as string ,
5
+ apiHost : "https://jsearch.p.rapidapi.com" ,
6
+ } ;
7
+
8
+ export const POST = async ( request : Request ) => {
9
+ const {
10
+ page = 1 ,
11
+ pageSize = 1 ,
12
+ filter = "us" ,
13
+ searchQuery = "Software Engineer" ,
14
+ } = await request . json ( ) ;
15
+
16
+ try {
17
+ const response = await fetch (
18
+ `${ config . apiHost } /search?query=${ searchQuery } &location=${ filter } &page=${ page } &num_pages=${ pageSize } ` ,
19
+ {
20
+ method : "GET" ,
21
+ headers : {
22
+ "X-RapidAPI-Key" : config . apiKey ,
23
+ "X-RapidAPI-Host" : config . apiHost ,
24
+ } ,
25
+ }
26
+ ) ;
27
+
28
+ const responseData = await response . json ( ) ;
29
+ const result = responseData . result ;
30
+
31
+ return NextResponse . json ( { result } ) ;
32
+ } catch ( error : any ) {
33
+ return NextResponse . json ( { error : error . message } ) ;
34
+ }
35
+ } ;
You can’t perform that action at this time.
0 commit comments