Upload langchain_react.py
Browse files- langchain_react.py +55 -0
langchain_react.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 完整部署示例代码
|
| 2 |
+
from langchain_community.llms import Ollama
|
| 3 |
+
from langchain_community.document_loaders import DirectoryLoader
|
| 4 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_community.vectorstores import Chroma
|
| 6 |
+
from langchain_ollama import OllamaEmbeddings
|
| 7 |
+
from langchain import hub
|
| 8 |
+
from langchain.agents import create_react_agent, AgentExecutor
|
| 9 |
+
from langchain.tools.retriever import create_retriever_tool
|
| 10 |
+
import nltk
|
| 11 |
+
nltk.download('punkt_tab')
|
| 12 |
+
nltk.download('averaged_perceptron_tagger_eng')
|
| 13 |
+
# 首先还得保证下面的库安装了
|
| 14 |
+
# pip install unstructured
|
| 15 |
+
# pip install chromadb
|
| 16 |
+
# pip install typing_extensions (如果python版本是3.8及以下)
|
| 17 |
+
|
| 18 |
+
# 1. 初始化LLM
|
| 19 |
+
llm = Ollama(model="llama3.1:8b-instruct", temperature=0, num_ctx=8192)
|
| 20 |
+
|
| 21 |
+
# 2. 加载和准备文档 TODO: 修改path/to/articles为每个TCE对应的实际目录
|
| 22 |
+
loader = DirectoryLoader('/home/weishaohang/workspace/Omni-Temp/test_articles', glob="**/*.txt")
|
| 23 |
+
# test_articles是一个目录,下面是该次被检索的文件
|
| 24 |
+
# .txt文件是该次被检索的文件
|
| 25 |
+
|
| 26 |
+
documents = loader.load()
|
| 27 |
+
"""[Document(metadata={'source': '/home/weishaohang/workspace/Omni-Temp/test_articles/article2.txt'}, page_content='bbbbbb'), Document(metadata={'source': '/home/weishaohang/workspace/Omni-Temp/test_articles/article3.txt'}, page_content='cccccc'), Document(metadata={'source': '/home/weishaohang/workspace/Omni-Temp/test_articles/article1.txt'}, page_content='aaaaaa')]"""
|
| 28 |
+
|
| 29 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=128) # 512是每个chunk的大小,128是chunk之间的重叠大小。通过重复内容桥接相邻文本块,保障关键信息不因分割而丢失。
|
| 30 |
+
texts = text_splitter.split_documents(documents)
|
| 31 |
+
"""[Document(metadata={'source': '/home/weishaohang/workspace/Omni-Temp/test_articles/article2.txt'}, page_content='bbbbbb'), Document(metadata={'source': '/home/weishaohang/workspace/Omni-Temp/test_articles/article3.txt'}, page_content='cccccc'), Document(metadata={'source': '/home/weishaohang/workspace/Omni-Temp/test_articles/article1.txt'}, page_content='aaaaaa')]"""
|
| 32 |
+
|
| 33 |
+
# 3. 创建向量数据库
|
| 34 |
+
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
| 35 |
+
vectorstore = Chroma.from_documents(documents=texts, embedding=embeddings, persist_directory="./chroma_db")
|
| 36 |
+
print(vectorstore)
|
| 37 |
+
exit()
|
| 38 |
+
|
| 39 |
+
# 4. 创建检索器工具
|
| 40 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 3}) # search_kwargs是搜索参数,k是返回的文档数量
|
| 41 |
+
retriever_tool = create_retriever_tool(
|
| 42 |
+
retriever,
|
| 43 |
+
name="local_knowledge_base",
|
| 44 |
+
description="Search for information in local articles collection."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# 5. 创建ReAct Agent
|
| 48 |
+
prompt = hub.pull("hwchase17/react")
|
| 49 |
+
print(prompt)
|
| 50 |
+
# agent = create_react_agent(llm, [retriever_tool], prompt)
|
| 51 |
+
# agent_executor = AgentExecutor(agent=agent, tools=[retriever_tool], verbose=True)
|
| 52 |
+
|
| 53 |
+
# # 6. 运行Agent
|
| 54 |
+
# response = agent_executor.invoke({"input": "Your benchmark question here?"})
|
| 55 |
+
# print(response['output'])
|