-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathdemo_verbose_pruner.py
More file actions
92 lines (73 loc) · 2.39 KB
/
demo_verbose_pruner.py
File metadata and controls
92 lines (73 loc) · 2.39 KB
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
#!/usr/bin/env python3
"""
演示 PruneContext 的 verbose 功能
"""
from autocoder.common.pruner.context_pruner import PruneContext
from autocoder.common import AutoCoderArgs, SourceCode
from autocoder.sdk import get_llm
from autocoder.common.tokens import count_string_tokens
def main():
print("=" * 80)
print("🔍 PruneContext Verbose 功能演示")
print("=" * 80)
# 创建测试数据
file1_content = '''def add(a, b):
"""加法函数"""
return a + b
def subtract(a, b):
"""减法函数"""
return a - b
def multiply(a, b):
"""乘法函数"""
return a * b
'''
file2_content = '''def format_string(s):
"""格式化字符串"""
return s.strip().lower()
def reverse_string(s):
"""反转字符串"""
return s[::-1]
'''
file_sources = [
SourceCode(
module_name="math_utils.py",
source_code=file1_content,
tokens=count_string_tokens(file1_content)
),
SourceCode(
module_name="string_utils.py",
source_code=file2_content,
tokens=count_string_tokens(file2_content)
)
]
conversations = [
{"role": "user", "content": "如何实现加法和减法运算?"}
]
# 创建参数
args = AutoCoderArgs(
source_dir=".",
context_prune=True,
context_prune_strategy="extract",
context_prune_sliding_window_size=10,
context_prune_sliding_window_overlap=2,
query="如何实现加法和减法运算?"
)
# 获取 LLM
llm = get_llm("v3_chat", product_mode="lite")
print("\n🔇 非 Verbose 模式:")
print("-" * 40)
# 创建非 verbose 的 pruner
pruner_normal = PruneContext(max_tokens=50, args=args, llm=llm, verbose=False)
result_normal = pruner_normal._extract_code_snippets(file_sources, conversations)
print(f"处理完成,返回 {len(result_normal)} 个文件")
print("\n🔊 Verbose 模式:")
print("-" * 40)
# 创建 verbose 的 pruner
pruner_verbose = PruneContext(max_tokens=50, args=args, llm=llm, verbose=True)
result_verbose = pruner_verbose._extract_code_snippets(file_sources, conversations)
print(f"\n处理完成,返回 {len(result_verbose)} 个文件")
print("\n" + "=" * 80)
print("✅ 演示完成!")
print("=" * 80)
if __name__ == "__main__":
main()