liveBook · Manning

Language model

As explained in Data Pipelines with Apache Airflow, Second Edition

A large language model is a machine learning model trained on extensive text data to understand and generate natural-language text.

A large language model (LLM) is a generative AI model trained on a large collection of text, or text corpus, to produce natural-language responses. It can explain, summarize, and translate recipes, but a family recipe absent from its training data presents a practical question: how should the application give the model access to it? section 14.1 section 14.2

Two ways to incorporate a recipe are:

  • Fine-tune an existing model with an additional training cycle. This can incorporate the recipe while retaining the model’s other capabilities, but new batches require retraining. Computing, hosting, and distinguishing original knowledge from added recipes are further challenges. section 14.2
  • Use retrieval-augmented generation (RAG). Store recipes in a vector database, retrieve relevant documents for a question, and give them to the language model through a prompt so it can generate an answer. section 14.0 section 14.10.3

Answering a recipe question

A recipe chat asks, “What’s the recipe for chocolate cookies?” Its Python workflow connects a vector store and an AzureChatOpenAI model to a retrieval chain. When a requested recipe is stored, the assistant can return ingredients and steps; when it is not, the assistant says it does not know that recipe. Stored recipes can also support translation and suggestions based on available ingredients. section 14.10.3

Listing 14.16 Interacting with the vector database via Python
#  IMPORTANT!! To run this implementation, Don't forget to make sure
#  the environment variables we declared in the .env files 
#  are accessible in the Python environment.

from langchain_openai.chat_models import AzureChatOpenAI
from langchain_openai import AzureOpenAIEmbeddings
from langchain_weaviate.vectorstores import WeaviateVectorStore

from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain

from vectorvault.chat.pages.utils import PROMPT

import weaviate
import os
import json

question = "Whats the recipe for chocolate cookies?"

openai_client = AzureChatOpenAI(  #1
   model_name="gpt-4",
   deployment_name = "gpt-4",
   api_version="2024-02-01",
)

with weaviate.connect_to_local(
    port=8083,
    grpc_port=50051,
    headers = {"X-Azure-Api-Key":  os.getenv('OPENAI_API_KEY')},
) as weaviate_client:  #2

   weaviate_db = WeaviateVectorStore(  #3
      client=weaviate_client,
      index_name="recipes",
      text_key="chunk",
      embedding=AzureOpenAIEmbeddings(model="text-embedding-3-large")
   )

   rag_chain = create_retrieval_chain(
      weaviate_db.as_retriever(),  #4
      create_stuff_documents_chain(openai_client, PROMPT),  #5
   )

   output = rag_chain.invoke({#6
         "input": question, 
         "chat_history": None  #7
      }
    )

   print(json.loads(output["answer"]))  #8


{
   'answer': 'Here is the recipe for the Best Chocolate Chip Cookies:\n\n
             Ingredients:\n- 1 cup butter, softened\n- 1 cup white sugar\n
             - 1 cup packed brown sugar\n- 2 large eggs\n- 2 teaspoo...',
    'provided_recipe': True
}

The Python listing shows the named model client, vector store, retrieval-chain imports, and sample question used in the recipe workflow. section 14.10.3

How Data Pipelines with Apache Airflow, Second Edition covers it

How deep it goes
Explains text corpora, compares fine-tuning with retrieval, and shows an application call; does not detail model architecture or training algorithms.
Examples
Uses a family-recipe chat, a chocolate-cookie question, translation, ingredient-based suggestions, and a Python retrieval-chain listing.
What it assumes
Introduces the text corpus from zero; the worked retrieval example assumes prepared recipes, a vector database, a prompt, and accessible environment variables.
What you can do after
Apply it via AzureChatOpenAI and a retrieval chain to answer questions using recipes stored in a vector database.
How it connects
Connects language models to text corpora, fine-tuning, retrieval-augmented generation, vector databases, prompts, and Airflow data preparation.

Also explained in

Compare how 67 books explain language model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Pipelines with Apache Airflow, Second Edition. The section links point to the parts of the book it draws on.