python+deepseek v3 api实现类似cursor的AI编程助手代码

代码语言:python

所属分类:人工智能

代码描述:python+deepseek v3 api实现类似cursor的AI编程助手代码

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

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

#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*
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

api_key="sk-apikey"#api申请地址:https://platform.deepseek.com/api_keys
base_url="https://api.deepseek.com"
main_model="deepseek-chat"
code_model="deepseek-chat"

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 .........完整代码请登录后点击上方下载按钮下载查看

网友评论0