ChatGPT мощный, но

ChatGPT, разработанный OpenAI, представляет собой замечательную языковую модель, известную своей способностью генерировать человеческие ответы и обрабатывать широкий спектр тем. Он превосходен в понимании контекста и предоставлении соответствующих ответов, что делает его идеальным для интерактивных диалоговых систем. Тем не менее, есть ограничения, которые следует учитывать. Во-первых, обучающие данные ChatGPT доступны только до сентября 2021 года, поэтому им может не хватать сведений о недавних событиях или научных открытиях. Кроме того, он был обучен на общедоступных данных, что ограничивало его способность обрабатывать запросы, связанные с частной или частной информацией.

Для приложений, которым требуется доступ к частным или незнакомым источникам данных, ограничения ChatGPT становятся очевидными. Его обучение на общедоступных данных и отсутствие доступа к закрытым базам данных или внутренним документам затрудняют его понимание и точность. Точно так же, если приложение использует данные, опубликованные после сентября 2021 года, ограничение знаний ChatGPT создает ограничение.

Для преодоления этих ограничений существуют потенциальные решения. Точная настройка модели с использованием соответствующих проприетарных данных может предоставить ChatGPT знания и контекст, специфичные для предметной области, что позволит получать более точные ответы. Интеграция внешних источников данных или каналов данных в режиме реального времени также может поддерживать чат-бот в актуальном состоянии с самой последней информацией, повышая ее актуальность и полезность.

Затем выделяются два подхода к расширению возможностей ChatGPT. Во-первых, это доработка базовых моделей GPT. Второй подход предполагает изучение инструмента под названием LangChain, который будет обсуждаться в последующих разделах статьи. Оба подхода предлагают потенциал для создания более мощного чат-бота, адаптированного к конкретным случаям использования.

В этой статье я покажу вам, как создать чат-бота, более мощного, чем ChatGPT, используя GPT в качестве базовой языковой модели. Фактически, вместо GPT мы можем использовать другие модели LLM, такие как LLama, Alpaca, MPT-7B, Falcon-40B, HuggingChat и т. д. Ниже приведены ингредиенты построения мощного чат-бот, чтобы иметь возможность быстро интегрировать личные данные или недавно опубликованные общедоступные данные:

- Модель LLM, такая как GPT 3.0, GPT 3.5 или GPT 4.0, которые являются базовыми моделями ChatGPT
- LangChain для объединения моделей в цепочку и предоставления модели LLM доступа к другим инструментам
- Weaviate: база данных векторов для хранения векторов (эмбеддингов) и быстрого вычисления сходства текста
- Система пользовательского поиска Google или API поисковой системы Bing для поиска последних общедоступных данных в Интернете.
- Модуль Википедия, чтобы модель читала Википедию для получения быстрой ценной информации.
- LLM-Math для математических вычислений.
- Немного соли и перца для вкуса
- Чашка кофе или чая для приятного обучения и развития

Что такое LangChain?

LangChain — это комплексная платформа, которая позволяет разрабатывать очень мощные приложения для чат-ботов, расширяя возможности ChatGPT. Основная идея LangChain заключается в расширении функциональности ChatGPT за счет предоставления доступа к различным источникам информации помимо данных обучения. Эти источники включают поиск Google, Википедию, частные данные и общедоступные данные, опубликованные после сентября 2021 года. LangChain исходит из убеждения, что наиболее эффективные приложения должны не только полагаться на языковые модели, но также демонстрировать осведомленность о данных и агентное поведение, что позволяет языку модели для взаимодействия с окружающей средой.

Платформа LangChain построена на принципах осведомленности о данных и агентности, что позволяет разработчикам создавать более надежные и дифференцированные приложения. Он состоит из нескольких основных модулей, которые служат строительными блоками любого приложения на основе языковой модели (LLM). Каждый модуль оснащен стандартными и расширяемыми интерфейсами, что делает его гибким и адаптируемым к различным вариантам использования. Кроме того, LangChain предоставляет внешние интеграции и сквозные реализации, которые можно легко использовать.

  • Модели: модули, охватываемые LangChain, включают модели, которые охватывают различные типы моделей, используемых в платформе, такие как LLM, модели чата и модели встраивания текста.
  • Подсказки: управление подсказками, их оптимизация и сериализация облегчаются с помощью модуля подсказок, в котором представлена ​​концепция создания входных данных с помощью шаблонов подсказок. Эти шаблоны позволяют создавать динамические и гибкие подсказки, часто включая примеры, выбранные на лету.
  • Память. Кроме того, LangChain включает в себя концепцию памяти, позволяющую сохранять состояние между вызовами цепочки или агента. Это особенно ценно для таких приложений, как чат-боты, где сохранение предыдущих взаимодействий необходимо для сохранения контекста и достижения большей связности разговора. Фреймворк предлагает компоненты памяти, в том числе утилиты для управления сообщениями чата и манипулирования ими, которые можно легко интегрировать в цепочки.
  • Индексы играют решающую роль в структуре LangChain, позволяя языковым моделям использовать данные, специфичные для приложения. Модуль Indexes предоставляет интерфейсы и интеграции для загрузки, запроса и обновления внешних источников данных. Он предлагает служебные функции для работы с документами и различными типами указателей, уделяя особое внимание поисковым механизмам. Ретриверы, неотъемлемая часть модуля, облегчают выбор релевантных документов на основе пользовательских запросов, превращая индексы в мощный инструмент для поиска информации.
  • Цепи: цепочки и агенты являются неотъемлемыми компонентами LangChain, облегчающими структурированные последовательности вызовов и процессы принятия решений. Цепочки позволяют объединять LLM или другие утилиты, предоставляя стандартизированный интерфейс и общие реализации для простоты использования.
  • Агенты, с другой стороны, представляют концепцию принятия решений на основе директив и инструментов высокого уровня. Агенты действий и Агенты планирования и выполнения предлагают разные подходы к выполнению действий, причем последний включает этапы планирования для достижения долгосрочных целей.
  • Обратные вызовы: LangChain включает систему обратных вызовов, которая позволяет разработчикам регистрировать и передавать промежуточные шаги в цепочке, предлагая расширенные возможности наблюдения, отладки и оценки. Подписываясь на различные события через аргумент callbacks, разработчики могут получить доступ к подробной информации и настроить поведение своих приложений LLM.

Сходство запроса и текста:

Одна из проблем, с которыми сталкиваются модели больших языков (LLM), заключается в их ограниченных знаниях, ограниченных тем, что они узнали во время обучения. Однако есть способ позволить LLM эффективно использовать частные данные или недавно опубликованные общедоступные данные. Подход включает предоставление доступа к новым текстовым данным для LLM посредством процесса обнаружения данных. Типичный метод достижения этого — преобразование частных данных во вложения и их сохранение в векторной базе данных. Вот обзор процесса:

  • Разбиение данных: личные данные делятся на более мелкие фрагменты или фрагменты.
  • Обработка LLM. Каждый фрагмент данных затем обрабатывается LLM, а последний уровень сети создает представление семантического вектора, которое служит встраиванием для этих конкретных данных.
  • Хранение базы данных. Векторные представления данных хранятся в базе данных, что позволяет легко извлекать и восстанавливать исходную информацию.

Когда задан вопрос, его можно преобразовать во вложение, известное как запрос. Путем поиска подобных вложений в пространстве можно идентифицировать соответствующие фрагменты данных. Затем эти документы можно передать в LLM, что позволит ему извлечь из них необходимые ответы. Такой подход позволяет LLM эффективно использовать личные данные и повышать свою способность предоставлять точные и обоснованные ответы.

Чтобы предоставить моделям больших языков (LLM) доступ к дополнительным данным, частным или общедоступным, мы выполняем следующую последовательность шагов:

  1. Чтение данных. Сначала мы считываем данные в память, учитывая различные форматы, такие как файлы PDF, просканированные данные веб-сайтов или архивы. Для целей этого обсуждения мы предполагаем, что данные могут быть размещены в памяти. Однако для больших наборов данных необходимо учитывать дополнительные соображения.
  2. Разбиение текста на фрагменты. Затем текст делится на более мелкие фрагменты для облегчения обработки и анализа.
  3. Преобразование встраивания: фрагментированные тексты преобразуются во встраивания, которые представляют собой векторные представления, отражающие сущность данных. Эти вложения хранятся в векторных базах данных, которые мы рассмотрим далее в следующем разделе.
  4. Измерение сходства: при выполнении поискового запроса мы вычисляем сходство между запросом и векторами, хранящимися в базе данных, используя такие меры, как косинусное сходство. Базы данных векторов индексируются, что позволяет эффективно обрабатывать функцию подобия для быстрого поиска наиболее похожих векторов.
  5. Интеграция с Langchain и LLM. Извлеченные тексты, полученные в результате поиска по сходству, передаются в Langchain и LLM для дальнейшей обработки, используя возможности языковых моделей для генерации ответов или извлечения информации.

Следуя этим шагам, мы можем расширить возможности LLM, включив дополнительные источники данных и обеспечив эффективный поиск и анализ соответствующей информации.

Почему векторные базы данных:

Базы данных векторов необходимы, потому что они хранят данные в виде многомерных векторов, обеспечивая быстрый и точный поиск и извлечение сходства. Эти базы данных используют математические представления функций или атрибутов, созданные путем применения функций внедрения к необработанным данным, таким как текст, изображения или аудио. В отличие от традиционных баз данных, векторные базы данных позволяют выполнять запросы на основе семантического или контекстуального значения, находя похожие элементы на основе визуального содержания, темы, настроения или характеристик. Для выполнения поиска по сходству используется вектор запроса, представляющий желаемые критерии, вместе с мерой подобия для вычисления векторного расстояния. Результаты представляют ранжированный список векторов с наивысшими показателями сходства, что позволяет получить доступ к соответствующим необработанным данным. Векторные базы данных улучшают поиск данных и позволяют использовать расширенные приложения в различных областях.

Давайте использовать Weaviate:

Weaviate — это база данных векторов с открытым исходным кодом, сочетающая векторный поиск со структурированной фильтрацией. Он предоставляет ряд возможностей и преимуществ, что делает его ценным инструментом для различных вариантов использования.

По своей сути Weaviate позволяет хранить и извлекать объекты данных на основе их семантических свойств, индексируя их с помощью векторов. Его можно использовать как отдельную базу данных или в сочетании с модулями, которые обрабатывают векторизацию и расширяют ее основные функции. С GraphQL API доступ к данным становится беспрепятственным.

К преимуществам Weaviate относятся быстрые запросы, позволяющие осуществлять поиск ближайшего соседа среди миллионов объектов менее чем за 100 мс. Он поддерживает различные типы мультимедиа через модули Weaviate, используя модели искусственного интеллекта, такие как Transformers, для векторизации данных во время операций поиска и запросов. Weaviate упрощает комбинированный векторный и скалярный поиск, обеспечивая эффективный поиск объектов и векторов без необходимости использования стороннего хранилища объектов.

Weaviate обеспечивает постоянный доступ к данным в режиме реального времени, даже во время импорта или обновления, и предлагает горизонтальную масштабируемость для удовлетворения различных потребностей. Высокая доступность также находится в планах. Кроме того, Weaviate обеспечивает экономическую эффективность, позволяя частично хранить большие наборы данных в памяти, обеспечивая баланс между скоростью и стоимостью.

База данных позволяет создавать графоподобные связи между объектами, имитируя реальные связи, и предлагает модульный механизм индексации векторов, а текущий подключаемый модуль представляет собой многослойный граф Hierarchical Navigable Small World (HNSW).

Подход Weaviate к векторной базе данных превосходит традиционные поисковые системы, поскольку он может находить семантически связанные данные, даже если точные совпадения фраз недоступны. Он поддерживает векторизацию для различных типов моделей машинного обучения, включая текст, изображения, аудио, видео и гены.

Weaviate рекомендуется для сценариев, когда существующие поисковые системы дают неудовлетворительные результаты или когда вам требуется текстовый поиск и поиск по сходству изображений с использованием современных моделей машинного обучения. Он превосходно сочетает семантический и скалярный поиск и поддерживает масштабирование моделей машинного обучения до производственных размеров. Weaviate находит применение в семантическом поиске, поиске изображений, обнаружении аномалий, механизмах рекомендаций, поиске в электронной коммерции, классификации данных, анализе угроз кибербезопасности и многом другом.

Фактически Weaviate можно заменить другими векторными базами данных, такими как Zilliz, Milvus, Vespa, Chroma, Pinecoin и Embeddinghub. Помимо замечательных функций Weaviate, он предлагает бесплатные пробные версии, которые могут быть очень удобны, если вы просто хотите пройти это руководство бесплатно!

Приступаем к кодированию:

В этом уроке я использую Google Colab для кодирования. Поскольку мы не собираемся обучать какую-либо модель машинного обучения, нам не нужно ничего, кроме процессора. Начнем с установки необходимых модулей Python:

!pip install langchain   
!pip install OpenAI
!pip install wikipedia
!pip install unstructured
!pip install pdf2image    # Since we will be loading pdf documents
!pip install tiktoken     # tiktoken is a fast BPE tokeniser for use with OpenAI's models
!pip install weaviate-client

Для простоты этого урока я скачал pdf-файлы и сохранил их на Google Диске. Таким образом, следующий код можно использовать для подключения к Google Диску и установки корневого рабочего каталога:

import os

from google.colab import drive
drive.mount('/content/drive')

# Root directory of the project
ROOT_DIR = os.path.abspath("/content/gdrive/.../Smart_Chatter/Raw_Data/FOMC/")
os.chdir(ROOT_DIR)

Для этого руководства мы собираемся загрузить документы заседания Федерального комитета по открытым рынкам (FOMC) за 2020–2023 годы. Этот комитет является организацией в США, которая принимает решения о повышении процентных ставок. Возможно, с помощью усиленного чат-бота мы сможем понять, почему они так высоко повысили процентные ставки! Всего я скачал 66 pdf-файлов и сохранил их на Google Диске. Обратите внимание, что мы также можем сделать его автоматизированным путем прямой загрузки данных в базу данных или веб-сканирования данных.

Затем нам нужно создать ключи API OpenAI:

os.environ["OPENAI_API_KEY"] ="..."

Где получить ключ API openAI:
- После регистрации на платформе openAI вы можете перейти здесь, чтобы создать свой API.

Затем мы можем использовать следующий код для загрузки наших файлов PDF в память:

from langchain.document_loaders import DirectoryLoader

doc_loader = DirectoryLoader(
    '', # the relative directory address, remember we set root directory above
    glob='**/*.pdf',     # Let's load only pdf files in every subdirectory
    show_progress=True
)
docs = doc_loader.load()

Обратите внимание, что это руководство не будет касаться масштабируемости или других показателей производительности, поскольку оно в основном сосредоточено на функциональности расширения возможностей ChatGPT с помощью дополнительных инструментов. Итак, мы предполагаем, что у нас нет проблем с загрузкой всех необработанных данных в память!

Теперь давайте разделим данные на куски:

from langchain.text_splitter import CharacterTextSplitter

splitter = CharacterTextSplitter(
    chunk_size=1000, 
    chunk_overlap=300
)
splitted_docs_list = splitter.split_documents(docs)

Вот часть разделенных данных:

Следующим шагом будет преобразование каждого разбиения текста во вложения и сохранение векторов в нашей векторной базе данных. Итак, давайте настроим нашу векторную базу данных Weaviate. Ниже приведены шаги по созданию векторной базы данных и подготовке ее для хранения наших данных:
- Создайте учетную запись в Weaviate. Вы можете зарегистрироваться бесплатно.

  • Затем можно создать кластер здесь. Вы можете назначить имя, которое будет использоваться для создания имени кластера. Для этого руководства вам не нужно вносить какие-либо изменения в настройки по умолчанию.
  • Через несколько секунд база данных векторов будет готова, и вы можете щелкнуть детали, чтобы получить URL-адрес кластера базы данных векторов и ключ API.

  • Установите ключ API базы данных:
os.environ["WEAVIATE_API_KEY"] ="..."
  • Создайте подключение к базе данных с помощью инструментов подключения weaviate langchain. Мы используем ключ API базы данных и URL-адрес кластера в следующем коде для аутентификации. Мы используем OpenAIEmbeddings для встраивания наших текстовых фрагментов и сохранения их в базе данных Weaviate.
from langchain.vectorstores.weaviate import Weaviate
import weaviate
from langchain.embeddings import OpenAIEmbeddings

auth_config = weaviate.auth.AuthApiKey(api_key=os.environ.get('WEAVIATE_API_KEY'))
client = weaviate.Client(
    url="...",
    auth_client_secret=auth_config,
    additional_headers={
        "X-OpenAI-Api-Key": os.environ.get('OPENAI_API_KEY')
    }
)

# We need to set index_name and vectorizer for the database, 
# otherwise we will not be able to measure text similarities
# langchain is supposed to set this for you, add this if needed
# You just need to do it the very first time setting the class
class_obj = {
    "class": "LangChain",
    "vectorizer": "text2vec-openai",
}

try:
  # Add the class to the schema
  client.schema.create_class(class_obj)
except:
  print("Class already exists")

embeddings = OpenAIEmbeddings()
# I use 'LangChain' for index_name and 'text' for text_key
vectorstore = Weaviate(client, "LangChain", "text", embedding=embeddings)

Вот как выглядит схема класса векторной базы данных:

{'classes': [{'class': 'LangChain',
   'invertedIndexConfig': {'bm25': {'b': 0.75, 'k1': 1.2},
    'cleanupIntervalSeconds': 60,
    'stopwords': {'additions': None, 'preset': 'en', 'removals': None}},
   'moduleConfig': {'text2vec-openai': {'model': 'ada',
     'modelVersion': '002',
     'type': 'text',
     'vectorizeClassName': True}},
   'properties': [],
   'replicationConfig': {'factor': 1},
   'shardingConfig': {'virtualPerPhysical': 128,
    'desiredCount': 1,
    'actualCount': 1,
    'desiredVirtualCount': 128,
    'actualVirtualCount': 128,
    'key': '_id',
    'strategy': 'hash',
    'function': 'murmur3'},
   'vectorIndexConfig': {'skip': False,
    'cleanupIntervalSeconds': 300,
    'maxConnections': 64,
    'efConstruction': 128,
    'ef': -1,
    'dynamicEfMin': 100,
    'dynamicEfMax': 500,
    'dynamicEfFactor': 8,
    'vectorCacheMaxObjects': 1000000000000,
    'flatSearchCutoff': 40000,
    'distance': 'cosine',
    'pq': {'enabled': False,
     'bitCompression': False,
     'segments': 0,
     'centroids': 256,
     'encoder': {'type': 'kmeans', 'distribution': 'log-normal'}}},
   'vectorIndexType': 'hnsw',
   'vectorizer': 'text2vec-openai'}]}

Теперь мы можем добавить фрагменты текста в векторную базу данных Weaviate, используя следующий код:

documents = docs_split_list

texts = [d.page_content for d in documents]
metadatas = [d.metadata for d in documents]

vectorstore.add_texts(texts, metadatas=metadatas, embedding=embeddings)

Вместо описанного выше подхода (создания класса и последующего добавления векторов) мы можем использовать следующий код для создания класса из текстов:

vectorstore = Weaviate.from_texts(
    texts,
    embeddings,
    metadatas=metadatas,
    client=client,
)

Теперь давайте посмотрим, как мы можем найти содержимое, похожее на запрос:

query = "What was Core PCE inflation in December 2022?"
vectorstore.similarity_search(query)

Вот идентифицированные похожие фрагменты текста:

Теперь давайте воспользуемся LangChain RetrievalQA, чтобы познакомить наше векторное хранилище с моделью llm. Подробнее о RetrievalQA можно узнать здесь.

from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI()
retrieval_qa = RetrievalQA.from_chain_type(
    llm=llm, 
    chain_type='stuff',
    retriever=vectorstore.as_retriever(),
)

query = "How was core pce inflation's trend in 2022?"
retrieval_qa.run(query)

Вот результат:

Отлично, мы добавили одну силу в модель llm. Должны ли мы остановиться на этом и подготовить его к производству? Нет! Не так быстро!

Нашего дополнительного текста может быть недостаточно для получения лучших ответов. Давайте добавим в нашу модель еще несколько инструментов. Мы можем добавить модуль Википедии в нашу модель, чтобы она могла получать оттуда последние обновления. Для этой цели мы можем использовать агентов LangChain. Как вы помните из приведенных выше обсуждений, в контексте создания мощного чат-бота существует два основных типа агентов: агенты действия и агенты планирования и выполнения. Агенты действий принимают решения и выполняют действия шаг за шагом, что делает их подходящими для небольших задач. С другой стороны, агенты Plan-and-Execute сначала планируют ряд действий, а затем выполняют их последовательно, что делает их более подходящими для сложных или длительных задач. На практике часто бывает полезно иметь агента действия, ответственного за выполнение плана для агента «планируй и выполняй».

Агенты играют решающую роль в функциональности чат-бота. Они получают пользовательский ввод, решают, какой инструмент использовать (если есть) и какие данные предоставить этому инструменту. Затем вызывается выбранный инструмент, и наблюдение записывается. Этот процесс повторяется до тех пор, пока агент не решит, что ему больше не нужно использовать инструмент, и не ответит непосредственно пользователю. Агенты состоят из нескольких абстракций, включая самого агента, в котором находится логика приложения, и агента-исполнителя, отвечающего за итеративный запуск агента и инструментов до тех пор, пока не будет выполнен критерий остановки.

Для создания агента типичный подход включает использование PromptTemplate для создания подсказки на основе пользовательского ввода и предыдущих шагов, языковой модели для обработки подсказки и создания вывода и синтаксического анализатора вывода для анализа вывода в объект AgentAction или AgentFinish. . Документация по агентам в LangChain представляет собой пошаговое руководство, начиная с записной книжки «Приступая к работе», за которой следуют разделы, посвященные инструментам, агентам, наборам инструментов и классу Agent Executor. В этих разделах рассматриваются типы инструментов, агентов и наборов инструментов, поддерживаемых LangChain, а также параметры настройки для Agent Executor.

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

# llm = ChatOpenAI() # which was defined before
# we load wikipedia
tools = load_tools(['wikipedia'], llm=llm)

# We initialize agent with tools, llm, and type of agent
# The zero-shot-react-description agent leverages the ReAct framework to identify the appropriate tool solely based on its description. It can handle multiple tools simultaneously, and it is essential to provide a description for each tool when using this agent.
agent = initialize_agent(
    tools, 
    llm, 
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, 
    verbose=True
)

agent.run('why Silicon Valley Bank collapsed in 2023?')

Вот вывод приведенного выше кода, который может вызывать Википедию несколько раз:

> Entering new AgentExecutor chain...
I'm not sure why Silicon Valley Bank collapsed, I should try to look it up
Action: Wikipedia
Action Input: "Silicon Valley Bank collapse 2023"
Observation: Page: Collapse of Silicon Valley Bank
Summary: On Friday, March 10, 2023, Silicon Valley Bank (SVB) failed after a bank run, marking the third-largest bank failure in United States history and the largest since the 2007–2008 financial crisis. It was one of three bank failures, along with Silvergate Bank and Signature Bank, in March 2023 in the United States. 
Seeking higher investment returns from its burgeoning deposits, SVB had dramatically increased its holdings of long-term securities since 2021, accounting for them on a hold-to-maturity basis. The market value of these bonds decreased significantly through 2022 and into 2023 as the Federal Reserve raised interest rates to curb an inflation surge, causing unrealized losses on the portfolio. Higher interest rates also raised borrowing costs throughout the economy and some Silicon Valley Bank clients started pulling money out to meet their liquidity needs. To raise cash to pay withdrawals by its depositors, SVB announced on Wednesday, March 8 that it had sold over US$21 billion worth of securities, borrowed $15 billion, and would hold an emergency sale of some of its treasury stock to raise $2.25 billion. The announcement, coupled with warnings from prominent Silicon Valley investors, caused a bank run as customers withdrew funds totaling $42 billion by the following day, Thursday.
On the morning of March 10, the California Department of Financial Protection and Innovation seized SVB and placed it under the receivership of the Federal Deposit Insurance Corporation (FDIC). An additional $100 billion were expected to be withdrawn during Friday. About 89 percent of the bank's $172 billion in deposit liabilities exceeded the maximum insured by the FDIC. Two days after the failure, the FDIC received exceptional authority from the Treasury and announced jointly with other agencies that all depositors would have full access to their funds the next morning. Seeking to auction off all or parts of the bank, the FDIC reopened it on Monday March 13 as a newly organized bridge bank, Silicon Valley Bridge Bank, N.A. Although some characterized the government response as a bailout, the plan did not entail rescuing the bank, its management or shareholders, but rather making uninsured depositors whole from the proceeds of selling the bank's assets, without the use of taxpayer money.The collapse of SVB had significant consequences for startup companies in the U.S. and abroad, with many briefly unable to withdraw money from the bank. Other large technology companies, media companies, and wineries were also affected. For a number of founders and their venture capital backers, this was the bank of choice.

Page: Silicon Valley Bank
Summary: Silicon Valley Bank (SVB) was a state-chartered commercial bank headquartered in Santa Clara, California. It operated branches in California and Massachusetts. The bank was the primary subsidiary of SVB Financial Group, a publicly traded bank holding company that had offices in 15 U.S. states and over a dozen international jurisdictions.As a regional bank in the Bay Area, SVB offered services specifically designed to meet the needs of the tech industry, and soon became the largest bank by deposits in Silicon Valley and the preferred bank of almost half of all venture-backed tech startups. In March 2023, after central bank–endorsed interest rate hikes during the 2021–2023 inflation surge, there was a bank run on its deposits, which led to its collapse and seizure on March 10, 2023 by the California Department of Financial Protection and Innovation (DFPI), its regulator. Citing inadequate liquidity and insolvency, state officials at the DFPI appointed the Federal Deposit Insurance Corporation (FDIC) receiver of the bank. This marked the third-largest bank failure in U.S. history and took place alongside the March 2023 United States bank failures.On March 12, 2023, a joint statement was issued by Secretary of the Treasury Janet Yellen, Federal Reserve Chairman
Thought:Based on the information I found, it seems that Silicon Valley Bank collapsed in 2023 due to a combination of factors, including the bank's increased holdings of long-term securities, rising interest rates, and withdrawals by some of its clients. This led to a bank run, and the bank was seized by the California Department of Financial Protection and Innovation and placed under the receivership of the Federal Deposit Insurance Corporation. The collapse had significant consequences for startup companies in the U.S. and abroad, as well as other large technology companies, media companies, and wineries. 
Final Answer: Silicon Valley Bank collapsed in 2023 due to a bank run caused by factors such as increased holdings of long-term securities, rising interest rates, and withdrawals by some of its clients.

> Finished chain.
Silicon Valley Bank collapsed in 2023 due to a bank run caused by factors such as increased holdings of long-term securities, rising interest rates, and withdrawals by some of its clients.

Еще одним полезным инструментом, который будет добавлен к нашему чат-боту, будет инструмент поиска в Интернете, такой как Система пользовательского (программируемого) поиска Google или API веб-поиска Bing. Давайте воспользуемся поисковой системой Google для этой цели:

  1. В консоли GCP после выбора или создания проекта настройте API пользовательского поиска:
  • Перейдите на панель API и служб.
  • Нажмите «Включить API и службы».
  • Найдите API пользовательского поиска и включите его.
  • Перейдите на страницу учетных данных.
  • Нажмите «Создать учетные данные».
  • Выберите «Ключ API», скопируйте сгенерированный ключ и установите его в свой код.

2. Включите API пользовательского поиска для своего проекта.

  • Перейдите на страницу системы пользовательского поиска.
  • Нажмите «Добавить», чтобы создать новую поисковую систему.
  • Следуйте подсказкам, чтобы настроить поисковую систему, выбрав поиск во всей сети или на определенных сайтах.
  • После создания поисковой системы нажмите «Панель управления».
  • Перейдите в «Основы», скопируйте идентификатор поисковой системы и введите его в свой код.
# Google Custom Search Engine Keys 
os.environ['GCP_CSE_ID'] = "..."
os.environ['GCP_CSE_API_KEY'] = "..."

Итак, давайте добавим несколько инструментов в нашу модель LLM:

from langchain.agents import Tool

llm = ChatOpenAI()
tools = load_tools([
    'wikipedia', 
    'google-search',
    'llm-math'
], llm=llm)

tools.append(Tool.from_function(
        func=qa.run,
        name = "FOMC DB",
        description="Useful for federal reserve documents for interest rates and inflation."
        # coroutine= ... <- you can specify an async method if desired as well
    ))

Нам также нужно добавить памяти в наш LLM. Как вы помните из приведенного выше обсуждения, LangChain предоставляет отличный метод добавления памяти в LLM. В LangChain память играет решающую роль в таких приложениях, как чат-боты, где важно помнить предыдущие взаимодействия. По умолчанию цепочки и агенты не имеют состояния и обрабатывают каждый запрос независимо. Чтобы решить эту проблему, LangChain предлагает компоненты памяти в двух формах. Во-первых, он предоставляет вспомогательные утилиты для управления сообщениями чата и манипулирования ими, разработанные так, чтобы быть универсальными и адаптируемыми. Во-вторых, LangChain облегчает интеграцию этих утилит в цепочки.

Память включает в себя поддержание состояния, которое фиксирует взаимодействие пользователя с языковой моделью. LangChain достигает этого с помощью ChatMessages, которые включают прием, сбор, преобразование и извлечение знаний из последовательности сообщений. Существуют различные типы памяти, каждый из которых предлагает разные подходы к управлению этим процессом.

Для каждого типа памяти существуют отдельные функции, извлекающие информацию из последовательностей сообщений. Кроме того, эти типы памяти могут быть объединены в цепочки для дальнейшего использования. Память может содержать множество фрагментов информации, например N последних сообщений или сводку всех предыдущих сообщений. Возвращаемая информация может быть в виде строки или списка сообщений.

В этом руководстве мы используем ConversationBufferMemory для памяти чат-бота.

from langchain.memory import ConversationBufferMemory
from langchain.experimental.plan_and_execute import (
    PlanAndExecute, 
    load_agent_executor, 
    load_chat_planner
)

memory = ConversationBufferMemory(memory_key='chat_history')
planner = load_chat_planner(llm)
executor = load_agent_executor(llm, tools, verbose=True)

agent = PlanAndExecute(
    planner=planner, 
    executor=executor, 
    verbose=True, 
    reduce_k_below_max_tokens=True
)

Давайте зададим сложный вопрос от нашего уполномоченного чат-бота, на который ChatGPT не смог ответить:

agent.run("Compare what the fed said about inflation in USA, in March 2023 and December 2022?")

Вот результат запроса:

> Entering new PlanAndExecute chain...
steps=[Step(value="Search for the Federal Reserve's statements on inflation in March 2023."), Step(value="Search for the Federal Reserve's statements on inflation in December 2022."), Step(value='Compare the language used in both statements.'), Step(value="Identify any changes or shifts in the Fed's stance on inflation."), Step(value='Summarize the similarities and differences between the two statements.'), Step(value="Respond to the user's original question with the findings.\n\n")]

> Entering new AgentExecutor chain...
Action:
```
{
  "action": "Financial DB",
  "action_input": "What did the Federal Reserve say about inflation in March 2023?"
}
``` 

Observation: The Federal Reserve mentioned that both market and survey-based measures continued to point to expectations for a moderation of inflation over the coming year.
Thought:
The human's previous objective was to search for the Federal Reserve's statements on inflation in March 2023, and the Financial DB tool was used to find this information. 

Action:
```
{
  "action": "Financial DB",
  "action_input": "What did the Federal Reserve say about inflation in March 2023?"
}
```

Observation: The Federal Reserve mentioned that both market- and survey-based measures continued to point to expectations for a moderation of inflation over the coming year.
Thought:
The human's previous objective was successfully achieved using the Financial DB tool. 

Action:
```
{
  "action": "Final Answer",
  "action_input": "The Federal Reserve mentioned that both market- and survey-based measures continued to point to expectations for a moderation of inflation over the coming year in March 2023."
}
```

> Finished chain.
*****

Step: Search for the Federal Reserve's statements on inflation in March 2023.

Response: The Federal Reserve mentioned that both market- and survey-based measures continued to point to expectations for a moderation of inflation over the coming year in March 2023.

> Entering new AgentExecutor chain...
Action:
```
{
  "action": "Financial DB",
  "action_input": "What did the Federal Reserve say about inflation in December 2022?"
}
``` 

Observation: In December 2022, the Federal Reserve acknowledged that elevated inflation had persisted for longer than anticipated and removed the reference to "transitory" factors affecting inflation. They noted that supply and demand imbalances continued to contribute to elevated inflation. The Federal Reserve also noted the risk of new variants of the virus in their assessment of risks to the economic outlook.
Thought:
Based on the previous step, it seems like the Federal Reserve's stance on inflation changed between March and December 2022. The human might want to know more about this shift.
Action:
```
{
  "action": "Wikipedia",
  "action_input": "What caused the shift in the Federal Reserve's stance on inflation between March and December 2022?"
}
``` 

Observation: Page: Causes of the Great Depression
Summary: The causes of the Great Depression in the early 20th century in the United States have been extensively discussed by economists and remain a matter of active debate. They are part of the larger debate about economic crises and recessions. The specific economic events that took place during the Great Depression are well established. 
There was an initial stock market crash that triggered a "panic sell-off" of assets. This was followed by a deflation in asset and commodity prices, dramatic drops in demand and the total quantity of money in the economy, and disruption of trade, ultimately resulting in widespread unemployment (over 13 million people were unemployed by 1932) and impoverishment. However, economists and historians have not reached a consensus on the causal relationships between various events and government economic policies in causing or ameliorating the Depression.
Current mainstream theories may be broadly classified into two main points of view. The first are the demand-driven theories, from Keynesian and institutional economists who argue that the depression was caused by a widespread loss of confidence that led to drastically lower investment and persistent underconsumption. The demand-driven theories argue that the financial crisis following the 1929 crash led to a sudden and persistent reduction in consumption and investment spending, causing the depression that followed. Once panic and deflation set in, many people believed they could avoid further losses by keeping clear of the markets. Holding money therefore became profitable as prices dropped lower and a given amount of money bought ever more goods, exacerbating the drop in demand. 
Second, there are the monetarists, who believe that the Great Depression started as an ordinary recession, but that significant policy mistakes by monetary authorities (especially the Federal Reserve) caused a shrinking of the money supply which greatly exacerbated the economic situation, causing a recession to descend into the Great Depression. Related to this explanation are those who point to debt deflation causing those who borrow to owe ever more in real terms.
There are also several various heterodox theories that reject the explanations of the Keynesians and monetarists. Some new classical macroeconomists have argued that various labor market policies imposed at the start caused the length and severity of the Great Depression.

Page: Janet Yellen
Summary: Janet Louise Yellen (born August 13, 1946) is an American economist serving as the 78th United States secretary of the treasury since January 26, 2021. She previously served as the 15th chair of the Federal Reserve from 2014 to 2018. She is the first person to hold those positions having also led the White House Council of Economic Advisers and the first woman to hold either post.
Born and raised in Bay Ridge, Brooklyn, Yellen graduated from Brown University in 1967 and earned a Ph.D. in economics from Yale University in 1971. She taught as an assistant professor at Harvard University from 1971 until 1976 when she began working for the Federal Reserve Board as a staff economist from 1977 to 1978 before joining the faculty of the London School of Economics from 1978 to 1980. Yellen is professor emeritus at the Haas School of Business and the University of California, Berkeley, where she has been a faculty member since 1980 and became the Eugene E. and Catherine M. Trefethen Professor of Business Administration and Professor of Economics.
Yellen served as a member of the Federal Reserve Board of Governors from 1994 to 1997 and was nominated to the position by President Bill Clinton, who then named her chair of the Council of Economic Advisers from 1997 to 1999. She subsequently returned to academia before being appointed president and chief executive officer of the Federal Reserve Bank of San Francisco from 2004 until 2010. Afterward, President Barack Obama chose her to repl
Thought:
Oops, it looks like the Wikipedia tool returned information unrelated to the question. Let me try again to find an explanation for the shift in the Federal Reserve's stance on inflation. 
Action:
```
{
  "action": "Google Search",
  "action_input": "What caused the Federal Reserve to change its stance on inflation between March and December 2022?"
}
``` 

Observation: Feb 15, 2023 ... To combat high inflation, the Federal Reserve sharply increased the ... Federal Open Market Committee since last March; in December 2022, ... Mar 16, 2022 ... For release at 2:00 p.m., EDT, March 16, 2022 ... Change in real GDP ... The December projections were made in conjunction with the meeting ... May 25, 2022 ... CBO expects the Federal Reserve to rapidly increase its target range ... wages for the year ending in March 2022 was 6.1 percent among the ... Mar 3, 2023 ... The Committee's primary means of adjusting the stance of monetary policy is through changes in the target range for the federal funds rate. Dec 17, 2021 ... The Federal Reserve stepped in with a broad array of actions to keep credit flowing to limit the economic damage from the pandemic. Mar 9, 2022 ... Advances in digital and distributed ledger technology for financial services have led to dramatic growth in markets for digital assets, ... Lasting from 1965 to 1982, it led economists to rethink the policies of the Fed and other central banks. Close-up of a&nbsp;"Whip Inflation Now"&nbsp;[WIN]. Apr 23, 2022 ... In March consumer prices were 8.5% higher than a year earlier, the fastest annual rise since 1981. In Washington inflation-watching is usually ... Mar 6, 2023 ... Third, especially in view of the transmission lags from shifts in the ... (i.e. Dec 2022) HICP data available at the time the Q1 2023 round ... 6 days ago ... Since March 2022, the Fed has raised the target federal funds rate by 5.00% ... of credit in the broader marketplace, might alter the Fed's ...
Thought:
It looks like the Google Search tool didn't provide a clear answer to the question. Let me try asking a more specific question. 
Action:
```
{
  "action": "Google Search",
  "action_input": "What factors caused the Federal Reserve to remove the reference to 'transitory' factors affecting inflation between March and December 2022?"
}
``` 

Observation: May 25, 2022 ... It is expected to remain high in 2022 because of various factors that ... CBO expects the Federal Reserve to rapidly increase its target ... Apr 12, 2021 ... We expect these three factors will likely be transitory, and that their impact should fade over time as the economy recovers from the ... Feb 15, 2023 ... CBO projects a federal budget deficit of $1.4 trillion for 2023. ... inflation gradually slows in 2023 as pressures ease from factors that, ... Aug 27, 2021 ... Inflation at these levels is, of course, a cause for concern. ... take for granted that inflation due to transitory factors will fade. Nov 16, 2021 ... The clearly temporary nature of those factors suggests we should not ... close to the Federal Reserve's target from the first month of the ... Directors serve as a link between the Federal Reserve and the private sector. ... Many factors affect spending, output, employment, and inflation. Mar 6, 2023 ... These factors contributed to the sharp rise in inflation since the middle of ... The Survey of Monetary Analysts and the December 2022 Broad ... Nov 30, 2022 ... Our inflation goal is for total inflation, of course, as food and ... Several factors contributed to this slowing growth, including the ... Dec 17, 2021 ... The Federal Reserve stepped in with a broad array of actions to keep credit flowing to limit the economic damage from the pandemic. Assistance listings refers to the publicly available listing of Federal ... It cannot be used as a factor during the merit review of applications or ...
Thought:
It seems like the Google Search tool is not providing a clear answer to the question and might not be the best tool to use in this case. Let me try using the Financial DB tool to see if it has any information on the factors that caused the Federal Reserve to remove the reference to "transitory" factors affecting inflation between March and December 2022.
Action:
```
{
  "action": "Financial DB",
  "action_input": "What factors caused the Federal Reserve to remove the reference to 'transitory' factors affecting inflation between March and December 2022?"
}
``` 

Observation: The Federal Reserve removed the reference to "transitory" factors affecting inflation between March and December 2022 because elevated inflation had persisted for longer than they had previously anticipated, and supply and demand imbalances continued to contribute to elevated inflation. The increase in inflation was seen as reflecting temporary factors, such as the rebound in spending as the economy continued to reopen and supply bottlenecks limiting how quickly production could respond to demand in the near term. However, members also viewed these increases in prices as likely to have only transitory effects on inflation.
Thought:
Based on the information from the Financial DB tool, it seems that the removal of the reference to "transitory" factors affecting inflation was due to the persistence of elevated inflation and ongoing supply and demand imbalances. It also appears that the initial view of the increase in inflation as temporary factors was revised. I can now provide a response to the human's question.
Action:
```
{
  "action": "Final Answer",
  "action_input": "The Federal Reserve removed the reference to 'transitory' factors affecting inflation between March and December 2022 due to the persistence of elevated inflation and ongoing supply and demand imbalances. The initial view of the increase in inflation as temporary factors was revised."
}
``` 

> Finished chain.
*****

Step: Search for the Federal Reserve's statements on inflation in December 2022.

Response: The Federal Reserve removed the reference to 'transitory' factors affecting inflation between March and December 2022 due to the persistence of elevated inflation and ongoing supply and demand imbalances. The initial view of the increase in inflation as temporary factors was revised.

> Entering new AgentExecutor chain...
Action:
```
{
  "action": "Google Search",
  "action_input": "Language used in Federal Reserve statements on inflation in March 2023 and December 2022"
}
``` 


Observation: Meeting calendars, statements, and minutes (2018-2023) ... (Released February 16, 2022). March. 15-16*. Statement: ... December. 14-15*. Statement: Mar 9, 2022 ... Digital assets may also be used as a tool to circumvent United States ... representatives of the Board of Governors of the Federal Reserve ... Mar 22, 2023 ... Table 1. Economic projections of Federal Reserve Board members and Federal Reserve Bank presidents, under their individual assumptions of ... Inflation expectations declined to 4.4 percent at the one-year-ahead horizon but increased slightly to 2.9 percent and 2.6 percent, respectively, ... Mar 23, 2023 ... Factors Affecting Reserve Balances of Depository Institutions and Condition Statement of Federal Reserve Banks. March 23, 2023 ... May 1, 2023 ... However, inflation rates remain above the Federal Reserve's 2 percent inflation ... points to 62.6 percent from December 2022 to March 2023. Enforcement Actions. 3/24/2023. Federal Reserve Board releases annual audited financial statements. Other Announcements. 3/24/2023. Feb 1, 2023 ... The Federal Reserve on Wednesday released its decision on interest rates following its two-day meeting. Feb 15, 2023 ... To combat high inflation, the Federal Reserve sharply increased the ... terms) is on track to increase by 6 percent from 2022 to 2023. This dataset provides high-level information on the federal government's outstanding debts, holdings, and the statutory debt limit on a monthly basis.
Thought:WARNING:langchain.chat_models.openai:Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 2.0 seconds as it raised RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-VsX0Fe8EKYd3geoLcIzmqkNp 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 wasn't able to find a direct comparison of the language used in the two statements, but I did find relevant information in the minutes of the meetings. In March 2023, the Federal Reserve mentioned that both market- and survey-based measures continued to point to expectations for a moderation of inflation over the coming year. In December 2022, the Federal Reserve removed the reference to 'transitory' factors affecting inflation between March and December 2022 due to the persistence of elevated inflation and ongoing supply and demand imbalances. The initial view of the increase in inflation as temporary factors was revised. 

Action:
```
{
  "action": "Final Answer",
  "action_input": "In March 2023, the Federal Reserve mentioned expectations for a moderation of inflation over the coming year. In December 2022, the Federal Reserve removed the reference to 'transitory' factors affecting inflation and revised their initial view of the increase in inflation as temporary factors."
}
```

> Finished chain.
*****

Step: Compare the language used in both statements.

Response: In March 2023, the Federal Reserve mentioned expectations for a moderation of inflation over the coming year. In December 2022, the Federal Reserve removed the reference to 'transitory' factors affecting inflation and revised their initial view of the increase in inflation as temporary factors.

> Entering new AgentExecutor chain...
Action:
```
{
  "action": "Final Answer",
  "action_input": "The Federal Reserve's stance on inflation shifted from considering it a temporary factor to acknowledging its persistence in December 2022. In March 2023, they mentioned that both market- and survey-based measures continued to point to expectations for a moderation of inflation over the coming year."
}
```

> Finished chain.
*****

Step: Identify any changes or shifts in the Fed's stance on inflation.

Response: The Federal Reserve's stance on inflation shifted from considering it a temporary factor to acknowledging its persistence in December 2022. In March 2023, they mentioned that both market- and survey-based measures continued to point to expectations for a moderation of inflation over the coming year.

> Entering new AgentExecutor chain...
Action:
```
{
  "action": "Final Answer",
  "action_input": "In December 2022, the Federal Reserve revised their view of inflation as temporary and removed the reference to 'transitory' factors affecting inflation. In March 2023, the Federal Reserve mentioned expectations for a moderation of inflation over the coming year. Both statements acknowledge the persistence of elevated inflation and ongoing supply and demand imbalances."
}
```

> Finished chain.
*****

Step: Summarize the similarities and differences between the two statements.

Response: In December 2022, the Federal Reserve revised their view of inflation as temporary and removed the reference to 'transitory' factors affecting inflation. In March 2023, the Federal Reserve mentioned expectations for a moderation of inflation over the coming year. Both statements acknowledge the persistence of elevated inflation and ongoing supply and demand imbalances.

> Entering new AgentExecutor chain...
Action:
```{
  "action": "Final Answer",
  "action_input": "The Federal Reserve revised their view of inflation as temporary and removed the reference to 'transitory' factors affecting inflation in December 2022. In March 2023, the Federal Reserve mentioned that both market- and survey-based measures continued to point to expectations for a moderation of inflation over the coming year. Both statements acknowledge the persistence of elevated inflation and ongoing supply and demand imbalances."
}```


> Finished chain.
*****

Step: Respond to the user's original question with the findings.


Response: The Federal Reserve revised their view of inflation as temporary and removed the reference to 'transitory' factors affecting inflation in December 2022. In March 2023, the Federal Reserve mentioned that both market- and survey-based measures continued to point to expectations for a moderation of inflation over the coming year. Both statements acknowledge the persistence of elevated inflation and ongoing supply and demand imbalances.
> Finished chain.
The Federal Reserve revised their view of inflation as temporary and removed the reference to 'transitory' factors affecting inflation in December 2022. In March 2023, the Federal Reserve mentioned that both market- and survey-based measures continued to point to expectations for a moderation of inflation over the coming year. Both statements acknowledge the persistence of elevated inflation and ongoing supply and demand imbalances.

Отличный! Давайте прочитаем окончательный ответ еще раз:

Я не финансовый консультант, но похоже, что инфляция контролируется, вроде как!

Поздравляем! Мы могли бы разработать чат-бота, более мощного, чем ChatGPT! Я надеюсь, что одной чашки кофе/чая было достаточно, чтобы закончить этот урок ;)

Я надеюсь, что вам понравилась эта статья, и, пожалуйста, дайте мне знать, если у вас возникнут какие-либо проблемы или вы хотели бы, чтобы я написал другие статьи, подобные этой, о LLM в частности или ML в целом! Спасибо и хорошего дня!