import os
import gradio as gr
from langchain_groq import ChatGroq
from langchain.prompts import ChatPromptTemplate
from langchain.chains import RetrievalQA, ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
from langchain_chroma import Chroma
def rag_retriever(message, history, system_prompt, num_sources=4, temperature=0):
chat = ChatGroq(temperature=temperature, model_name="llama3-70b-8192", api_key=os.getenv("GROQ_API_KEY"))
embeddings = HuggingFaceEmbeddings(model_name="avsolatorio/GIST-large-Embedding-v0")
store = Chroma(persist_directory='/home/user/app/db/', embedding_function=embeddings, collection_name='ai_act')
prompt_template = ChatPromptTemplate.from_messages([
("system", system_prompt+"""
Use the following pieces of context to answer the user's question.
----------------
{context}"""),
("human", "{question}")
])
memory = ConversationBufferMemory(memory_key="chat_history", output_key="answer", return_messages=True)
retriever = store.as_retriever(search_type="similarity", search_kwargs={'k': num_sources})
chain = ConversationalRetrievalChain.from_llm(llm=chat,
retriever=retriever,
return_source_documents=True,
memory=memory,
combine_docs_chain_kwargs={"prompt": prompt_template})
output = chain.invoke({"question": message})
sources = ""
for doc in output['source_documents']:
source_content = doc.page_content.strip().replace("\r\n", " ").replace("\r", " ").replace("\n", " ")
sources += f'Страница: {doc.metadata["page"]+1}
{source_content}
'
response = f"""