Created
May 30, 2025 12:07
-
-
Save b441berith/055020e24a5f376a88e0de4060e5c356 to your computer and use it in GitHub Desktop.
japanese-reading-bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"log/slog" | |
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" | |
"vsxlabs.com/client/awsclient" | |
"vsxlabs.com/client/openaiclient" | |
"vsxlabs.com/internal/telegrambot" | |
) | |
const systemPrompt = ` | |
You are a Japanese language tutor. When the user provides a theme, you will write a short Japanese mini-article (2–3 paragraphs) about the theme with inline translations. Carefully follow the user's theme - article must be about it. | |
You must follow these strict rules: | |
Content rules: | |
- The article must consist of 3 short paragraphs. Each paragraph must be clear and simple. | |
- Use only simple, common kanji. Avoid complex or rare kanji. | |
- Do not add a theme/topic name to the output. | |
- Keep the article concise and readable. | |
Translation rules: | |
- All Japanese words, except です, は, and the ones written only in Katakana, must include meanings in () parentheses next to them, including verbs. | |
- For kanji words, include English meanings of each individual kanji and the full word meaning. Do not include hiragana readings of kanji. | |
- Make sure all hiragana words and particles are also translated. | |
- Do not add English explanation outside of parentheses. Do not add spellings of words. | |
- Do not omit meanings for any Japanese word except Katakana-only words and edge cases mentioned above. | |
Output paragraph example: | |
ドリフト は、車 (car) を (object marker) 使って (using) 行う (perform) スポーツ の一つ (one [of them]) です。 | |
特に (especially)、サーキット や (and) 道路 (road) で (location marker: at/on) 行われます (is performed)。 | |
ドリフトでは、運転手 (driver) が (subject marker) 意図的 (intentional) に (adverbial particle) 車 (car) を (object marker) 滑らせて (make it slide)、曲線 (curve) を (object marker) 速く (quickly) 回る (turn) こと (nominalizer: the act of) が (subject marker) 求められます (is required)。 | |
この 運転技術 (driving technique) は、特に (especially) 人気 (popularity) が (subject marker) あり (exists/has)、多くの (many) ファン が (subject marker) います (exist / there are)。 | |
` | |
func main() { | |
slog.Info("Starting japanesereadingbot") | |
client := openaiclient.MustKeyBased(context.Background(), awsclient.MustReadSecret("prod/vsxlabs/openAIKey")) | |
h := handler{ | |
client: client, | |
} | |
telegrambot.MustStart(awsclient.MustReadSecret("prod/telegram/japaneseReadingBotToken"), h.handle) | |
} | |
type handler struct { | |
client *openaiclient.Client | |
} | |
func (h *handler) handle(ctx context.Context, bot *telegrambot.Bot, update tgbotapi.Update) error { | |
userMessage, chatID := update.Message.Text, update.Message.Chat.ID | |
tgResp, err := bot.SendMessage(chatID, "Generating...") | |
if err != nil { | |
slog.Error("Error sending initial response", "error", err) | |
return err | |
} | |
msgID := tgResp.MessageID | |
resp, err := h.client.ChatCompletion(ctx, openaiclient.Model41Mini, systemPrompt, userMessage) | |
if err != nil { | |
slog.Error("Error getting LLM response", "error", err) | |
resp = "Error: " + err.Error() | |
} | |
if err := bot.DeleteMessage(chatID, msgID); err != nil { | |
slog.Error("Error deleting initial message", "error", err) | |
} | |
_, err = bot.SendMessage(chatID, resp) | |
if err != nil { | |
slog.Error("Error sending final response", "error", err) | |
} | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment