Giter Club home page Giter Club logo

mojuan's Introduction

wlcome show

中文English

项目背景

【1】受到很多项目启示,比如 chatbot-ui, opengpts,gpt research ...

【2】很多项目使用 Docker 和其他一些第三方服务,这些服务可能在某些情况下不方便使用

【3】很多项目使用 openai 等一些国内不方便使用的模型

【4】 展示如何使用 Langchain 进行 LLm 应用开发

【5】 使用国内优秀的大语言模型、嵌入模型

亮点

  • 这个项目尝试使用国内的模型,通过相对简单的方式,比如使用本地向量数据库,关系数据库

    来建立一个易于个人使用的本地 AI 应用。

  • 前端采用 Vite + ReAct, 后端使用 FastApi + Langchain

  • 向量数据库:Chroma/Elastic Search

  • 关系数据库:Postgres


Demo

View the latest demo : www.chatroller.cn

项目快速启动

前端:

cd frontend
npm install
npm run dev

后端

【1】 安装必要的包

请不要手动安装langchain包或者将项目放在有langchian的环境
项目使用特定版本的langchain

cd backend
pip install -r requirements.txt

【2】安装和配置 Postgres 数据库

生成项目相关的表 : 打开 pgAdmin, 通过 UI 界面设置相关表单

<1> 建立数据库 : chatroller

db_1.png

<2> 打开 Query Tool

db_2.png

<3> 依次输入下面的命令并执行 (在chatroller 中创建表

【1】
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

【2】
CREATE TABLE cusers (
    id SERIAL,
    username VARCHAR(255) UNIQUE NOT NULL,
    user_email VARCHAR(255) UNIQUE NOT NULL,
    hashed_password TEXT NOT NULL,
    doc_visit_days INTEGER DEFAULT 0,
    last_login TIMESTAMP WITH TIME ZONE,
    user_id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    user_mask INTEGER DEFAULT 1,
    user_group VARCHAR(50) DEFAULT 'free',
    user_status VARCHAR(50) DEFAULT 'available',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC'),
    hl_llm_visit_days INTEGER DEFAULT 0
);

【3】
CREATE TABLE email_verification_codes (
    id SERIAL PRIMARY KEY,
    email VARCHAR(255) NOT NULL,
    code VARCHAR(255) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP NOT NULL,
    is_used BOOLEAN NOT NULL DEFAULT FALSE
);

【4】
CREATE TABLE IF NOT EXISTS assistant (
    assistant_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id VARCHAR(255) NOT NULL,
    name VARCHAR(255) NOT NULL,
    config JSON NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC'),
    public BOOLEAN NOT NULL
);

【5】
CREATE TABLE IF NOT EXISTS thread (
    thread_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    assistant_id UUID REFERENCES assistant(assistant_id) ON DELETE SET NULL,
    user_id VARCHAR(255) NOT NULL,
    name VARCHAR(255) NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
);

【6】
CREATE TABLE IF NOT EXISTS checkpoints (
    thread_id TEXT,
    checkpoint BYTEA,
    thread_ts TIMESTAMPTZ,
    parent_ts TIMESTAMPTZ
);

【7】
ALTER TABLE checkpoints
    DROP CONSTRAINT IF EXISTS checkpoints_pkey,
    ADD PRIMARY KEY (thread_id),
    ADD COLUMN IF NOT EXISTS thread_ts TIMESTAMPTZ,
    ADD COLUMN IF NOT EXISTS parent_ts TIMESTAMPTZ;


【8】
UPDATE checkpoints
    SET thread_ts = CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
WHERE thread_ts IS NULL;

【9】
ALTER TABLE checkpoints
    DROP CONSTRAINT IF EXISTS checkpoints_pkey,
    ADD PRIMARY KEY (thread_id, thread_ts);

【10】
ALTER TABLE assistant
    DROP CONSTRAINT IF EXISTS fk_assistant_user_id,
    ALTER COLUMN user_id TYPE VARCHAR USING (user_id::text);

【11】
ALTER TABLE thread
    DROP CONSTRAINT IF EXISTS fk_thread_user_id,
    ALTER COLUMN user_id TYPE VARCHAR USING (user_id::text);

【3】设置项目使用的参数

找到 backend/app/llm_cfg.py,增加下面的参数

# 智谱AI(必须)  https://bigmodel.cn/dev/howuse/introduction
ZHIPU_AK = os.environ["ZHIPU_AK"]

# 千帆(必须)  https://qianfan.cloud.baidu.com/
MY_QIANFAN_AK = os.environ["MY_QIANFAN_AK"]
MY_QIANFAN_SK = os.environ["MY_QIANFAN_SK"]

# 通义千问(必须) :https://help.aliyun.com/zh/dashscope/developer-reference/api-details
DASHSCOPE_API_KEY = os.environ["DASHSCOPE_API_KEY"]

# Langsmith Key(可选)
MY_LANGCHAIN_API_KEY = os.environ["LANGCHAIN_API_KEY"]


# Postgres数据库: 本地、云端 均可 (必须)
POSTGRES_HOST = 'localhost'
POSTGRES_PORT = 5432
POSTGRES_DB = 'chatroller'
POSTGRES_USER = 'postgres'
POSTGRES_PASSWORD = os.environ["POSTGRES_PASSWORD"]

【4】启动后端

cd backend
uvicorn app.server:app --reload --port 8100

【5】注册登录

登录时需要先注册(邮箱并没有发送验证码,你可以自己实现),邮箱的验证码需要从数据库里查看。可以用 pgAdmin 打开 email_verification_codes 查看

select * from email_verification_codes;

image.png

有问题麻烦发送到我的邮箱 [email protected]

B 站视频:https://www.bilibili.com/video/BV1rz421875f/

启动对话界面: http://localhost:5173 登录界面: http://localhost:5173/signin

mojuan's People

Contributors

susirial avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar wysstartgo avatar Hugo avatar  avatar  avatar

Watchers

 avatar  avatar

mojuan's Issues

你好,安装好之后,chatbot和agent正常,但是rag回答不正常,后台也没有报错。

image

我上传了西游记开头的几个章节文本。

INFO:     127.0.0.1:2855 - "GET /assistants/42370e59-0007-4f0c-b664-1328e4d390b7 HTTP/1.1" 200 OK
INFO:     127.0.0.1:2853 - "GET /assistants/358c4e08-d6d1-4689-a85a-807847ef3389 HTTP/1.1" 200 OK
INFO:app.auth.charrol_handlers:Authorization header: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjQ0MTAyMzgsImlhdCI6MTcyMzgwNTQzOCwic3ViIjoiN2U1Y2UxODQtM2JmNy00NjgyLTk0NjctZTM2M2FmYjE2M2JjIn0.YqCsUSwVZMpN-utXyLVhge_QG4cUrxV1tdoH1F_JGUc
INFO:app.auth.charrol_handlers:Token decoded successfully: {'exp': 1724410238, 'iat': 1723805438, 'sub': '7e5ce184-3bf7-4682-9467-e363afb163bc'}
INFO:app.auth.charrol_handlers:Auth wrapper decoded user_id: 7e5ce184-3bf7-4682-9467-e363afb163bc
INFO:root:[获得聊天记录] user_id:[7e5ce184-3bf7-4682-9467-e363afb163bc] thread_id[e0acc1aa-6a5b-49da-9bad-ac8f67543378]
INFO:     127.0.0.1:2857 - "GET /threads/e0acc1aa-6a5b-49da-9bad-ac8f67543378 HTTP/1.1" 200 OK

fastapi.exceptions.ResponseValidationError: 1 validation errors:

后端和前端正常启动,可以注册和登录;但是添加助手时间,报错如下:

INFO:app.auth.charrol_handlers:Authorization header: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjEwNTg3ODQsImlhdCI6MTcyMDQ1Mzk4NCwic3ViIjoiN2U1Y2UxODQtM2JmNy00NjgyLTk0NjctZTM2M2FmYjE2M2JjIn0.D7O0t5ZUbK65cMitIQEWURbOEiGBW7K5CAYpA571xic  
INFO:app.auth.charrol_handlers:Token decoded successfully: {'exp': 1721058784, 'iat': 1720453984, 'sub': '7e5ce184-3bf7-4682-9467-e363afb163bc'}
INFO:app.auth.charrol_handlers:Auth wrapper decoded user_id: 7e5ce184-3bf7-4682-9467-e363afb163bc
INFO:root:[获得助手] user_id:[7e5ce184-3bf7-4682-9467-e363afb163bc] assistant_id[dc6b04af-4e5c-4746-aaee-e4f8027e0a89]
INFO:     127.0.0.1:6447 - "GET /assistants/dc6b04af-4e5c-4746-aaee-e4f8027e0a89 HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "F:\miniconda3\envs\py1101\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 426, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\miniconda3\envs\py1101\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\miniconda3\envs\py1101\Lib\site-packages\fastapi\applications.py", line 292, in __call__
    await super().__call__(scope, receive, send)
  File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\middleware\errors.py", line 184, in __call__
    raise exc
  File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\middleware\errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\middleware\exceptions.py", line 79, in __call__
    raise exc
  File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\middleware\exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "F:\miniconda3\envs\py1101\Lib\site-packages\fastapi\middleware\asyncexitstack.py", line 20, in __call__
    raise e
  File "F:\miniconda3\envs\py1101\Lib\site-packages\fastapi\middleware\asyncexitstack.py", line 17, in __call__
    await self.app(scope, receive, send)
  File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\routing.py", line 66, in app
    raise ResponseValidationError(
fastapi.exceptions.ResponseValidationError: 1 validation errors:
  {'type': 'dict_type', 'loc': ('response',), 'msg': 'Input should be a valid dictionary', 'input': <Record assistant_id='dc6b04af-4e5c-4746-aaee-e4f8027e0a89' user_id='7e5ce184-3bf7-4682-9467-e363afb163bc' name='111' config={'configurable': {'type': 'agent', 'type==agent/agent_type': '智谱清言GLM4', 'type==agent/interrupt_before_action': False, 'type==agent/retrieval_description': 'Can be used to look up information that was uploaded to this assistant.\nIf the user is referencing particular files, that is often a good hint that information may be here.\nIf the user asks a vague question, they are likely meaning to look up info from this retriever, and you should call it!', 'type==agent/system_message': '你是一个乐于助人的AI助手', 'type==agent/tools': [], 'type==chat_retrieval/llm_type': '智谱清言GLM4', 'type==chat_retrieval/system_message': '你是一个乐于助人的AI助手', 'type==chatbot/llm_type': '智谱清言GLM4', 'type==chatbot/system_message': '你是一个乐于助人的AI助手'}} updated_at=datetime.datetime(2024, 7, 8, 15, 57, 42, 666137, tzinfo=datetime.timezone.utc) public=False>, 'url': 'https://errors.pydantic.dev/2.8/v/dict_type'}

INFO:     127.0.0.1:6468 - "GET / HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:6468 - "GET /favicon.ico HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:6468 - "GET /docs HTTP/1.1" 200 OK
INFO:     127.0.0.1:6468 - "GET /openapi.json HTTP/1.1" 200 [OK]

环境是使用的poetry安装的,python3.11版本;
对应的参数和模型都下载好了。

附件是pyproject.toml和poetry.lock

backend_pyproject.toml_poetry.lock.zip

fastapi.exceptions.ResponseValidationError: 1 validation errors:

创建助手出现
INFO:app.auth.charrol_handlers:Authorization header: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjE4MTIwMTAsImlhdCI6MTcyMTIwNzIxMCwic3ViIjoiN2U1Y2UxODQtM2JmNy00NjgyLTk0NjctZTM2M2FmYjE2M2JjIn0.fKSP4O-DbOylROVZrbOLKdOD_p77wa2sQyhbjgYi_Pc INFO:app.auth.charrol_handlers:Token decoded successfully: {'exp': 1721812010, 'iat': 1721207210, 'sub': '7e5ce184-3bf7-4682-9467-e363afb163bc'} INFO:app.auth.charrol_handlers:Auth wrapper decoded user_id: 7e5ce184-3bf7-4682-9467-e363afb163bc INFO:root:[获得助手] user_id:[7e5ce184-3bf7-4682-9467-e363afb163bc] assistant_id[6ee7cee2-d3fd-42e3-bd9a-9e1ffb6cf4cb] INFO: 127.0.0.1:2969 - "GET /assistants/6ee7cee2-d3fd-42e3-bd9a-9e1ffb6cf4cb HTTP/1.1" 500 Internal Server Error ERROR: Exception in ASGI application Traceback (most recent call last): File "F:\miniconda3\envs\py1101\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 426, in run_asgi result = await app( # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\miniconda3\envs\py1101\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 84, in __call__ return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:\miniconda3\envs\py1101\Lib\site-packages\fastapi\applications.py", line 292, in __call__ await super().__call__(scope, receive, send) File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\applications.py", line 122, in __call__ await self.middleware_stack(scope, receive, send) File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\middleware\errors.py", line 184, in __call__ raise exc File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\middleware\errors.py", line 162, in __call__ await self.app(scope, receive, _send) File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\middleware\exceptions.py", line 79, in __call__ raise exc File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\middleware\exceptions.py", line 68, in __call__ await self.app(scope, receive, sender) File "F:\miniconda3\envs\py1101\Lib\site-packages\fastapi\middleware\asyncexitstack.py", line 20, in __call__ raise e File "F:\miniconda3\envs\py1101\Lib\site-packages\fastapi\middleware\asyncexitstack.py", line 17, in __call__ await self.app(scope, receive, send) File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\routing.py", line 718, in __call__ await route.handle(scope, receive, send) File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\routing.py", line 276, in handle await self.app(scope, receive, send) File "F:\miniconda3\envs\py1101\Lib\site-packages\starlette\routing.py", line 66, in app response = await func(request) ^^^^^^^^^^^^^^^^^^^ File "F:\miniconda3\envs\py1101\Lib\site-packages\fastapi\routing.py", line 291, in app content = await serialize_response( raise ResponseValidationError( fastapi.exceptions.ResponseValidationError: 1 validation errors: {'type': 'dict_type', 'loc': ('response',), 'msg': 'Input should be a valid dictionary', 'input': <Record assistant_id='6ee7cee2-d3fd-42e3-bd9a-9e1ffb6cf4cb' user_id='7e5ce184-3bf7-4682-9467-e363afb163bc' name='assistant1' config={'configurable': {'type': 'agent', 'type==agent/agent_type': '智谱清言GLM4', 'type==agent/interrupt_before_action': False, 'type==agent/retrieval_description': 'Can be used to look up information that was uploaded to this assistant.\nIf the user is referencing particular files, that is often a good hint that information may be here.\nIf the user asks a vague question, they are likely meaning to look up info from this retriever, and you should call it!', 'type==agent/system_message': '你是一个乐于助人的AI助手', 'type==agent/tools': [], 'type==chat_retrieval/llm_type': '智谱清言GLM4', 'type==chat_retrieval/system_message': '你是一个乐于助人的AI助手', 'type==chatbot/llm_type': '智谱清言GLM4', 'type==chatbot/system_message': '你是一个乐于助人的AI助手'}} updated_at=datetime.datetime(2024, 7, 17, 9, 7, 15, 590181, tzinfo=datetime.timezone.utc) public=False>, 'url': 'https://errors.pydantic.dev/2.8/v/dict_type'}

image
过了一会儿就会跳出登录:
image

请问呢这些安装包为什么不加入pyproject.toml里

`
pip install libmagic (linux)

pip install python-magic-bin (windows)

pip install qianfan

pip install zhipuai

pip install dashscope

pip install elasticsearch

pip install pandas

pip install appbuilder

pip install pydantic[email]

pip install passlib

pip install uvicorn

pip install bcrypt

pip install chromadb

pip install python-magic

`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.