ChatwithLLM / app.py
Npps's picture
Update app.py
8f8a5f2 verified
raw
history blame
2.88 kB
import os
import uuid
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)
try:
response = with_message_history.invoke({"messages": [human_message]}, config=config)
except Exception as e:
response = AIMessage(content=f"An error occurred: {str(e)}")
return response
# Gradio session function
def get_session_id(request: gr.Request):
session_id = request.cookies.get("session_id")
if not session_id:
session_id = str(uuid.uuid4())
response = gr.Response().cookies.set("session_id", session_id)
else:
response = gr.Response()
return session_id, response
# Gradio interface
iface = gr.Interface(
fn=chat,
inputs=[gr.Textbox(lines=1, placeholder="Enter your message")],
outputs="text",
title="LangChain Chatbot",
description="A chatbot that remembers your past interactions. Enter your message."
)
# Add session management
iface = iface.wrap_inputs(get_session_id, gr.Textbox)
# Launch the app
iface.launch()