Streaming with Codex is not accurate

I’ve been trying for several hours to get an accurate response using streaming with Codex without luck.
This is my code:

var eventSource = new EventSource("https://api.openai.com/v1/engines/code-davinci-002/completions/browser_stream", { 
	headers: { 
	"Content-Type": "application/json", 
	Authorization: "Bearer " + OPENAI_API_KEY, 
	}, 
	method: "POST", 
	payload: JSON.stringify({ 
	prompt: `# File type is .${fileExtension}\n \n ${fileTextToCursor}: \n\"\"\"`,
	temperature: 0.1,
  	top_p: 1.0,
  	frequency_penalty: 0,
  	presence_penalty: 0,
	max_tokens: 150,
	n: 3,
	stop: ["#", "\"\"\""],
	})
	});

	// listen to the event source for the message event
	eventSource.onmessage = (event) => {
		const data = JSON.parse(event.data);
		console.log(`Completion: ${data.choices[0].text}`);
			
		}
		eventSource.onerror = (err) => {
		console.error("EventSource failed:", err);
		};
		eventSource.stream();

Answers are very erratic. I’ve made a lot of variations in the url for example changing it to Davinci 001, adding the model in the payload, etc but nothing works. Anyone has this same problem?

Example response:
Prompt: # Create a fibonacci series recursively

Completion: In

Completion: a

Completion: recent

Completion: post

Completion: at

Completion: the

Completion: intersection

Completion: of

Completion: typ

Completion: ography

Completion: and

1 Like