Notebooks
W
Weaviate
Rag With Citations Anthropic Claude 3 5 Sonnet

Rag With Citations Anthropic Claude 3 5 Sonnet

vector-searchvector-databaseretrieval-augmented-generationllm-frameworksweaviate-featuresfunction-callingweaviate-recipesmodel-providersPythongenerative-aianthropic

Open In Colab

RAG with Weaviate and Anthropic's Citations API

Notebook author: Danny Williams @ Weaviate

Overview

Anthropic's Citations API is an integration from Anthropic which cites relevant parts of documents in the response from the LLM.

It will output the text of the relevant parts of the document, as well as the positions of the response from the LLM that the citation is from.

This notebook shows how to use the Citations API with Weaviate's vector database retriever and Anthropic's LLM.

Data (ML Wikipedia Articles)

To run this section you will need to

pip install wikipedia

This section searches for a term with Wikipedia, takes the most relevant first page and then scrapes the page and all the links on the page. So a search for "machine learning" will return also every embedded link on the page for 'machine learning' and all the associated pages. This will create a large number of documents that will serve as our knowledge base for the RAG pipeline.

[1]

Now we need to add the data to Weaviate. We will use the text2vec-openai vectorizer to embed the data, but the actual vectorizer is not important for the Citations API, this is just for the embeddings to search with via the vector database retriever.

[2]
Error inserting item 182. Total missing items: 1/882

import weaviate from weaviate.classes.init import Auth from weaviate.classes.config import Property, DataType, Configure

import os client = weaviate.connect_to_weaviate_cloud( cluster_url=os.getenv("WCD_URL"), auth_credentials=Auth.api_key(os.environ.get("WCD_API_KEY")), headers = {"X-OpenAI-Api-Key": os.getenv("OPENAI_API_KEY")} # use an openai vectorizer - just for the embeddings (not the generative/LLM part) ) client.collections.delete("wikipedia")

RAG with Weaviate and Anthropic

These functions are used for the stages of the RAG pipeline.

[3]

Run the RAG pipeline

For this section we will use the anthropic library to interact with Anthropic's LLM. I.e.

pip install anthropic
[4]

Format the response

We will use the textwrap3 library to format the response and the citations.

pip install textwrap3

This is just used to colour code the citations in the response, so the relevant parts of the LLM's response are highlighted, and underneath, the references are listed in the same colour.

[5]
Based on the provided documents, I'll explain the different types of machine learning
models:

1. Classification Models:
 Classification is performed by computers using statistical methods. Individual
observations are analyzed using a set of quantifiable properties called explanatory
variables or features.

Key aspects of classification:
-  Probabilistic classification is a common subclass that:
- Uses statistical inference to find the best class
- Outputs probabilities for each possible class
- Usually selects the class with highest probability
- Can output confidence values
- Can abstain when confidence is too low

2. Types of Classification:
 Classification can be:
- Binary classification: involves only two classes
- Multiclass classification: involves assigning an object to one of several classes
- Many classification methods are specifically designed for binary classification and
require combination for multiclass problems

3. Clustering Models:
 Two main methods in unsupervised learning are principal component and cluster
analysis. Clustering:
- Groups datasets with shared attributes
- Works with unlabeled, unclassified, and uncategorized data
- Identifies commonalities in data
- Reacts based on presence/absence of commonalities
- Helps detect anomalous data points

4. Unsupervised Learning Models:
 Unsupervised learning is where algorithms learn patterns exclusively from unlabeled
data, unlike supervised learning. There are also weak- or semi-supervision approaches
where a small portion of data is tagged.

5. Predictive Models:
 Predictive modeling:
- Uses statistics to predict outcomes
- Can be applied to future events or any unknown event
- Often used to detect crimes and identify suspects
- Often tries to guess probability of outcomes given input data

6. Deep Learning Models:
 Deep generative models (DGMs) combine generative models with deep neural networks.
They require:
- Increased scale of neural networks
- Increased scale of training data
Popular types include:
- Variational autoencoders (VAEs)
- Generative adversarial networks (GANs)
- Auto-regressive models

7. Language and Text Models:
 Recent trends show very large deep generative models:
- GPT-3 and GPT-2 are auto-regressive neural language models with billions of parameters
- BigGAN and VQ-VAE are used for image generation with hundreds of millions of parameters
- Jukebox is a large generative model for musical audio with billions of parameters

8. Model Categories Based on Statistical Approach:
 There are three broad categories:
1. Parametric models: Make specific assumptions about population parameters and underlying
distributions
2. Non-parametric models: Involve fewer structural assumptions but usually have strong
independence assumptions
3. Semi-parametric models: Include features of both parametric and non-parametric models

REFERENCES
__________

(Source 1) Statistical classification:
When classification is performed by a computer, statistical methods are normally used to
develop the algorithm. Often, the individual observations are analyzed into a set of
quantifiable properties, known variously as explanatory variables or features.

(Source 2) Statistical classification:
A common subclass of classification is probabilistic classification. Algorithms of this
nature use statistical inference to find the best class for a given instance. Unlike other
algorithms, which simply output a "best" class, probabilistic algorithms output a
probability of the instance being a member of each of the possible classes. The best class
is normally then selected as the one with the highest probability. However, such an
algorithm has numerous advantages over non-probabilistic classifiers: It can output a
confidence value associated with its choice (in general, a classifier that can do this is
known as a confidence-weighted classifier). Correspondingly, it can abstain when its
confidence of choosing any particular output is too low.

(Source 3) Statistical classification:
== Binary and multiclass classification == Classification can be thought of as two
separate problems – binary classification and multiclass classification. In binary
classification, a better understood task, only two classes are involved, whereas
multiclass classification involves assigning an object to one of several classes. Since
many classification methods have been developed specifically for binary classification,
multiclass classification often requires the combined use of multiple binary classifiers.

(Source 4) Unsupervised learning:
== Probabilistic methods == Two of the main methods used in unsupervised learning are
principal component and cluster analysis. Cluster analysis is used in unsupervised
learning to group, or segment, datasets with shared attributes in order to extrapolate
algorithmic relationships. Cluster analysis is a branch of machine learning that groups
the data that has not been labelled, classified or categorized. Instead of responding to
feedback, cluster analysis identifies commonalities in the data and reacts based on the
presence or absence of such commonalities in each new piece of data. This approach helps
detect anomalous data points that do not fit into either group.

(Source 5) Unsupervised learning:
Unsupervised learning is a framework in machine learning where, in contrast to supervised
learning, algorithms learn patterns exclusively from unlabeled data. Other frameworks in
the spectrum of supervisions include weak- or semi-supervision, where a small portion of
the data is tagged, and self-supervision.

(Source 6) Predictive modelling:
Predictive modelling uses statistics to predict outcomes. Most often the event one wants
to predict is in the future, but predictive modelling can be applied to any type of
unknown event, regardless of when it occurred. For example, predictive models are often
used to detect crimes and identify suspects, after the crime has taken place. In many
cases, the model is chosen on the basis of detection theory to try to guess the
probability of an outcome given a set amount of input data, for example given an email
determining how likely that it is spam.

(Source 7) Generative model:
== Deep generative models == With the rise of deep learning, a new family of methods,
called deep generative models (DGMs), is formed through the combination of generative
models and deep neural networks. An increase in the scale of the neural networks is
typically accompanied by an increase in the scale of the training data, both of which are
required for good performance. Popular DGMs include variational autoencoders (VAEs),
generative adversarial networks (GANs), and auto-regressive models.

(Source 8) Generative model:
Recently, there has been a trend to build very large deep generative models. For example,
GPT-3, and its precursor GPT-2, are auto-regressive neural language models that contain
billions of parameters, BigGAN and VQ-VAE which are used for image generation that can
have hundreds of millions of parameters, and Jukebox is a very large generative model for
musical audio that contains billions of parameters.

(Source 9) Predictive modelling:
Broadly speaking, there are two classes of predictive models: parametric and non-
parametric. A third class, semi-parametric models, includes features of both. Parametric
models make "specific assumptions with regard to one or more of the population parameters
that characterize the underlying distribution(s)". Non-parametric models "typically
involve fewer assumptions of structure and distributional form [than parametric models]
but usually contain strong assumptions about independencies".