import os from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings from langchain.vectorstores import Chroma from langchain.chat_models import ChatOpenAI from langchain.chains import ConversationalRetrievalChain from langchain.memory import ConversationBufferMemory from langchain.llms import HuggingFaceHub from dotenv import load_dotenv load_dotenv() def create_conversation() -> ConversationalRetrievalChain: persist_directory = 'db' embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl") db = Chroma( persist_directory=persist_directory, embedding_function=embeddings ) memory = ConversationBufferMemory( memory_key='chat_history', return_messages=False ) qa = ConversationalRetrievalChain.from_llm( llm=HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512}), chain_type='stuff', retriever=db.as_retriever(), memory=memory, get_chat_history=lambda h: h, verbose=True ) return qa