asyncdefmain_code_generator():
llm = LLMClient()
assistant = InteractiveCodeAssistant(llm)
# 示例 1:生成代码print("="*60)
print("示例 1:生成快速排序代码")
print("="*60)
result = await assistant.chat("用 Python 实现一个快速排序,要求有详细注释")
print(result)
# 示例 2:解释代码print("\n" + "="*60)
print("示例 2:解释代码")
print("="*60)
code = '''def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)'''
explanation = await assistant.chat(f"解释这段代码在做什么:\n`{code}`")
print(explanation)
if __name__ == "__main__":
asyncio.run(main_code_generator())
3.3 代码生成能力对比
功能
ChatGPT 网页版
本地 AI 工具
优势
生成速度
3-5 秒
2-3 秒
快 40%
代码可运行率
85%
90%+
自定义优化
安全检查
❌
✅
内置规则
测试生成
需额外要求
自动生成
一站式
批量处理
❌
✅
脚本化
成本
$20/月
¥10/月
省 60%
四、工具三:智能资料助手
4.1 系统架构
这个工具不仅仅是搜索,它还会聚合多个来源的信息,进行去重和排序,最后由 AI 综合整理出答案。
4.2 核心代码
import aiohttp
from typing importList, Dict, Optionalfrom dataclasses import dataclass
import re
from urllib.parse import quote, urljoin
import json
@dataclassclassSearchResult:
title: str
url: str
snippet: str
source: str# google / bing / docs / stackoverflow
relevance: float = 0.0@dataclassclassResearchResult:
answer: str
sources: List[SearchResult]
related_questions: List[str]
confidence: floatclassSearchEngine:
def__init__(self, bing_api_key: str = None):
self.bing_api_key = bing_api_key or os.getenv("BING_SEARCH_API_KEY")
self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
asyncdefsearch_bing(self, query: str, count: int = 10) -> List[SearchResult]:
ifnotself.bing_api_key:
returnawaitself._search_duckduckgo(query, count)
url = "https://api.bing.microsoft.com/v7.0/search"
params = {"q": query, "count": count, "responseFilter": "webpages"}
asyncwith aiohttp.ClientSession() as session:
asyncwith session.get(url, params=params, headers={"Ocp-Apim-Subscription-Key": self.bing_api_key}) as response:
data = await response.json()
results = []
for item in data.get("webPages", {}).get("value", []):
results.append(SearchResult(title=item["name"], url=item["url"], snippet=item["snippet"], source="bing"))
return results
asyncdef_search_duckduckgo(self, query: str, count: int = 10) -> List[SearchResult]:
url = f"https://html.duckduckgo.com/html/?q={quote(query)}"asyncwith aiohttp.ClientSession() as session:
asyncwith session.get(url, headers=self.headers) as response:
html = await response.text()
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
results = []
for result in soup.select('.result')[:count]:
title_elem = result.select_one('.result__a')
snippet_elem = result.select_one('.result__snippet')
url_elem = result.select_one('.result__url')
if title_elem and url_elem:
results.append(SearchResult(
title=title_elem.get_text(),
url=url_elem.get('href',''),
snippet=snippet_elem.get_text() if snippet_elem else'',
source="duckduckgo"
))
return results
asyncdefsearch_stackoverflow(self, query: str, count: int = 5) -> List[SearchResult]:
search_query = f"site:stackoverflow.com {query}"
results = awaitself._search_duckduckgo(search_query, count)
for r in results:
r.source = "stackoverflow"return results
asyncdefsearch_docs(self, query: str, docs_domain: str, count: int = 5) -> List[SearchResult]:
search_query = f"site:{docs_domain}{query}"
results = awaitself._search_duckduckgo(search_query, count)
for r in results:
r.source = "docs"return results
classIntelligentResearcher:
def__init__(self, llm_client: LLMClient, search_engine: SearchEngine):
self.llm = llm_client
self.search = search_engine
asyncdefresearch(self, question: str, depth: int = 1, sources: List[str] = None) -> ResearchResult:
print(f"🔍 正在研究:{question}")
# 1. 并行搜索多个源
search_tasks = []
ifnot sources or"google"in sources:
search_tasks.append(self.search.search_bing(question))
ifnot sources or"stackoverflow"in sources:
search_tasks.append(self.search.search_stackoverflow(question))
ifself._is_technical_question(question):
tech = awaitself._detect_tech_stack(question)
if tech:
docs_url = self._get_docs_url(tech)
search_tasks.append(self.search.search_docs(question, docs_url))
search_results_list = await asyncio.gather(*search_tasks)
all_results = []
for results in search_results_list:
all_results.extend(results)
print(f"📊 找到 {len(all_results)} 条相关结果")
# 2. 提取页面内容(深度研究)if depth > 1:
all_results = awaitself._fetch_page_contents(all_results[:5])
# 3. AI 分析并整合答案
answer = awaitself._synthesize_answer(question, all_results)
# 4. 生成相关问题
related = awaitself._generate_related_questions(question, answer)
# 5. 计算置信度
confidence = self._calculate_confidence(all_results)
return ResearchResult(answer=answer, sources=all_results[:5], related_questions=related, confidence=confidence)
def_is_technical_question(self, question: str) -> bool:
tech_keywords = ["python", "javascript", "java", "api", "函数", "如何使用", "怎么用", "documentation", "example"]
returnany(kw in question.lower() for kw in tech_keywords)
asyncdef_detect_tech_stack(self, question: str) -> Optional[str]:
prompt = f"""从以下问题中检测涉及的技术栈,只返回技术名称:
问题:{question}
技术栈(如 python、react、docker 等):"""
response = awaitself.llm.chat([Message(role="user", content=prompt)])
tech = response.strip().lower()
tech_docs = {"python": "docs.python.org", "javascript": "developer.mozilla.org", "react": "react.dev", "vue": "vuejs.org", "docker": "docs.docker.com", "kubernetes": "kubernetes.io"}
return tech_docs.get(tech)
def_get_docs_url(self, tech: str) -> str:
tech_docs = {"python": "docs.python.org", "javascript": "developer.mozilla.org", "react": "react.dev", "vue": "vuejs.org", "docker": "docs.docker.com"}
return tech_docs.get(tech, "docs.python.org")
asyncdef_fetch_page_contents(self, results: List[SearchResult]) -> List[SearchResult]:
asyncdeffetch_content(result: SearchResult):
try:
asyncwith aiohttp.ClientSession() as session:
asyncwith session.get(result.url, headers=self.search.headers, timeout=aiohttp.ClientTimeout(total=10)) as response:
html = await response.text()
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
for script in soup(['script', 'style', 'nav', 'footer']):
script.decompose()
text = soup.get_text(separator='\n', strip=True)
result.snippet = text[:2000] + "..."
result.relevance = 1.0except Exception as e:
print(f" ⚠️ 获取失败 {result.url}: {e}")
tasks = [fetch_content(r) for r in results]
await asyncio.gather(*tasks)
return results
asyncdef_synthesize_answer(self, question: str, results: List[SearchResult]) -> str:
context = "\n\n".join([f"来源{i+1}: {r.title}\n{r.snippet}\n链接:{r.url}"for i, r inenumerate(results[:5])])
prompt = f"""基于以下搜索结果回答问题,要求:
1. 准确引用信息来源
2. 综合多个来源的信息
3. 如果信息冲突,说明不同观点
4. 给出清晰的结构化答案
5. 标注信息来源(如 [来源 1])
问题:{question}
搜索结果:
{context}
请给出详细答案:"""
answer = awaitself.llm.chat([
Message(role="system", content="你是一个专业的研究助手,擅长综合多源信息给出准确答案"),
Message(role="user", content=prompt)
])
return answer
asyncdef_generate_related_questions(self, question: str, answer: str) -> List[str]:
prompt = f"""基于以下问答,生成 3-5 个相关的深入研究问题:
问题:{question}
答案:{answer[:500]}...
请生成相关问题,每行一个:"""
response = awaitself.llm.chat([Message(role="user", content=prompt)])
return [line.strip() for line in response.split('\n') if line.strip() andnot line.startswith('-')][:5]
def_calculate_confidence(self, results: List[SearchResult]) -> float:
ifnot results:
return0.0
base_confidence = min(1.0, len(results)/10)
has_docs = any(r.source == "docs"for r in results)
if has_docs:
base_confidence = min(1.0, base_confidence + 0.2)
returnround(base_confidence, 2)
# 使用示例asyncdefmain_researcher():
llm = LLMClient()
search = SearchEngine()
researcher = IntelligentResearcher(llm, search)
result = await researcher.research(question="Python 中 asyncio 和 multiprocessing 的区别是什么?", depth=2)
print("\n" + "="*60)
print("📚 研究结果")
print("="*60)
print(f"\n置信度:{result.confidence*100}%\n")
print(f"答案:\n{result.answer}\n")
print("📖 参考来源:")
for i, source inenumerate(result.sources, 1):
print(f"{i}. {source.title}")
print(f" {source.url}")
print(f" 来源:{source.source}\n")
print("❓ 相关问题:")
for q in result.related_questions:
print(f" • {q}")
if __name__ == "__main__":
asyncio.run(main_researcher())