ai creates booking

This commit is contained in:
androidlover5842
2026-01-24 19:22:37 +05:30
parent b8c9f8dac4
commit ac79d6d1c0
18 changed files with 559 additions and 12 deletions

View File

@@ -0,0 +1,85 @@
package com.android.trisolarisserver.component
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.PDPage
import org.apache.pdfbox.pdmodel.PDPageContentStream
import org.apache.pdfbox.pdmodel.common.PDRectangle
import org.apache.pdfbox.pdmodel.font.PDType1Font
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.time.OffsetDateTime
import java.util.UUID
@Component
class EmailStorage(
@Value("\${storage.emails.root:/home/androidlover5842/docs/emails}")
private val rootPath: String
) {
fun storePdf(propertyId: UUID?, messageId: String?, subject: String?, body: String): String {
val dir = if (propertyId != null) {
Paths.get(rootPath, propertyId.toString())
} else {
Paths.get(rootPath, "unassigned")
}
Files.createDirectories(dir)
val safeName = (messageId ?: UUID.randomUUID().toString()).replace(Regex("[^A-Za-z0-9._-]"), "_")
val fileName = "${safeName}_${OffsetDateTime.now().toEpochSecond()}.pdf"
val path = dir.resolve(fileName).normalize()
val document = PDDocument()
val page = PDPage(PDRectangle.LETTER)
document.addPage(page)
val header = "Subject: ${subject ?: ""}\n\n"
writeText(document, page, header + body)
document.save(path.toFile())
document.close()
return path.toString()
}
private fun writeText(document: PDDocument, firstPage: PDPage, text: String) {
var page = firstPage
var y = 730f
val marginX = 50f
val lineHeight = 14f
val maxLines = 48
var linesOnPage = 0
var content = PDPageContentStream(document, page)
content.beginText()
content.setFont(PDType1Font.HELVETICA, 11f)
content.newLineAtOffset(marginX, y)
fun newLine() {
content.newLineAtOffset(0f, -lineHeight)
y -= lineHeight
linesOnPage++
if (linesOnPage >= maxLines) {
content.endText()
content.close()
page = PDPage(PDRectangle.LETTER)
document.addPage(page)
content = PDPageContentStream(document, page)
content.beginText()
content.setFont(PDType1Font.HELVETICA, 11f)
y = 730f
linesOnPage = 0
content.newLineAtOffset(marginX, y)
}
}
val lines = text.split("\n")
for (line in lines) {
val chunks = line.chunked(90)
for (chunk in chunks) {
content.showText(chunk)
newLine()
}
}
content.endText()
content.close()
}
}

View File

@@ -15,10 +15,20 @@ class LlamaClient(
@Value("\${ai.llama.baseUrl}")
private val baseUrl: String
) {
private val systemPrompt =
"Look only at visible text. " +
"Return the exact text you can read verbatim. " +
"If the text is unclear, partial, or inferred, return NOT CLEARLY VISIBLE. " +
"Do not guess. Do not explain."
fun ask(imageUrl: String, question: String): String {
val payload = mapOf(
"model" to "qwen",
"messages" to listOf(
mapOf(
"role" to "system",
"content" to systemPrompt
),
mapOf(
"role" to "user",
"content" to listOf(
@@ -36,4 +46,27 @@ class LlamaClient(
val node = objectMapper.readTree(body)
return node.path("choices").path(0).path("message").path("content").asText()
}
fun askText(content: String, question: String): String {
val payload = mapOf(
"model" to "qwen",
"messages" to listOf(
mapOf(
"role" to "system",
"content" to systemPrompt
),
mapOf(
"role" to "user",
"content" to "${question}\n\nEMAIL:\n${content}"
)
)
)
val headers = HttpHeaders()
headers.contentType = MediaType.APPLICATION_JSON
val entity = HttpEntity(payload, headers)
val response = restTemplate.postForEntity(baseUrl, entity, String::class.java)
val body = response.body ?: return ""
val node = objectMapper.readTree(body)
return node.path("choices").path(0).path("message").path("content").asText()
}
}