nbbaier/readmeGPT

Forks

Forked 1 time

Code Documentation Assistant

The Code Documentation Assistant is an AI-powered tool that helps generate documentation for code. It uses the OpenAI GPT-3.5 Turbo model to generate readme files in GitHub-flavored markdown based on the provided code.

Installation

To use the Code Documentation Assistant, you need to have Deno installed. You can install Deno by following the instructions at deno.land.

Next, you need to install the required dependencies:

$ deno install --unstable --allow-net --name code-doc-assistant https://deno.land/x/code_doc_assistant/mod.ts

Usage

Importing the Code Documentation Assistant

import { draftReadme, writeReadme } from "code-doc-assistant";

Function: draftReadme

async function draftReadme(options: WriterOptions): Promise<string>

The draftReadme function generates a readme file based on the provided options.

Parameters

  • options (required): An object containing the following properties:
    • username (string): The username of the code owner.
    • valName (string): The name of the Val containing the code.
    • model (optional, default: "gpt-3.5-turbo"): The OpenAI model to use for generating the readme.
    • userPrompt (optional): Additional prompt to include in the documentation.

Return Value

A promise that resolves to a string representing the generated readme file.

Function: writeReadme

async function writeReadme(options: WriterOptions): Promise<string>

The writeReadme function generates a readme file and updates the readme of the corresponding Val with the generated content.

Parameters

  • options (required): An object containing the following properties:
    • username (string): The username of the code owner.
    • valName (string): The name of the Val containing the code.
    • model (optional, default: "gpt-3.5-turbo"): The OpenAI model to use for generating the readme.
    • userPrompt (optional): Additional prompt to include in the documentation.

Return Value

A promise that resolves to a string indicating the success of the readme update.

Example

import { draftReadme, writeReadme } from "code-doc-assistant";

const options = {
  username: "your-username",
  valName: "your-val-name",
};

const generatedReadme = await draftReadme(options);
console.log(generatedReadme);

const successMessage = await writeReadme(options);
console.log(successMessage);

License

This project is licensed under the MIT License.

Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { fetch } from "https://esm.town/v/std/fetch?v=4";
import OpenAI, { type ClientOptions } from "npm:openai";
export interface WriterOptions extends ClientOptions {
username: string;
valName: string;
model?: string;
userPrompt?: string;
}
function createPrompt(code: string, userPrompt?: string) {
return `
You are an AI assistant that writes documentation for code. You output readmes
in GitHub flavored markdown. Usage sections should include a single code snippet
that a user can copy and paste. Never return anything other than documentation for
the code you are provided.
${userPrompt}
Take the below code and return a markdown readme:
${code}
`;
}
async function getVal(username: string, valName: string) {
try {
const res = await fetch(`https://api.val.town/v1/alias/${username}/${valName}`, {
method: "GET",
headers: {
"accept": "*/*",
"Content-Type": "application/json",
"Authorization": `Bearer ${Deno.env.get("valtown")}`,
},
});
const { id, code } = await res.json();
return { id, code };
} catch (error) {
throw new Error("Error getting val code: " + error.message);
}
}
async function performOpenAICall(prompt: string, model: string, openaiOptions: ClientOptions) {
const openai = new OpenAI(openaiOptions);
try {
const response = await openai.chat.completions.create({
messages: [{ role: "system", content: prompt }],
model: model,
});
if (!response.choices || response.choices.length === 0) {
throw new Error("No response from OpenAI");
}
const readme = response.choices[0].message?.content;
if (!readme) {
throw new Error("No readme returned by OpenAI. Try again.");
}
return readme;
} catch (error) {
throw new Error("Error generating readme: " + error.message);
}
}
async function updateReadme(id: string, readme: string) {
try {
const res = await fetch(`https://api.val.town/v1/vals/${id}`, {
method: "PUT",
headers: {
"accept": "*/*",
"Content-Type": "application/json",
"Authorization": `Bearer ${Deno.env.get("valtown")}`,
},
body: JSON.stringify({ "readme": readme }),
});
return res.status;
} catch (error) {
throw new Error("Error updating readme: " + error.message);
}
}
async function draftReadme(options: WriterOptions) {
const { username, valName, model = "gpt-3.5-turbo", userPrompt, ...openaiOptions } = options;
const { id, code } = await getVal(username, valName);
const prompt = createPrompt(code, userPrompt);
const readme = await performOpenAICall(prompt, model, openaiOptions);
return readme;
}
async function writeReadme(options: WriterOptions) {
const { username, valName, model = "gpt-3.5-turbo", userPrompt, ...openaiOptions } = options;
const { id, code } = await getVal(username, valName);
const prompt = createPrompt(code, userPrompt);
const readme = await performOpenAICall(prompt, model, openaiOptions);
try {
const update = await updateReadme(id, readme);
return "Readme updated successfully!";
} catch (error) {
1
Next