Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
ByteTora
hello-rag
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
hello-rag
/
src
/
core
/
05_contextual_chunk_headers_rag_core.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
72 lines (58 loc) · 1.96 KB
main
Breadcrumbs
hello-rag
/
src
/
core
/
05_contextual_chunk_headers_rag_core.py
Copy path
Top
File metadata and controls
Code
Blame
72 lines (58 loc) · 1.96 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
"""
上下文标题块 核心函数
"""
import os
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI(
base_url=os.getenv("LLM_BASE_URL"),
api_key=os.getenv("LLM_API_KEY")
)
llm_model = os.getenv("LLM_MODEL_ID")
embedding_model = os.getenv("EMBEDDING_MODEL_ID")
####################################
# 将文本切分成块并为每块生成标题
####################################
def chunk_text_with_headers(text, n, overlap):
"""
将文本分割为较小的片段,并生成标题。
Args:
text (str): 要分块的完整文本
n (int): 每个块的字符数
overlap (int): 块之间的重叠字符数
Returns:
List[dict]: 包含 'header' 和 'text' 键的字典列表
"""
chunks = []
# 按指定的块大小和重叠量遍历文本
for i in range(0, len(text), n - overlap):
chunk = text[i:i + n]
header = generate_chunk_header(chunk) # 使用 LLM 为块生成标题
chunks.append({"header": header, "text": chunk}) # 将标题和块添加到列表中
return chunks
####################################
# 使用LLM 为块生成标题
####################################
def generate_chunk_header(chunk):
"""
使用 LLM 为给定的文本块生成标题/页眉
Args:
chunk (str): T要总结为标题的文本块
model (str): 用于生成标题的模型
Returns:
str: 生成的标题/页眉
"""
# 定义系统提示
system_prompt = "为给定的文本生成一个简洁且信息丰富的标题。"
# 根据系统提示和文本块生成
response = client.chat.completions.create(
model=llm_model,
temperature=0,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": chunk}
]
)
# 返回生成的标题/页眉,去除任何前导或尾随空格
return response.choices[0].message.content.strip()
You can’t perform that action at this time.