import os from langchain_huggingface import HuggingFaceEndpoint from langchain_core.messages import HumanMessage, SystemMessage from langchain_core.messages import AIMessage from langchain_community.chat_message_histories import ChatMessageHistory from langchain_core.chat_history import BaseChatMessageHistory from langchain_core.runnables.history import RunnableWithMessageHistory from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder import gradio as gr # Set your API keys from environment variables langchain_key = os.getenv("LANGCHAIN_API_KEY") HF_key = os.getenv("HUGGINGFACEHUB_TOKEN") LANGCHAIN_TRACING_V2=True LANGCHAIN_ENDPOINT="https://api.smith.langchain.com" LANGCHAIN_PROJECT="LLM_CHATBOT" os.environ["LANGCHAIN_TRACING_V2"] = str(LANGCHAIN_TRACING_V2) os.environ["LANGCHAIN_API_KEY"] = langchain_key os.environ["HUGGINGFACEHUB_TOKEN"] = HF_key os.environ["LANGCHAIN_ENDPOINT"] = LANGCHAIN_ENDPOINT os.environ["LANGCHAIN_PROJECT"] = LANGCHAIN_PROJECT # Initialize the Chat Model llm = HuggingFaceEndpoint( repo_id="microsoft/Phi-3-vision-128k-instruct", task="text-generation", max_new_tokens=150, do_sample=False, token =HF_key ) # Create a Chat Prompt Template prompt = ChatPromptTemplate.from_messages( [ ("system", "You are a helpful assistant. Answer all questions to the best of your ability."), MessagesPlaceholder(variable_name="messages"), ] ) # Set up the chain chain = prompt | llm # Set up message history store = {} def get_session_history(session_id: str) -> BaseChatMessageHistory: if session_id not in store: store[session_id] = ChatMessageHistory() return store[session_id] with_message_history = RunnableWithMessageHistory(chain, get_session_history) # Gradio chat function def chat(session_id, user_input): config = {"configurable": {"session_id": session_id}} human_message = HumanMessage(content=user_input) response = with_message_history.invoke({"messages": [human_message]}, config=config) return response # Gradio interface iface = gr.Interface( fn=chat, inputs=[gr.Textbox(lines=1, placeholder="Enter Session ID"), gr.Textbox(lines=7, placeholder="Enter your message")], outputs="text", title="LangChain Chatbot", description="A chatbot that remembers your past interactions. Enter your session ID and message." ) # Launch the app iface.launch()