Here we will walk you through a minimal working example of using MTLLM to generate translate a sentence from English to a target language.
Before we start, make sure you have installed MTLLM & Jaclang. If not, follow the instructions here. Following code snippet will be our starting point:
1linkcan translate(eng_sentence: str, target_lang: str) -> str {
2link """Normally this would include the translation logic such as calling an API.
3link For the sake of this example, we will return a dummy translated sentence."""
4link
5link return "Hola Mundo";
6link}
7link
8linkwith entry {
9link print(translate("Hello World", "es"));
10link}
Assuming we went with API based translation, target_lang
would be the language code of the target language. For example, es
for Spanish, fr
for French, etc. But Assume that you don't know the language code for the target language, or you would like to provide a context to target_lang
instead of a language code. for example Spanish
instead of es
or Language spoken in Somalia
. This is where you need the help of LLMs.
For this example, we will use OpenAI's GPT-3.5-turbo (default).
1linkimport:py from mtllm.llms, OpenAI;
2link
3linkllm = OpenAI();
4link
5link# Rest of the code
by LLM
keyword1linkimport:py from mtllm.llms, OpenAI;
2link
3linkllm = OpenAI();
4link
5linkcan translate(eng_sentence: str, target_lang: str) -> str by llm;
6link
7linkwith entry {
8link print(translate("Hello World", "Language spoken in Somalia"));
9link}
Thats it! 🎊
Now you can run the code and see the translated sentence by running the following command:
Makesure to export your OpenAI API key as an environment variable OPENAI_API_KEY
before running the code.
1link$jac run translator.jac
In this example, we dont need to add any additional support to the LLMs. But if you want to add additional support, you can do so by adding SemStrings
to variables, output type hint and abilities the following code snippet:
1linkimport:py from mtllm.llms, OpenAI;
2link
3linkllm = OpenAI();
4link
5linkcan 'Translate the given english sentence to the target language'
6linktranslate(eng_sentence: str, target_lang: str) -> 'Translation': str by llm;
7link
8linkwith entry {
9link print(translate("Hello World", "Language spoken in Somalia"));
10link}
You've successfully created a working example using the Jaclang and MTLLM.
Feel free to adapt and expand upon this example to suit your specific use case while exploring the extensive capabilities of MTLLM.