LangChain Tutorial - The SECRET to Building Viral LLM Apps
Learn the secret techniques top AI developers use to easily manage LLMs, optimize prompts, chain models, and create conversational apps with 10x better results.
What is 🦜🔗LangChain?
Downloading Wikipedia Page
LLMs
Using OpenAI GPT-3.5-turbo LLM to answer the general question.
KDnuggets is an online platform and magazine for data scientists and data miners. It was established in 1997, and it is the longest continuously-running publication dedicated to data science. KDnuggets offers tips, tutorials, surveys, reviews, interviews, and background information about data science and related topics. It covers topics such as machine learning, deep learning, natural language processing, big data, data visualization, predictive analytics, and data mining. It is an invaluable resource for data scientists and data miners of all levels, from those just getting started to seasoned professionals.
You can also use other LLMs like HuggingFace LLaMA-2-7B-32K
Prompt Templates
LangChain enables robust prompt engineering and optimization through its flexible, prompt template system.
Chains
LangChain allows chaining LLMs together with prompt templates to create complex multi-step workflows.
Agents & Tools
LangChain Agents interface with external tools and services, using the LLMs to determine optimal sequences of actions to accomplish tasks like search, calculations, and data lookups.
Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
> Entering new AgentExecutor chain...
I should look up the distance between Earth and the moon and then convert it into miles.
Action: Wikipedia
Action Input: Earth-moon distanceRetrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
Observation: Page: Lunar distance (astronomy)
Summary: The instantaneous Earth–Moon distance, or distance to the Moon, is the distance from the center of Earth to the center of the Moon. Lunar distance (LD or
Δ
⊕
L
{\textstyle \Delta _{\oplus L}}
), or Earth–Moon characteristic distance, is a unit of measure in astronomy. More technically, it is the semi-major axis of the geocentric lunar orbit. The lunar distance is on average approximately 385,000 km (239,000 mi), or 1.28 light-seconds; this is roughly 30 times Earth's diameter or 9.5 times Earth's circumference. A little less than 400 lunar distances make up an astronomical unit.
The semi-major axis has a value of 384,399 km (238,854 mi). The time-averaged distance between the centers of Earth and the Moon is 385,000.6 km (239,228.3 mi). The actual distance varies over the course of the orbit of the Moon, from 356,500 km (221,500 mi) at the perigee to 406,700 km (252,700 mi) at apogee, resulting in a differential range of 50,200 km (31,200 mi).Lunar distance is commonly used to express the distance to near-Earth object encounters. Lunar semi-major axis is an important astronomical datum; the few millimeter precision of the range measurements determines semi-major axis to a few decimeters; it has implications for testing gravitational theories such as general relativity, and for refining other astronomical values, such as the mass, radius, and rotation of Earth. The measurement is also useful in characterizing the lunar radius, as well as the mass of and distance to the Sun.
Millimeter-precision measurements of the lunar distance are made by measuring the time taken for laser beam light to travel between stations on Earth and retroreflectors placed on the Moon. The Moon is spiraling away from Earth at an average rate of 3.8 cm (1.5 in) per year, as detected by the Lunar Laser Ranging experiment.
Page: Orbit of the Moon
Summary: The Moon orbits Earth in the prograde direction and completes one revolution relative to the Vernal Equinox and the stars in about 27.32 days (a tropical month and sidereal month) and one revolution relative to the Sun in about 29.53 days (a synodic month). Earth and the Moon orbit about their barycentre (common centre of mass), which lies about 4,670 km (2,900 mi) from Earth's centre (about 73% of its radius), forming a satellite system called the Earth–Moon system. On average, the distance to the Moon is about 385,000 km (239,000 mi) from Earth's centre, which corresponds to about 60 Earth radii or 1.282 light-seconds.
With a mean orbital velocity of 1.022 km/s (0.635 miles/s, 2,286 miles/h), the Moon covers a distance approximately its diameter, or about half a degree on the celestial sphere, each hour. The Moon differs from most satellites of other planets in that its orbit is close to the ecliptic plane instead of to its primary's (in this case, Earth's) equatorial plane. The Moon's orbital plane is inclined by about 5.1° with respect to the ecliptic plane, whereas the Moon's equatorial plane is tilted by only 1.5°.
Page: Moon
Summary: The Moon is Earth's only natural satellite. Its diameter is about one-quarter of Earth's (comparable to the width of Australia), making it the fifth largest satellite in the Solar System and the largest and most massive relative to its parent planet. It is larger than all known dwarf planets in the Solar System. The Moon is a planetary-mass object with a differentiated rocky body, making it a satellite planet under the geophysical definitions of the term. It lacks any significant atmosphere, hydrosphere, or magnetic field. Its surface gravity is about one-sixth of Earth's at 0.1654 g—Jupiter's moon Io is the only satellite in the Solar System known to have a higher surface gravity and density.
The Moon orbits Earth at an average distance of 384,400 km (238,900 mi), or about 30 times Earth's diameter. Its gravitational i
Thought:Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
I should now convert the distance into miles.
Action: Calculator
Action Input: 385400/1.609Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
Observation: Answer: 239527.65692977005
Thought:Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for default-text-davinci-003 in organization org-jLXWbL5JssIxj9KNgoFBK7Qi on requests per min. Limit: 3 / min. Please try again in 20s. Contact us through our help center at help.openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method..
I now know the final answer.
Final Answer: The distance between Earth and the Moon is approximately 239,527.66 miles.
> Finished chain.
Memory
LangChain provides simplified state management across chained LLMs and agent calls through a consistent memory interface, enabling smooth workflows.
> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
Human: How can one overcome anxiety?
AI:
> Finished chain.
> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
Human: How can one overcome anxiety?
AI: To overcome anxiety, it may be helpful to focus on the present and identify what is causing the anxiety. Once the source has been identified, it is important to find healthy strategies to cope with the anxiety. This can include engaging in relaxation techniques, such as deep breathing and progressive muscle relaxation, as well as participating in activities that bring pleasure, such as connecting with nature or engaging in creative activities like journaling or painting.
Human: Tell me more..
AI:
> Finished chain.
Document Loader
LangChain allows seamlessly augmenting foundation models with custom datasets - like CSVs, Markdown, PDFs, etc. - to enable querying personalized and domain-specific information.
Indexes
LangChain Indexes provide vector search over unstructured data - embedding text corpora into vectors, indexing them for efficient similarity lookup, enabling fast retrieval of relevant information to queries.
Created a chunk of size 1146, which is longer than the specified 1000
Gregory I. Piatetsky-Shapiro (born 7 April 1958) is a data scientist and the co-founder of the KDD conferences, and co-founder and past chair of the Association for Computing Machinery SIGKDD group for Knowledge Discovery, Data Mining and Data Science. He is the founder and president of KDnuggets, a discussion and learning website for Business Analytics, Data Mining and Data Science.
Retrieval
LangChain enables combining database retrievers with a foundation model to return natural language responses to queries rather than just retrieving and displaying raw text from documents. This allows for generating more natural and conversational responses.
Save & Load DBs
LangChain provides simple APIs for saving and loading different databases to enable persistence and reuse across sessions.
Gregory I. Piatetsky-Shapiro (born 7 April 1958) is a data scientist and the co-founder of the KDD conferences, and co-founder and past chair of the Association for Computing Machinery SIGKDD group for Knowledge Discovery, Data Mining and Data Science. He is the founder and president of KDnuggets, a discussion and learning website for Business Analytics, Data Mining and Data Science.