from langchain_anthropic import ChatAnthropic from langchain_cohere import ChatCohere from langchain_groq import ChatGroq from langchain_openai import ChatOpenAI from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from utils import Translation import time import os import gradio as gr NAME2CHAT = {"Cohere": ChatCohere, "claude-3-opus-20240229": ChatAnthropic, "claude-3-sonnet-20240229": ChatAnthropic, "claude-3-haiku-20240307": ChatAnthropic, "llama3-8b-8192": ChatGroq, "llama3-70b-8192": ChatGroq, "mixtral-8x7b-32768": ChatGroq, "gemma-7b-it": ChatGroq, "gpt-4o": ChatOpenAI, "gpt-3.5-turbo-0125": ChatOpenAI} NAME2APIKEY = {"Cohere": "COHERE_API_KEY", "claude-3-opus-20240229": "ANTHROPIC_API_KEY", "claude-3-sonnet-20240229": "ANTHROPIC_API_KEY", "claude-3-haiku-20240307": "ANTHROPIC_API_KEY", "llama3-8b-8192": "GROQ_API_KEY", "llama3-70b-8192": "GROQ_API_KEY", "mixtral-8x7b-32768": "GROQ_API_KEY", "gemma-7b-it": "GROQ_API_KEY", "gpt-4o": "OPENAI_API_KEY", "gpt-3.5-turbo-0125": "OPENAI_API_KEY"} parser = StrOutputParser() def reply(message, history, name, api_key, temperature, max_new_tokens, system_template): global pdfdb os.environ[NAME2APIKEY[name]] = api_key if name == "Cohere": model = NAME2CHAT[name](temperature=temperature, max_tokens=max_new_tokens) else: model = NAME2CHAT[name](model=name,temperature=temperature, max_tokens=max_new_tokens) prompt_template = ChatPromptTemplate.from_messages( [("system", system_template), ("user", "{text}")] ) chain = prompt_template | model | parser txt = Translation(message, "en") if txt.original == "en": response = chain.invoke({"text": message}) r = '' for c in response: r+=c time.sleep(0.001) yield r else: translation = txt.translatef() response = chain.invoke({"text": message}) t = Translation(response, txt.original) res = t.translatef() r = '' for c in res: r+=c time.sleep(0.001) yield r chat_model = gr.Dropdown( [m for m in list(NAME2APIKEY)], label="Chat Model", info="Choose one of the available chat models" ) user_api_key = gr.Textbox( label="API key", info="Paste your API key here", lines=1, type="password", ) user_temperature = gr.Slider(0, 1, value=0.5, label="Temperature", info="Select model temperature") user_max_new_tokens = gr.Slider(0, 8192, value=1024, label="Max new tokens", info="Select max output tokens (higher number of tokens will result in a longer latency)") user_session_id = gr.Textbox(label="System Template",info="Customize your assistant with your instructions",value="You are an helpful assistant") additional_accordion = gr.Accordion(label="Parameters to be set before you start chatting", open=True) demo = gr.ChatInterface(fn=reply, additional_inputs=[chat_model, user_api_key, user_temperature, user_max_new_tokens, user_session_id], additional_inputs_accordion=additional_accordion, title="Chat with Anthropic, OpenAI, Groq and Cohere Models🤖") if __name__=="__main__": demo.launch(server_name="0.0.0.0", share=False)