1 Star 0 Fork 0

贺辉0912/bigmodel

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
url.py 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
andy.he 提交于 2024-03-01 11:04 . 1
from langchain_community.vectorstores import Chroma
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_community.embeddings import JinaEmbeddings
from langchain_community.llms import Tongyi
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
import os
os.environ["DASHSCOPE_API_KEY"] = "sk-cc1c8314fdbd43ceaf26ec1824d5dd3b"
llm = Tongyi()
from langchain_community.document_loaders import UnstructuredURLLoader
urls = [
"https://en.wikipedia.org/wiki/Android_(operating_system)",
# "https://answer.baidu.com/answer/land?params=MSZrNRfsun5P549PG8zUdV6PXibobtP6242M%2FwAkrm0sWWOs5IAFrz6XAVTZu6sZDpxyjT4AEJir6bCqxEWwPoy%2F7dCKyABf%2BFgxpeKWkX0isoUgqs7ViRSvL3B%2BBsajzbX1Ai05uEVz4Owgwf361B4xj1CwAAbsAD3PBqPnJT4%3D&from=dqa&lid=ebb4fc0600ddb6f0&word=%E4%BB%80%E4%B9%88%E6%98%AF%E8%8B%B9%E6%9E%9C",
# "https://www.understandingwar.org/backgrounder/russian-offensive-campaign-assessment-february-8-2023",
# "https://blog.csdn.net/oHeHui1/article/details/136261119?spm=1001.2014.3001.5502"
]
loader = UnstructuredURLLoader(urls=urls)
documents = loader.load_and_split()
print(documents)
embeddings = JinaEmbeddings(
jina_api_key="jina_c5d02a61c97d4d79b88234362726e94aVLMTvF38wvrElYqpGYSxFtC5Ifhj", model_name="jina-embeddings-v2-base-en"
)
# # 第一次存入本地
vectorstore = Chroma.from_documents(documents, embeddings,persist_directory="./wikipedia")
# # 从本地加载
# vectorstore = Chroma(persist_directory="./wikipedia", embedding_function=embeddings)
retriever = vectorstore.as_retriever()
template = """Answer the question based only on the following context,if can not ,please just say: I do not know,
please think step by step:
{context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
llm = ChatGLM.ChatGLM_LLM(verbose=False)
output_parser = StrOutputParser()
setup_and_retrieval = RunnableParallel(
{"context": retriever, "question": RunnablePassthrough()}
)
chain = setup_and_retrieval | prompt | llm | output_parser
# print(chain.invoke("苹果是什么"))
# print(chain.invoke("苹果有哪些功效"))
# print(chain.invoke("如何创建虚拟环境"))
print(chain.invoke("what is android"))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/he-hui-0912/bigmodel.git
git@gitee.com:he-hui-0912/bigmodel.git
he-hui-0912
bigmodel
bigmodel
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385