Skip to content

Commit

Permalink
Merge branch 'feat/1606-Deepl-integration-new' into 'sncf-master'
Browse files Browse the repository at this point in the history
theopenconversationkit#1606 Add Deepl translator module - second set of corrections

See merge request factory-ia/bots/tock!3
  • Loading branch information
Hervé-vandenbulcke Fabien committed Jul 10, 2024
2 parents a198d17 + 06f720f commit 130b955
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
40 changes: 31 additions & 9 deletions translator/deepl-translate/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
Module for translation with Deepl
# TOCK Deepl Translation

Here are the configurable variables:

- tock_translator_deepl_target_languages : set of supported languages - ex : en,es
- tock_translator_deepl_api_url : Deepl api url (default pro api url : https://api.deepl.com/v2/translate).
- `tock_translator_deepl_target_languages`: set of supported languages - ex : en,es
- `tock_translator_deepl_api_url`: Deepl api url (default pro api url : https://api.deepl.com/v2/translate).
If you have problems with pro api, you can use free api : https://api-free.deepl.com/v2/translate
- tock_translator_deepl_api_key : Deepl api key to use (see your account)
- tock_translator_deepl_glossaryId: glossary identifier to use in translation
- `tock_translator_deepl_api_key` : Deepl api key to use (see your account)
- `tock_translator_deepl_glossary_id`: glossary identifier to use in translation

Deepl documentation: https://developers.deepl.com/docs
> Deepl documentation: https://developers.deepl.com/docs
To integrate the module into a custom Tock Admin, pass the module as a parameter to the ai.tock.nlp.admin.startAdminServer() function.
To integrate the module into a custom Tock Admin, pass the module as a parameter to the `ai.tock.nlp.admin.startAdminServer()` function.

Example:

```kt
package ai.tock.bot.admin

import ai.tock.nlp.admin.startAdminServer
import ai.tock.translator.deepl.deeplTranslatorModule

fun main() {
startAdminServer(deeplTranslatorModule)
}
startAdminServer(deeplTranslatorModule())
}
```

## Http Client Configuration

You can configure the Deepl client, including proxy settings, by passing a parameter to `deeplTranslatorModule`:

```kt
startAdminServer(deeplTranslatorModule(OkHttpDeeplClient {
proxyAuthenticator { _: Route?, response: Response ->
// https://square.github.io/okhttp/3.x/okhttp/index.html?okhttp3/Authenticator.html
if (response.challenges().any { it.scheme.equals("OkHttp-Preemptive", ignoreCase = true) }) {
response.request.newBuilder()
.header("Proxy-Authorization", credential)
.build()
} else {
null
}
}
}))
```
26 changes: 21 additions & 5 deletions translator/deepl-translate/src/main/kotlin/DeeplClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package ai.tock.translator.deepl

import ai.tock.shared.jackson.mapper
import ai.tock.shared.property
import ai.tock.shared.propertyOrNull
import com.fasterxml.jackson.module.kotlin.readValue
import java.io.IOException
import java.util.regex.Pattern
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
import java.util.regex.Pattern

internal data class TranslationResponse(
val translations: List<Translation>
Expand All @@ -34,8 +36,22 @@ internal data class Translation(

const val TAG_HANDLING = "xml"

internal class DeeplClient(private val apiURL: String, private val apiKey: String?) {
private val client = OkHttpClient()
interface DeeplClient {
fun translate(
text: String,
sourceLang: String,
targetLang: String,
preserveFormatting: Boolean,
glossaryId: String?
): String?
}

class OkHttpDeeplClient(
private val apiURL: String = property("tock_translator_deepl_api_url", "https://api.deepl.com/v2/translate"),
private val apiKey: String? = propertyOrNull("tock_translator_deepl_api_key"),
okHttpCustomizer: OkHttpClient.Builder.() -> Unit = {}
) : DeeplClient {
private val client = OkHttpClient.Builder().apply(okHttpCustomizer).build()

private fun replaceSpecificPlaceholders(text: String): Pair<String, List<String>> {
// Store original placeholders for later restoration
Expand All @@ -61,7 +77,7 @@ internal class DeeplClient(private val apiURL: String, private val apiKey: Strin
return resultText
}

fun translate(
override fun translate(
text: String,
sourceLang: String,
targetLang: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@

package ai.tock.translator.deepl

import ai.tock.shared.property
import ai.tock.shared.propertyOrNull
import ai.tock.translator.TranslatorEngine
import org.apache.commons.text.StringEscapeUtils
import java.util.Locale
import org.apache.commons.text.StringEscapeUtils

internal object DeeplTranslatorEngine : TranslatorEngine {
internal class DeeplTranslatorEngine(client: DeeplClient) : TranslatorEngine {
private val supportedLanguagesProperty = propertyOrNull("tock_translator_deepl_target_languages")
private val supportedLanguages: Set<String>? = supportedLanguagesProperty?.split(",")?.map { it.trim() }?.toSet()

private val deeplClient = DeeplClient(
property("tock_translator_deepl_api_url", "https://api.deepl.com/v2/translate"),
propertyOrNull("tock_translator_deepl_api_key")
)
private val glossaryId = propertyOrNull("tock_translator_deepl_glossaryId")
private val deeplClient = client
private val glossaryId = propertyOrNull("tock_translator_deepl_glossary_id")
override val supportAdminTranslation: Boolean = true

override fun translate(text: String, source: Locale, target: Locale): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ import com.github.salomonbrys.kodein.Kodein
import com.github.salomonbrys.kodein.bind
import com.github.salomonbrys.kodein.provider

val deeplTranslatorModule = Kodein.Module {
bind<TranslatorEngine>(overrides = true) with provider { DeeplTranslatorEngine }
fun deeplTranslatorModule(client: DeeplClient = OkHttpDeeplClient()) = Kodein.Module {
bind<TranslatorEngine>(overrides = true) with provider { DeeplTranslatorEngine(client) }
}

0 comments on commit 130b955

Please sign in to comment.