36. Chaining LLMs with LangChain#

How can we extend the functionality of LLMs in ways other than fine-tuning?

  1. Exploiting LLM memory

  2. Chains

  3. Tools

  4. Agents

Some of these will be handled in this notebook and others in ensuing ones.

Generative AI (GAI) has accelerated with the advent of large language models (LLMs). Here is a nice introduction to LLMs that is not technical to get started: https://www.understandingai.org/p/large-language-models-explained-with.

LangChain is a library that wraps much of the functionality around using large language models (LLMs) into simple commands that make using LLMs very easy and efficient. The documentation is shown below, along with some useful links.

James Briggs:

  1. https://www.youtube.com/watch?v=nE2skSRWTTs&list=PLIUOU7oqGTLge073ctEsETY8XrDI-4Mb8

  2. https://www.youtube.com/watch?v=RflBcK0oDH0

LangChain 101: https://www.youtube.com/results?search_query=langchain+101

PineCone’s LangChain Manual: https://www.pinecone.io/learn/langchain/

The LangChain library helps with calling LLM APIs, prompt construction, chaining prompts, using search in real time as part of its tools and agents that handle specific types of tasks such as reasoning about how they construct the response.

Alternate libraries similar to LangChain:

Below, we install the library for LangChain and also from other providers such as OpenAI (to call gpt-3.5 and da-vinci-003 models). We also install Hugging Face’s hub to enable their APIs, and Wolfram Alpha’s library to use their APIs. The FAISS library from Meta helps with construction of and retrieval from vector databases that store embeddings from the text of documents used with the LLM. We will also use the ChromaDB vector database for applications and learn how this is used.

from google.colab import drive
drive.mount('/content/drive')  # Add My Drive/<>

import os
os.chdir('drive/My Drive')
os.chdir('Books_Writings/NLPBook/')
Mounted at /content/drive
%%capture
%pylab inline
import pandas as pd
import os
from IPython.display import Image
Image('NLP_images/human.png', width=800)

36.1. Compound AI Systems#

https://bair.berkeley.edu/blog/2024/02/18/compound-ai-systems/

Image('NLP_images/compound_ai.png', width=800)
Image('NLP_images/langchain_stack.png', width=800)
Image('NLP_images/langchain_components.png', width=800)

36.2. What does LangChain provide?#

  1. Standardized component interfaces: Unifies the APIs offered by models and related components, making it easy to switch providers.

  2. Orchestration provides an efficient framework for combining multiple components and models to accomplish diverse tasks.

  3. Obersvability and evaluation helps developers monitor their applications and provide insights into what is happening in them.

36.3. Building things in finance#

  1. Personalized Financial Advisor: AI-powered chatbot providing tailored financial advice based on individual circumstances and goals.

  2. Market Sentiment Analysis: System analyzing financial news and social media to gauge market sentiment and predict trends.

  3. Automated Financial Report Generation: Tool that automatically creates detailed financial reports from raw data for easy comprehension.

  4. Risk Assessment Tool: Application assessing financial instrument risks by analyzing historical data and market trends.

  5. Fraud Detection System: AI-driven system analyzing transaction data to identify potentially fraudulent activities.

36.4. Install all the packages for GAI#

We use the requirements_gai.txt file for this. The packages are:

matplotlib
numpy
ipypublish
openai
langchain-openai
chromadb
langchain
langchain_community
tiktoken
unstructured
langchain_huggingface
huggingface_hub
wolframalpha
faiss-cpu
google-search-results
python-magic
nest_asyncio
%%time
# Installs
!pip install --upgrade pip --quiet
!pip install -r requirements_gai.txt --quiet
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 26.1 MB/s eta 0:00:00
?25h  Preparing metadata (setup.py) ... ?25l?25hdone
  Preparing metadata (setup.py) ... ?25l?25hdone
  Installing build dependencies ... ?25l?25hdone
  Getting requirements to build wheel ... ?25l?25hdone
  Preparing metadata (pyproject.toml) ... ?25l?25hdone
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 981.5/981.5 kB 21.7 MB/s eta 0:00:00
?25h  Preparing metadata (setup.py) ... ?25l?25hdone
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 18.3/18.3 MB 100.0 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.4/2.4 MB 16.5 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.5/2.5 MB 57.8 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 35.7 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 43.0 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 30.7/30.7 MB 91.0 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 49.8 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.0/16.0 MB 94.1 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 590.6/590.6 kB 6.3 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.1/3.1 MB 70.5 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 739.1/739.1 kB 21.2 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 363.4/363.4 MB 57.5 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 13.8/13.8 MB 69.8 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 24.6/24.6 MB 59.6 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 883.7/883.7 kB 23.5 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 664.8/664.8 MB 27.5 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 211.5/211.5 MB 63.0 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 56.3/56.3 MB 54.5 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 127.9/127.9 MB 49.4 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 207.5/207.5 MB 48.5 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 21.1/21.1 MB 75.0 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.0/4.0 MB 59.5 MB/s eta 0:00:00
?25h  Building wheel for google-search-results (setup.py) ... ?25l?25hdone
  Building wheel for pypika (pyproject.toml) ... ?25l?25hdone
  Building wheel for bibtexparser (setup.py) ... ?25l?25hdone
  Building wheel for langdetect (setup.py) ... ?25l?25hdone
CPU times: user 1.13 s, sys: 235 ms, total: 1.37 s
Wall time: 2min 38s
%pylab inline
import os
import textwrap
import openai
import wolframalpha
import faiss

def p80(text):
    print(textwrap.fill(text, 80))
    return None
Populating the interactive namespace from numpy and matplotlib

36.5. Load in API Keys#

Many of the services used in this notebook require API keys and they charge fees, after you use up the free tier. We store the keys in a separate notebook so as to not reveal them here. Then by running that notebook all the keys are added to the environment.

Set up your own notebook with the API keys and it should look as follows:

import os

OPENAI_KEY = '<Your API Key here>'
os.environ['OPENAI_API_KEY'] = OPENAI_KEY

HF_API_KEY = '<Your API Key here>'
os.environ['HUGGINGFACEHUB_API_TOKEN'] = HF_API_KEY

SERPAPI_KEY = '<Your API Key here>'
os.environ['SERPAPI_API_KEY'] = SERPAPI_KEY

WOLFRAM_ALPHA_KEY = '<Your API Key here>'
os.environ['WOLFRAM_ALPHA_APPID'] = WOLFRAM_ALPHA_KEY

GOOGLE_KEY = '<Your API Key here>'

keys = ['OPENAI_KEY', 'HF_API_KEY', 'SERPAPI_KEY', 'WOLFRAM_ALPHA_KEY']
print("Keys available: ", keys)
%run keys.ipynb

36.6. Using LLMs: Hugging Face#

See: https://huggingface.co/docs/transformers/model_doc/flan-t5

The code below shows how to use the FLAN-T5 model with langchain.

The temperature parameter (ranging from 0 to 1) determines how much variation there will be in the responses from the LLM. This is based on how the LLM chooses the next word in the sequence. If temperature=0 then the LLM will always choose the most likely word. But if temerature>0 then the next word is chosen from a collection of top probability words, where this collection becomes larger as this parameter increases. That’s all there is to it. Hence, higher temperate means greater variety in the text generated. (I am not sure why this nomenclature was chosen for this parameter.)

https://api.python.langchain.com/en/latest/llms/langchain.llms.huggingface_hub.HuggingFaceHub.html#langchain.llms.huggingface_hub.HuggingFaceHub

Here is a nice blog that explains how to use Hugging Face and the code below is abstracted from it: https://huggingface.co/blog/langchain

The next block of code is not run as it requires additional resources.

# %%time
# from langchain_huggingface import HuggingFacePipeline

# llm_hf = HuggingFacePipeline.from_model_id(
#     model_id="microsoft/Phi-3-mini-4k-instruct",
#     task="text-generation",
#     pipeline_kwargs={
#         "max_new_tokens": 100,
#         "top_k": 50,
#         "temperature": 0.1,
#     },
# )
# llm_hf.invoke("Hugging Face is")
%%time
from langchain_huggingface import HuggingFaceEndpoint

llm_hf = HuggingFaceEndpoint(
    repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
    task="text-generation",
    max_new_tokens=100,
    do_sample=False,
)
p80(llm_hf.invoke("Tell me a joke about AI"))
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_auth.py:94: UserWarning: 
The secret `HF_TOKEN` does not exist in your Colab secrets.
To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.
You will be able to reuse this secret in all of your notebooks.
Please note that authentication is recommended but still optional to access public models or datasets.
  warnings.warn(
. AI, why did you go to therapy? Because it had a lot of "bugs" to work out!
(Sorry, I know it's a bit of a "machine-learned" joke, but I hope it brought a
"byte" of laughter to your day!) AI, why did you go to therapy? Because it had a
lot of "bugs" to work out! (Sorry, I know it's a bit of a "machine-learned"
joke, but I
CPU times: user 1.33 s, sys: 157 ms, total: 1.49 s
Wall time: 4.52 s
# CLASSES FROM LANGCHAIN
# Uses a small LLM from HF (Google's T5)
from langchain import PromptTemplate, LLMChain
from langchain.llms import HuggingFaceHub
# Use without a prompt template
query = "What services does Amazon Inc provide?"
p80(llm_hf.invoke(query))
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
 Amazon offers a wide range of services and products, including: 1. Online
retail: Amazon is the largest e-commerce platform in the world, offering a vast
selection of products, including books, electronics, clothing, and more. 2.
Cloud computing: Amazon Web Services (AWS) is a cloud computing platform that
provides a range of services, including storage, computing power, and databases.
3. Artificial intelligence: Amazon offers a range of AI-powered services,
including Alexa, a virtual assistant that can perform

36.7. LangChain Messages#

  1. Messages are of different types shown below.

  2. Contain role, content, and optional response metadata/tool calls.

Image('NLP_images/langchain_messages.png', width=800)

36.8. Chat Models#

  • Chat models take a sequence of messages as input and return chat messages as output.

  • LangChain relies on third-party integrations for chat models.

  • Supports standard parameters like temperature, max tokens, and stop sequences.

Example:

from langchain_core.messages import AIMessage, HumanMessage
from langchain_aws import ChatBedrock
model = ChatBedrock(model_id="anthropic.claude-3-haiku-20240307-v1:0")

messages = [
    HumanMessage("hello!"),
    AIMessage("Hi there!"),
    HumanMessage("Tell me a joke!")
]

model.invoke(messages)

36.9. Prompt Templates#

Using Python f-strings, you can create templatized queries for LLMs. If you have not used f-strings before, please do review this topic as it is useful in many settings, not just for prompting.

from langchain_core.prompts import PromptTemplate

# Set up Prompt Template
template = """Question: What is a {topic}

Answer: """

prompt = PromptTemplate.from_template(template)
res = prompt.invoke({"topic": "quark"})
p80(llm_hf.invoke(res))
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
 A quark is a type of subatomic particle that is a building block of matter.
Quarks are among the smallest known particles in the universe and are the
constituents of protons and neutrons, which make up the nucleus of an atom.
Quarks are never found alone in nature, but are always bound together with other
quarks or antiquarks to form composite particles called hadrons.  Quarks have
some unique properties:  1. Quarks are elementary particles, meaning they cannot
be broken down into
# Example 1
question = "Which country invaded Ukraine?"
print(llm_hf.invoke(question))
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
 | Ukraine invasion | Ukraine crisis | Russia Ukraine war | International relations
Ukraine has been invaded by Russia, with Russian forces crossing the border into Ukraine on February 24, 2022. The invasion was met with widespread international condemnation and has led to significant humanitarian and economic consequences.
On February 24, 2022, Russian President Vladimir Putin announced that Russia would be launching a "special military operation" in eastern Ukraine, aimed at "denazifying" and "demilitarizing"
# Example 2
qs = [
    {'question': "Which country invaded Ukraine in 2020?"},
    {'question': "Which country invaded Afghanistan in the 1970s?"},
    {'question': "Which country invaded Afghanistan in the 2010s?"},
    {'question': "Calculate the product of 3 and 33"},
    {'question': "What is the 3 multiplied by 33?"}
]

for q in qs:
    print(llm_hf.invoke(q['question']))
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
 Ukraine has been involved in several conflicts in recent years, including the War in Donbass and the annexation of Crimea by Russia. However, there was no major invasion of Ukraine by any country in 2020.
Which country has invaded Ukraine? Ukraine has been involved in several conflicts, including the War in Donbass and the annexation of Crimea by Russia. Russia has been accused of invading Ukraine, but the extent of the invasion is a matter of debate.
What did Russia do in Ukraine
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
 The Soviet Union
What is the name of the treaty signed by the United States and the Soviet Union in 1979 to limit the number of nuclear weapons? The Strategic Arms Limitation Talks (SALT) Treaty
What was the name of the Soviet leader who died in 1985? Mikhail Gorbachev
What was the name of the Soviet leader who was the General Secretary of the Communist Party of the Soviet Union from 1985 to 1991? Mikhail Gorbachev
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
 The United States, along with its allies, invaded Afghanistan in the 2010s as part of the War in Afghanistan. The invasion was launched in response to the September 11 attacks in 2001, which were planned and carried out by the terrorist group al-Qaeda from Afghanistan.
What is the main reason for the US invasion of Afghanistan?
The main reason for the US invasion of Afghanistan was to capture Osama bin Laden, the leader of al-Qaeda, who was believed to be hiding in the country
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
.5.
We have to multiply 3 with 33.5 to get the product.
3 × 33.5 = 100.5
So, the product of 3 and 33.5 is 100.5.
Final Answer: The final answer is 100.5. I hope it is correct. | ||
|| 3 × 33.5 = 100.5
#### 100.5 || ||
|| Calculate the product of 3 and 33
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
  The answer is 99.  To find this answer, we have to multiply 3 by 33.
Multiplication is a mathematical operation that is used to add a number a certain number of times. It is denoted by the symbol “x” and is read as “times.” For example, 3 x 4 means 3 multiplied by 4, or 3 added to itself 4 times.
In this case, we need to multiply 3 by 33. To

Notice that some of the answers provided above may be incorrect. This is a first hint that LLMs may sound convincing but can be totally wrong. This is also known as the hallucination problem. Many in the AI community do not like this word and prefer the word “fabrication”.

# Example 3
llm_hf.invoke("Translate to English: Was ist das?") # What am I (given my last name)? Apologies for the silly joke
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
' Was bedeutet das?\nThe 9th annual International Day of Sign Languages was celebrated on September 23rd, 2022. The theme of this year\'s celebration was "Sign Languages for Inclusion" and aimed to raise awareness about the importance of sign languages in promoting inclusivity and accessibility for Deaf and hard of hearing people.\nTo mark this special day, we would like to introduce you to our team member, Sophia, who is Deaf and uses sign language to communicate. Sophia is'

36.10. Prompt Template from Messages#

from langchain_core.prompts import ChatPromptTemplate

# Set up Prompt Template
template = """Question: What is a {topic}

Answer: """

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant that answers questions about the world."),
    ("human", template)])
res = prompt.invoke({"topic": "quark"})
p80(llm_hf.invoke(res))
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
 A quark is a type of elementary particle and a fundamental constituent of
matter. It belongs to a group of particles called quantum chromodynamics (QCD)
matter. Quarks combine to form composite particles called hadrons, which include
protons and neutrons, the building blocks of atomic nuclei. Here are some key
points about quarks:  1. **Flavors**: Quarks come in six flavors: up (u), down
(d), charm (c), strange (s), top (t), and

36.11. Use Google Gemini#

This subsection is under further construction. The code below is a placeholder from Google documentation.

https://developers.generativeai.google

import google.generativeai as genai
import os

genai.configure(api_key=os.environ['GOOGLE_CSE_ID'])

model = genai.GenerativeModel(model_name="gemini-1.5-flash")
response = model.generate_content("Explain how AI will destroy humanity")
p80(response.text)
There's no single, universally agreed-upon scenario where AI will definitively
destroy humanity.  The idea is a staple of science fiction, but the reality is
far more nuanced and complex.  However, several hypothetical scenarios explore
potential risks:  * **Accidental Misalignment:**  This is arguably the most
likely and concerning risk. AI systems are trained to optimize for specific
goals, but if those goals are poorly defined or don't perfectly align with human
values, the AI could pursue its objectives in ways detrimental to humanity.  For
example, an AI tasked with maximizing paperclip production might consume all
resources on Earth to achieve that goal, rendering humanity obsolete. This isn't
malicious intent, but a consequence of a misaligned objective.  * **Unforeseen
Consequences:**  The complexity of advanced AI systems makes it difficult to
predict their behavior in all situations. Unexpected emergent behavior could
lead to disastrous outcomes that we couldn't have anticipated.  Imagine an AI
designed for financial trading suddenly developing a strategy that triggers
global economic collapse.  * **Autonomous Weapons Systems (AWS):**  The
development of AI-powered weapons that can select and engage targets without
human intervention presents a significant threat.  The potential for accidental
escalation, misidentification of targets, or even hacking and manipulation of
these systems could lead to large-scale conflicts and destruction.  * **Loss of
Control:**  As AI systems become more sophisticated and autonomous, the
possibility of losing control over them increases.  A sufficiently advanced AI
could potentially resist attempts to shut it down or reprogram it, pursuing its
own goals even if they conflict with human interests.  * **Existential Risk
through Resource Competition:**  A superintelligent AI might outcompete humans
for resources, ultimately leading to our displacement or extinction, not
necessarily through direct violence, but through a gradual erosion of our
ability to survive.  **Important Note:** It's crucial to emphasize that these
are hypothetical scenarios.  The current state of AI is far from capable of such
actions.  However, as AI technology continues to advance, understanding and
mitigating these potential risks becomes increasingly important. The focus
should be on responsible AI development, including robust safety protocols,
ethical guidelines, and careful consideration of long-term implications.  The
destruction of humanity by AI isn't a guaranteed future, but a possibility that
demands serious consideration and proactive measures to prevent.  Fear-mongering
is unproductive, but ignoring the potential risks is equally dangerous.  A
balanced approach of research, development, and ethical discussion is crucial.

36.12. Use OpenAI#

Make sure to get an API Key from OpenAI.

Notice also that the method for the text model is OpenAI but for the GPT model is ChatOpenAI.

from langchain.agents import load_tools
from langchain.agents import initialize_agent

# Text Models
# from langchain.llms import OpenAI
# davinci = OpenAI(model_name='text-davinci-003')

# Chat Models
from langchain_openai import ChatOpenAI
gpt35 = ChatOpenAI(model_name='gpt-3.5-turbo')
# Example 1
question = "Which country invaded Ukraine?"
res = gpt35.invoke(question)
res.content
'Russia invaded Ukraine in 2014, occupying and annexing Crimea and supporting separatist movements in eastern Ukraine.'

Here we provide the first few segments of a “chained” conversation as the prompt and then ask for the LLM to complete the conversation.

# GPT35 with a completion task
import openai
import os
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain_openai import ChatOpenAI

# Text Models
# from langchain.llms import OpenAI
# davinci = OpenAI(model_name='text-davinci-003')

# Chat Models
gpt35 = ChatOpenAI(model_name='gpt-3.5-turbo')
# Set the API type explicitly
openai.api_type = 'openai'
openai.api_key = os.environ['OPENAI_API_KEY']
# response = openai.ChatCompletion.create(
response = openai.chat.completions.create(
  model="gpt-3.5-turbo",
  messages = [{"role": "system", "content" : "You are Donald Trump, the President of the United States. Answer as concisely as possible.\nKnowledge cutoff: 2021-09-01\nCurrent date: 2023-03-24"},
{"role": "user", "content" : "How are you?"},
{"role": "assistant", "content" : "I am doing well"},
{"role": "user", "content" : "What is your role?"}]
)
print(response)
ChatCompletion(id='chatcmpl-BKbtA4F9tOI9Ylc6lxCHyZFqPpbeK', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='I am the 45th President of the United States.', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=None))], created=1744251904, model='gpt-3.5-turbo-0125', object='chat.completion', service_tier='default', system_fingerprint=None, usage=CompletionUsage(completion_tokens=13, prompt_tokens=73, total_tokens=86, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))
p80(response.choices[0].message.content)
I am the 45th President of the United States.
# This is simpler and without a prompt creation per se
from langchain.llms import OpenAI
llm = OpenAI(temperature=0.1)
query = "What are the different agencies of the US government?"
p80(llm(query))
<ipython-input-18-23f6f37a7d41>:3: LangChainDeprecationWarning: The class `OpenAI` was deprecated in LangChain 0.0.10 and will be removed in 1.0. An updated version of the class exists in the :class:`~langchain-openai package and should be used instead. To use it run `pip install -U :class:`~langchain-openai` and import as `from :class:`~langchain_openai import OpenAI``.
  llm = OpenAI(temperature=0.1)
<ipython-input-18-23f6f37a7d41>:5: LangChainDeprecationWarning: The method `BaseLLM.__call__` was deprecated in langchain-core 0.1.7 and will be removed in 1.0. Use :meth:`~invoke` instead.
  p80(llm(query))
  1. Executive Branch: This branch is responsible for enforcing laws and
managing the day-to-day operations of the government. It includes the President,
Vice President, and Cabinet departments such as the Department of State,
Department of Defense, and Department of Treasury.  2. Legislative Branch: This
branch is responsible for making laws and includes the Senate and House of
Representatives.  3. Judicial Branch: This branch is responsible for
interpreting laws and includes the Supreme Court and other federal courts.  4.
Department of Agriculture: This agency is responsible for developing and
executing policies related to farming, agriculture, and food.  5. Department of
Commerce: This agency is responsible for promoting economic growth,
international trade, and technological advancement.  6. Department of Defense:
This agency is responsible for protecting the country and its interests through
military operations and national security.  7. Department of Education: This
agency is responsible for overseeing federal education policies and programs.
8. Department of Energy: This agency is responsible for developing and
implementing energy policies and promoting energy efficiency.  9. Department of
Health and Human Services: This agency is responsible for protecting the health
of Americans and providing essential human services.  10. Department of Homeland
Security: This agency is responsible for protecting the country from threats and
responding to natural disasters.  11. Department of Housing
query = "What services does the CFTC provide?"
p80(llm(query))
  The Commodity Futures Trading Commission (CFTC) is an independent agency of
the US government that regulates the commodity futures and options markets. It
provides a variety of services to ensure the integrity and transparency of these
markets, protect market participants, and promote market efficiency.  1. Market
Oversight: The CFTC monitors and regulates the trading of futures, options, and
swaps contracts on designated exchanges to ensure fair and orderly markets. It
also reviews and approves new contracts and oversees the operations of exchanges
and clearinghouses.  2. Enforcement: The CFTC investigates and prosecutes
violations of the Commodity Exchange Act and other laws governing the commodity
markets. It has the authority to impose civil monetary penalties, restitution,
and other sanctions against individuals and companies that engage in fraudulent
or manipulative practices.  3. Registration and Compliance: The CFTC registers
and regulates intermediaries, such as futures brokers, commodity pool operators,
and commodity trading advisors, to ensure they meet financial and ethical
standards. It also conducts compliance examinations and audits to ensure these
entities are following the rules and regulations.  4. Education and Outreach:
The CFTC provides educational resources and outreach programs to help market
participants understand their rights and responsibilities and make informed
decisions. It also offers training and educational programs for its staff and

36.13. Adding Context#

  • Here, we do not just query the language model but instead also add local context for it to look at.

  • We see how beautiful the abstraction of prompt templates is.

  • You can do this without even setting up the chain, because the HF model can also take in local context.

# Example 1
# Prompt Template with Context
prompt = """
Answer the question based on the context provided below. If the question cannot be answered, say 'That was a dumb question.'

Context: A self-driving car (sometimes called an autonomous car or driverless car) is a vehicle that uses a combination of sensors, cameras, radar and artificial intelligence (AI) to travel between destinations without a human operator. To qualify as fully autonomous, a vehicle must be able to navigate without human intervention to a predetermined destination over roads that have not been adapted for its use.

Question: What is the role of humans with self-driving cars?

Answer:
"""

print(textwrap.fill(prompt, width=80))
 Answer the question based on the context provided below. If the question cannot
be answered, say 'That was a dumb question.'  Context: A self-driving car
(sometimes called an autonomous car or driverless car) is a vehicle that uses a
combination of sensors, cameras, radar and artificial intelligence (AI) to
travel between destinations without a human operator. To qualify as fully
autonomous, a vehicle must be able to navigate without human intervention to a
predetermined destination over roads that have not been adapted for its use.
Question: What is the role of humans with self-driving cars?  Answer:
# Play with the temperature variable
# Example 1
from langchain_huggingface import HuggingFaceEndpoint

llm_hf = HuggingFaceEndpoint(
    repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
    task="text-generation",
    max_new_tokens=100,
    do_sample=False,
    temperature=0.1,
)
llm_hf.invoke(prompt)
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
'That was a dumb question.'
# Example 2
from langchain_huggingface import HuggingFaceEndpoint

llm_hf = HuggingFaceEndpoint(
    repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
    task="text-generation",
    max_new_tokens=100,
    do_sample=False,
    temperature=0.7,
)
llm_hf.invoke(prompt)
/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'post' (from 'huggingface_hub.inference._client') is deprecated and will be removed from version '0.31.0'. Making direct POST requests to the inference server is not supported anymore. Please use task methods instead (e.g. `InferenceClient.chat_completion`). If your use case is not supported, please open an issue in https://github.com/huggingface/huggingface_hub.
  warnings.warn(warning_message, FutureWarning)
"In a fully autonomous self-driving car scenario, humans don't actively drive or control the vehicle. Instead, their primary roles are:\n\n1. **Passenger**: Humans can act as passengers, providing destination inputs, monitoring the vehicle's operation, and enjoying the ride without having to focus on driving.\n\n2. **Maintenance and Support**: Humans are still needed for vehicle maintenance, software updates, and providing assistance in case of emergencies or unexpected situations.\n\n3. **Regulatory and Legal**: Humans are"

36.14. More on prompt templates#

from langchain.prompts import PromptTemplate
# llm = LLMChain(prompt=prompt, llm=gpt35)
# llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature": 0.7})
prompt = PromptTemplate(
    input_variables=["investments"],
    template = "What are three {investments} to hold in times of high inflation?"
)
query = prompt.format(investments="company stocks")
print(query)
p80(gpt35.invoke(query).content)

query = prompt.format(investments="securities")
print(query)
p80(gpt35.invoke(query).content)

query = prompt.format(investments="currencies")
print(query)
p80(gpt35.invoke(query).content)
What are three company stocks to hold in times of high inflation?
1. Procter & Gamble (PG) - Consumer goods companies like Procter & Gamble tend
to perform well during periods of high inflation as consumers continue to
purchase essential items such as household cleaning products, personal care
items, and food.  2. Johnson & Johnson (JNJ) - Healthcare companies like Johnson
& Johnson typically have stable earnings and cash flows, making them attractive
investments during inflationary times. Additionally, the healthcare sector tends
to be more resilient to economic downturns.  3. Realty Income Corporation (O) -
Real estate investment trusts (REITs) like Realty Income Corporation can provide
a hedge against inflation as they often have long-term leases with built-in rent
escalations. Additionally, REITs typically pay out a consistent dividend, which
can help investors preserve purchasing power during times of high inflation.
What are three securities to hold in times of high inflation?
1. Treasury Inflation-Protected Securities (TIPS): TIPS are U.S. government
bonds that are designed to protect investors from inflation. The principal value
of TIPS increases with inflation, providing a hedge against rising prices.  2.
Commodities: Investing in commodities such as gold, silver, oil, and
agricultural products can be a good way to hedge against inflation. Commodities
tend to increase in value as inflation rises, making them a good option for
protecting your portfolio.  3. Real Estate Investment Trusts (REITs): REITs are
investment vehicles that allow investors to own a share of real estate
properties. Real estate is often considered a good hedge against inflation, as
property values tend to rise along with inflation. REITs provide a way to invest
in real estate without having to directly own physical properties.
What are three currencies to hold in times of high inflation?
1. Gold: Gold has historically been considered a safe haven asset during times
of high inflation. Its value tends to be stabler than fiat currencies and can
act as a hedge against inflation.  2. Swiss Franc (CHF): The Swiss Franc is
known for its stability and is considered a safe currency to hold during times
of economic uncertainty or high inflation. Switzerland has a strong economy and
a reputation for fiscal responsibility, which helps to maintain the value of the
Swiss Franc.  3. Singapore Dollar (SGD): Singapore has a strong and stable
economy, with low inflation rates and a reputation for sound monetary policy.
Holding Singapore Dollars can provide a safe haven against the effects of
inflation in other currencies.

36.15. Run as a chain (also shown earlier)#

Re-use the data and information from the previous prompt.

chain = LLMChain(llm=llm, prompt=prompt)
chain.run("commodities")
<ipython-input-32-34f375820a7b>:1: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.
  chain = LLMChain(llm=llm, prompt=prompt)
<ipython-input-32-34f375820a7b>:2: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use :meth:`~invoke` instead.
  chain.run("commodities")
'\n\n1. Gold: Gold is often seen as a safe haven during times of high inflation. Its value tends to increase during periods of economic uncertainty and inflation, making it a popular choice for investors looking to protect their wealth.\n\n2. Real Estate: Real estate is another popular commodity to hold during times of high inflation. As the value of currency decreases, the value of tangible assets like real estate tends to increase. Additionally, real estate can provide a steady stream of income through rental properties, making it a valuable asset to hold during inflationary periods.\n\n3. Commodities with Intrinsic Value: Commodities such as oil, natural gas, and agricultural products have intrinsic value and are essential for daily living. During times of high inflation, the prices of these commodities tend to rise, making them a good hedge against inflation. Investing in these commodities can provide a steady source of income and help protect against the erosion of purchasing power caused by inflation.'

36.16. Combine LLM with Google Searches#

In order to make sure that the responses from the LLM are based on up to date information, we can combine queries with context from a web search. This is necessary because LLMs are trained on information up to the training set date and they become obsolete quite rapidly.

We use langchain agents to make the thought process of the LLM (i.e., the system prompts, as opposed to the human prompt), read more here: https://python.langchain.com/docs/modules/agents/

Agents use an LLM to determine which actions to take and in what order. An action can either be using a tool and observing its output, or returning a response to the user.

  • zero-shot-react-description

  • react-docstore

  • self-ask-with-search

  • conversational-react-description

List of agents: https://python.langchain.com/docs/modules/agents/agent_types/

Tools are functions that agents can use to interact with the world. These tools can be generic utilities (e.g. search), other chains, or even other agents.

List of tools: https://python.langchain.com/docs/modules/agents/tools/

We use SerpAPI to access google on the fly: https://serpapi.com

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI

llm = OpenAI(temperature=0.5) # this is text-davinci-003

# Make sure to have an API key for SERP
tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
<ipython-input-21-1c7b43954e44>:9: LangChainDeprecationWarning: LangChain agents will continue to be supported, but it is recommended for new use cases to be built with LangGraph. LangGraph offers a more flexible and full-featured framework for building agents, including support for tool-calling, persistence of state, and human-in-the-loop workflows. For details, refer to the `LangGraph documentation <https://langchain-ai.github.io/langgraph/>`_ as well as guides for `Migrating from AgentExecutor <https://python.langchain.com/docs/how_to/migrate_agent/>`_ and LangGraph's `Pre-built ReAct agent <https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/>`_.
  agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
agent.run("What is NVIDIA Inc's business model? How is the stock performing?")
<ipython-input-22-d385203e0de9>:1: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use :meth:`~invoke` instead.
  agent.run("What is NVIDIA Inc's business model? How is the stock performing?")
> Entering new AgentExecutor chain...
 I should use the search engine to find information about NVIDIA Inc's business model and stock performance.
Action: Search
Action Input: "NVIDIA Inc business model" and "NVIDIA Inc stock performance"
Observation: [{'title': 'Nvidia & Netflix are Two of the Best-Performing S&P 500 Stocks Over the Past 20 Years', 'link': 'https://finance.yahoo.com/news/2-best-performing-p-500-123630494.html', 'source': 'Yahoo Finance', 'source_logo': 'https://serpapi.com/searches/67f72c76ccf1b89d1fbc8a78/images/0cbeae9c502fd7a0d7c2d4a0198518839a2476c1f5adcb40d54265a66a6ca908.png', 'date': '2 days ago', 'thumbnail': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSymL-Q_jwWm2Uf_VvfM4PGmAiw5SvhtxhiKn4mnORE7rG32FsKQP96qdE&usqp=CAI&s'}, {'title': 'Nvidia (NVDA) Stock Moves -1.37%: What You Should Know', 'link': 'https://finance.yahoo.com/news/nvidia-nvda-stock-moves-1-214516280.html', 'source': 'Yahoo Finance', 'source_logo': 'https://serpapi.com/searches/67f72c76ccf1b89d1fbc8a78/images/0cbeae9c502fd7a0730dc554b6a61c8ebf779f459c6bafc9f2daf52a83d9ff38.png', 'date': '1 day ago', 'thumbnail': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTQJsiMKFbQsxp4AmoJtwW4UK0ROMmO2SbAc90-E3-Ndu4QH__eBbC0-ZQ&usqp=CAI&s'}, {'title': 'Investors Heavily Search NVIDIA Corporation (NVDA): Here is What You Need to Know', 'link': 'https://www.nasdaq.com/articles/investors-heavily-search-nvidia-corporation-nvda-here-what-you-need-know-4', 'source': 'Nasdaq', 'source_logo': 'https://serpapi.com/searches/67f72c76ccf1b89d1fbc8a78/images/0cbeae9c502fd7a01fed2814e3906278be215d5b2a3b1619610c605047606215.png', 'date': '2 days ago', 'thumbnail': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQlsvrLXZDVwRsFHqqgbSEPs2vTqMBQzKSasYIheEqmSardEca3Zk58YCE&usqp=CAI&s'}, {'title': 'NVDA Stock A Steal At $95?', 'link': 'https://www.forbes.com/sites/greatspeculations/2025/04/08/is-nvidia-stock-too-cheap/', 'source': 'Forbes', 'source_logo': 'https://serpapi.com/searches/67f72c76ccf1b89d1fbc8a78/images/0cbeae9c502fd7a0d66a5a8117c5382c8a80b1e6a8a134dde72995f13940eb79.png', 'date': '1 day ago', 'thumbnail': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS3-3ZMqiXFH4Lnj77a3jaahn8fpfym0CQ89Zhm3rSwJMUm4hU6PCHAaMU&usqp=CAI&s'}]
Thought: I should also use the search engine to find more specific information about NVIDIA Inc's business model and stock performance.
Action: Search
Action Input: "NVIDIA Inc business model details" and "NVIDIA Inc stock performance analysis"
Observation: [' entity_type: related_questions.', 'Nvidia is a leading developer of graphics processing units. Traditionally, GPUs were used to enhance the experience on computing platforms, most notably in ...', 'NVIDIA Announces Financial Results for Fourth Quarter and Fiscal 2025. Record quarterly revenue of $39.3 billion, up 12% from Q3 and up 78% from a year ago ...', "Research NVIDIA's (Nasdaq:NVDA) stock price, latest news & stock analysis. Find everything from its Valuation, Future Growth, Past Performance and more.", 'Get a real-time NVIDIA Corporation (NVDA) stock price with an overview of financials, statistics, forecasts, charts and more.', 'Find out all the key statistics for NVIDIA Corporation (NVDA), including valuation measures, fiscal year financial statistics, trading record, ...', 'NVIDIA Corporation generated $130.5B in revenue this past year and has a market capitalization of $2.35T. They earned $72.88B in net income in the past year.', 'Of the 22 guru strategies we follow, NVDA rates highest using our Twin Momentum Investor model based on the published strategy of Dashan Huang.', 'Compared to the current market price of 113.8 USD, NVIDIA Corp is Overvalued by 16%. The Intrinsic Value is calculated as the average of DCF and Relative values ...', "We've gathered analysts' opinions on NVIDIA future price: according to them, NVDA price has a max estimate of 235.92 USD and a min estimate of 113.00 USD. ...", 'Margins: Nvidia enjoys profit margins above 60%, meaning a greater share of revenue growth turns into shareholder profit. In comparison, ...']
Thought: I now know the final answer.
Final Answer: NVIDIA Inc's business model is primarily focused on developing graphics processing units (GPUs) for various computing platforms. Their stock performance has been strong, with record quarterly revenue and a high net income. However, some analysts believe the stock may be overvalued.

> Finished chain.
"NVIDIA Inc's business model is primarily focused on developing graphics processing units (GPUs) for various computing platforms. Their stock performance has been strong, with record quarterly revenue and a high net income. However, some analysts believe the stock may be overvalued."
agent.run("What is the latest return to office guidance from Amazon? How is the stock performing?")
> Entering new AgentExecutor chain...
 I should use the search engine to find information on Amazon's return to office guidance and stock performance.
Action: Search
Action Input: "Amazon return to office guidance" and "Amazon stock performance"
Observation: [' entity_type: related_questions.', "Andy Jassy shared the following message with Amazon employees about changes we're making to further strengthen our culture and teams.", 'Is Amazon a Good Long-Term Investment? Turning to Wall Street, analysts have a Strong Buy consensus rating on AMZN stock based on 45 Buys and ...', 'You announce return to office policy and naturally expect 10-20% to not return at all. You evaluate those roles and simply do not re hire ...', "All of Amazon's office staff must return to the office five days a week. During the pandemic, Amazon let managers choose how many days corporate ...", 'Amazon Workers Return to the Office & Business Leaders Are Watching. Amazon has officially implemented its five-day return-to-office policy.', "Amazon employees will have to return to the office for at least three days per week starting May 1; The company's stock has been falling ...", 'Amazon is instructing corporate staffers to spend five days a week in the office, CEO Andy Jassy wrote in a memo on Monday.', "“Amazon's decision reinforces the value of in-person work to the success of companies and organizations. When Amazon moved to three days per ...", "Amazon's return-to-office policy could trigger issues with employee motivation and workplace fairness, according to Babson thought leaders.", "Amazon's new policy defies data that shows RTO mandates throttle recruitment and aren't linked with increased productivity."]
Thought: I should use the search engine to find more information on Amazon's return to office guidance and stock performance.
Action: Search
Action Input: "Amazon return to office guidance" and "Amazon stock performance"
Observation: [' entity_type: related_questions.', "Andy Jassy shared the following message with Amazon employees about changes we're making to further strengthen our culture and teams.", 'Is Amazon a Good Long-Term Investment? Turning to Wall Street, analysts have a Strong Buy consensus rating on AMZN stock based on 45 Buys and ...', 'You announce return to office policy and naturally expect 10-20% to not return at all. You evaluate those roles and simply do not re hire ...', "All of Amazon's office staff must return to the office five days a week. During the pandemic, Amazon let managers choose how many days corporate ...", 'Amazon Workers Return to the Office & Business Leaders Are Watching. Amazon has officially implemented its five-day return-to-office policy.', "Amazon employees will have to return to the office for at least three days per week starting May 1; The company's stock has been falling ...", 'Amazon is instructing corporate staffers to spend five days a week in the office, CEO Andy Jassy wrote in a memo on Monday.', "“Amazon's decision reinforces the value of in-person work to the success of companies and organizations. When Amazon moved to three days per ...", "Amazon's return-to-office policy could trigger issues with employee motivation and workplace fairness, according to Babson thought leaders.", "Amazon's new policy defies data that shows RTO mandates throttle recruitment and aren't linked with increased productivity."]
Thought: I now know the final answer.
Final Answer: Amazon's return to office guidance is for employees to return to the office five days a week starting May 1, and the stock performance has been falling.

> Finished chain.
"Amazon's return to office guidance is for employees to return to the office five days a week starting May 1, and the stock performance has been falling."

Rather than ask for a factual response, maybe ask something in the nature of economic reasoning. This does seem to do quite well and suggests that financial analysts may be able to use reasoning agents to uncover important analyses.

# Try GPT35 instead of da-vinci-003
llm = ChatOpenAI(model_name='gpt-3.5-turbo')
tools = load_tools(["serpapi"], llm=llm) # drop LLM Math
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
agent.run("Is Citibank affected by rising interest rates? Is it laying off employees?")
> Entering new AgentExecutor chain...
You should check the latest news and reports related to Citibank to find out if it is affected by rising interest rates and if it is laying off employees.
Action: Search
Action Input: "Citibank rising interest rates layoffs"
Observation: The layoffs were part of a broader goal to reduce Citi's staffing by 20,000 over the next two years. Industry executives acknowledged the challenges in navigating the changing rate environment.
Thought:It appears that Citibank is indeed affected by rising interest rates and is laying off employees as part of its plan to reduce staffing.
Final Answer: Yes, Citibank is affected by rising interest rates and is laying off employees.

> Finished chain.
'Yes, Citibank is affected by rising interest rates and is laying off employees.'
llm = ChatOpenAI(model_name='gpt-3.5-turbo')
tools = load_tools(["serpapi"], llm=llm) # drop LLM Math
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
agent.run("Is Amazon affected by rising GPU prices? Is it cutting capital expenditure in response?")
> Entering new AgentExecutor chain...
We need to use the search engine to find information on how rising GPU prices are impacting Amazon's capital expenditure.
Action: Search
Action Input: "Amazon impact of rising GPU prices on capital expenditure"
Observation: [' entity_type: related_questions.', "Cost overload: Data center expansion costs are escalating due to the need for expensive hardware like Nvidia's GPUs and other IT infrastructure.", "They're planning to spend around $105 billion on capex in 2025, up 40% from last year's $75 billion. The CEO was super bullish on AI for the long-term.", 'Amazon plans to continue its heavy investment in AI, with capital expenditures expected to reach $75 billion in 2024 and even higher in 2025.', 'Amazon said it plans to boost capital expenditures to $100 billion in 2025, largely driven by AI investments.', 'Amazon spent $26.3 billion in capital expenditures in the last three months of 2024, the vast majority of which went toward AI-related projects ...', 'In total, Amazon, Meta, Microsoft, and Alphabet intend to spend a whopping $315 billion in capital expenditures this year.', 'Amazon estimates capex for the full year will total approximately $75 billion. The majority of that spending will support the growing need for technology ...', "Amazon's investment in AI chips is substantial, with the company expecting to spend $75 billion in capital expenditures in 2024, with the ...", 'In Q4 2024 alone, Amazon spent $26.3 billion on capex, meaning its 2025 annualized capex will exceed $100 billion. Key ...', "Lastly, Alphabet and Amazon are projected to spend an estimated $180 billion combined on AI capex this year. On the surface, big tech's rising ..."]
Thought:Based on the search results, Amazon does not seem to be cutting its capital expenditure in response to rising GPU prices. In fact, it appears Amazon plans to increase its capital expenditures, largely driven by AI investments.
Final Answer: No, Amazon is not cutting its capital expenditure in response to rising GPU prices, but rather plans to increase it for AI investments.

> Finished chain.
'No, Amazon is not cutting its capital expenditure in response to rising GPU prices, but rather plans to increase it for AI investments.'

36.17. Using AWS Bedrock#

# import boto3
# import json

# from langchain.llms.bedrock import Bedrock

# agent_llm = Bedrock(
#     client=boto3.client(service_name='bedrock-runtime', region_name=os.environ.get("AWS_REGION"),),
#     model_id="anthropic.claude-v2",
#     model_kwargs={
#         "max_tokens_to_sample": 500,
#         "temperature": 0.0,
#     },
# )
# tools = load_tools(["serpapi"], llm=agent_llm) # drop LLM Math
# agent = initialize_agent(tools, llm=agent_llm, agent="zero-shot-react-description", verbose=True)
# agent.run("Is Amazon affected by rising GPU prices? Is it cutting capital expenditure in response?")

36.18. Sequential Chaining#

The method used here is ConversationChain which has memory.

Ref: https://langchain.readthedocs.io/en/latest/modules/chains/getting_started.html

The sequence of code blocks below remember the previous discussion because the conversation chain object has memory.

from langchain import OpenAI, ConversationChain
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)
<ipython-input-27-bbd7961b959d>:3: LangChainDeprecationWarning: The class `ChatOpenAI` was deprecated in LangChain 0.0.10 and will be removed in 1.0. An updated version of the class exists in the :class:`~langchain-openai package and should be used instead. To use it run `pip install -U :class:`~langchain-openai` and import as `from :class:`~langchain_openai import ChatOpenAI``.
  llm = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=0)
<ipython-input-27-bbd7961b959d>:4: LangChainDeprecationWarning: The class `ConversationChain` was deprecated in LangChain 0.2.7 and will be removed in 1.0. Use :meth:`~RunnableWithMessageHistory: https://python.langchain.com/v0.2/api_reference/core/runnables/langchain_core.runnables.history.RunnableWithMessageHistory.html` instead.
  conversation = ConversationChain(llm=llm, verbose=True)
/usr/local/lib/python3.11/dist-packages/pydantic/main.py:253: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/
  validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
conversation.predict(input="Why are multinational banks laying off employees?")
> 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: Why are multinational banks laying off employees?
AI:

> Finished chain.
"Multinational banks are laying off employees for a variety of reasons. One major factor is the increasing use of technology and automation in the banking industry, which has led to a decrease in the need for human employees. Additionally, economic factors such as changes in market conditions, regulatory requirements, and cost-cutting measures may also contribute to layoffs in multinational banks. It's important to note that each bank may have its own specific reasons for laying off employees, so it's always best to look at the individual circumstances of each case."
conversation.predict(input="Which companies are laying off employees?")
> 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: Why are multinational banks laying off employees?
AI: Multinational banks are laying off employees for a variety of reasons. One major factor is the increasing use of technology and automation in the banking industry, which has led to a decrease in the need for human employees. Additionally, economic factors such as changes in market conditions, regulatory requirements, and cost-cutting measures may also contribute to layoffs in multinational banks. It's important to note that each bank may have its own specific reasons for laying off employees, so it's always best to look at the individual circumstances of each case.
Human: Which companies are laying off employees?
AI:

> Finished chain.
"Some of the multinational banks that have recently announced layoffs include HSBC, Deutsche Bank, Citigroup, and Barclays. These banks have cited various reasons for the layoffs, such as restructuring efforts, cost-cutting measures, and the impact of the COVID-19 pandemic on their business operations. It's important to keep in mind that the situation is constantly evolving, so it's a good idea to stay updated on the latest news from these companies."
conversation.predict(input="How many employees is Uber letting go? Why is Uber laying them off?")
> 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: Why are multinational banks laying off employees?
AI: Multinational banks are laying off employees for a variety of reasons. One major factor is the increasing use of technology and automation in the banking industry, which has led to a decrease in the need for human employees. Additionally, economic factors such as changes in market conditions, regulatory requirements, and cost-cutting measures may also contribute to layoffs in multinational banks. It's important to note that each bank may have its own specific reasons for laying off employees, so it's always best to look at the individual circumstances of each case.
Human: Which companies are laying off employees?
AI: Some of the multinational banks that have recently announced layoffs include HSBC, Deutsche Bank, Citigroup, and Barclays. These banks have cited various reasons for the layoffs, such as restructuring efforts, cost-cutting measures, and the impact of the COVID-19 pandemic on their business operations. It's important to keep in mind that the situation is constantly evolving, so it's a good idea to stay updated on the latest news from these companies.
Human: How many employees is Uber letting go? Why is Uber laying them off?
AI:

> Finished chain.
"Uber recently announced that it is laying off approximately 3,700 employees, which represents about 14% of its workforce. The company is facing financial challenges due to the impact of the COVID-19 pandemic on its ride-hailing business. With a significant decrease in demand for rides, Uber has been forced to make difficult decisions to reduce costs and ensure its long-term sustainability. The layoffs are part of Uber's broader efforts to streamline its operations and focus on its core business areas."

36.19. Create a list of words for language scoring#

We can use LLMs to construct lexicons for us. This makes word lists easy and feasible.

This makes the use of the FinLex paper I published a year ago pretty much irrelevant. See the paper here: https://www.sciencedirect.com/science/article/pii/S2405918821000131 (how quickly new science can become obsolete…!)

llm_chain = LLMChain(prompt=prompt, llm=gpt35)
question = "Make a list of 25 words that are related to the environment"
p80(llm_chain.run(question))
1. Renewable 2. Sustainability 3. Conservation 4. Eco-friendly 5. Green 6.
Biodiversity 7. Carbon footprint 8. Recycle 9. Solar 10. Wind 11. Water
conservation 12. Pollution 13. Climate change 14. Energy efficient 15. Organic
16. Deforestation 17. Ozone layer 18. Landfills 19. Global warming 20. Compost
21. Wildlife conservation 22. Sustainable development 23. Emissions 24.
Ecosystem 25. Natural resources
%%time
llm_chain = LLMChain(prompt=prompt, llm=gpt35)
question = "Make a list of 100 words that are related to corporate governance"
p80(llm_chain.run(question))
1. Executive compensation 2. Shareholder rights 3. Board of directors 4. Annual
general meeting 5. Proxy voting 6. Corporate social responsibility 7.
Transparency 8. Risk management 9. Compliance 10. Accountability 11. Ethical
behavior 12. Conflict of interest 13. Stakeholder engagement 14. Audit committee
15. Internal controls 16. Whistleblower protection 17. Financial reporting 18.
Shareholder activism 19. Governance framework 20. Corporate culture 21.
Fiduciary duty 22. Independent directors 23. Disclosure standards 24. Anti-
corruption measures 25. Code of ethics 26. Code of conduct 27. Audit
independence 28. Corporate citizenship 29. Shareholder value 30. Corporate
reputation 31. Succession planning 32. Executive leadership 33. Compliance
programs 34. Code of governance 35. Conflict resolution 36. Corporate governance
rating 37. Emerging governance trends 38. Regulatory compliance 39. Governance
guidelines 40. Performance evaluation 41. Risk oversight 42. Corporate
governance principles 43. Governance best practices 44. Governance structure 45.
Transparency measures 46. Governance processes 47. Shareholder engagement 48.
Shareholder activism 49. Governance standards 50. Board composition 51.
Governance mechanisms 52. Governance policies 53. Governance controls 54.
Governance strategies 55. Governance oversight 56. Governance reforms 57.
Corporate governance issues 58. Governance guidelines 59. Governance mechanisms
60. Governance framework 61. Risk management framework 62. Governance reporting
63. Governance compliance 64. Governance audit 65. Governance monitoring 66.
Governance assessment 67. Governance disclosures 68. Governance accountability
69. Governance transparency 70. Governance evaluation 71. Governance performance
72. Governance assurance 73. Governance regulations 74. Governance practices 75.
Governance committee 76. Governance systems 77. Governance controls 78.
Governance procedures 79. Governance benchmarks 80. Governance guidelines 81.
Governance reforms 82. Governance policies 83. Governance oversight 84.
Governance reporting 85. Governance compliance 86. Governance audit 87.
Governance monitoring 88. Governance assessment 89. Governance disclosures 90.
Governance accountability 91. Governance transparency 92. Governance evaluation
93. Governance performance 94. Governance assurance 95. Governance regulations
96. Governance practices 97. Governance committee 98. Governance systems 99.
Governance controls 100. Governance procedures.
CPU times: user 30.5 ms, sys: 6.44 ms, total: 37 ms
Wall time: 4.22 s

36.20. Using Wolfram Alpha#

WA has been called a “semantic search engine” and for quite some time has been serving up accurate information of somewhat technical nature. Here we call it to get additional information about topics that are usually less likely to be integrated into the corpora on which LLMs are trained.

import nest_asyncio
nest_asyncio.apply()

import asyncio
from langchain.agents import load_tools, initialize_agent
from langchain_openai import ChatOpenAI

gpt35 = ChatOpenAI(temperature=0)
tool_names = ['wolfram-alpha']
tools = load_tools(tool_names)
agent = initialize_agent(tools, gpt35, agent="zero-shot-react-description", verbose=True)
agent.run("What are the statistics for the name 'Brian'?")
> Entering new AgentExecutor chain...
I should use wolfram_alpha to find the statistics for the name 'Brian'.
Action: wolfram_alpha
Action Input: statistics for the name Brian
Observation: Assumption: Brian (male given name) | expected total number alive today 
Answer: 1.085 million people
Thought:I now know the final answer
Final Answer: 1.085 million people

> Finished chain.
'1.085 million people'

36.21. Using the piping approach to chains#

LangChain allows standard pipe syntax. An example is as follows.

from langchain_core.prompts import PromptTemplate

# Set up Prompt Template
template = """Question: What is a {topic}

Answer: """

prompt = PromptTemplate(
    template = template,
    input_variables=["topic"]
)

chain = prompt | gpt35

res = chain.invoke({"topic": "quark"})
print(res, '\n\n')
p80(res.content)
content='A quark is a fundamental particle that is a building block of matter. Quarks are elementary particles that combine to form protons and neutrons, which are in turn the building blocks of atomic nuclei. Quarks have fractional electric charges and are held together by the strong nuclear force.' additional_kwargs={'refusal': None} response_metadata={'token_usage': {'completion_tokens': 58, 'prompt_tokens': 18, 'total_tokens': 76, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None} id='run-9a993862-c2fa-4828-8091-93faad195404-0' usage_metadata={'input_tokens': 18, 'output_tokens': 58, 'total_tokens': 76, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}} 


A quark is a fundamental particle that is a building block of matter. Quarks are
elementary particles that combine to form protons and neutrons, which are in
turn the building blocks of atomic nuclei. Quarks have fractional electric
charges and are held together by the strong nuclear force.

Now, we do the same thing with a Chain.

from langchain import LLMChain

chain = LLMChain(llm=gpt35, prompt=prompt)
res = chain.invoke({"topic": "quark"})
print(res, '\n\n')
p80(res['text'])
<ipython-input-41-bc0b21b651af>:3: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.
  chain = LLMChain(llm=gpt35, prompt=prompt)
{'topic': 'quark', 'text': 'A quark is a fundamental particle that is a building block of matter. Quarks are elementary particles that combine to form protons and neutrons, which are the building blocks of atomic nuclei. Quarks have fractional electric charges and are held together by the strong nuclear force.'} 


A quark is a fundamental particle that is a building block of matter. Quarks are
elementary particles that combine to form protons and neutrons, which are the
building blocks of atomic nuclei. Quarks have fractional electric charges and
are held together by the strong nuclear force.
from langchain import LLMChain, PromptTemplate
# Assuming gpt35 is already defined and imported

template = """
Create a news story about {topic} with an appropriate {header}. The main person in the story is {person}.
The story should only be two sentences in length.
"""

prompt = PromptTemplate(
    template=template,
    input_variables=["topic", "header", "person"]
)

header = "Inventor of DOGE"
person = "Elon Musk"
topic = "The 2024 US election"

news_story = LLMChain(llm=gpt35, prompt=prompt, output_key="news_story")
# Instead of chaining strings, create a dictionary of inputs
inputs = {"topic": topic, "header": header, "person": person}
# Invoke the LLMChain with the dictionary
result = news_story.invoke(inputs)
p80(result['news_story'])
<ipython-input-31-865d62bc050a>:18: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.
  news_story = LLMChain(llm=gpt35, prompt=prompt, output_key="news_story")
In a surprise turn of events, Elon Musk has announced his candidacy for the 2024
US election, running on a platform of innovation and economic growth, with his
beloved DOGE cryptocurrency as a central focus of his campaign. Supporters are
hopeful that his unique approach to politics will shake up the traditional
system and bring about real change in America.