Files
chatgpt-telegram-bot/bot/plugins/ddg_translate.py
2023-11-23 21:08:01 +05:00

32 lines
1.0 KiB
Python

from typing import Dict
from duckduckgo_search import DDGS
from .plugin import Plugin
class DDGTranslatePlugin(Plugin):
"""
A plugin to translate a given text from a language to another, using DuckDuckGo
"""
def get_source_name(self) -> str:
return "DuckDuckGo Translate"
def get_spec(self) -> [Dict]:
return [{
"name": "translate",
"description": "Translate a given text from a language to another",
"parameters": {
"type": "object",
"properties": {
"text": {"type": "string", "description": "The text to translate"},
"to_language": {"type": "string", "description": "The language to translate to (e.g. 'it')"}
},
"required": ["text", "to_language"],
},
}]
async def execute(self, function_name, helper, **kwargs) -> Dict:
with DDGS() as ddgs:
return ddgs.translate(kwargs['text'], to=kwargs['to_language'])