LLMs โ OpenAI API & LangChain
Integrate GPT-4 and other LLMs โ streaming responses, function calling, RAG with vector databases, and building AI agents with LangChain.
Part 1: What You Will Learn
- Keep API keys outside source code by using environment variables.
- Send a prompt to an OpenAI model with the Responses API.
- Build a reusable prompt chain with LangChain.
- Understand how retrieval-augmented generation (RAG) adds relevant external context before an LLM call.
Part 2: Calling an LLM with the OpenAI SDK
import os
from openai import OpenAI
api_key = os.environ["OPENAI_API_KEY"]
model_name = os.environ["OPENAI_MODEL"]
client = OpenAI(api_key=api_key)
response = client.responses.create(
model=model_name,
input=(
"Explain Python list comprehensions to a beginner "
"in no more than four sentences."
),
)
print(response.output_text)pip install openai set OPENAI_API_KEY=your_key_here set OPENAI_MODEL=your_available_model_name
The model name is read from OPENAI_MODEL instead of being hard-coded, so the lesson does not depend on a particular model remaining available. Never commit API keys to GitHub.
Part 3: Adding LangChain
import os
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
prompt = ChatPromptTemplate.from_messages([
("system", "You are a concise Python tutor."),
("human", "Explain {topic} and give one short example."),
])
model = ChatOpenAI(
model=os.environ["OPENAI_MODEL"],
temperature=0,
)
chain = prompt | model
result = chain.invoke({"topic": "dictionary comprehensions"})
print(result.content)pip install langchain langchain-openai
Part 4: From Prompting to RAG
RAG normally follows four steps: split source material into chunks, convert the chunks into searchable representations, retrieve the most relevant chunks for a question, and place those chunks into the prompt before calling the model. This lets the application answer from selected documents instead of relying only on the model's built-in knowledge.
For production applications, validate retrieved content, limit the amount of context sent to the model, log failures without logging secrets, and clearly separate trusted instructions from untrusted document text.
Part 5: Hands-On Practice
Mini project โ Python Study Assistant. Create a small collection of your own Python notes. Ask the user for a question, retrieve the most relevant note by keyword or similarity, then include that note as context in an LLM request. Display both the answer and the title of the source note used.
Part 6: Next Steps
After your first LLM-powered application works, continue to Lesson 34 to explore Python metaprogramming and the object model.