• LangChain结合milvus向量数据库以及GPT3.5结合做知识库问答之二 --->代码实现


    1. from langchain.embeddings.openai import OpenAIEmbeddings
    2. from langchain.embeddings.cohere import CohereEmbeddings
    3. from langchain.text_splitter import CharacterTextSplitter
    4. from langchain.vectorstores import Milvus
    5. from langchain.document_loaders import PyPDFLoader, TextLoader
    6. from langchain.chains import RetrievalQAWithSourcesChain
    7. import os
    8. from langchain.llms import OpenAI
    9. OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
    10. chain = None
    11. def do_work():
    12. global chain
    13. loader = PyPDFLoader("./王天龙-算法工程师-研究生.pdf")
    14. docs = loader.load_and_split()
    15. embeddings = OpenAIEmbeddings()
    16. vector_db = Milvus.from_documents(
    17. docs,
    18. embeddings,
    19. connection_args={
    20. "host": "ljxwtl.cn", "port": "19530"
    21. }
    22. )
    23. chain = RetrievalQAWithSourcesChain.from_chain_type(
    24. OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY),
    25. chain_type="map_reduce",
    26. retriever=vector_db.as_retriever()
    27. )
    28. def __test_do_work():
    29. do_work()
    30. def query(question):
    31. global chain
    32. response = chain(
    33. inputs={"question": question},
    34. return_only_outputs=True
    35. )
    36. print(response)
    37. def __test_query():
    38. query("王天龙的简介")
    39. if __name__ == '__main__':
    40. __test_do_work()
    41. __test_query()

     

    1. from langchain.embeddings.openai import OpenAIEmbeddings
    2. from langchain.embeddings.cohere import CohereEmbeddings
    3. from langchain.text_splitter import CharacterTextSplitter
    4. from langchain.vectorstores import Milvus
    5. from langchain.document_loaders import PyPDFLoader, TextLoader
    6. from langchain.chains import RetrievalQAWithSourcesChain
    7. import os
    8. from langchain.llms import OpenAI
    9. OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
    10. chain = None
    11. def do_work():
    12. global chain
    13. loader = PyPDFLoader("./王天龙-算法工程师-研究生.pdf")
    14. docs = loader.load_and_split()
    15. embeddings = OpenAIEmbeddings()
    16. vector_db = Milvus.from_documents(
    17. docs,
    18. embeddings,
    19. connection_args={
    20. "host": "ljxwtl.cn", "port": "19530"
    21. }
    22. )
    23. chain = RetrievalQAWithSourcesChain.from_chain_type(
    24. OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY),
    25. chain_type="map_reduce",
    26. retriever=vector_db.as_retriever()
    27. )
    28. def __test_do_work():
    29. do_work()
    30. def query(question):
    31. global chain
    32. response = chain(
    33. inputs={"question": question},
    34. return_only_outputs=True
    35. )
    36. print(response)
    37. def __test_query():
    38. query("王天龙的工作经验")
    39. if __name__ == '__main__':
    40. __test_do_work()
    41. __test_query()

  • 相关阅读:
    独立站市场回暖,三个角度深度剖析广阔前景
    C# 基础面试题(万字)
    HashMap安全嘛? 不安全该怎么办【几种解决方法】【详细】
    【PTA-训练day10】L2-022 重排链表 + L1-043 阅览室
    简单回顾矩阵的相乘(点乘)230101
    卖出看涨期权和买入看跌期权有什么区别?卖出看跌期权有什么用?
    Ovalbumin-PEG-DMPE 鸡卵白蛋白-聚乙二醇-二肉豆蔻酰基磷脂酰乙醇胺,DMPE-OVA卵清蛋白
    网络架构新升级:400G OSFP光模块系列产品概述
    Git-03-分支操作
    shell脚本如何判断上一个命令执行是否成功
  • 原文地址:https://blog.csdn.net/wtl1992/article/details/133846133