File size: 2,842 Bytes
90f65f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# file: 01_basic_summarizer.py

import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

OPENROUTER_API_URL = "https://openrouter.ai/api/v1"

def main():
    """
    Main function to load environment variables, create a summarization chain,
    and process a sample text, as described in Chapter 2.
    """
    # Load environment variables from .env file
    load_dotenv()

    # 1. Define the LLM we want to use
    # We select a fast and cost-effective model from the Gemini family and
    # configure the client to use OpenRouter's API.
    '''chat_model = ChatOpenAI(
        model="google/gemini-2.0-flash-001",
        temperature=0.3,
        openai_api_key=os.getenv("OPENROUTER_API_KEY"),
        openai_api_base=OPENROUTER_API_URL # Using the constant defined above
    )'''

    chat_model = ChatOpenAI(
        model="qwen/qwen3-coder:free", # Swapped from Gemini to Qwen's Coder
        temperature=0.3,
        openai_api_key=os.getenv("OPENROUTER_API_KEY"),
        openai_api_base=OPENROUTER_API_URL
    )

    # 2. Create our prompt template
    # This guides the model on what to do with the input.
    template = """
    You are a helpful assistant who summarizes text.
    Summarize the following text in 1 sentence:

    Text: "{text_to_summarize}"
    """
    prompt = ChatPromptTemplate.from_template(template)

    # 3. Create the output parser
    # This will convert the model's chat message output into a string.
    output_parser = StrOutputParser()

    # 4. Create the "chain" by piping the components together
    # This uses the LangChain Expression Language (LCEL) to define the flow of data.
    summarization_chain = prompt | chat_model | output_parser

    # 5. Invoke the chain with a sample text
    # We pass the input text from the book to test our chain.
    sample_text = """
    AI engineering represents a significant paradigm shift from traditional machine
    learning. While traditional ML focused heavily on model training and optimization
    in environments like Jupyter Notebooks, modern AI engineering treats pre-trained
    Large Language Models (LLMs) as components, akin to a CPU. The primary challenge
    is no longer just building an accurate model, but architecting a robust system
    around it. This requires a focus on three pillars: observability, to understand
    the system's internal state; reliability, to ensure predictable and structured
    outputs; and scalability, to manage cost and performance effectively.
    """
    
    print("--- Summarizing Text ---")
    summary = summarization_chain.invoke({"text_to_summarize": sample_text})
    print(summary)
    print("------------------------")

if __name__ == "__main__":
    main()