Spaces:
Sleeping
Sleeping
File size: 2,575 Bytes
5e7788a 87d0d98 f026756 87d0d98 54dcef0 1c049c6 87d0d98 54dcef0 1c049c6 54dcef0 87d0d98 54dcef0 1c049c6 87d0d98 54dcef0 1c049c6 87d0d98 54dcef0 87d0d98 54dcef0 f026756 87d0d98 54dcef0 1c049c6 ae6a72f 54dcef0 1c049c6 f026756 ae6a72f 87d0d98 54dcef0 f026756 54dcef0 87d0d98 f026756 87d0d98 54dcef0 87d0d98 f026756 87d0d98 f026756 87d0d98 f026756 54dcef0 87d0d98 54dcef0 87d0d98 54dcef0 1c049c6 f026756 07a238c f026756 5e7788a 54dcef0 |
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 73 74 75 76 77 78 79 80 81 82 |
import gradio as gr
from langchain.chains import create_retrieval_chain
from langchain.vectorstores import Chroma
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from langchain.memory.chat_message_histories import ChatMessageHistory
from langchain_openai import ChatOpenAI
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.embeddings import HuggingFaceEmbeddings
# Embedding model
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# Vector store setup
persist_directory = 'vec_db'
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_model)
vectordb_retriever = vectordb.as_retriever(search_kwargs={'k': 5})
# LLM
llm = ChatOpenAI(model="gpt-4.1-nano", temperature=0.7)
# Load instructions
with open("instructions.txt", 'r') as file:
instructions = file.read()
# Custom prompt
custom_prompt = ChatPromptTemplate.from_messages([
("system", instructions),
MessagesPlaceholder(variable_name="chat_history"),
("user", "Question: {input}\nContext: {context}")
])
# Memory
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
# Chains
question_answer_chain = create_stuff_documents_chain(llm, custom_prompt)
chain = create_retrieval_chain(vectordb_retriever, question_answer_chain)
# Main interaction function
def conversate_assistant(query, history):
greetings = {"hey", "hi", "hello"}
normalized_query = query.strip().lower()
# Load the last 6 messages from memory
chat_history = memory.load_memory_variables({})["chat_history"]
chat_history = chat_history[-6:] if len(chat_history) >= 6 else chat_history
# If greeting, skip context retrieval
if normalized_query in greetings:
response = question_answer_chain.invoke({
"input": query,
"context": [], # Empty context for greetings
"chat_history": chat_history
})
answer = response
else:
response = chain.invoke({
"input": query,
"chat_history": chat_history
})
answer = response['answer']
# Save the interaction in memory
memory.save_context({"input": query}, {"output": answer})
return answer
# Gradio interface
demo = gr.ChatInterface(
conversate_assistant,
type="messages",
title="Mini-Sid Ai Assistant"
)
# Launch the app
demo.launch()
|