File size: 2,415 Bytes
beaaf68
 
 
 
 
 
 
 
 
 
6aee7ca
beaaf68
 
 
6aee7ca
 
 
beaaf68
 
 
 
 
 
 
 
 
 
 
 
 
6aee7ca
beaaf68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aee7ca
beaaf68
 
 
 
 
6aee7ca
beaaf68
 
6aee7ca
beaaf68
 
 
6aee7ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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()