1fd7486a85
Each of the 122 code slides now has a standalone .py script in examples/slide-NNN/script.py with the LLM provider pattern from bro-js/agents/teacher/assistant/src/angry_teacher/llm.py: - load_dotenv() + ChatOpenAI with base_url=https://llm.brojs.ru/v1 - OPENAI_API_KEY from .env (with safe (or '' or None) pattern) - Same comment block with alternative model/base_url choices Layout: - sec1/sec5: code extracted from slide-NN.js (addCodeBlock) - sec2/sec3/sec4: code extracted from sectionN.pptx via python-pptx (JetBrains Mono font shapes) 100 scripts contain runnable code, 22 are visual-only (dividers, intro, recap) and just print the slide title. Setup: cp .env.example .env # fill OPENAI_API_KEY (or symlink to your # teacher/assistant/.env) pip install -r requirements.txt python examples/slide-005/script.py
64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
"""
|
|
Slide 18: Vector store -> retriever -> RAG-цепочка
|
|
Section 1: LangChain 1.0
|
|
Source: slides/section1-chains/slide-18.js
|
|
|
|
Сгенерировано автоматически из slide-NN.js / sectionN.pptx.
|
|
Паттерн провайдера скопирован из
|
|
bro-js/agents/teacher/assistant/src/angry_teacher/llm.py.
|
|
|
|
Запуск:
|
|
cd langchain-evolution-deck
|
|
cp .env.example .env # заполни OPENAI_API_KEY
|
|
python examples/slide-018/script.py
|
|
"""
|
|
import os
|
|
from dotenv import load_dotenv
|
|
from langchain_openai import ChatOpenAI
|
|
|
|
load_dotenv()
|
|
|
|
llm = ChatOpenAI(
|
|
# model="openrouter/free",
|
|
# model="qwen/qwen3.5-35b-a3b",
|
|
model="openai/gpt-oss-20b",
|
|
# model="nvidia/nemotron-3-nano",
|
|
# model="qwen/qwen3.5-9b",
|
|
base_url="https://llm.brojs.ru/v1",
|
|
# base_url="https://api.minimax.io/v1",
|
|
# base_url="http://0.0.0.0:8090/v1",
|
|
# base_url="https://openrouter.ai/api/v1",
|
|
# api_key=os.getenv("MINIMAX_API_KEY"),
|
|
api_key=(os.getenv("OPENAI_API_KEY") or "").strip() or None,
|
|
temperature=0.7,
|
|
stream_usage=True,
|
|
)
|
|
|
|
|
|
# === Code from slide 18 ===
|
|
from langchain_openai import OpenAIEmbeddings
|
|
from langchain_community.vectorstores import FAISS
|
|
from langchain_core.runnables import RunnablePassthrough
|
|
|
|
# 1. Ðмбеддинги и индекÑ
|
|
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
|
|
docs = ["Cats are mammals.", "Python is a programming language.",
|
|
"LangChain helps build LLM apps."]
|
|
vectorstore = FAISS.from_texts(docs, embedding=embeddings)
|
|
|
|
# 2. .as_retriever() пÑевÑаÑÐ°ÐµÑ store в Runnable
|
|
retriever = vectorstore.as_retriever(search_kwargs={"k": 2})
|
|
|
|
# 3. RAG-ÑепоÑка ÑеÑез LCEL: context + question -> answer
|
|
from langchain_core.prompts import ChatPromptTemplate
|
|
from langchain.chat_models import init_chat_model
|
|
|
|
prompt = ChatPromptTemplate.from_template(
|
|
"Answer based on context.\nContext: {context}\nQ: {question}"
|
|
)
|
|
model = init_chat_model("openai:gpt-4.1-mini")
|
|
rag = (
|
|
{"context": retriever, "question": RunnablePassthrough()}
|
|
| prompt | model | StrOutputParser()
|
|
)
|