Articles tagged with GitHub

A collection of articles tagged with GitHub on my blog!

An example of OpenAI API and ReadableStream with GPT 3 in SvelteKit

From this GitHub snippet:

async function OpenAITextStream(search: string) {
	const payload: OpenAITextPayload = {
		model: 'text-davinci-003',
		prompt: search,
		temperature: 0,
		max_tokens: 2048,
		frequency_penalty: 0.0,
		stream: true,
		presence_penalty: 0.0,
		n: 1
	};

	const encoder = new TextEncoder();
	const decoder = new TextDecoder();

	let counter = 0;

	const res = await fetch('https://api.openai.com/v1/completions', {
		headers: {
			'Content-Type': 'application/json',
			Authorization: `Bearer ${key}`
		},
		method: 'POST',
		body: JSON.stringify(payload)
	});

	const stream = new ReadableStream({
		async start(controller) {
			// eslint-disable-next-line @typescript-eslint/no-explicit-any
			function onParse(event: ParseEvent) {
				if (event.type === 'event') {
					const data = event.data;
					// https://beta.openai.com/docs/api-reference/completions/create#completions/create-stream
					if (data === '[DONE]') {
						controller.close();
						return;
					}
					try {
						const json = JSON.parse(data);
						const text = json.choices[0].text;

						if (counter < 2 && (text.match(/\n/) || []).length) {
							// this is a prefix character (i.e., "\n\n"), do nothing
							return;
						}
						const queue = encoder.encode(text);
						controller.enqueue(queue);
						counter++;
					} catch (e) {
						controller.error(e);
					}
				}
			}

			// stream response (SSE) from OpenAI may be fragmented into multiple chunks
			// this ensures we properly read chunks and invoke an event for each SSE event stream
			const parser = createParser(onParse);
			// https://web.dev/streams/#asynchronous-iteration
			// eslint-disable-next-line @typescript-eslint/no-explicit-any
			for await (const chunk of res.body as any) {
				parser.feed(decoder.decode(chunk));
			}
		}
	});
	return stream;
}

dendron pages fix - appy to apply in tutorial 696

Fix: Appy to Apply in Tutorial #696

What Does This PR Do?

Replaces all instances of appy with apply in the tutorial (spelling fix).

Merge Requirements Satisfied

For more information, visit the Contributing Guide for Documentation

  • Check rendered output to ensure formatting is correct for renderings of wikilinks, note refs, tables, and images. Dendron: Show Preview can be used in your workspace to confirm the page renders as expected. Sometimes, Dendron: Reload Index needs to be ran if certain wikilinks aren’t working as expected. (ignore this if contribution is made via the Edit this page on GitHub button from the published site)
  • If this PR includes any refactoring (renaming notes, renaming headers, etc.), make sure that these changes were done via Dendron Refactoring Commands. This will ensure that any other impacted areas of the documentation, that link to or embed modified notes (via note references), are automatically updated.

Verify GitHub Actions tests are passing, which can be seen in the Checks tab of the PR:

  • URL validator GitHub Action passes
  • Dendron site build GitHub Action passes

NOTE: If you are a new contributor, a maintainer will need to provide approval for GitHub Actions to run on your PRs.

)