LangChain Document Loader
%%bash
pip install faiss-cpu
Faiss 是一个用于高维向量相似性搜索和聚类的库,它支持 GPU 运算加速。要让 Faiss 使用 GPU,需要执行以下步骤:
确认您的计算机上已经安装了 CUDA 和 cuDNN。Faiss 支持的 CUDA 版本可以在 Faiss 文档中查看。
安装 Faiss GPU 版本。您可以使用 pip 安装 Faiss GPU 版本,方法是执行以下命令:
pip install faiss-gpu在代码中指定 Faiss 使用 GPU 进行计算。在创建 Faiss Index 对象时,可以指定使用哪个设备。例如,如果要使用默认的 GPU 设备,可以执行以下代码:
import faiss # 创建一个 128 维的向量空间 d = 128 # 创建一个 IndexFlatL2 对象,使用默认的 GPU 设备 index = faiss.IndexFlatL2(d)
如果您有多个 GPU 设备,也可以在创建 Index 对象时指定使用哪个 GPU。例如,如果要使用第二个 GPU 设备,可以执行以下代码:
index = faiss.IndexFlatL2(d) co = faiss.GpuMultipleClonerOptions() co.select_device(1) index = faiss.index_cpu_to_gpu(faiss.StandardGpuResources(), 1, index, co)
在这个例子中,我们使用了
faiss.GpuMultipleClonerOptions()创建了一个选项对象,然后调用了co.select_device(1)方法,将第二个 GPU 设备选为使用的设备。最后,我们使用faiss.index_cpu_to_gpu()方法将 Index 对象复制到 GPU 上。
注意,使用 Faiss GPU 版本时,所有输入向量都必须是 float32 类型。如果您的向量是其他类型,需要先进行类型转换。此外,您需要将所有向量一起加载到 GPU 内存中,以便 Faiss 可以使用 GPU 进行计算。您可以使用 faiss.StandardGpuResources() 创建一个 GPU 资源对象,然后使用 index.add() 方法将向量添加到 Index 对象中。
%%bash
pip install --upgrade faiss-gpu
在 Mac 上安装 CUDA 驱动程序需要进行以下步骤:
首先,你需要安装 Xcode 和 CUDA Toolkit。如果你已经安装了 Xcode,你可以在终端中输入以下命令来安装 CUDA Toolkit:
brew install --cask cuda安装完成后,你需要将 CUDA 环境变量添加到你的
~/.bash_profile文件中。你可以使用以下命令来打开该文件:open ~/.bash_profile然后在文件末尾添加以下行:
export PATH="/usr/local/cuda/bin:$PATH" export DYLD_LIBRARY_PATH="/usr/local/cuda/lib:$DYLD_LIBRARY_PATH"接下来,你需要安装 cuDNN(CUDA Deep Neural Network library)。你可以在 NVIDIA 的网站上下载 cuDNN 库,下载完成后,你需要将库文件解压到
/usr/local/cuda/lib目录下。你可以使用以下命令来解压文件:tar -xvf cudnn-11.4-macos-arm64-v8.2.4.15.tgz sudo cp cuda/include/cudnn*.h /usr/local/cuda/include sudo cp cuda/lib/libcudnn* /usr/local/cuda/lib sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib/libcudnn*最后,你需要重新启动终端,以使环境变量生效。你可以使用以下命令来检查 CUDA 是否已正确安装:
nvcc -V如果 CUDA 已正确安装,则会输出 CUDA 版本号和其他信息。
请注意,AMD Radeon Pro 5500M 是一款 AMD 显卡,而 CUDA 是 NVIDIA 的技术。因此,如果你需要使用 CUDA,你需要在你的 Mac 上安装 NVIDIA 显卡。如果你想使用 AMD 显卡来进行深度学习等计算密集型任务,你可以考虑使用其他的技术,如 ROCm。
%%bash
HOMEBREW_NO_AUTO_UPDATE=1 brew install --cask cuda
ROCm(Radeon Open Compute)是一款由 AMD 开发的开源软件平台,旨在为 AMD 显卡提供高性能计算和深度学习功能。ROCm 包括对编程语言和编程模型的支持,包括 OpenMP、OpenCL、HIP(Heterogeneous-compute Interface for Portability)、TensorFlow 和 PyTorch 等,可以为各种计算密集型应用提供支持。
ROCm 支持 Linux 平台和部分 Windows 平台,以及 AMD 的 Radeon 显卡和服务器级显卡,如 Radeon Instinct。ROCm 还支持一些第三方硬件,如 IBM 的 PowerPC 平台和 Cavium 的 ThunderX2。
ROCm 不仅提供了 GPU 加速的计算能力,还支持 CPU 和 GPU 的协同计算,可以提高整个系统的计算性能。此外,ROCm 也为深度学习提供了一些特定的工具和库,如 MIOpen 和 RCCL。
总之,ROCm 是一款功能强大的开源软件平台,为 AMD 显卡用户提供了许多高性能计算和深度学习的功能和工具。
Faiss是一款基于GPU的高性能相似度搜索库,可以用于许多机器学习和深度学习任务。Faiss可以使用ROCm进行加速,从而提高搜索效率和准确性。
以下是在Mac上使用ROCm和Faiss的一些基本步骤:
首先,你需要安装ROCm。你可以从官方网站下载并安装ROCm。
安装完成后,你需要设置一些环境变量。你可以在你的
~/.bash_profile文件中添加以下行:
export PATH=$PATH:/opt/rocm/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/rocm/lib:/opt/rocm/hip/lib
export HIP_PLATFORM=hcc
export HCC_HOME=/opt/rocm/hcc
- 然后,你需要安装Faiss。你可以使用以下命令在终端中安装Faiss:
pip install faiss-gpu
- 安装完成后,你可以使用以下示例代码来测试Faiss是否正常工作:
import numpy as np
import faiss
# 生成一些随机数据
d = 64 # 向量维度
nb = 100000 # 向量数量
xb = np.random.random((nb, d)).astype('float32')
# 构建索引
index = faiss.IndexFlatL2(d)
print(index.is_trained)
index.add(xb)
# 搜索
k = 4
xq = np.random.random((1, d)).astype('float32')
D, I = index.search(xq, k)
print(I)
print(D)
如果Faiss正常工作,则应该能够正确地输出搜索结果。
注意,使用ROCm加速Faiss需要满足一些硬件要求,如AMD显卡和支持ROCm的CPU。如果你的硬件不符合要求,你可能无法使用ROCm加速Faiss,或者搜索效率和准确性可能会受到影响。
%%bash
pip install langchain
%%bash
wget -r -A .html -P rtdocs https://langchain.readthedocs.io/en/latest/
from langchain.document_loaders import ReadTheDocsLoader
ReadTheDocsLoader('rtdocs', features='lxml').load()
%%bash
rm -rf rtdocs
TODO:
- 提示模版的中文化
- 启示、自我提示、自我认知的模型化?
Introduction¶
Have you ever encountered a problem when using ChatGPT to search for the latest information? The current language model of ChatGPT (gpt-3.5-turbo-0301) was trained on data up until September 2021, so it may not be able to answer questions about the latest information accurately.
In this article, we will explain how to create a chatbot that can use chain of thought to respond, by teaching ChatGPT new knowledge.
Preparing and importing training data¶
First, clone a repository as training data.
Next, import the repository files as text files using the following code, which contains the OpenAI API key.
import os
import pickle
from langchain.document_loaders import DirectoryLoader, TextLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores.faiss import FAISS
def get_docs(dir_name):
# (1) Import a series of documents.
loader = DirectoryLoader(dir_name, loader_cls=TextLoader, silent_errors=True)
raw_documents = loader.load()
# (2) Split them into small chunks.
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=800,
chunk_overlap=200,
)
return text_splitter.split_documents(raw_documents)
def ingest_docs(dir_name):
documents = get_docs(dir_name)
# (3) Create embeddings for each document (using text-embedding-ada-002).
embeddings = OpenAIEmbeddings()
return FAISS.from_documents(documents, embeddings)
vectorstore = ingest_docs('_posts/ultimate-facts')
import os
from EdgeGPT import Chatbot as Bing, ConversationStyle
bing = Bing(cookiePath = os.path.expanduser('~/.config/EdgeGPT/cookies.json'))
async def ask(prompt):
res = (await bing.ask(
prompt = prompt,
conversation_style = ConversationStyle.balanced,
))['item']['messages'][1]
print(res['text'])
print('\n---\n')
print(res['adaptiveCards'][0]['body'][0]['text'])
await ask('''
text-embedding-ada-002 是什么?
''')
Creating a chatbot¶
Now, we will create a simple chatbot using the LLM chain.
/usr/local/anaconda3/envs/biobot/lib/python3.10/site-packages/langchain/chains/conversational_retrieval/base.py:191: UserWarning: `ChatVectorDBChain` is deprecated - please use `from langchain.chains import ConversationalRetrievalChain`
LangChain GitHub
- LangChain Document Loaders
- Loader that uses Selenium to load URLs
- Loader that uses Playwright to load URLs
- Loads files from a Git repository
- Loader that loads .ipynb notebook files
- Load HTML files of URLs => from unstructured.partition.auto import partition
- Read tweets of user twitter handle
- Load data from Google Drive
- Load PDF files
- Load Python files as text files
- Load EPub files as unstructured files
from langchain.chains.llm import LLMChain
from langchain.callbacks.base import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.chains.chat_vector_db.prompts import CONDENSE_QUESTION_PROMPT, QA_PROMPT
from langchain.chains.question_answering import load_qa_chain
from langchain.vectorstores.base import VectorStore
from langchain.chains import ConversationalRetrievalChain
from langchain.chat_models import ChatOpenAI
# Callback function to stream answers to stdout.
manager = CallbackManager([StreamingStdOutCallbackHandler()])
streaming_llm = ChatOpenAI(streaming=True, callback_manager=manager, verbose=True, temperature=0)
question_gen_llm = ChatOpenAI(temperature=0, verbose=True, callback_manager=manager)
# Prompt to generate independent questions by incorporating chat history and a new question.
question_generator = LLMChain(llm=question_gen_llm, prompt=CONDENSE_QUESTION_PROMPT)
# Pass in documents and a standalone prompt to answer questions.
doc_chain = load_qa_chain(streaming_llm, chain_type='stuff', prompt=QA_PROMPT)
# Generate prompts from embedding model.
qa = ConversationalRetrievalChain(retriever=vectorstore.as_retriever(), combine_docs_chain=doc_chain, question_generator=question_generator)
The prompt given to ChatGPT's API is created in the following steps.
question = 'What makes Remix different from existing frameworks? Please list in bullet points in English.'
qa({'question': question, 'chat_history': []})
question = '你知道什么?'
qa({'question': question, 'chat_history': []})
question = '你知道什么?'
qa({'question': question, 'chat_history': []})
Ask a question¶
How is Remix different from existing frameworks?
Please list in bullet points in Japanese.
The following bullet points will be output as an answer summarizing the context.
# Get context related to the question from the embedding model
for context in vectorstore.similarity_search(question):
print(f'{context}\n')
Remix's job is to cross the center of the stack and then get out of your way. We avoid as many "Remixisms" as possible and instead make it easier to use the standard APIs the web already has.
This one is more for us. We've been educators for the 5 years before Remix. Our tagline is Build Better Websites. We also think of it with a little extra on the end: Build Better Websites, Sometimes with Remix. If you get good at Remix, you will accidentally get good at web development in general.
Remix's APIs make it convenient to use the fundamental Browser/HTTP/JavaScript, but those technologies are not hidden from you.
Additionally, if Remix doesn't have an adapter for your server already, you can look at the source of one of the adapters and build your own.
## Server Framework
If you're familiar with server-side MVC web frameworks like Rails and Laravel, Remix is the View and Controller, but it leaves the Model up to you. There are a lot of great databases, ORMs, mailers, etc. in the JavaScript ecosystem to fill that space. Remix also has helpers around the Fetch API for cookie and session management.
Instead of having a split between View and Controller, Remix Route modules take on both responsibilities.
Most server-side frameworks are "model focused". A controller manages multiple URLs for a single model.
## Welcome to Remix!
We are happy you're here!
Remix is a full stack web framework that lets you focus on the user interface and work back through web fundamentals to deliver a fast, slick, and resilient user experience that deploys to any Node.js server and even non-Node.js environments at the edge like Cloudflare Workers.
Want to know more? Read the Technical Explanation of Remix
This repository contains the Remix source code. This repo is a work in progress, so we appreciate your patience as we figure things out.
## Documentation
For documentation about Remix, please
Final Answers:¶
- Remix aims to make it easy to use standard APIs.
- You can learn about web development in general with Remix.
- Remix is a framework that plays the role of both the View and Controller.
- Remix leaves the model to the user.
- Remix provides helpers for the Fetch API.
- Remix can be deployed in non-Node.js environments such as Node.js servers and Cloudflare Workers.
Notes on API usage:¶
Regarding ChatGPT on the web, it is currently still an opt-out format (not used for retraining if you apply) as of March 10, 2023, but the ChatGPI API is an opt-in format (not used for retraining unless you apply), and it has been decided that it will not be used for actual model improvement. (It is stored for 30 days for legal monitoring purposes.) Since execution via the API reduces the risk of leakage to third parties other than OpenAI and is not used for retraining, the threshold for using confidential information with ChatGPT seems to have been lowered. The price of the API (gpt-3.5-turbo) is relatively inexpensive at 0.002 dollars per 1000 tokens, while the embedded model (text-embedding-ada-002) is 0.0004 dollars per 1000 tokens. However, if you try to create an embedded model for a large number of files, it will cost more than expected. If the number of tokens cannot be predicted in advance, it is a good idea to calculate the price in advance and decide whether to execute it as follows:
import tiktoken
encoding = tiktoken.encoding_for_model('text-embedding-ada-002')
text = ''
for doc in get_docs('_posts'):
text += doc.page_content.replace(' ', ' ')
token_count = len(encoding.encode(text, allowed_special='all'))
print(f'Estimated price: {token_count*0.00000004} USD')
Summary:¶
Since ChatGPT learns using past data, it cannot answer questions about the latest information or information that is not publicly available on the internet. This time, by mixing context related to the question content into the prompt, we were able to answer questions about the latest data and files saved locally.
If this article has been even a little helpful to you, I would be delighted. If you have any questions or comments, please feel free to contact me.