Tutorial: How to Use Langchain to Host FastChat-T5-3B-v1.0 on Runpod
Step 1: Install Required Packages
First, you need to install the necessary packages. Open your terminal or command prompt and run the following commands:
pip3 install langchain
pip3 install fschat
Step 2: Set Up the FastChat Server
To set up the FastChat server, you need to run three commands in separate terminal windows.
In the first terminal, run the following command to start the FastChat controller:
python3 -m fastchat.serve.controller --host 0.0.0.0
In the second terminal, run the following command to start the FastChat model worker:
python3 -m fastchat.serve.model_worker --model-names "gpt-3.5-turbo,text-davinci-003,text-embedding-ada-002" --model-path lmsys/fastchat-t5-3b-v1.0 --host 0.0.0.0
In the third terminal, run the following command to start the FastChat OpenAI API server:
python3 -m fastchat.serve.openai_api_server --host 0.0.0.0 --port 8000
Step 3: Configure Langchain
Now, we need to configure Langchain to work with FastChat. In your terminal, run the following commands:
export OPENAI_API_BASE=http://localhost:8000/v1
export OPENAI_API_KEY=EMPTY
Step 4: Download Sample Text Data
For this tutorial, we will use a sample text file called "state_of_the_union.txt." Download the file using the following command:
wget https://raw.githubusercontent.com/hwchase17/langchain/v0.0.200/docs/modules/state_of_the_union.txt
Step 5: Initialize Langchain
In your terminal, run the following commands to initialize Langchain:
export FASTCHAT_WORKER_API_EMBEDDING_BATCH_SIZE=1
Step 6: Import Required Modules
To use Langchain for querying the FastChat model, we need to import some modules. Run the following command in your terminal:
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import TextLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.indexes import VectorstoreIndexCreator
Step 7: Create Embedding and Load Text Data
Next, we will create an embedding object and load the text data. Run the following commands:
embedding = OpenAIEmbeddings(model="text-embedding-ada-002")
loader = TextLoader("state_of_the_union.txt")
Step 8: Create Vectorstore Index
We will now create a Vectorstore index using the embedding and text loader. Run the following command:
from langchain.text_splitter import CharacterTextSplitter
index = VectorstoreIndexCreator(embedding=embedding, text_splitter=CharacterTextSplitter(separator="\n", chunk_size=400, chunk_overlap=100)).from_loaders([loader])
Step 9: Query the FastChat Model
In this step, we will query the FastChat model using Langchain. Run the following commands:
llm = ChatOpenAI(model="gpt-3.5-turbo")
questions = ["Who is the speaker", "What did the president say about Ketanji Brown Jackson", "What are the threats to America", "Who are mentioned in the speech", "Who is the vice president", "How many projects were announced"]
for query in questions:
print("Query:", query)
print("Answer:", index.query(query, llm=llm))