1. Get an TuneAI key from the end user.
  2. Validate the user’s TuneAI key.
  3. Get a text prompt, source language and target language from the user.
  4. Authenticate TuneAI with the user’s key.
  5. Send the user’s prompt to invoke langchain functionalities.
  6. Get a response and display it.

Translating text using the LangChain API

  1. Import the necessary python libraries
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

import streamlit as st
  1. Create a function called create_chat_openai_instance that takes a parameter tuneai_api_keyThis function is responsible for instantiating the langchain ChatOpenAI class and configuring it withtuneai_api_key provided by the user.

The ChatOpenAI instance is created with the following arguments

  • openai_api_key: This argument is set to the value of tuneai_api_key
  • base_url: This argument is set to "", which is the base URL for the OpenAI API endpoint
  • model: This argument is set to "rohan/tune-grok-mixtral-8x7b", which is the language model that will be used by ChatOpenAI instance
def create_chat_openai_instance(tuneai_api_key):
    return ChatOpenAI(
  1. Create a function format_prompt that takes source_language, target_language, input_text as parameters and returns the formatted prompt. This function is responsible to format the chat prompt for the translation.
def format_prompt(source_language, target_language, input_text):
    return ChatPromptTemplate.from_messages([
    ("system", "Translate from {source_language} to {target_language}. You respond only in {target_language}"),
    ("user", "{input}")
  1. Create a function translate process the data fetched from the ui and returns the translated text. It calls create_chat_openai_instance to get the llm, formats the prompt using format_prompt, invokes the llm chain, parses the output and returns the response
def translate(input_text, source_language, target_language, tuneai_api_key):
    llm = create_chat_openai_instance(tuneai_api_key)
    prompt = format_prompt(source_language, target_language, input_text)
    output_parser = StrOutputParser()
    chain = prompt | llm | output_parser
    response = chain.invoke({"input": input_text, "source_language": source_language, "target_language": target_language})
    return response

Building the app using Streamlit

  1. Import the necessary python libraries

  2. Create the app’s title using st.title
st.title('Tune Translator App')
  1. Add a text input box for the user to enter their TuneAI API key.
tuneai_api_key = st.sidebar.text_input('TuneAI API Key', type='password')
  1. In the form, get user’s prompt, souce_language and target_language. Add a submit button that allows users to record their text
with st.form('my_form'):
    user_text = st.text_area("Enter text to translate")
    source_language = st.selectbox("Select source language", ["english", "hindi", "french", "sanskrit", "spanish", "german"])
    target_language = st.selectbox("Select target language", ["english", "hindi", "french", "sanskrit", "spanish", "german"])
    submit_text = st.form_submit_button('Submit')
  1. Once the user has submitted the prompt, handle the necessary edge cases. Get the response from the translate function created in the previous section and display it in the app using
    if submit_text:
        if not tuneai_api_key.startswith('nbx'):
            st.warning('Please enter your TuneAI API key!', icon='⚠')
        elif source_language == target_language:
            st.warning('Source and target languages must be different!', icon='⚠')
        elif user_text == "":
            st.warning('Please enter text to translate!', icon='⚠')
        elif source_language == "":
            st.warning('Please select a source language!', icon='⚠')
        elif target_language == "":
            st.warning('Please select a target language!', icon='⚠')
            # Call the translation function here
            response = langchain_translator.translate(user_text, source_language, target_language, tuneai_api_key)

You can find the complete code for the translator app here