Today, we're going to focus on building tools using the LangChain library. These tools can be used by agents, essentially large language models that can decide and utilize tools to enhance their abilities beyond just text completion. In our case, we're creating a tool to handle queries related to Lex Fridman podcasts.
When I refer to an agent in this context, it's a large language model (LLM) that can decide and use tools to answer queries more effectively. These tools allow the agent to fetch, process, and utilize additional data beyond its initial training set.
Typically, you have a query that is inputted into an LLM, and it outputs a completion. An agent, however, will ask whether any available tools can help answer the query better. It then decides which tool to use, generates input for that tool, and processes the tool's output to form the final answer.
For instance, if the tool is a Lex Fridman database, the agent will generate a query for this database, send it, process the response, and eventually produce a final thought for the user.
First, we need to install several libraries, including Hugging Face datasets, Pinecone, LangChain, OpenAI, and TQDM.
!pip install datasets pod-gpt grpcio-tools langchain openai tqdm
You'll also need API keys for OpenAI and Pinecone.
Next, we'll download the dataset of Lex Fridman transcripts:
from datasets import load_dataset
data = load_dataset("lexfridman/lex-transcripts")
We'll reformat this data to fit the Pod GPT indexer, which involves creating a specific structure with IDs, text, and metadata.
formatted_data = [
("id": row["id"], "text": row["transcript"], "metadata": {"title": row["title"], "url": row["source"])}
for row in data
]
Initialize the indexer object and add the reformatted data. This will process the data and chunk it into smaller parts suitable for embedding.
from pod_gpt import GPTIndexer
indexer = GPTIndexer()
for item in formatted_data:
indexer.add(item)
Initialize Pinecone, create the index object, and set up retrieval components within LangChain.
import pinecone
pinecone.init(api_key="YOUR_API_KEY", environment="us-west1-gcp")
index = pinecone.Index("pod-gpt")
from langchain import OpenAI
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Pinecone
llm = OpenAI(api_key="YOUR_OPENAI_KEY")
embeddings = OpenAIEmbeddings()
vectorstore = Pinecone(index, embeddings)
We create a tool that the agent will use to query the Lex Fridman database.
from langchain.agents import Tool
def retrieval_function(query):
return retriever.run(query)
tool_description = "Use this tool to answer queries using Lex Fridman podcasts."
tool = Tool(name="LexFridmanDB", func=retrieval_function, description=tool_description)
Configure the agent with memory, agent type, and maximum iterations.
from langchain.memory import ConversationBufferWindowMemory
from langchain.agents import initialize_agent
memory = ConversationBufferWindowMemory(k=5, memory_key="chat_history")
agent = initialize_agent(agent_type="chat-bot", tools=[tool], llm=llm, memory=memory, max_iterations=2)
Customize the initial system message for the prompt.
from langchain.prompts import ChatPromptTemplate
system_message = ChatPromptTemplate(system_message="Hello! I'm a knowledgeable assistant here to help with Lex Fridman podcast queries.")
agent.update(system_message=system_message)
Let's test the agent with some initial queries:
response = agent(("query": "Hi, how are you?"))
print(response)
response = agent(("query": "Ask Lex, what is the future of AI?"))
print(response)
response = agent(("query": "What does he think about space exploration?"))
print(response)
By using LangChain and GPT-3.5, we've built a sophisticated agent capable of querying a database of Lex Fridman podcasts. This setup allows the model to leverage external data sources dynamically, making it far more versatile than a standard LLM.
An agent is a large language model that can decide and use various tools to answer queries more effectively than simple text completion.
We used the LangChain library, OpenAI's GPT-3.5, and Pinecone for vector database indexing and retrieval.
The agent uses a reasoning-action loop to determine whether using a specific tool will improve its ability to answer the query. It then generates an input for the tool based on the user's query.
Yes, the same principles can be applied to other podcasts, media forms, internal company documents, PDFs, and more.
Conversational memory allows the agent to remember previous interactions, enhancing the context and coherence of multi-turn conversations.
Setting a maximum number of iterations prevents the agent from getting stuck in an infinite loop of tool usage and query refinement.
In addition to the incredible tools mentioned above, for those looking to elevate their video creation process even further, Topview.ai stands out as a revolutionary online AI video editor.
TopView.ai provides two powerful tools to help you make ads video in one click.
Materials to Video: you can upload your raw footage or pictures, TopView.ai will edit video based on media you uploaded for you.
Link to Video: you can paste an E-Commerce product link, TopView.ai will generate a video for you.