3import { Hono } from "npm:hono@3";
4import { html } from "npm:hono@3/html";
5import { OpenAI } from "npm:openai";
6
7const app = new Hono();
8const openai = new OpenAI(Deno.env.get("OPENAI_API_KEY_VOICE"));
9
10class TranscriptionService {
11 async transcribeAudio(audioFile) {
12 try {
13 const transcription = await openai.audio.transcriptions.create({
14 file: audioFile,
15 model: "whisper-1",
19 return transcription;
20 } catch (error) {
21 console.error("OpenAI API error:", error);
22 throw error;
23 }
405
406 try {
407 const response = await openai.chat.completions.create({
408 model: "gpt-3.5-turbo",
409 messages: [
423 return c.text(translation);
424 } catch (error) {
425 console.error("OpenAI API error:", error);
426 return c.text("Error occurred during translation", 500);
427 }
438
439 try {
440 const mp3 = await openai.audio.speech.create({
441 model: "tts-1",
442 voice: voice,
450 });
451 } catch (error) {
452 console.error("OpenAI API error:", error);
453 return c.text("Error occurred during speech generation", 500);
454 }
174export default async function server(request: Request): Promise<Response> {
175 if (request.method === "POST" && new URL(request.url).pathname === "/generate") {
176 const { OpenAI } = await import("https://esm.town/v/std/openai");
177 const openai = new OpenAI();
178
179 const { prompt } = await request.json();
181 try {
182 const completion = await retryWithBackoff(() =>
183 openai.chat.completions.create({
184 messages: [
185 {
87export default async function server(request: Request): Promise<Response> {
88 if (request.method === "POST" && new URL(request.url).pathname === "/comment") {
89 const { OpenAI } = await import("https://esm.town/v/std/openai");
90 const openai = new OpenAI();
91
92 const { code, language } = await request.json();
93
94 const completion = await openai.chat.completions.create({
95 messages: [
96 { role: "system", content: "You are a helpful assistant that adds concise and useful comments to code." },
1# Code Commenter
2
3Code Commenter is a web application that automatically adds concise and useful comments to your code using OpenAI's language model. It supports JavaScript, TypeScript, and C# languages.
4
5## Features
162. The user selects the appropriate language from the dropdown menu
173. When the "Add Comments" button is clicked, the code is sent to the server
184. The server uses OpenAI's API to generate comments for the code
195. The commented code is returned and displayed in the right panel with syntax highlighting
20
28- Deno: Runtime environment
29- Val Town: Hosting platform
30- OpenAI API: For generating code comments
31
32## Usage
41## Limitations
42
43- The maximum length of code that can be processed is limited by the OpenAI API's token limit
44- The quality of comments may vary depending on the complexity of the code and the AI model's capabilities
45- Internet connection is required to use the application
47## Privacy Considerations
48
49Please note that the code you submit is sent to OpenAI's servers for processing. Do not submit sensitive or proprietary code that you don't want to be processed by a third-party service.
50
51## Future Improvements
1import { OpenAI } from "https://esm.town/v/std/openai";
2
3export default async function(req: Request): Promise<Response> {
11 });
12 }
13 const openai = new OpenAI();
14
15 try {
28 }
29
30 const stream = await openai.chat.completions.create(body);
31
32 if (!body.stream) {
240 const finalBudget = budget.length > 0 ? budget : [budgetOptions[Math.floor(Math.random() * budgetOptions.length)]];
241
242 const { OpenAI } = await import("https://esm.town/v/std/openai");
243 const openai = new OpenAI();
244
245 const completion = await openai.chat.completions.create({
246 messages: [{
247 role: "system",
259 try {
260 const content = completion.choices[0].message.content || "{}";
261 console.log("Raw OpenAI response:", content);
262 const parsedContent = JSON.parse(content);
263
275 }));
276 } catch (error) {
277 console.error("Failed to parse OpenAI response:", error);
278 return new Response(JSON.stringify({ error: "Failed to generate suggestions" }), {
279 status: 500,
1import { zodResponseFormat } from "https://esm.sh/openai/helpers/zod";
2import { z } from "https://esm.sh/zod";
3import { OpenAI } from "https://esm.town/v/std/openai";
4
5const openai = new OpenAI();
6
7const CalendarEvent = z.object({
11});
12
13const completion = await openai.beta.chat.completions.parse({
14 model: "gpt-4o-mini",
15 messages: [
1import { OpenAI } from "https://esm.town/v/std/openai";
2
3const openai = new OpenAI();
4
5export default async function(req: Request): Promise<Response> {
16
17 const messages = prepareMessages(selection, followUp, context);
18 log.info("Prepared messages for OpenAI:", JSON.stringify(messages));
19
20 const openai = new OpenAI();
21 const stream = await openai.chat.completions.create({
22 messages,
23 model: config.MODEL_NAME,
26 stream: true,
27 });
28 log.info("OpenAI stream created successfully");
29
30 return streamResponse(stream);
31 } catch (error) {
32 log.error("Error in OpenAI request:", error);
33 return handleError(error);
34 }
90 const content = chunk.choices[0]?.delta?.content || "";
91 if (content) {
92 // log.debug("Received chunk from OpenAI:", content);
93 const encodedChunk = encoder.encode(JSON.stringify({ chunk: content }) + "\n");
94 controller.enqueue(encodedChunk);
514 const url = new URL(request.url);
515 const { blob } = await import("https://esm.town/v/std/blob");
516 const { OpenAI } = await import("https://esm.town/v/std/openai");
517
518 const openai = new OpenAI();
519
520 // Simple rate limiting
553
554 try {
555 const completion = await openai.chat.completions.create({
556 messages: [
557 {
1import { fetch } from "https://esm.town/v/std/fetch";
2import { OpenAI } from "https://esm.town/v/std/openai";
3import process from "node:process";
4
5const openai = new OpenAI();
6
7// Depending on the day do one of these:
43 console.log(prompt);
44
45 const completion = await openai.chat.completions.create({
46 messages: [
47 { role: "user", content: prompt },