python+qwen实现似cursor的 AI编程助手代码

代码语言:python

所属分类:人工智能

代码描述:python+qwen实现似cursor的 AI编程助手代码

代码标签: python qwen cursor AI 编程 助手 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

from asynchat import async_chat
from itertools import tee
import os
import json
import re
import sys
from pathlib import Path
from typing import List, Dict
from openai import OpenAI
import aiohttp

api_key
="sk-apikey"#自己去阿里百炼开通
base_url
="https://dashscope.aliyuncs.com/compatible-mode/v1"

class CodeAssistant:
   
def __init__(self, project_dir: str):
       
"""初始化代码助手"""
        self
.project_dir = Path(project_dir)
        self
.project_dir.mkdir(parents=True, exist_ok=True)
       
   
def _get_file_context(self) -> str:
       
"""获取项目目录中所有代码文件的内容作为上下文"""
        context
= []
       
for file_path in self.project_dir.rglob("*"):
           
if file_path.is_file() and file_path.suffix in [
               
'.php', '.js', '.html', '.css', '.json', '.py', '.java', '.cpp', '.c',
               
'.cs', '.rb', '.go', '.rs', '.ts', '.swift', '.kt', '.m', '.sh', '.xml',
               
'.yml', '.yaml', '.sql', '.r', '.pl', '.lua', '.scala', '.vb', '.hs',
               
'.erl', '.ex', '.exs', '.dart', '.jl', '.f90', '.f95', '.f03', '.f08'
           
]:
               
try:
                    relative_path
= file_path.relative_to(self.project_dir)
                   
with open(file_path, 'r', encoding='utf-8') as f:
                        content
= f.read()
                        context
.append(f"File: {relative_path}\n{content}\n")
               
except Exception as e:
                   
print(f"Error reading file {file_path}: {e}")
       
return "\n".join(context)

   
def _save_files(self, files: List[Dict[str, str]]):
       
"""保存生成的代码文件"""
       
for file_info in files:
            file_path
= self.project_dir / file_info['name']
            file_path
.parent.mkdir(parents=True, exist_ok=True)
            content
= file_info['content']

.........完整代码请登录后点击上方下载按钮下载查看

网友评论0