GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — genAI/07-rag-langchain-azure.py at main · Ngugi1/genAI

Expand file tree

/

07-rag-langchain-azure.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
99 lines (79 loc) · 2.95 KB
/

07-rag-langchain-azure.py

Copy path

File metadata and controls

99 lines (79 loc) · 2.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
from dotenv import load_dotenv
from langchain_openai import AzureChatOpenAI, AzureOpenAIEmbeddings
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from bs4 import SoupStrainer
from langchain.tools import tool
from langchain.agents import create_agent
load_dotenv()
# The LLM in RAg Pipeline
model = AzureChatOpenAI(
azure_deployment= 'gpt-4.1',
model= 'gpt-4.1',
api_version='2025-01-01-preview'
)
# Embeddings Model Configuration
embeddings = AzureOpenAIEmbeddings(
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
azure_deployment=os.environ["OPENAI_EMBEDDING_MODEL"],
openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
)
# Use in memory vector store for simplicity
vector_store = InMemoryVectorStore(embeddings)
def load_documents(url: str = "https://lilianweng.github.io/posts/2023-06-23-agent/"):
# Only keep post title, headers, and content from the full HTML.
bs4_strainer = SoupStrainer(class_=("post-title", "post-header", "post-content"))
loader = WebBaseLoader(
web_paths=(url,),
bs_kwargs={"parse_only": bs4_strainer},
)
# Load the documents frqom the web page
docs = loader.load()
print(f"Total documents loaded: {len(docs)}")
print(f"Total documents loaded: {docs[0].page_content[:500]}...")
return docs
# Split the documents into smaller chunks
def split_text(docs: str):
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
add_start_index=True
)
splits = text_splitter.split_documents(docs)
print(f"Total splits: {len(splits)}")
return splits
# Index the documents
def index_documents(docs):
return vector_store.add_documents(docs)
# Execute the RAG pipeline (Indexing)
doc_ids = index_documents(split_text(load_documents()))
# Define a simple retrieval function
@tool(response_format="content_and_artifact")
def retrieve_context(query: str):
"""Retrieve information to help answer a query."""
retrieved_docs = vector_store.similarity_search(query, k=2)
serialized = "\n\n".join(
f"Source:{doc.metadata} \n Content:{doc.page_content}"
for doc in retrieved_docs
)
return serialized, retrieved_docs
def get_tools():
return [retrieve_context]
def get_system_prompt():
return (
"You have access to a tool that retrieves context from a blog post"
"Use the tool to help answer user queries"
)
def get_agent():
return create_agent(model, [retrieve_context], system_prompt = get_system_prompt())
query = (
"What is the standard method for Task Decomposition?\n\n"
"Once you get the answer, look up common extensions of that method."
)
for event in get_agent().stream(
{"messages": [{"role": "user", "content": query}]},
stream_mode="values",
):
event["messages"][-1].pretty_print()
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.