VS Code+DevChat助力非专业开发也能玩转代码编程

一、前言

在这里插入图片描述

偶然间网上瞎逛,看到DevChat 发布了一款 VS Code 插件,可提供类似chatgpt一样的“一站式 AI 辅助编程”体验。据说, DevChat 直接对接 GPT-4 还让免费用,目前免费注册收邮件即可获取key,再也不用麻烦的外部手机号了;基于此,便拿来看看,于是有了本文,记录供初学者配置体验;下图是一位生动的形象描述:

在这里插入图片描述
DevChat是一个开源平台,它赋予开发人员更有效地将人工智能集成到代码生成和文档中的能力。DevChat的目标是超越简单的代码自动补全和对代码片段的有限操作。DevChat为开发人员提供了一种高度实用和有效的方式,让他们与大型语言模型(LLMs)进行交互和合作。

相关资源devchat官网Devchat安装配置指导devchat-ai GithubPython中文网devchat-vscode

二、环境部署

在这里插入图片描述

2.1、部署前准备

1)安装 Python 3 环境( 3.8 或以上)

过程略, 请注意安装完成后,请将Python 3 加入到系统 PATH 环境变量中;DevChat 也可使用python,但请确保python --version返回 3.8 以上版本。如果版本低于3.8,下图图标为灰色:

在这里插入图片描述

2)安装 VS Code

过程略

3)Git安装:(依赖环境,必须安装)

下载地址:https://git-scm.com/download,安装过程略,可参考windows安装git,安装完会自动配置环境变量。
或从https://community.chocolatey.org/packages/git安装社区版,执行:choco install git;完成安装后,在命令行终端运行 git --version 返回版本号表明成功。

在这里插入图片描述

在这里插入图片描述

之后创建版本库:版本库又名仓库,英文名repository,可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。由于git是分布式版本管理工具,所以git在不需要联网的情况下也具有完整的版本管理能力。

创建一个版本库可以使用git bash也可以使用tortoiseGit创建一个空目录(F:\temp\git\repository),git bash里执行:git init,完成创建版本库;如使用TortoiseGit,只需要在空目录中点击右键菜单选择“在这里创建版本库”即可。版本库创建成功后会在你定义的库目录下创建一个.git的隐藏目录,其中".git" 目录就是真正版本库, 将来文件都需要保存到版本库当中,而我们刚开始定义的目录称为:工作目录,它包含 ".git"目录,是它的父目录。Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。把文件往Git版本库里添加的时候,是分两步执行的:

第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;

第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。

本地仓库同步到远程,以便多人协作,可在git bash中执行如下语句:

git remote add test1 [email protected]:jun/临时Python项目库   //添加远程仓库并定义别名
git push -u test1 master  //将id_rsa.pub文件内容添加到下入,建立信任,将本地的master分支推送到远程仓库test1

在这里插入图片描述
4)依赖 language-bridge

在这里插入图片描述

2.2、插件安装及配置

1)打开 VS Code安装插件

在 VS Code 插件市场中搜索“devchat”,点击安装(或访问 Visual Studio Marketplace,点击 Install),安装完成后reload下,效果如下:
在这里插入图片描述

2)输入邮件中的key:

点击左下角管理(“齿轮”图标)—命令面板(Command Palette)或 F1,打开命令面板,输入“devchat access key”回车,再输入邮件收到的 key,待右下角状态栏DevChat出现✅,表明可以正常使用;点击✅出现对话框。建议拖动 到右侧,避免在左侧工具栏中来回切换的麻烦。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

三、附录:

3.1、报错:ImportError: Bad git executable.需配置$GIT_PYTHON_GIT_EXECUTABLE

2023-11-10 09:52:29.715 [error] Error: Traceback (most recent call last):
  File "C:\Users\Administrator\.chat\mamba\envs\devchat\Lib\site-packages\git\__init__.py", line 166, in <module>
    refresh()
  File "C:\Users\Administrator\.chat\mamba\envs\devchat\Lib\site-packages\git\__init__.py", line 153, in refresh
    if not Git.refresh(path=path):
           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\.chat\mamba\envs\devchat\Lib\site-packages\git\cmd.py", line 391, in refresh
    raise ImportError(err)
ImportError: Bad git executable.
The git executable must be specified in one of the following ways:
    - be included in your $PATH
    - be set via $GIT_PYTHON_GIT_EXECUTABLE
    - explicitly set via git.refresh()

All git commands will error until this is rectified.

This initial warning can be silenced or aggravated in the future by setting the
$GIT_PYTHON_REFRESH environment variable. Use one of the following values:
    - quiet|q|silence|s|none|n|0: for no warning or exception
    - warn|w|warning|1: for a printed warning
    - error|e|raise|r|2: for a raised exception

Example:
    export GIT_PYTHON_REFRESH=quiet


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\Administrator\.chat\mamba\envs\devchat\Scripts\devchat.exe\__main__.py", line 4, in <module>
  File "C:\Users\Administrator\.chat\mamba\envs\devchat\Lib\site-packages\devchat\_cli\__init__.py", line 1, in <module>
    from .log import log
  File "C:\Users\Administrator\.chat\mamba\envs\devchat\Lib\site-packages\devchat\_cli\log.py", line 9, in <module>
    from devchat._cli.utils import handle_errors, init_dir, get_model_config
  File "C:\Users\Administrator\.chat\mamba\envs\devchat\Lib\site-packages\devchat\_cli\utils.py", line 6, in <module>
    from git import Repo, InvalidGitRepositoryError, GitCommandError
  File "C:\Users\Administrator\.chat\mamba\envs\devchat\Lib\site-packages\git\__init__.py", line 168, in <module>
    raise ImportError("Failed to initialize: {0}".format(_exc)) from _exc
ImportError: Failed to initialize: Bad git executable.
The git executable must be specified in one of the following ways:
    - be included in your $PATH
    - be set via $GIT_PYTHON_GIT_EXECUTABLE
    - explicitly set via git.refresh()

All git commands will error until this is rectified.

This initial warning can be silenced or aggravated in the future by setting the
$GIT_PYTHON_REFRESH environment variable. Use one of the following values:
    - quiet|q|silence|s|none|n|0: for no warning or exception
    - warn|w|warning|1: for a printed warning
    - error|e|raise|r|2: for a raised exception

Example:
    export GIT_PYTHON_REFRESH=quiet

解决办法:如上所示,秩序指定 git.exe 的路径就可以解决这个问题,但如果你的电脑里不想安装git的话,可如下处理:

#1、在你自己写的代码中第一个使用 git 的地方之前,写这行代码即可:

os.environ['GIT_PYTHON_REFRESH'] = 'quiet'
#然后在引用即可,比如:
from git import Repo
#2、再包引用的第一个函数前加入:
os.environ['GIT_PYTHON_REFRESH'] = 'quiet'

#3、如果安装了git,配置环境变量,让python可正常调用git
set GIT_PYTHON_GIT_EXECUTABLE=C:\Program Files\Git\cmd\git.exe

在这里插入图片描述
现场:编辑C:\Users\Administrator.chat\mamba\envs\devchat\Lib\site-packages\git文件,如下修改:
在这里插入图片描述
在这里插入图片描述

python3 -m pip install --user pipx
python3 -m pipx ensurepath
pipx install devchat

在这里插入图片描述
在这里插入图片描述

:DevChat 插件依赖并自动安装的组件包括:pip、pipx、devchat 等。Python 和 DevChat 都默认使用 UTF-8 编码,windows配置set PYTHONUTF8=1;DevChat 目前默认使用 gpt-4 模型,可以通过左下角管理(“齿轮”图标)—设置—DevChat.OpenAI: Model 进行修改。
在这里插入图片描述

3.2、把python程序打包成exe文件

借助于如下工具实现:

1)PyInstaller

命令格式pyinstaller [options] script

-D:将所有文件打包到一个目录下
-F:将所有文件打包成一个可执行文件
-W:不显示黑色控制台窗口
-i <FILE.ico>:指定可执行文件的封面图片

2)pyi-makespec

命令格式pyi-makespec [options] script

eg1:pyi-makespec -i img.ico -D packtest.py

猜你喜欢

转载自blog.csdn.net/ximenjianxue/article/details/134331405