Mercurial Hosting > lang
view src/lib/ai/claude/Translator.luan @ 75:b96cf27e719d default tip
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 28 Aug 2025 14:36:03 -0600 |
parents | 64e35a92d163 |
children |
line wrap: on
line source
local Luan = require "luan:Luan.luan" local error = Luan.error local Parsers = require "luan:Parsers.luan" local json_parse = Parsers.json_parse or error() local json_string = Parsers.json_string or error() local Claude = require "site:/lib/ai/claude/Claude.luan" local claude_chat = Claude.chat or error() local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "claude/Translator" local Translator = {} function Translator.translate(text,lang) local thread = { system = `%> Translate <%=lang%> in the text you get to English. Preserve formatting. The text may also contain English. Just leave that unchanged. <% ` messages = {{ role = "user" content = text }} temperature = 0 } local resultJson = claude_chat(thread) local result = json_parse(resultJson) -- logger.info(json_string(result)) result.type == "message" or error() result.role == "assistant" or error() result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error() local content = result.content or error() #content==1 or error() content = content[1] content.type == "text" or error() return content.text or error() end return Translator