contributing (#4043)

* 添加了贡献说明 docs/contributing,包含了一些代码仓库说明和开发规范,以及在model_providers下面编写了一些单元测试的示例

* 关于providers的配置说明
This commit is contained in:
glide-the 2024-05-19 21:39:47 +08:00 committed by GitHub
parent 5bd8a4ed8e
commit a6e78f219f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
26 changed files with 510 additions and 134 deletions

View File

@ -1,49 +1,4 @@
### 加入开发
#### 依赖管理Poetry与env/dependency管理方法
这个项目使用 Poetry 来管理依赖。
> 注意在安装Poetry之前如果您使用Conda请创建并激活一个新的Conda env例如`conda create-n chatchat python=3.9`
Install Poetry: [documentation on how to install it.](https://python-poetry.org/docs/#installing-with-pipx)
> 注意: 如果您使用 Conda 或 Pyenv 作为您的环境/包管理器在安装Poetry之后
> 使用如下命令使Poetry使用virtualenv python environment (`poetry config virtualenvs.prefer-active-python true`)
#### 本地开发环境安装
- 选择主项目目录
```shell
cd chatchat
```
- 安装chatchat依赖(for running chatchat lint\tests):
```shell
poetry install --with lint,test
```
#### 格式化和代码检查
在提交PR之前,请在本地运行以下命令;CI系统也会进行检查。
##### 代码格式化
本项目使用ruff进行代码格式化。
要对某个库进行格式化,请在相应的库目录下运行相同的命令:
```shell
cd {model-providers|chatchat|chatchat-server|chatchat-frontend}
make format
```
此外,你可以使用format_diff命令仅对当前分支中与主分支相比已修改的文件进行格式化:
```shell
make format_diff
```
当你对项目的一部分进行了更改,并希望确保更改的部分格式正确,而不影响代码库的其他部分时,这个命令特别有用。
### 开始使用
> 环境配置完成后启动步骤为先启动chatchat-server然后启动chatchat-frontend。
@ -55,7 +10,8 @@ make format_diff
pip install langchain-chatchat -U
```
- 复制配置文件
> 后面我们会提供一个一键初始化的脚本,现在您可以手动复制配置文件
> 请注意:这个命令会清空数据库,如果您有重要数据,请备份
```shell
cd chatchat-server/
mkdir ~/.config/chatchat/
@ -79,12 +35,9 @@ chatchat -a
#### 源码启动chatchat-server
- 初始化依赖
```shell
cd chatchat-server/chatchat
python copy_config_example.py
```
- 初始化仓库
> 请注意:这个命令会清空数据库,如果您有重要数据,请备份
```shell
cd chatchat-server/chatchat
python init_database.py

View File

@ -0,0 +1,30 @@
### 贡献指南
各位开发者chatchat由个人组织经过了不断地迭代和完善作为一个不断进化的项目
我们欢迎您的参与无论是提交bug报告、功能请求、代码贡献、文档编写、测试、或者其他形式的贡献。
#### 贡献方式
这里列出了一些您可以为chatchat做出贡献的方式:
- [Code](code.md): 帮助我们编写代码修复bug或者改进基础设施。
- Documentation: 帮助我们编写文档使chatchat更容易使用。
- Discussions: 帮助回答用户的使用问题,讨论问题。
当您的贡献被接受后,我们会将您的名字添加到贡献者列表中。
在功能贡献达到2次以上我们会考虑将您加入到chatchat的开发团队中。
#### Github Issue
我们需要跟踪功能请求与bug报告
目前我们有使用Issue默认模板来让用户更好的描述问题但是目前这个大部分issue的用户可能依然选择只回复一句话
对于这样的情况我们需要告知用户如何更好的描述问题。在15天内没有回复的issue我们会关闭issue。
关于功能请求备受关注的RAG话题我们会考虑加入到我们的开发计划中。
我们努力保持issue的更新但是我们也需要您的帮助如果您发现有问题没有得到及时回复请在issue下@我们
#### 寻求帮助
我们尽量使开发者更容易上手,当您遇到问题时,请联系维护人员,我们会尽力帮助您。
类似的 diff、formatting、linting、testing等问题如果您不确定如何解决请随时联系我们
很多时候规则校验会阻碍一些开发者,您的思路如果足够优秀,我们会考虑调整规则。

58
docs/contributing/code.md Normal file
View File

@ -0,0 +1,58 @@
### 代码贡献
贡献此仓库的代码时,请查阅 ["fork and pull request"](https://docs.github.com/en/get-started/exploring-projects-on-github/contributing-to-a-project) 流程,除非您是项目的维护者。请不要直接提交到主分支。
在提交PR之前请检查按照pull request模板的指导进行操作。注意我们的CI系统会自动运行linting和测试以确保您的代码符合我们的标准。
更重要的是,我们需要保持良好的单元测试和文档,如果你做了如下操作:
- 添加新功能
更新受影响的操作文档
- 修复bug
尽可能添加一个单元测试在tests/integration_tests或tests/unit_tests中
#### 依赖管理Poetry与env/dependency管理方法
这个项目使用 Poetry 来管理依赖。
> 注意在安装Poetry之前如果您使用Conda请创建并激活一个新的Conda env例如`conda create-n chatchat python=3.9`
Install Poetry: [documentation on how to install it.](https://python-poetry.org/docs/#installing-with-pipx)
> 注意: 如果您使用 Conda 或 Pyenv 作为您的环境/包管理器在安装Poetry之后
> 使用如下命令使Poetry使用virtualenv python environment (`poetry config virtualenvs.prefer-active-python true`)
#### 本地开发环境安装
- 选择主项目目录
```shell
cd chatchat
```
- 安装chatchat依赖(for running chatchat lint\tests):
```shell
poetry install --with lint,test
```
> Poetry install后会在你的site-packages安装一个chatchat-`<version>`.dist-info文件夹带有direct_url.json文件这个文件指向你的开发环境
#### 格式化和代码检查
在提交PR之前,请在本地运行以下命令;CI系统也会进行检查。
##### 代码格式化
本项目使用ruff进行代码格式化。
##### 关于
要对某个库进行格式化,请在相应的库目录下运行相同的命令:
```shell
cd {model-providers|chatchat-server|chatchat-frontend}
make format
```
此外,你可以使用format_diff命令仅对当前分支中与主分支相比已修改的文件进行格式化:
```shell
make format_diff
```
当你对项目的一部分进行了更改,并希望确保更改的部分格式正确,而不影响代码库的其他部分时,这个命令特别有用。

View File

@ -0,0 +1,36 @@
### 仓库结构
如果您想要贡献代码,您需要了解仓库的结构。这将有助于您找到您想要的文件,以及了解如何将您的代码提交到仓库。
chatchat沿用了 monorepo的组织方式, 项目的代码库包含了多个包。
以下是可视化为树的结构:
```shell
.
├── docs # 文档
├── chatchat-server # 服务端
│ └── tests
│ ├── integration_tests # 集成测试 (每个包都有,为了简洁没有展示)
│ └── unit_tests # 单元测试 (每个包都有,为了简洁没有展示)
├── frontend # 前端
└── model-providers # 模型平台
```
根目录还包含以下文件:
pyproject.toml: 用于构建文档和文档linting的依赖项cookbook。
Makefile: 包含用于构建linting和文档和cookbook的快捷方式的文件。
根目录中还有其他文件,但它们的都应该是顾名思义的,请查看相应的文件夹以了解更多信息。
### 代码
代码库中的代码分为两个部分:
- /chatchat-server目录包含chatchat服务端代码。
- /frontend目录包含chatchat前端代码。
- /model-providers目录包含对供应商模型转换的代码。
详细的

View File

@ -1,50 +1,4 @@
### 加入开发
#### 依赖管理Poetry与env/dependency管理方法
这个项目使用 Poetry 来管理依赖。
> 注意在安装Poetry之前如果您使用Conda请创建并激活一个新的Conda env例如`conda create-n chatchat python=3.9`
Install Poetry: [documentation on how to install it.](https://python-poetry.org/docs/#installing-with-pipx)
> 注意: 如果您使用 Conda 或 Pyenv 作为您的环境/包管理器在安装Poetry之后
> 使用如下命令使Poetry使用virtualenv python environment (`poetry config virtualenvs.prefer-active-python true`)
#### 本地开发环境安装
- 选择主项目目录
```shell
cd model-providers
```
- 安装model-providers依赖(for running model-providers lint\tests):
```shell
poetry install --with lint,test
```
#### 格式化和代码检查
在提交PR之前,请在本地运行以下命令;CI系统也会进行检查。
##### 代码格式化
本项目使用ruff进行代码格式化。
要对某个库进行格式化,请在相应的库目录下运行相同的命令:
```shell
cd {model-providers|chatchat|chatchat-server|chatchat-frontend}
make format
```
此外,你可以使用format_diff命令仅对当前分支中与主分支相比已修改的文件进行格式化:
```shell
make format_diff
```
当你对项目的一部分进行了更改,并希望确保更改的部分格式正确,而不影响代码库的其他部分时,这个命令特别有用。
### 开始使用
当项目安装完成,配置这个`model_providers.yaml`文件,即可完成平台加载
@ -54,9 +8,126 @@ model_providers包含了不同平台提供的 全局配置`provider_credential`,
不同平台所加载的配置有所不同,关于如何配置这个文件
请查看包`model_providers.core.model_runtime.model_providers`下方的平台 `yaml`文件
例如`zhipuai.yaml`,这里给出了`provider_credential_schema`,其中包含了一个变量`api_key`
关于`schemas`信息详细描述设计,请查看 [README_CN.md](model_providers/core/model_runtime/README_CN.md)
要加载智谱平台,操作如下
### 相关平台配置说明
#### 要加载xinference平台操作如下
- 查看`schemas`信息
在[xinference.yaml](model_providers/core/model_runtime/model_providers/xinference/xinference.yaml)包含了如下信息,
`supported_model_types`描述支持了`llm``text-embedding``rerank` 模型类型
`configurate_methods`描述包含变量 `customizable-model`表示这个是一个可以自定义模型的平台
`model_credential_schema`描述包含了自定义模型需要的凭据信息
- 安装sdk
```shell
$ pip install xinference-client
```
- 编辑`model_providers.yaml`
```yaml
xinference:
model_credential:
- model: 'chatglm3-6b'
model_type: 'llm'
model_credentials:
server_url: 'http://127.0.0.1:9997/'
model_uid: 'chatglm3-6b'
```
#### 要加载ollama平台操作如下
- 查看`schemas`信息
在[ollama.yaml](model_providers/core/model_runtime/model_providers/ollama/ollama.yaml)包含了如下信息,
`supported_model_types`描述支持了`llm``text-embedding`两种模型类型
`configurate_methods`描述包含变量
- `customizable-model`表示这个是一个可以自定义模型的平台
`model_credential_schema`描述包含了自定义模型需要的凭据信息
- 安装sdk
```shell
$ pip install openai
```
- 编辑`model_providers.yaml`
```yaml
ollama:
model_credential:
- model: 'llama3'
model_type: 'llm'
model_credentials:
base_url: 'http://172.21.80.1:11434/v1'
```
#### 要加载openai平台操作如下
- 查看`schemas`信息
在[openai.yaml](model_providers/core/model_runtime/model_providers/openai/openai.yaml)包含了如下信息,
`supported_model_types`描述支持了`llm``text-embedding`两种模型类型
`configurate_methods`描述包含变量
- `predefined-model`表示这个是一个使用预定义模型的平台
- `customizable-model`表示这个是一个可以自定义模型的平台
`model_credential_schema`描述包含了自定义模型需要的凭据信息
`provider_credential_schema`描述包含平台的凭据信息
- 安装sdk
```shell
$ pip install openai
```
- 编辑`model_providers.yaml`
```yaml
openai:
model_credential:
- model: 'gpt-3.5-turbo'
model_type: 'llm'
model_credentials:
openai_api_key: 'sk-'
openai_organization: ''
openai_api_base: ''
- model: 'gpt-4'
model_type: 'llm'
model_credentials:
openai_api_key: 'sk-'
openai_organization: ''
openai_api_base: ''
provider_credential:
openai_api_key: 'sk-'
openai_organization: ''
openai_api_base: ''
```
#### 要加载智谱平台,操作如下
- 查看`schemas`信息
在[zhipuai.yaml](model_providers/core/model_runtime/model_providers/zhipuai/zhipuai.yaml)包含了如下信息,
`supported_model_types`描述支持了`llm``text-embedding`两种模型类型
`configurate_methods`描述包含变量 `predefined-model`表示这个是一个使用预定义模型的平台
`provider_credential_schema`描述包含平台的凭据信息
- 安装sdk
```shell
@ -73,8 +144,36 @@ zhipuai:
api_key: 'd4fa0690b6dfa205204cae2e12aa6fb6.2'
```
- `model-providers`可以运行pytest 测试
```shell
poetry run pytest tests/server_unit_test/test_init_server.py
```
#### 要加载deepseek平台操作如下
- 查看`schemas`信息
在[deepseek.yaml](model_providers/core/model_runtime/model_providers/deepseek/deepseek.yaml)包含了如下信息,
`supported_model_types`描述支持了`llm``text-embedding`两种模型类型
`configurate_methods`描述包含变量 `predefined-model`表示这个是一个使用预定义模型的平台
`provider_credential_schema`描述包含平台的凭据信息
- 安装sdk
```shell
$ pip install openai
```
- 编辑`model_providers.yaml`
```yaml
deepseek:
model_credential:
- model: 'deepseek-chat'
model_type: 'llm'
model_credentials:
base_url: 'https://api.deepseek.com'
api_key: 'sk-dcb625fcbc1e497d80b7b9493b51d758'
```

View File

@ -185,9 +185,17 @@ class RESTFulOpenAIBootstrapBaseWeb(OpenAIBootstrapBaseWeb):
provider=provider, model_type=model_type
)
)
# 获取预定义模型
llm_models.extend(
provider_model_bundle.model_type_instance.predefined_models()
)
# 获取自定义模型
for model in provider_model_bundle.configuration.custom_configuration.models:
llm_models.append(provider_model_bundle.model_type_instance.get_model_schema(
model=model.model,
credentials=model.credentials,
))
except Exception as e:
logger.error(
f"Error while fetching models for provider: {provider}, model_type: {model_type}"

View File

@ -15,6 +15,7 @@ help:
supported_model_types:
- llm
configurate_methods:
- predefined-model
- customizable-model
model_credential_schema:
model:
@ -42,3 +43,22 @@ model_credential_schema:
placeholder:
zh_Hans: 在此输入您的 API Base
en_US: Enter your API Base
provider_credential_schema:
credential_form_schemas:
- variable: api_key
label:
en_US: API Key
type: secret-input
required: true
placeholder:
zh_Hans: 在此输入您的 API Key
en_US: Enter your API Key
- variable: base_url
label:
zh_Hans: API Base
en_US: API Base
type: text-input
required: false
placeholder:
zh_Hans: 在此输入您的 API Base
en_US: Enter your API Base

View File

@ -136,7 +136,7 @@ def init_server(logging_conf: dict, providers_file: str) -> None:
yield f"http://127.0.0.1:20000"
finally:
print("")
# boot.destroy()
boot.destroy()
except SystemExit:

View File

@ -0,0 +1,15 @@
xinference:
model_credential:
- model: 'chatglm3-6b'
model_type: 'llm'
model_credentials:
server_url: 'http://127.0.0.1:9997/'
model_uid: 'chatglm3-6b'
- model: 'chatglm31-6b'
model_type: 'llm'
model_credentials:
server_url: 'http://127.0.0.1:9997/'
model_uid: 'chatglm3-6b'

View File

@ -0,0 +1,37 @@
from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
import pytest
import logging
logger = logging.getLogger(__name__)
@pytest.mark.requires("xinference_client")
def test_llm(init_server: str):
llm = ChatOpenAI(
model_name="glm-4",
openai_api_key="YOUR_API_KEY", openai_api_base=f"{init_server}/xinference/v1")
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)
llm_chain = LLMChain(prompt=prompt, llm=llm)
responses = llm_chain.run("你好")
logger.info("\033[1;32m" + f"llm_chain: {responses}" + "\033[0m")
@pytest.mark.requires("xinference-client")
def test_embedding(init_server: str):
embeddings = OpenAIEmbeddings(model="text_embedding",
openai_api_key="YOUR_API_KEY",
openai_api_base=f"{init_server}/xinference/v1")
text = "你好"
query_result = embeddings.embed_query(text)
logger.info("\033[1;32m" + f"embeddings: {query_result}" + "\033[0m")

View File

@ -1,18 +1,19 @@
import asyncio
import logging
from typing import List
import pytest
from omegaconf import OmegaConf
from model_providers import BootstrapWebBuilder, _to_custom_provide_configuration
from model_providers.core.model_manager import ModelManager
from model_providers.core.model_runtime.entities.model_entities import ModelType
from model_providers.core.model_runtime.entities.model_entities import ModelType, AIModelEntity
from model_providers.core.provider_manager import ProviderManager
logger = logging.getLogger(__name__)
def test_ollama_provider_manager_models(logging_conf: dict, providers_file: str) -> None:
def test_provider_manager_models(logging_conf: dict, providers_file: str) -> None:
logging.config.dictConfig(logging_conf) # type: ignore
# 读取配置文件
cfg = OmegaConf.load(
@ -32,8 +33,17 @@ def test_ollama_provider_manager_models(logging_conf: dict, providers_file: str)
provider_model_bundle_llm = provider_manager.get_provider_model_bundle(
provider="deepseek", model_type=ModelType.LLM
)
predefined_models = (
llm_models: List[AIModelEntity] = []
for model in provider_model_bundle_llm.configuration.custom_configuration.models:
llm_models.append(provider_model_bundle_llm.model_type_instance.get_model_schema(
model=model.model,
credentials=model.credentials,
))
# 获取预定义模型
llm_models.extend(
provider_model_bundle_llm.model_type_instance.predefined_models()
)
logger.info(f"predefined_models: {predefined_models}")
logger.info(f"predefined_models: {llm_models}")

View File

@ -12,7 +12,7 @@ from model_providers.core.provider_manager import ProviderManager
logger = logging.getLogger(__name__)
def test_ollama_provider_manager_models(logging_conf: dict, providers_file: str) -> None:
def test_provider_manager_models(logging_conf: dict, providers_file: str) -> None:
logging.config.dictConfig(logging_conf) # type: ignore
# 读取配置文件
cfg = OmegaConf.load(
@ -32,11 +32,18 @@ def test_ollama_provider_manager_models(logging_conf: dict, providers_file: str)
provider_model_bundle_llm = provider_manager.get_provider_model_bundle(
provider="ollama", model_type=ModelType.LLM
)
provider_model_bundle_emb = provider_manager.get_provider_model_bundle(
provider="ollama", model_type=ModelType.TEXT_EMBEDDING
)
predefined_models = (
llm_models: List[AIModelEntity] = []
for model in provider_model_bundle_llm.configuration.custom_configuration.models:
llm_models.append(provider_model_bundle_llm.model_type_instance.get_model_schema(
model=model.model,
credentials=model.credentials,
))
# 获取预定义模型
llm_models.extend(
provider_model_bundle_llm.model_type_instance.predefined_models()
)
logger.info(f"predefined_models: {predefined_models}")
logger.info(f"predefined_models: {llm_models}")

View File

@ -17,17 +17,3 @@ openai:
openai_api_key: 'sk-'
openai_organization: ''
openai_api_base: ''
xinference:
model_credential:
- model: 'chatglm3-6b'
model_type: 'llm'
model_credentials:
server_url: 'http://127.0.0.1:9997/'
model_uid: 'chatglm3-6b'
zhipuai:
provider_credential:
api_key: 'd4fa0690b6dfa205204cae2e12aa6fb6.1'

View File

@ -32,11 +32,17 @@ def test_provider_manager_models(logging_conf: dict, providers_file: str) -> Non
provider_model_bundle_llm = provider_manager.get_provider_model_bundle(
provider="openai", model_type=ModelType.LLM
)
provider_model_bundle_emb = provider_manager.get_provider_model_bundle(
provider="openai", model_type=ModelType.TEXT_EMBEDDING
)
predefined_models = (
provider_model_bundle_emb.model_type_instance.predefined_models()
llm_models: List[AIModelEntity] = []
for model in provider_model_bundle_llm.configuration.custom_configuration.models:
llm_models.append(provider_model_bundle_llm.model_type_instance.get_model_schema(
model=model.model,
credentials=model.credentials,
))
# 获取预定义模型
llm_models.extend(
provider_model_bundle_llm.model_type_instance.predefined_models()
)
logger.info(f"predefined_models: {predefined_models}")
logger.info(f"predefined_models: {llm_models}")

View File

@ -0,0 +1,9 @@
xinference:
model_credential:
- model: 'chatglm3-6b'
model_type: 'llm'
model_credentials:
server_url: 'http://127.0.0.1:9997/'
model_uid: 'chatglm3-6b'

View File

@ -0,0 +1,48 @@
import asyncio
import logging
import pytest
from omegaconf import OmegaConf
from model_providers import BootstrapWebBuilder, _to_custom_provide_configuration
from model_providers.core.model_manager import ModelManager
from model_providers.core.model_runtime.entities.model_entities import ModelType
from model_providers.core.provider_manager import ProviderManager
logger = logging.getLogger(__name__)
def test_provider_manager_models(logging_conf: dict, providers_file: str) -> None:
logging.config.dictConfig(logging_conf) # type: ignore
# 读取配置文件
cfg = OmegaConf.load(
providers_file
)
# 转换配置文件
(
provider_name_to_provider_records_dict,
provider_name_to_provider_model_records_dict,
) = _to_custom_provide_configuration(cfg)
# 创建模型管理器
provider_manager = ProviderManager(
provider_name_to_provider_records_dict=provider_name_to_provider_records_dict,
provider_name_to_provider_model_records_dict=provider_name_to_provider_model_records_dict,
)
provider_model_bundle_llm = provider_manager.get_provider_model_bundle(
provider="xinference", model_type=ModelType.LLM
)
llm_models: List[AIModelEntity] = []
for model in provider_model_bundle_llm.configuration.custom_configuration.models:
llm_models.append(provider_model_bundle_llm.model_type_instance.get_model_schema(
model=model.model,
credentials=model.credentials,
))
# 获取预定义模型
llm_models.extend(
provider_model_bundle_llm.model_type_instance.predefined_models()
)
logger.info(f"predefined_models: {llm_models}")

View File

@ -0,0 +1,6 @@
zhipuai:
provider_credential:
api_key: 'd4fa0690b6dfa205204cae2e12aa6fb6.1'

View File

@ -0,0 +1,48 @@
import asyncio
import logging
import pytest
from omegaconf import OmegaConf
from model_providers import BootstrapWebBuilder, _to_custom_provide_configuration
from model_providers.core.model_manager import ModelManager
from model_providers.core.model_runtime.entities.model_entities import ModelType
from model_providers.core.provider_manager import ProviderManager
logger = logging.getLogger(__name__)
def test_provider_manager_models(logging_conf: dict, providers_file: str) -> None:
logging.config.dictConfig(logging_conf) # type: ignore
# 读取配置文件
cfg = OmegaConf.load(
providers_file
)
# 转换配置文件
(
provider_name_to_provider_records_dict,
provider_name_to_provider_model_records_dict,
) = _to_custom_provide_configuration(cfg)
# 创建模型管理器
provider_manager = ProviderManager(
provider_name_to_provider_records_dict=provider_name_to_provider_records_dict,
provider_name_to_provider_model_records_dict=provider_name_to_provider_model_records_dict,
)
provider_model_bundle_llm = provider_manager.get_provider_model_bundle(
provider="zhipuai", model_type=ModelType.LLM
)
llm_models: List[AIModelEntity] = []
for model in provider_model_bundle_llm.configuration.custom_configuration.models:
llm_models.append(provider_model_bundle_llm.model_type_instance.get_model_schema(
model=model.model,
credentials=model.credentials,
))
# 获取预定义模型
llm_models.extend(
provider_model_bundle_llm.model_type_instance.predefined_models()
)
logger.info(f"predefined_models: {llm_models}")