export default {
  async fetch(request, env) {
	  	let token="sk-"
			let endpoint='https://api.openai.com/v1/completions'
			const { pathname } = new URL(request.url);
			let paths = pathname.split( '/' );
			if(paths[1] != 'api') return new Response("Hello api!");
			if(paths[2] == null) return new Response("Hello World!");
			let prompt = paths[2];
			let data2 = {
					'model': "text-davinci-003",
				  'prompt': prompt,//"给出3个GPT3能完成任务案例",// 写一段代码,使用cloudflare works部署openai的GPT3的api
				  'max_tokens': 800,
				  'temperature': 0.5,
				  'top_p':1,
				  'frequency_penalty':0,
				  'presence_penalty':0
		  }
			const request2 = {
		    body: JSON.stringify(data2),
		    method: 'POST',
		    headers: {
		      'Authorization': `Bearer ${token}`,
		      'Content-Type': 'application/json',
		    },
		  };
		  let result = await fetch(endpoint, request2);
		  result = await result.json();
			const json = JSON.stringify(result, null, 2);
			return new Response(json, {
				headers: {
					'content-type': 'application/json;charset=UTF-8',
				},
			});
  }
}