mirror of
https://github.com/RYDE-WORK/Langchain-Chatchat.git
synced 2026-01-29 18:29:44 +08:00
* publish 0.2.10 (#2797) 新功能: - 优化 PDF 文件的 OCR,过滤无意义的小图片 by @liunux4odoo #2525 - 支持 Gemini 在线模型 by @yhfgyyf #2630 - 支持 GLM4 在线模型 by @zRzRzRzRzRzRzR - elasticsearch更新https连接 by @xldistance #2390 - 增强对PPT、DOC知识库文件的OCR识别 by @596192804 #2013 - 更新 Agent 对话功能 by @zRzRzRzRzRzRzR - 每次创建对象时从连接池获取连接,避免每次执行方法时都新建连接 by @Lijia0 #2480 - 实现 ChatOpenAI 判断token有没有超过模型的context上下文长度 by @glide-the - 更新运行数据库报错和项目里程碑 by @zRzRzRzRzRzRzR #2659 - 更新配置文件/文档/依赖 by @imClumsyPanda @zRzRzRzRzRzRzR - 添加日文版 readme by @eltociear #2787 修复: - langchain 更新后,PGVector 向量库连接错误 by @HALIndex #2591 - Minimax's model worker 错误 by @xyhshen - ES库无法向量检索.添加mappings创建向量索引 by MSZheng20 #2688 * Update README.md * Add files via upload * Update README.md * 修复PDF旋转的BUG * Support Chroma * perf delete unused import * 忽略测试代码 * 更新文件 * API前端丢失问题解决 * 更新了chromadb的打印的符号 * autodl代号错误 * Update README.md * Update README.md * Update README.md * 修复milvus相关bug * 支持星火3.5模型 * 修复es 知识库查询bug (#2848) * 修复es 知识库查询bug (#2848) * 更新zhipuai请求方式 * 增加对 .htm 扩展名的显式支持 * 更新readme * Docker镜像制作与K8S YAML部署操作说明 (#2892) * Dev (#2280) * 修复Azure 不设置Max token的bug * 重写agent 1. 修改Agent实现方式,支持多参数,仅剩 ChatGLM3-6b和 OpenAI GPT4 支持,剩余模型将在暂时缺席Agent功能 2. 删除agent_chat 集成到llm_chat中 3. 重写大部分工具,适应新Agent * 更新架构 * 删除web_chat,自动融合 * 移除所有聊天,都变成Agent控制 * 更新配置文件 * 更新配置模板和提示词 * 更改参数选择bug * 修复模型选择的bug * 更新一些内容 * 更新多模态 语音 视觉的内容 1. 更新本地模型语音 视觉多模态功能并设置了对应工具 * 支持多模态Grounding 1. 美化了chat的代码 2. 支持视觉工具输出Grounding任务 3. 完善工具调用的流程 * 支持XPU,修改了glm3部分agent * 添加 qwen agent * 对其ChatGLM3-6B与Qwen-14B * fix callback handler * 更新Agent工具返回 * fix: LLMChain no output when no tools selected * 跟新了langchain 0.1.x需要的依赖和修改的代码 * 更新chatGLM3 langchain0.1.x Agent写法 * 按照 langchain 0.1 重写 qwen agent * 修复 callback 无效的问题 * 添加文生图工具 * webui 支持文生图 * 集成openai plugins插件 * 删除fastchat的配置 * 增加openai插件 * 集成openai plugins插件 * 更新模型执行列表和今晚修改的内容 * 集成openai_plugins/imitater插件 * 集成openai_plugins/imitater插件 * 集成openai_plugins/imitater插件 * 减少错误的显示 * 标准配置 * vllm参数配置 * 增加智谱插件 * 删除本地fschat配置 * 删除本地fschat配置,pydantic升级到2 * 删除本地fschat workers * openai-plugins-list.json * 升级agent,pydantic升级到2 * fix model_config是系统关键词问题 * embeddings模块集成openai plugins插件,使用统一api调用 * loom模型服务update_store更新逻辑 * 集成LOOM在线embedding业务 * 本地知识库搜索字段修改 * 知识库在线api接入点配置在线api接入点配置更新逻辑 * Update model_config.py.example * 修改模型配置方式,所有模型以 openai 兼容框架的形式接入,chatchat 自身不再加载模型。 改变 Embeddings 模型改为使用框架 API,不再手动加载,删除自定义 Embeddings Keyword 代码 修改依赖文件,移除 torch transformers 等重依赖 暂时移出对 loom 的集成 后续: 1、优化目录结构 2、检查合并中有无被覆盖的 0.2.10 内容 * move document_loaders & text_splitter under server * make torch & transformers optional import pydantic Model & Field from langchain.pydantic_v1 instead of pydantic.v1 * - pydantic 限定为 v1,并统一项目中所有 pydantic 导入路径,为以后升级 v2 做准备 - 重构 api.py: - 按模块划分为不同的 router - 添加 openai 兼容的转发接口,项目默认使用该接口以实现模型负载均衡 - 添加 /tools 接口,可以获取/调用编写的 agent tools - 移除所有 EmbeddingFuncAdapter,统一改用 get_Embeddings - 待办: - /chat/chat 接口改为 openai 兼容 - 添加 /chat/kb_chat 接口,openai 兼容 - 改变 ntlk/knowledge_base/logs 等数据目录位置 * 移除 llama-index 依赖;修复 /v1/models 错误 * 原因:windows下启动失败提示补充python-multipart包 (#3184) 改动:requirements添加python-multipart==0.0.9 版本:0.0.9 Requires: Python >=3.8 Co-authored-by: XuCai <liangxc@akulaku.com> * 添加 xinference 本地模型和自定义模型配置 UI: streamlit run model_loaders/xinference_manager.py * update xinference manager ui * fix merge conflict * model_config 中补充 oneapi 默认在线模型;/v1/models 接口支持 oneapi 平台,统一返回模型列表 * 重写 calculate 工具 * 调整根目录结构,kb/logs/media/nltk_data 移动到专用数据目录(可配置,默认 data)。注意知识库文件要做相应移动 * update kb_config.py.example * 优化 ES 知识库 - 开发者 - get_OpenAIClient 的 local_wrap 默认值改为 False,避免 API 服务未启动导致其它功能受阻(如Embeddings) - 修改 ES 知识库服务: - 检索策略改为 ApproxRetrievalStrategy - 设置 timeout 为 60, 避免文档过多导致 ConnecitonTimeout Error - 修改 LocalAIEmbeddings,使用多线程进行 embed_texts,效果不明显,瓶颈可能主要在提供 Embedding 的服务器上 * 修复glm3 agent被注释的agent会话文本结构解析代码 看起来输出的文本占位符如下,目前解析代码是有问题的 Thought <|assistant|> Action\r ```python tool_call(action_input) ```<|observation|> * make qwen agent work with langchain>=0.1 (#3228) * make xinference model manager support xinference 0.9.x * 使用多进程提高导入知识库的速度 (#3276) * xinference的代码 先传 我后面来改 * Delete server/xinference directory * Create khazic * diiii diii * Revert "xinference的代码" * fix markdown header split (#1825) (#3324) * dify model_providers configuration This module provides the interface for invoking and authenticating various models, and offers Dify a unified information and credentials form rule for model providers. * fix merge conflict: langchain Embeddings not imported in server.utils * 添加 react 编写的新版 WEBUI (#3417) * feat:提交前端代码 * feat:提交logo样式切换 * feat:替换avatar、部分位置icon、chatchat相关说明、git链接、Wiki链接、关于、设置、反馈与建议等功能,关闭lobehub自检更新功能 * fix:移除多余代码 --------- Co-authored-by: liunux4odoo <41217877+liunux4odoo@users.noreply.github.com> * model_providers bootstrap * model_providers bootstrap * update to pydantic v2 (#3486) * 使用poetry管理项目 * 使用poetry管理项目 * dev分支解决pydantic版本冲突问题,增加ollama配置,支持ollama会话和向量接口 (#3508) * dev分支解决pydantic版本冲突问题,增加ollama配置,支持ollama会话和向量接口 1、因dev版本的pydantic升级到了v2版本,由于在class History(BaseModel)中使用了from server.pydantic_v1,而fastapi的引用已变为pydantic的v2版本,所以fastapi用v2版本去校验用v1版本定义的对象,当会话历史histtory不为空的时候,会报错:TypeError: BaseModel.validate() takes 2 positional arguments but 3 were given。经测试,解方法为在class History(BaseModel)中也使用v2版本即可; 2、配置文件参照其它平台配置,增加了ollama平台相关配置,会话模型用户可根据实际情况自行添加,向量模型目前支持nomic-embed-text(必须升级ollama到0.1.29以上)。 3、因ollama官方只在会话部分对openai api做了兼容,向量api暂未适配,好在langchain官方库支持OllamaEmbeddings,因而在get_Embeddings方法中添加了相关支持代码。 * 修复 pydantic 升级到 v2 后 DocumentWithVsID 和 /v1/embeddings 兼容性问题 --------- Co-authored-by: srszzw <srszzw@163.com> Co-authored-by: liunux4odoo <liunux@qq.com> * 对python的要求降级到py38 * fix bugs; make poetry using tsinghua mirror of pypi * update gitignore; remove unignored files * update wiki sub module * 20240326 * 20240326 * qqqq * 删除历史文件 * 移动项目模块 * update .gitignore; fix model version error in api_schemas * 封装ModelManager * - 重写 tool 部分: (#3553) - 简化 tool 的定义方式 - 所有 tool 和 tool_config 支持热加载 - 修复:json_schema_extra warning * 使用yaml加载用户配置适配器 * 格式化代码 * 格式化 * 优化工具定义;添加 openai 兼容的统一 chat 接口 (#3570) - 修复: - Qwen Agent 的 OutputParser 不再抛出异常,遇到非 COT 文本直接返回 - CallbackHandler 正确处理工具调用信息 - 重写 tool 定义方式: - 添加 regist_tool 简化 tool 定义: - 可以指定一个用户友好的名称 - 自动将函数的 __doc__ 作为 tool.description - 支持用 Field 定义参数,不再需要额外定义 ModelSchema - 添加 BaseToolOutput 封装 tool 返回结果,以便同时获取原始值、给LLM的字符串值 - 支持工具热加载(有待测试) - 增加 openai 兼容的统一 chat 接口,通过 tools/tool_choice/extra_body 不同参数组合支持: - Agent 对话 - 指定工具调用(如知识库RAG) - LLM 对话 - 根据后端功能更新 webui * 修复:search_local_knowledge_base 工具返回值错误;/tools 路由错误;webui 中“正在思考”一直显示 (#3571) * 添加 openai 兼容的 files 接口 (#3573) * 使用BootstrapWebBuilder适配RESTFulOpenAIBootstrapBaseWeb加载 * 格式化和代码检查说明 * 模型列表适配 * make format * chat_completions接口报文适配 * make format * xinference 插件示例 * 一些默认参数 * exec path fix * 解决ollama部署的qwen,执行agent,返回的json格式不正确问题。 * provider_configuration.py 查询所有的平台信息,包含计费策略和配置schema_validators(参数必填信息校验规则) /workspaces/current/model-providers 查询平台模型分类的详细默认信息,包含了模型类型,模型参数,模型状态 workspaces/current/models/model-types/{model_type} * 开发手册 * 兼容model_providers,集成webui及API中平台配置的初始化 (#3625) * provider_configuration init of MODEL_PLATFORMS * 开发手册 * 兼容model_providers,集成webui及API中平台配置的初始化 * Dev model providers (#3628) * gemini 初始化参数问题 * gemini 同步工具调用 * embedding convert endpoint * 修复 --api -w命令 * /v1/models 接口返回值由 List[Model] 改为 {'data': List[Model]},兼容最新版 xinference * 3.8兼容 (#3769) * 增加使用说明 * 3.8兼容性配置 * fix * formater * 不同平台兼容测试用例 * embedding兼容 * 增加日志信息 * pip源仓库设置,一些版本问题,启动说明 配置说明 (#3854) * 仓库设置,一些版本问题 * pip源仓库设置,一些版本问题,启动说明 * 配置说明 * 泛型标记错误 (#3855) * 仓库设置,一些版本问题 * pip源仓库设置,一些版本问题,启动说明 * 配置说明 * 发布的依赖信息 * 泛型标记错误 * 泛型标记错误 * CICD github action build publish pypi、Release Tag (#3886) * 测试用例 * CICD 流程 * CICD 流程 * CICD 流程 * 一些agent数据处理的问题,model_runtime模块的说明文档 (#3943) * 一些agent数据出来的问题 * Changes: - Translated and updated the Model Runtime documentation to reflect the latest changes and features. - Clarified the decoupling benefits of the Model Runtime module from the Chatchat service. - Removed outdated information regarding the model configuration storage module. - Detailed the retained functionalities post-removal of the Dify configuration page. - Provided a comprehensive overview of the Model Runtime's three-layered structure. - Included the status of the `fetch-from-remote` feature and its non-implementation in Dify. - Added instructions for custom service provider model capabilities. * - 新功能 (#3944) - streamlit 更新到 1.34,webui 支持 Dialog 操作 - streamlit-chatbox 更新到 1.1.12,更好的多会话支持 - 开发者 - 在 API 中增加项目图片路由(/img/{file_name}),方便前端使用 * 修改包名 * 修改包信息 * ollama配置解析问题 * 用户配置动态加载 (#3951) * version = "0.3.0.20240506" * version = "0.3.0.20240506" * version = "0.3.0.20240506" * version = "0.3.0.20240506" * 启动说明 * 一些bug * 修复了一些配置重载的bug * 配置的加载行为修改 * 配置的加载行为修改 * agent代码优化 * ollama 代码升级,使用openai协议 * 支持deepseek客户端 * contributing (#4043) * 添加了贡献说明 docs/contributing,包含了一些代码仓库说明和开发规范,以及在model_providers下面编写了一些单元测试的示例 * 关于providers的配置说明 * python3.8兼容 * python3.8兼容 * ollama兼容 * ollama兼容 * 一些兼容 pydantic<3,>=1.9.0 的代码, * 一些兼容 pydantic<3,>=1.9.0 model_config 的代码, * make format * test * 更新版本 * get_img_base64 * get_img_base64 * get_img_base64 * get_img_base64 * get_img_base64 * 统一模型类型编码 * 向量处理问题 * 优化目录结构 (#4058) * 优化目录结构 * 修改一些测试问题 --------- Co-authored-by: glide-the <2533736852@qq.com> * repositories * 调整日志 * 调整日志zdf * 增加可选依赖extras * feat:Added some documentation. (#4085) * feat:Added some documentation. * feat:Added some documentation. * feat:Added some documentation. --------- Co-authored-by: yuehuazhang <yuehuazhang@tencent.com> * fix code.md typos * fix chatchat-server/pyproject.toml typos * feat:README (#4118) Co-authored-by: yuehuazhang <yuehuazhang@tencent.com> * 初始化数据库集成model_providers * 关闭守护进程 * 1、修改知识库列表接口,返回全量属性字段,同时修改受影响的相关代码。 (#4119) 2、run_in_process_pool改为run_in_thread_pool,解决兼容性问题。 3、poetry配置文件修复。 * 动态更新Prompt中的知识库描述信息,使大模型更容易判断使用哪个知识库。 (#4121) * 1、修改知识库列表接口,返回全量属性字段,同时修改受影响的相关代码。 2、run_in_process_pool改为run_in_thread_pool,解决兼容性问题。 3、poetry配置文件修复。 * 1、动态更新Prompt中的知识库描述信息,使大模型更容易判断使用哪个知识库。 * fix: 补充 xinference 配置信息 (#4123) * feat:README * feat:补充 xinference 平台 llm 和 embedding 模型配置. --------- Co-authored-by: yuehuazhang <yuehuazhang@tencent.com> * 知识库工具的下拉列表改为动态获取,不必重启服务。 (#4126) * 1、知识库工具的下拉列表改为动态获取,不必重启服务。 * update README and imgs * update README and imgs * update README and imgs * update README and imgs * 修改安装说明描述问题 * make formater * 更新版本"0.3.0.20240606 * Update code.md * 优化知识库相关功能 (#4153) - 新功能 - pypi 包新增 chatchat-kb 命令脚本,对应 init_database.py 功能 - 开发者 - _model_config.py 中默认包含 xinference 配置项 - 所有涉及向量库的操作,前置检查当前 Embed 模型是否可用 - /knowledge_base/create_knowledge_base 接口增加 kb_info 参数 - /knowledge_base/list_files 接口返回所有数据库字段,而非文件名称列表 - 修正 xinference 模型管理脚本 * 消除警告 * 一些依赖问题 * 增加text2sql工具,支持特定表、智能判定表,支持对表名进行额外说明 (#4154) * 1、增加text2sql工具,支持特定表、智能判定表,支持对表名进行额外说明 * 支持SQLAlchemy大部分数据库、新增read-only模式,提高安全性、增加text2sql使用建议 (#4155) * 1、修改text2sql连接配置,支持SQLAlchemy大部分数据库; 2、新增read-only模式,若有数据库写保护需求,会从大模型判断、SQLAlchemy拦截器两个层面进行写拦截,提高安全性; 3、增加text2sql使用建议; * dotenv * dotenv 配置 * 用户工作空间操作 (#4156) 工作空间的配置预设,提供ConfigBasic建造方法产生实例。 该类的实例对象用于存储工作空间的配置信息,如工作空间的路径等 工作空间的配置信息存储在用户的家目录下的.config/chatchat/workspace/workspace_config.json文件中。 注意:不存在则读取默认 提供了操作入口 指令` chatchat-config` 工作空间配置 options: ``` -h, --help show this help message and exit -v {true,false}, --verbose {true,false} 是否开启详细日志 -d DATA, --data DATA 数据存放路径 -f FORMAT, --format FORMAT 日志格式 --clear 清除配置 ``` * 配置路径问题 * fix faiss_cache bug * Feature(File RAG): add file_rag in chatchat-server, add ensemble retriever and vectorstore retriever. * Feature(File RAG): add file_rag in chatchat-server, add ensemble retriever and vectorstore retriever. * fix xinference manager bug * Fix(File RAG): use jieba instead of cutword * Fix(File RAG): update kb_doc_api.py * 工作空间的配置预设,提供ConfigBasic建造 实例。 (#4158) - ConfigWorkSpace接口说明 ```text ConfigWorkSpace是一个配置工作空间的抽象类,提供基础的配置信息存储和读取功能。 提供ConfigFactory建造方法产生实例。 该类的实例对象用于存储工作空间的配置信息,如工作空间的路径等 工作空间的配置信息存储在用户的家目录下的.chatchat/workspace/workspace_config.json文件中。 注意:不存在则读取默认 ``` * 编写配置说明 * 编写配置说明 --------- Co-authored-by: liunux4odoo <41217877+liunux4odoo@users.noreply.github.com> Co-authored-by: glide-the <2533736852@qq.com> Co-authored-by: tonysong <tonysong@digitalgd.com.cn> Co-authored-by: songpb <songpb@gmail.com> Co-authored-by: showmecodett <showmecodett@gmail.com> Co-authored-by: zR <2448370773@qq.com> Co-authored-by: zqt <1178747941@qq.com> Co-authored-by: zqt996 <67185303+zqt996@users.noreply.github.com> Co-authored-by: fengyaojie <fengyaojie@xdf.cn> Co-authored-by: Hans WAN <hanswan@tom.com> Co-authored-by: thinklover <thinklover@gmail.com> Co-authored-by: liunux4odoo <liunux@qq.com> Co-authored-by: xucailiang <74602715+xucailiang@users.noreply.github.com> Co-authored-by: XuCai <liangxc@akulaku.com> Co-authored-by: dignfei <913015993@qq.com> Co-authored-by: Leb <khazzz1c@gmail.com> Co-authored-by: Sumkor <sumkor@foxmail.com> Co-authored-by: panhong <381500590@qq.com> Co-authored-by: srszzw <741992282@qq.com> Co-authored-by: srszzw <srszzw@163.com> Co-authored-by: yuehua-s <41819795+yuehua-s@users.noreply.github.com> Co-authored-by: yuehuazhang <yuehuazhang@tencent.com>
683 lines
24 KiB
Python
683 lines
24 KiB
Python
from __future__ import annotations
|
|
|
|
import inspect
|
|
import os
|
|
from datetime import date, datetime
|
|
from typing import TYPE_CHECKING, Any, Callable, Generic, Type, TypeVar, Union, cast
|
|
|
|
import pydantic
|
|
import pydantic.generics
|
|
from pydantic.fields import FieldInfo
|
|
from typing_extensions import (
|
|
ClassVar,
|
|
Literal,
|
|
Protocol,
|
|
Required,
|
|
TypedDict,
|
|
TypeGuard,
|
|
Unpack,
|
|
final,
|
|
override,
|
|
runtime_checkable,
|
|
)
|
|
|
|
from ._compat import (
|
|
PYDANTIC_V2,
|
|
ConfigDict,
|
|
field_get_default,
|
|
get_args,
|
|
get_model_config,
|
|
get_model_fields,
|
|
get_origin,
|
|
is_literal_type,
|
|
is_union,
|
|
parse_obj,
|
|
)
|
|
from ._compat import (
|
|
GenericModel as BaseGenericModel,
|
|
)
|
|
from ._types import (
|
|
IncEx,
|
|
ModelT,
|
|
)
|
|
from ._utils import (
|
|
PropertyInfo,
|
|
coerce_boolean,
|
|
extract_type_arg,
|
|
is_annotated_type,
|
|
is_given,
|
|
is_list,
|
|
is_mapping,
|
|
lru_cache,
|
|
parse_date,
|
|
parse_datetime,
|
|
strip_annotated_type,
|
|
strip_not_given,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from pydantic_core.core_schema import LiteralSchema, ModelField, ModelFieldsSchema
|
|
|
|
__all__ = ["BaseModel", "GenericModel"]
|
|
|
|
_T = TypeVar("_T")
|
|
|
|
|
|
@runtime_checkable
|
|
class _ConfigProtocol(Protocol):
|
|
allow_population_by_field_name: bool
|
|
|
|
|
|
class BaseModel(pydantic.BaseModel):
|
|
if PYDANTIC_V2:
|
|
model_config: ClassVar[ConfigDict] = ConfigDict(
|
|
extra="allow",
|
|
defer_build=coerce_boolean(os.environ.get("DEFER_PYDANTIC_BUILD", "true")),
|
|
)
|
|
else:
|
|
|
|
@property
|
|
@override
|
|
def model_fields_set(self) -> set[str]:
|
|
# a forwards-compat shim for pydantic v2
|
|
return self.__fields_set__ # type: ignore
|
|
|
|
class Config(pydantic.BaseConfig): # pyright: ignore[reportDeprecated]
|
|
extra: Any = pydantic.Extra.allow # type: ignore
|
|
|
|
def to_dict(
|
|
self,
|
|
*,
|
|
mode: Literal["json", "python"] = "python",
|
|
use_api_names: bool = True,
|
|
exclude_unset: bool = True,
|
|
exclude_defaults: bool = False,
|
|
exclude_none: bool = False,
|
|
warnings: bool = True,
|
|
) -> dict[str, object]:
|
|
"""Recursively generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
|
|
|
|
By default, fields that were not set by the API will not be included,
|
|
and keys will match the API response, *not* the property names from the model.
|
|
|
|
For example, if the API responds with `"fooBar": true` but we've defined a `foo_bar: bool` property,
|
|
the output will use the `"fooBar"` key (unless `use_api_names=False` is passed).
|
|
|
|
Args:
|
|
mode:
|
|
If mode is 'json', the dictionary will only contain JSON serializable types. e.g. `datetime` will be turned into a string, `"2024-3-22T18:11:19.117000Z"`.
|
|
If mode is 'python', the dictionary may contain any Python objects. e.g. `datetime(2024, 3, 22)`
|
|
|
|
use_api_names: Whether to use the key that the API responded with or the property name. Defaults to `True`.
|
|
exclude_unset: Whether to exclude fields that have not been explicitly set.
|
|
exclude_defaults: Whether to exclude fields that are set to their default value from the output.
|
|
exclude_none: Whether to exclude fields that have a value of `None` from the output.
|
|
warnings: Whether to log warnings when invalid fields are encountered. This is only supported in Pydantic v2.
|
|
"""
|
|
return self.model_dump(
|
|
mode=mode,
|
|
by_alias=use_api_names,
|
|
exclude_unset=exclude_unset,
|
|
exclude_defaults=exclude_defaults,
|
|
exclude_none=exclude_none,
|
|
warnings=warnings,
|
|
)
|
|
|
|
def to_json(
|
|
self,
|
|
*,
|
|
indent: int | None = 2,
|
|
use_api_names: bool = True,
|
|
exclude_unset: bool = True,
|
|
exclude_defaults: bool = False,
|
|
exclude_none: bool = False,
|
|
warnings: bool = True,
|
|
) -> str:
|
|
"""Generates a JSON string representing this model as it would be received from or sent to the API (but with indentation).
|
|
|
|
By default, fields that were not set by the API will not be included,
|
|
and keys will match the API response, *not* the property names from the model.
|
|
|
|
For example, if the API responds with `"fooBar": true` but we've defined a `foo_bar: bool` property,
|
|
the output will use the `"fooBar"` key (unless `use_api_names=False` is passed).
|
|
|
|
Args:
|
|
indent: Indentation to use in the JSON output. If `None` is passed, the output will be compact. Defaults to `2`
|
|
use_api_names: Whether to use the key that the API responded with or the property name. Defaults to `True`.
|
|
exclude_unset: Whether to exclude fields that have not been explicitly set.
|
|
exclude_defaults: Whether to exclude fields that have the default value.
|
|
exclude_none: Whether to exclude fields that have a value of `None`.
|
|
warnings: Whether to show any warnings that occurred during serialization. This is only supported in Pydantic v2.
|
|
"""
|
|
return self.model_dump_json(
|
|
indent=indent,
|
|
by_alias=use_api_names,
|
|
exclude_unset=exclude_unset,
|
|
exclude_defaults=exclude_defaults,
|
|
exclude_none=exclude_none,
|
|
warnings=warnings,
|
|
)
|
|
|
|
@override
|
|
def __str__(self) -> str:
|
|
# mypy complains about an invalid self arg
|
|
return f'{self.__repr_name__()}({self.__repr_str__(", ")})' # type: ignore[misc]
|
|
|
|
# Override the 'construct' method in a way that supports recursive parsing without validation.
|
|
# Based on https://github.com/samuelcolvin/pydantic/issues/1168#issuecomment-817742836.
|
|
@classmethod
|
|
@override
|
|
def construct(
|
|
cls: Type[ModelT],
|
|
_fields_set: set[str] | None = None,
|
|
**values: object,
|
|
) -> ModelT:
|
|
m = cls.__new__(cls)
|
|
fields_values: dict[str, object] = {}
|
|
|
|
config = get_model_config(cls)
|
|
populate_by_name = (
|
|
config.allow_population_by_field_name
|
|
if isinstance(config, _ConfigProtocol)
|
|
else config.get("populate_by_name")
|
|
)
|
|
|
|
if _fields_set is None:
|
|
_fields_set = set()
|
|
|
|
model_fields = get_model_fields(cls)
|
|
for name, field in model_fields.items():
|
|
key = field.alias
|
|
if key is None or (key not in values and populate_by_name):
|
|
key = name
|
|
|
|
if key in values:
|
|
fields_values[name] = _construct_field(
|
|
value=values[key], field=field, key=key
|
|
)
|
|
_fields_set.add(name)
|
|
else:
|
|
fields_values[name] = field_get_default(field)
|
|
|
|
_extra = {}
|
|
for key, value in values.items():
|
|
if key not in model_fields:
|
|
if PYDANTIC_V2:
|
|
_extra[key] = value
|
|
else:
|
|
_fields_set.add(key)
|
|
fields_values[key] = value
|
|
|
|
object.__setattr__(m, "__dict__", fields_values)
|
|
|
|
if PYDANTIC_V2:
|
|
# these properties are copied from Pydantic's `model_construct()` method
|
|
object.__setattr__(m, "__pydantic_private__", None)
|
|
object.__setattr__(m, "__pydantic_extra__", _extra)
|
|
object.__setattr__(m, "__pydantic_fields_set__", _fields_set)
|
|
else:
|
|
# init_private_attributes() does not exist in v2
|
|
m._init_private_attributes() # type: ignore
|
|
|
|
# copied from Pydantic v1's `construct()` method
|
|
object.__setattr__(m, "__fields_set__", _fields_set)
|
|
|
|
return m
|
|
|
|
if not TYPE_CHECKING:
|
|
# type checkers incorrectly complain about this assignment
|
|
# because the type signatures are technically different
|
|
# although not in practice
|
|
model_construct = construct
|
|
|
|
if not PYDANTIC_V2:
|
|
# we define aliases for some of the new pydantic v2 methods so
|
|
# that we can just document these methods without having to specify
|
|
# a specific pydantic version as some users may not know which
|
|
# pydantic version they are currently using
|
|
|
|
@override
|
|
def model_dump(
|
|
self,
|
|
*,
|
|
mode: Literal["json", "python"] | str = "python",
|
|
include: IncEx = None,
|
|
exclude: IncEx = None,
|
|
by_alias: bool = False,
|
|
exclude_unset: bool = False,
|
|
exclude_defaults: bool = False,
|
|
exclude_none: bool = False,
|
|
round_trip: bool = False,
|
|
warnings: bool | Literal["none", "warn", "error"] = True,
|
|
context: dict[str, Any] | None = None,
|
|
serialize_as_any: bool = False,
|
|
) -> dict[str, Any]:
|
|
"""Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump
|
|
|
|
Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
|
|
|
|
Args:
|
|
mode: The mode in which `to_python` should run.
|
|
If mode is 'json', the dictionary will only contain JSON serializable types.
|
|
If mode is 'python', the dictionary may contain any Python objects.
|
|
include: A list of fields to include in the output.
|
|
exclude: A list of fields to exclude from the output.
|
|
by_alias: Whether to use the field's alias in the dictionary key if defined.
|
|
exclude_unset: Whether to exclude fields that are unset or None from the output.
|
|
exclude_defaults: Whether to exclude fields that are set to their default value from the output.
|
|
exclude_none: Whether to exclude fields that have a value of `None` from the output.
|
|
round_trip: Whether to enable serialization and deserialization round-trip support.
|
|
warnings: Whether to log warnings when invalid fields are encountered.
|
|
|
|
Returns:
|
|
A dictionary representation of the model.
|
|
"""
|
|
if mode != "python":
|
|
raise ValueError("mode is only supported in Pydantic v2")
|
|
if round_trip != False:
|
|
raise ValueError("round_trip is only supported in Pydantic v2")
|
|
if warnings != True:
|
|
raise ValueError("warnings is only supported in Pydantic v2")
|
|
if context is not None:
|
|
raise ValueError("context is only supported in Pydantic v2")
|
|
if serialize_as_any != False:
|
|
raise ValueError("serialize_as_any is only supported in Pydantic v2")
|
|
return super().dict( # pyright: ignore[reportDeprecated]
|
|
include=include,
|
|
exclude=exclude,
|
|
by_alias=by_alias,
|
|
exclude_unset=exclude_unset,
|
|
exclude_defaults=exclude_defaults,
|
|
exclude_none=exclude_none,
|
|
)
|
|
|
|
@override
|
|
def model_dump_json(
|
|
self,
|
|
*,
|
|
indent: int | None = None,
|
|
include: IncEx = None,
|
|
exclude: IncEx = None,
|
|
by_alias: bool = False,
|
|
exclude_unset: bool = False,
|
|
exclude_defaults: bool = False,
|
|
exclude_none: bool = False,
|
|
round_trip: bool = False,
|
|
warnings: bool | Literal["none", "warn", "error"] = True,
|
|
context: dict[str, Any] | None = None,
|
|
serialize_as_any: bool = False,
|
|
) -> str:
|
|
"""Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump_json
|
|
|
|
Generates a JSON representation of the model using Pydantic's `to_json` method.
|
|
|
|
Args:
|
|
indent: Indentation to use in the JSON output. If None is passed, the output will be compact.
|
|
include: Field(s) to include in the JSON output. Can take either a string or set of strings.
|
|
exclude: Field(s) to exclude from the JSON output. Can take either a string or set of strings.
|
|
by_alias: Whether to serialize using field aliases.
|
|
exclude_unset: Whether to exclude fields that have not been explicitly set.
|
|
exclude_defaults: Whether to exclude fields that have the default value.
|
|
exclude_none: Whether to exclude fields that have a value of `None`.
|
|
round_trip: Whether to use serialization/deserialization between JSON and class instance.
|
|
warnings: Whether to show any warnings that occurred during serialization.
|
|
|
|
Returns:
|
|
A JSON string representation of the model.
|
|
"""
|
|
if round_trip != False:
|
|
raise ValueError("round_trip is only supported in Pydantic v2")
|
|
if warnings != True:
|
|
raise ValueError("warnings is only supported in Pydantic v2")
|
|
if context is not None:
|
|
raise ValueError("context is only supported in Pydantic v2")
|
|
if serialize_as_any != False:
|
|
raise ValueError("serialize_as_any is only supported in Pydantic v2")
|
|
return super().json( # type: ignore[reportDeprecated]
|
|
indent=indent,
|
|
include=include,
|
|
exclude=exclude,
|
|
by_alias=by_alias,
|
|
exclude_unset=exclude_unset,
|
|
exclude_defaults=exclude_defaults,
|
|
exclude_none=exclude_none,
|
|
)
|
|
|
|
|
|
def _construct_field(value: object, field: FieldInfo, key: str) -> object:
|
|
if value is None:
|
|
return field_get_default(field)
|
|
|
|
if PYDANTIC_V2:
|
|
type_ = field.annotation
|
|
else:
|
|
type_ = cast(type, field.outer_type_) # type: ignore
|
|
|
|
if type_ is None:
|
|
raise RuntimeError(f"Unexpected field type is None for {key}")
|
|
|
|
return construct_type(value=value, type_=type_)
|
|
|
|
|
|
def is_basemodel(type_: type) -> bool:
|
|
"""Returns whether or not the given type is either a `BaseModel` or a union of `BaseModel`"""
|
|
if is_union(type_):
|
|
for variant in get_args(type_):
|
|
if is_basemodel(variant):
|
|
return True
|
|
|
|
return False
|
|
|
|
return is_basemodel_type(type_)
|
|
|
|
|
|
def is_basemodel_type(type_: type) -> TypeGuard[type[BaseModel] | type[GenericModel]]:
|
|
origin = get_origin(type_) or type_
|
|
return issubclass(origin, BaseModel) or issubclass(origin, GenericModel)
|
|
|
|
|
|
def construct_type(*, value: object, type_: object) -> object:
|
|
"""Loose coercion to the expected type with construction of nested values.
|
|
|
|
If the given value does not match the expected type then it is returned as-is.
|
|
"""
|
|
# we allow `object` as the input type because otherwise, passing things like
|
|
# `Literal['value']` will be reported as a type error by type checkers
|
|
type_ = cast("type[object]", type_)
|
|
|
|
# unwrap `Annotated[T, ...]` -> `T`
|
|
if is_annotated_type(type_):
|
|
meta: tuple[Any, ...] = get_args(type_)[1:]
|
|
type_ = extract_type_arg(type_, 0)
|
|
else:
|
|
meta = tuple()
|
|
|
|
# we need to use the origin class for any types that are subscripted generics
|
|
# e.g. Dict[str, object]
|
|
origin = get_origin(type_) or type_
|
|
args = get_args(type_)
|
|
|
|
if is_union(origin):
|
|
try:
|
|
return validate_type(type_=cast("type[object]", type_), value=value)
|
|
except Exception:
|
|
pass
|
|
|
|
# if the type is a discriminated union then we want to construct the right variant
|
|
# in the union, even if the data doesn't match exactly, otherwise we'd break code
|
|
# that relies on the constructed class types, e.g.
|
|
#
|
|
# class FooType:
|
|
# kind: Literal['foo']
|
|
# value: str
|
|
#
|
|
# class BarType:
|
|
# kind: Literal['bar']
|
|
# value: int
|
|
#
|
|
# without this block, if the data we get is something like `{'kind': 'bar', 'value': 'foo'}` then
|
|
# we'd end up constructing `FooType` when it should be `BarType`.
|
|
discriminator = _build_discriminated_union_meta(
|
|
union=type_, meta_annotations=meta
|
|
)
|
|
if discriminator and is_mapping(value):
|
|
variant_value = value.get(
|
|
discriminator.field_alias_from or discriminator.field_name
|
|
)
|
|
if variant_value and isinstance(variant_value, str):
|
|
variant_type = discriminator.mapping.get(variant_value)
|
|
if variant_type:
|
|
return construct_type(type_=variant_type, value=value)
|
|
|
|
# if the data is not valid, use the first variant that doesn't fail while deserializing
|
|
for variant in args:
|
|
try:
|
|
return construct_type(value=value, type_=variant)
|
|
except Exception:
|
|
continue
|
|
|
|
raise RuntimeError(f"Could not convert data into a valid instance of {type_}")
|
|
|
|
if origin == dict:
|
|
if not is_mapping(value):
|
|
return value
|
|
|
|
_, items_type = get_args(type_) # Dict[_, items_type]
|
|
return {
|
|
key: construct_type(value=item, type_=items_type)
|
|
for key, item in value.items()
|
|
}
|
|
|
|
if not is_literal_type(type_) and (
|
|
issubclass(origin, BaseModel) or issubclass(origin, GenericModel)
|
|
):
|
|
if is_list(value):
|
|
return [
|
|
cast(Any, type_).construct(**entry) if is_mapping(entry) else entry
|
|
for entry in value
|
|
]
|
|
|
|
if is_mapping(value):
|
|
if issubclass(type_, BaseModel):
|
|
return type_.construct(**value) # type: ignore[arg-type]
|
|
|
|
return cast(Any, type_).construct(**value)
|
|
|
|
if origin == list:
|
|
if not is_list(value):
|
|
return value
|
|
|
|
inner_type = args[0] # List[inner_type]
|
|
return [construct_type(value=entry, type_=inner_type) for entry in value]
|
|
|
|
if origin == float:
|
|
if isinstance(value, int):
|
|
coerced = float(value)
|
|
if coerced != value:
|
|
return value
|
|
return coerced
|
|
|
|
return value
|
|
|
|
if type_ == datetime:
|
|
try:
|
|
return parse_datetime(value) # type: ignore
|
|
except Exception:
|
|
return value
|
|
|
|
if type_ == date:
|
|
try:
|
|
return parse_date(value) # type: ignore
|
|
except Exception:
|
|
return value
|
|
|
|
return value
|
|
|
|
|
|
@runtime_checkable
|
|
class CachedDiscriminatorType(Protocol):
|
|
__discriminator__: DiscriminatorDetails
|
|
|
|
|
|
class DiscriminatorDetails:
|
|
field_name: str
|
|
"""The name of the discriminator field in the variant class, e.g.
|
|
|
|
```py
|
|
class Foo(BaseModel):
|
|
type: Literal['foo']
|
|
```
|
|
|
|
Will result in field_name='type'
|
|
"""
|
|
|
|
field_alias_from: str | None
|
|
"""The name of the discriminator field in the API response, e.g.
|
|
|
|
```py
|
|
class Foo(BaseModel):
|
|
type: Literal['foo'] = Field(alias='type_from_api')
|
|
```
|
|
|
|
Will result in field_alias_from='type_from_api'
|
|
"""
|
|
|
|
mapping: dict[str, type]
|
|
"""Mapping of discriminator value to variant type, e.g.
|
|
|
|
{'foo': FooVariant, 'bar': BarVariant}
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
*,
|
|
mapping: dict[str, type],
|
|
discriminator_field: str,
|
|
discriminator_alias: str | None,
|
|
) -> None:
|
|
self.mapping = mapping
|
|
self.field_name = discriminator_field
|
|
self.field_alias_from = discriminator_alias
|
|
|
|
|
|
def _build_discriminated_union_meta(
|
|
*, union: type, meta_annotations: tuple[Any, ...]
|
|
) -> DiscriminatorDetails | None:
|
|
if isinstance(union, CachedDiscriminatorType):
|
|
return union.__discriminator__
|
|
|
|
discriminator_field_name: str | None = None
|
|
|
|
for annotation in meta_annotations:
|
|
if (
|
|
isinstance(annotation, PropertyInfo)
|
|
and annotation.discriminator is not None
|
|
):
|
|
discriminator_field_name = annotation.discriminator
|
|
break
|
|
|
|
if not discriminator_field_name:
|
|
return None
|
|
|
|
mapping: dict[str, type] = {}
|
|
discriminator_alias: str | None = None
|
|
|
|
for variant in get_args(union):
|
|
variant = strip_annotated_type(variant)
|
|
if is_basemodel_type(variant):
|
|
if PYDANTIC_V2:
|
|
field = _extract_field_schema_pv2(variant, discriminator_field_name)
|
|
if not field:
|
|
continue
|
|
|
|
# Note: if one variant defines an alias then they all should
|
|
discriminator_alias = field.get("serialization_alias")
|
|
|
|
field_schema = field["schema"]
|
|
|
|
if field_schema["type"] == "literal":
|
|
for entry in cast("LiteralSchema", field_schema)["expected"]:
|
|
if isinstance(entry, str):
|
|
mapping[entry] = variant
|
|
else:
|
|
field_info = cast("dict[str, FieldInfo]", variant.__fields__).get(
|
|
discriminator_field_name
|
|
) # pyright: ignore[reportDeprecated, reportUnnecessaryCast]
|
|
if not field_info:
|
|
continue
|
|
|
|
# Note: if one variant defines an alias then they all should
|
|
discriminator_alias = field_info.alias
|
|
|
|
if field_info.annotation and is_literal_type(field_info.annotation):
|
|
for entry in get_args(field_info.annotation):
|
|
if isinstance(entry, str):
|
|
mapping[entry] = variant
|
|
|
|
if not mapping:
|
|
return None
|
|
|
|
details = DiscriminatorDetails(
|
|
mapping=mapping,
|
|
discriminator_field=discriminator_field_name,
|
|
discriminator_alias=discriminator_alias,
|
|
)
|
|
cast(CachedDiscriminatorType, union).__discriminator__ = details
|
|
return details
|
|
|
|
|
|
def _extract_field_schema_pv2(
|
|
model: type[BaseModel], field_name: str
|
|
) -> ModelField | None:
|
|
schema = model.__pydantic_core_schema__
|
|
if schema["type"] != "model":
|
|
return None
|
|
|
|
fields_schema = schema["schema"]
|
|
if fields_schema["type"] != "model-fields":
|
|
return None
|
|
|
|
fields_schema = cast("ModelFieldsSchema", fields_schema)
|
|
|
|
field = fields_schema["fields"].get(field_name)
|
|
if not field:
|
|
return None
|
|
|
|
return cast("ModelField", field) # pyright: ignore[reportUnnecessaryCast]
|
|
|
|
|
|
def validate_type(*, type_: type[_T], value: object) -> _T:
|
|
"""Strict validation that the given value matches the expected type"""
|
|
if inspect.isclass(type_) and issubclass(type_, pydantic.BaseModel):
|
|
return cast(_T, parse_obj(type_, value))
|
|
|
|
return cast(_T, _validate_non_model_type(type_=type_, value=value))
|
|
|
|
|
|
# our use of subclasssing here causes weirdness for type checkers,
|
|
# so we just pretend that we don't subclass
|
|
if TYPE_CHECKING:
|
|
GenericModel = BaseModel
|
|
else:
|
|
|
|
class GenericModel(BaseGenericModel, BaseModel):
|
|
pass
|
|
|
|
|
|
if PYDANTIC_V2:
|
|
from pydantic import TypeAdapter as _TypeAdapter
|
|
|
|
_CachedTypeAdapter = cast(
|
|
"TypeAdapter[object]", lru_cache(maxsize=None)(_TypeAdapter)
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from pydantic import TypeAdapter
|
|
else:
|
|
TypeAdapter = _CachedTypeAdapter
|
|
|
|
def _validate_non_model_type(*, type_: type[_T], value: object) -> _T:
|
|
return TypeAdapter(type_).validate_python(value)
|
|
|
|
elif not TYPE_CHECKING: # TODO: condition is weird
|
|
|
|
class RootModel(GenericModel, Generic[_T]):
|
|
"""Used as a placeholder to easily convert runtime types to a Pydantic format
|
|
to provide validation.
|
|
|
|
For example:
|
|
```py
|
|
validated = RootModel[int](__root__="5").__root__
|
|
# validated: 5
|
|
```
|
|
"""
|
|
|
|
__root__: _T
|
|
|
|
def _validate_non_model_type(*, type_: type[_T], value: object) -> _T:
|
|
model = _create_pydantic_model(type_).validate(value)
|
|
return cast(_T, model.__root__)
|
|
|
|
def _create_pydantic_model(type_: _T) -> Type[RootModel[_T]]:
|
|
return RootModel[type_] # type: ignore
|