from langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.embeddings.cohere import CohereEmbeddingsfrom langchain.text_splitter import CharacterTextSplitterfrom langchain.vectorstores import Milvusfrom langchain.document_loaders import PyPDFLoader, TextLoaderfrom langchain.chains import RetrievalQAWithSourcesChainimport osfrom langchain.llms import OpenAIOPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")chain = Nonedef do_work():global chainloader = PyPDFLoader("./王天龙-算法工程师-研究生.pdf")docs = loader.load_and_split()embeddings = OpenAIEmbeddings()vector_db = Milvus.from_documents(docs,embeddings,connection_args={"host": "ljxwtl.cn", "port": "19530"})chain = RetrievalQAWithSourcesChain.from_chain_type(OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY),chain_type="map_reduce",retriever=vector_db.as_retriever())def __test_do_work():do_work()def query(question):global chainresponse = chain(inputs={"question": question},return_only_outputs=True)print(response)def __test_query():query("王天龙的简介")if __name__ == '__main__':__test_do_work()__test_query()

from langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.embeddings.cohere import CohereEmbeddingsfrom langchain.text_splitter import CharacterTextSplitterfrom langchain.vectorstores import Milvusfrom langchain.document_loaders import PyPDFLoader, TextLoaderfrom langchain.chains import RetrievalQAWithSourcesChainimport osfrom langchain.llms import OpenAIOPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")chain = Nonedef do_work():global chainloader = PyPDFLoader("./王天龙-算法工程师-研究生.pdf")docs = loader.load_and_split()embeddings = OpenAIEmbeddings()vector_db = Milvus.from_documents(docs,embeddings,connection_args={"host": "ljxwtl.cn", "port": "19530"})chain = RetrievalQAWithSourcesChain.from_chain_type(OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY),chain_type="map_reduce",retriever=vector_db.as_retriever())def __test_do_work():do_work()def query(question):global chainresponse = chain(inputs={"question": question},return_only_outputs=True)print(response)def __test_query():query("王天龙的工作经验")if __name__ == '__main__':__test_do_work()__test_query()