├── .gitignore
├── .mvn
└── wrapper
│ ├── maven-wrapper.jar
│ └── maven-wrapper.properties
├── LICENSE
├── README.md
├── bootstrap
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── honvay
│ │ │ └── flychat
│ │ │ └── application
│ │ │ ├── FlychatApplication.java
│ │ │ └── config
│ │ │ ├── GlobalExceptionAdvice.java
│ │ │ ├── MyBatisPlusConfiguration.java
│ │ │ └── WebMvcConfiguration.java
│ └── resources
│ │ ├── application.yml
│ │ └── static
│ │ ├── index.html
│ │ └── index2.html
│ └── test
│ └── java
│ ├── impl
│ ├── ApplicationDomainServiceImplTest.java
│ ├── KnowledgeBaseApplicationServiceImplTest.java
│ └── KnowledgeChatServiceImplTest.java
│ └── mapper
│ └── KnowledgeBaseDetailMapperTest.java
├── chat
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── honvay
│ └── flychat
│ └── chat
│ ├── application
│ ├── ChatApplicationService.java
│ └── impl
│ │ └── ChatApplicationServiceImpl.java
│ ├── domain
│ ├── model
│ │ ├── Chat.java
│ │ ├── ChatMessage.java
│ │ ├── ChatQuote.java
│ │ └── User.java
│ ├── repository
│ │ └── ChatRepository.java
│ └── service
│ │ ├── ChatDomainServiceImpl.java
│ │ └── impl
│ │ └── ChatDomainService.java
│ ├── infra
│ ├── converter
│ │ └── ChatConverter.java
│ ├── mapper
│ │ ├── ChatMapper.java
│ │ ├── ChatMessageMapper.java
│ │ └── ChatQuoteMapper.java
│ ├── po
│ │ ├── ChatMessagePo.java
│ │ ├── ChatPo.java
│ │ └── ChatQuotePo.java
│ └── repository
│ │ └── ChatRepositoryImpl.java
│ └── web
│ ├── ChatController.java
│ └── model
│ ├── ChatMessageVo.java
│ └── ChatVo.java
├── conversation
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── honvay
│ └── flychat
│ └── conversation
│ ├── application
│ ├── ConversationApplicationService.java
│ ├── ConversationModel.java
│ └── impl
│ │ └── ConversationApplicationServiceImpl.java
│ ├── controller
│ ├── ConversationController.java
│ └── ConversationVo.java
│ ├── domain
│ ├── Conversation.java
│ ├── Knowledge.java
│ ├── Model.java
│ └── Relation.java
│ ├── infra
│ ├── ConversationMessageProvider.java
│ ├── KnowledgeMessageProvider.java
│ ├── MessageProvider.java
│ ├── MessageProviderDelegator.java
│ └── MockChatModelService.java
│ └── types
│ └── ChatModel.java
├── docs
└── flychat.sql
├── framework
├── framework-core
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── com
│ │ └── honvay
│ │ └── cola
│ │ └── framework
│ │ └── core
│ │ ├── ErrorConstants.java
│ │ ├── ErrorMessage.java
│ │ ├── enums
│ │ └── GeneralEnum.java
│ │ ├── exception
│ │ └── ServiceException.java
│ │ └── protocol
│ │ └── Response.java
├── framework-web
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── com
│ │ └── honvay
│ │ └── cola
│ │ └── framework
│ │ └── web
│ │ ├── ResponsePayload.java
│ │ └── ResponsePayloadAdvise.java
└── pom.xml
├── frontend
├── .env.development
├── .env.production
├── .eslintignore
├── .eslintrc.js
├── .gitignore
├── .prettierignore
├── .prettierrc.js
├── .stylelintrc.js
├── babel.config.js
├── commitlint.config.js
├── components.d.ts
├── config
│ ├── plugin
│ │ ├── arcoResolver.ts
│ │ ├── arcoStyleImport.ts
│ │ ├── compress.ts
│ │ ├── imagemin.ts
│ │ └── visualizer.ts
│ ├── utils
│ │ └── index.ts
│ ├── vite.config.base.ts
│ ├── vite.config.dev.ts
│ └── vite.config.prod.ts
├── index.html
├── package.json
├── pnpm-lock.yaml
├── postcss.config.js
├── src
│ ├── App.vue
│ ├── api
│ │ ├── chat.ts
│ │ ├── common.ts
│ │ ├── dashboard.ts
│ │ ├── interceptor.ts
│ │ ├── list.ts
│ │ ├── message.ts
│ │ └── user.ts
│ ├── assets
│ │ ├── images
│ │ │ ├── avatar.jpg
│ │ │ └── login-banner.png
│ │ ├── logo.svg
│ │ ├── style
│ │ │ ├── breakpoint.less
│ │ │ ├── github-markdown.less
│ │ │ ├── global.less
│ │ │ ├── highlight.less
│ │ │ └── tailwind.css
│ │ └── world.json
│ ├── components
│ │ ├── breadcrumb
│ │ │ └── index.vue
│ │ ├── chart
│ │ │ └── index.vue
│ │ ├── common
│ │ │ └── SvgIcon
│ │ │ │ └── index.vue
│ │ ├── footer
│ │ │ └── index.vue
│ │ ├── global-setting
│ │ │ ├── block.vue
│ │ │ ├── form-wrapper.vue
│ │ │ └── index.vue
│ │ ├── index.ts
│ │ ├── menu
│ │ │ ├── index.vue
│ │ │ └── use-menu-tree.ts
│ │ ├── message-box
│ │ │ ├── index.vue
│ │ │ ├── list.vue
│ │ │ └── locale
│ │ │ │ ├── en-US.ts
│ │ │ │ └── zh-CN.ts
│ │ ├── navbar
│ │ │ └── index.vue
│ │ └── tab-bar
│ │ │ ├── index.vue
│ │ │ ├── readme.md
│ │ │ └── tab-item.vue
│ ├── config
│ │ └── settings.json
│ ├── directive
│ │ ├── index.ts
│ │ └── permission
│ │ │ └── index.ts
│ ├── env.d.ts
│ ├── hooks
│ │ ├── chart-option.ts
│ │ ├── icon-render.ts
│ │ ├── layout.ts
│ │ ├── loading.ts
│ │ ├── locale.ts
│ │ ├── permission.ts
│ │ ├── request.ts
│ │ ├── responsive.ts
│ │ ├── scroll.ts
│ │ ├── themes.ts
│ │ ├── user.ts
│ │ └── visible.ts
│ ├── layout
│ │ ├── default-layout.vue
│ │ └── page-layout.vue
│ ├── locale
│ │ ├── en-US.ts
│ │ ├── en-US
│ │ │ └── settings.ts
│ │ ├── index.ts
│ │ ├── zh-CN.ts
│ │ └── zh-CN
│ │ │ └── settings.ts
│ ├── main.ts
│ ├── mock
│ │ ├── index.ts
│ │ ├── message-box.ts
│ │ └── user.ts
│ ├── router
│ │ ├── app-menus
│ │ │ └── index.ts
│ │ ├── constants.ts
│ │ ├── guard
│ │ │ ├── index.ts
│ │ │ ├── permission.ts
│ │ │ └── userLoginInfo.ts
│ │ ├── index.ts
│ │ ├── routes
│ │ │ ├── base.ts
│ │ │ ├── index.ts
│ │ │ ├── modules
│ │ │ │ ├── chat.ts
│ │ │ │ └── knowledge.ts
│ │ │ └── types.ts
│ │ └── typings.d.ts
│ ├── store
│ │ ├── index.ts
│ │ └── modules
│ │ │ ├── app
│ │ │ ├── index.ts
│ │ │ └── types.ts
│ │ │ ├── chat
│ │ │ ├── index.ts
│ │ │ └── types.ts
│ │ │ ├── tab-bar
│ │ │ ├── index.ts
│ │ │ └── types.ts
│ │ │ └── user
│ │ │ ├── index.ts
│ │ │ └── types.ts
│ ├── types
│ │ ├── echarts.ts
│ │ ├── global.ts
│ │ └── mock.ts
│ ├── utils
│ │ ├── auth.ts
│ │ ├── copy.ts
│ │ ├── env.ts
│ │ ├── event.ts
│ │ ├── index.ts
│ │ ├── is.ts
│ │ ├── monitor.ts
│ │ ├── route-listener.ts
│ │ ├── setup-mock.ts
│ │ └── storage.ts
│ └── views
│ │ ├── chat
│ │ ├── chat-list.vue
│ │ ├── container
│ │ │ └── index.vue
│ │ ├── header
│ │ │ └── index.vue
│ │ ├── index.vue
│ │ └── message
│ │ │ ├── avatar.vue
│ │ │ ├── index.vue
│ │ │ ├── style.less
│ │ │ └── text.vue
│ │ ├── dashboard
│ │ └── workplace
│ │ │ ├── components
│ │ │ ├── announcement.vue
│ │ │ ├── banner.vue
│ │ │ ├── carousel.vue
│ │ │ ├── categories-percent.vue
│ │ │ ├── content-chart.vue
│ │ │ ├── data-panel.vue
│ │ │ ├── docs.vue
│ │ │ ├── popular-content.vue
│ │ │ ├── quick-operation.vue
│ │ │ └── recently-visited.vue
│ │ │ ├── index.vue
│ │ │ ├── locale
│ │ │ ├── en-US.ts
│ │ │ └── zh-CN.ts
│ │ │ └── mock.ts
│ │ ├── knowledge
│ │ ├── components
│ │ │ ├── card-wrap.vue
│ │ │ ├── quality-inspection.vue
│ │ │ ├── rules-preset.vue
│ │ │ └── the-service.vue
│ │ ├── index.vue
│ │ ├── locale
│ │ │ ├── en-US.ts
│ │ │ └── zh-CN.ts
│ │ └── mock.ts
│ │ ├── login
│ │ ├── components
│ │ │ ├── banner.vue
│ │ │ └── login-form.vue
│ │ ├── index.vue
│ │ └── locale
│ │ │ ├── en-US.ts
│ │ │ └── zh-CN.ts
│ │ ├── not-found
│ │ └── index.vue
│ │ └── redirect
│ │ └── index.vue
├── tailwind.config.js
└── tsconfig.json
├── knowledge
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── honvay
│ │ │ └── flychat
│ │ │ └── knowledge
│ │ │ ├── application
│ │ │ ├── KnowledgeApplicationService.java
│ │ │ ├── KnowledgeChatService.java
│ │ │ ├── KnowledgeEmbeddingService.java
│ │ │ └── impl
│ │ │ │ ├── KnowledgeApplicationServiceImpl.java
│ │ │ │ ├── KnowledgeChatServiceImpl.java
│ │ │ │ └── KnowledgeEmbeddingServiceImpl.java
│ │ │ ├── domain
│ │ │ ├── event
│ │ │ │ └── KnowledgeItemCreateEvent.java
│ │ │ ├── model
│ │ │ │ ├── Application.java
│ │ │ │ ├── ApplicationKnowledge.java
│ │ │ │ ├── ApplicationModel.java
│ │ │ │ ├── KnowledgeBase.java
│ │ │ │ ├── KnowledgeChat.java
│ │ │ │ ├── KnowledgeDetail.java
│ │ │ │ ├── KnowledgeItem.java
│ │ │ │ ├── KnowledgeItemStatus.java
│ │ │ │ ├── Owner.java
│ │ │ │ ├── Relevant.java
│ │ │ │ └── SplitType.java
│ │ │ ├── repository
│ │ │ │ ├── ApplicationKnowledgeRepository.java
│ │ │ │ ├── ApplicationRepository.java
│ │ │ │ ├── KnowledgeDetailRepository.java
│ │ │ │ ├── KnowledgeItemRepository.java
│ │ │ │ └── KnowledgeRepository.java
│ │ │ └── service
│ │ │ │ ├── ApplicationDomainService.java
│ │ │ │ ├── KnowledgeBaseDomainService.java
│ │ │ │ └── impl
│ │ │ │ ├── ApplicationDomainServiceImpl.java
│ │ │ │ └── KnowledgeBaseDomainServiceImpl.java
│ │ │ ├── infra
│ │ │ ├── document
│ │ │ │ ├── DocumentType.java
│ │ │ │ └── KnowledgeDocumentLoader.java
│ │ │ ├── factory
│ │ │ │ ├── ApplicationConverter.java
│ │ │ │ └── KnowledgeConverter.java
│ │ │ ├── interceptor
│ │ │ │ └── PgVectorInterceptor.java
│ │ │ ├── mapper
│ │ │ │ ├── ApplicationKnowledgeMapper.java
│ │ │ │ ├── ApplicationMapper.java
│ │ │ │ ├── KnowledgeBaseMapper.java
│ │ │ │ ├── KnowledgeDetailMapper.java
│ │ │ │ └── KnowledgeItemMapper.java
│ │ │ ├── po
│ │ │ │ ├── ApplicationKnowledgePo.java
│ │ │ │ ├── ApplicationPo.java
│ │ │ │ ├── KnowledgeBasePo.java
│ │ │ │ ├── KnowledgeDetailPo.java
│ │ │ │ └── KnowledgeItemPo.java
│ │ │ ├── repository
│ │ │ │ └── impl
│ │ │ │ │ ├── ApplicationKnowledgeRepositoryImpl.java
│ │ │ │ │ ├── ApplicationRepositoryImpl.java
│ │ │ │ │ ├── KnowledgeDetailRepositoryImpl.java
│ │ │ │ │ ├── KnowledgeItemRepositoryImpl.java
│ │ │ │ │ └── KnowledgeRepositoryImpl.java
│ │ │ └── splitter
│ │ │ │ ├── FixedTextSplitter.java
│ │ │ │ ├── KnowledgeSplitter.java
│ │ │ │ ├── ParagraphTextSplitter.java
│ │ │ │ ├── SentenceTextSplitter.java
│ │ │ │ └── TextSplitter.java
│ │ │ └── web
│ │ │ ├── controller
│ │ │ ├── KnowledgeChatController.java
│ │ │ └── KnowledgeController.java
│ │ │ ├── converter
│ │ │ ├── KnowledgeAssembler.java
│ │ │ └── KnowledgeChatAssembler.java
│ │ │ └── model
│ │ │ └── dto
│ │ │ ├── KnowledgeChatDto.java
│ │ │ ├── KnowledgeDto.java
│ │ │ └── KnowledgeItemDto.java
│ └── resources
│ │ └── mapper
│ │ └── KnowledgeDetailMapper.xml
│ └── test
│ ├── java
│ └── com
│ │ └── honvay
│ │ └── flychat
│ │ └── knowledge
│ │ ├── llama
│ │ └── OpenAiEmbeddingServiceImplTest.java
│ │ └── splitter
│ │ ├── FixedTextSplitterTest.java
│ │ ├── ParagraphTextSplitterTest.java
│ │ └── SentenceTextSplitterTest.java
│ └── resources
│ └── story-about-happy-carrot.pdf
├── langchain
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── honvay
│ │ └── flychat
│ │ └── langchain
│ │ ├── chat
│ │ ├── ChatModelService.java
│ │ ├── ChatSetup.java
│ │ ├── DefaultStreamChatObserver.java
│ │ ├── OpenAiChatModelService.java
│ │ └── StreamChatObserver.java
│ │ ├── document
│ │ └── StreamDocumentSource.java
│ │ ├── llama
│ │ ├── embedding
│ │ │ ├── EmbeddingService.java
│ │ │ └── OpenAiEmbeddingService.java
│ │ └── model
│ │ │ ├── OpenAiConverters.java
│ │ │ └── OpenAiStreamingChatLanguageModel.java
│ │ └── parser
│ │ └── WordDocumentParser.java
│ └── test
│ └── java
│ └── com
│ └── honvay
│ └── flychat
│ └── langchain
│ └── chat
│ └── OpenAiChatModelServiceTest.java
├── mvnw
├── mvnw.cmd
├── pom.xml
└── screenshots
└── img.png
/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**/target/
5 | !**/src/test/**/target/
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 | !**/src/main/**/build/
30 | !**/src/test/**/build/
31 |
32 | ### VS Code ###
33 | .vscode/
--------------------------------------------------------------------------------
/.mvn/wrapper/maven-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leecho/flychat/d238fbd2a60673243686764d8aaff3ae956177af/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | # Licensed to the Apache Software Foundation (ASF) under one
2 | # or more contributor license agreements. See the NOTICE file
3 | # distributed with this work for additional information
4 | # regarding copyright ownership. The ASF licenses this file
5 | # to you under the Apache License, Version 2.0 (the
6 | # "License"); you may not use this file except in compliance
7 | # with the License. You may obtain a copy of the License at
8 | #
9 | # https://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing,
12 | # software distributed under the License is distributed on an
13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | # KIND, either express or implied. See the License for the
15 | # specific language governing permissions and limitations
16 | # under the License.
17 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.7/apache-maven-3.8.7-bin.zip
18 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar
19 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 leecho
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FlyChat
2 |
3 | # 介绍
4 |
5 | 基于Java和ChatGPT的AI平台,已实现基本的问答和基于知识库的问题,暂时只有后端应用,没有前端。
6 |
7 | 
8 | # 框架
9 |
10 | - Spring Boot 2.7.13
11 | - JDK 17
12 | - Postgresql 15
13 | - MyBatis Plus
14 | - Lombok
15 |
16 |
17 | # 技术要点
18 |
19 | ## 在线问答
20 |
21 | ### 上下文聊天
22 |
23 | 通过 Postgresql 实现聊天数据存储来实现上下文聊天,已实现流式输出结果,可以通过配置参数 maxTokens 来限制上下问问题的数量。
24 |
25 | 数据库存储了每次聊天对话的记录,在选择上下文聊天时,通过 chatId 获取历史消息,将历史问题以及回答消息都发送给 GPT。
26 |
27 | ## 知识库问答
28 |
29 | ### 知识库分词
30 | 已实现按照句子分词、段落分词、固定长度分词等分词方式,并通过ChatGPT进行Embedding
31 |
32 | ### 知识库匹配
33 | 通过Postgresql 插件 PGvector进行相似度匹配,获取与问题比较匹配的知识点
34 |
35 | ### 知识库问答
36 | 通过对问题进行Embedding,然后匹配知识库,构建Prompt,发送给ChatGPT获取答案。
37 |
38 | # 私有部署问题
39 |
40 | 可导入知识库文件、文本对知识库进行构建,知识库存储在本地数据库,调用ChatGPT接口进行问答,简而言之,数据还是会给到ChatGPT,但是不是所有的数据都给过去,理论上是一个半私有或者部分私有部署的状态,如果要实现完全私有化则需要部署一套私有的大语言模型。
41 |
42 | # 风险声明
43 |
44 | 本项目仅供学习和研究使用,不鼓励用于商业用途。对于因使用本项目而导致的任何损失,我们不承担任何责任。
45 |
46 | # 后续计划
47 | 目前项目只有后端没有前端,所有期待感兴趣的前端、UI、测试小伙伴一起参与项目。
48 | 后续实现功能:
49 | - 增加类似与ChatPDF的功能
50 | - 增加AI应用功能
51 | - 集成其他大语言模型
52 |
53 | # 联系方式
54 |
55 | wechat:leecho571
56 |
57 | # LICENSE
58 | [MIT](LICENSE)
59 |
60 | 前端部分基于
61 |
62 | [Chagpt-Web](https://github.com/Chanzhaoyu/chatgpt-web)
--------------------------------------------------------------------------------
/bootstrap/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.honvay
8 | flychat
9 | 0.0.1-SNAPSHOT
10 |
11 |
12 | org.example
13 | bootstrap
14 |
15 |
16 | 17
17 | 17
18 | UTF-8
19 |
20 |
21 |
22 |
23 | com.honvay
24 | knowledge
25 | 0.0.1-SNAPSHOT
26 |
27 |
28 | com.honvay
29 | conversation
30 | 0.0.1-SNAPSHOT
31 |
32 |
33 | org.postgresql
34 | postgresql
35 | 42.2.27
36 |
37 |
38 | org.springframework.boot
39 | spring-boot-starter-test
40 | test
41 |
42 |
43 | org.springframework.boot
44 | spring-boot-starter-validation
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/bootstrap/src/main/java/com/honvay/flychat/application/FlychatApplication.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.application;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 |
7 | @SpringBootApplication(scanBasePackages = "com.honvay")
8 | public class FlychatApplication {
9 |
10 | public static void main(String[] args) {
11 | SpringApplication.run(FlychatApplication.class,args);
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/bootstrap/src/main/java/com/honvay/flychat/application/config/MyBatisPlusConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.application.config;
2 |
3 | import com.baomidou.mybatisplus.annotation.DbType;
4 | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
5 | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
6 | import com.honvay.flychat.knowledge.infra.interceptor.PgVectorInterceptor;
7 | import org.mybatis.spring.annotation.MapperScan;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 |
11 | @Configuration
12 | @MapperScan("com.honvay.**.mapper")
13 | public class MyBatisPlusConfiguration {
14 |
15 | @Bean
16 | public MybatisPlusInterceptor mybatisPlusInterceptor(){
17 | MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
18 | mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
19 | return mybatisPlusInterceptor;
20 | }
21 |
22 | @Bean
23 | public PgVectorInterceptor pdVectorInterceptor(){
24 | return new PgVectorInterceptor();
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/bootstrap/src/main/java/com/honvay/flychat/application/config/WebMvcConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.application.config;
2 |
3 | import org.springframework.context.annotation.Configuration;
4 | import org.springframework.http.HttpMethod;
5 | import org.springframework.web.servlet.config.annotation.CorsRegistry;
6 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
7 |
8 | @Configuration
9 | public class WebMvcConfiguration implements WebMvcConfigurer {
10 |
11 | @Override
12 | public void addCorsMappings(CorsRegistry registry) {
13 | registry.addMapping("/**")
14 | .allowCredentials(true)
15 | .allowedHeaders("*")
16 | .allowedMethods(HttpMethod.GET.name(),HttpMethod.POST.name())
17 | .allowedOriginPatterns("*");
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/bootstrap/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | datasource:
3 | url: jdbc:postgresql://localhost:5432/flychat
4 | username: postgres
5 | password: postgres
6 | mybatis-plus:
7 | configuration:
8 | cache-enabled: true
9 | use-generated-keys: true
10 | default-executor-type: REUSE
11 | use-actual-param-name: true
12 | log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
13 | type-handlers-package: com.honvay.**.handler
14 | logging:
15 | level:
16 | com.honvay: debug
17 | dev.langchain4j: debug
18 | openai:
19 | apiKey: ${OPENAI_API_KEY}
--------------------------------------------------------------------------------
/bootstrap/src/main/resources/static/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Sse测试文档
5 |
6 |
7 |
8 | sse测试
9 |
10 |
11 |
12 |
30 |
--------------------------------------------------------------------------------
/bootstrap/src/main/resources/static/index2.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Sse测试文档
5 |
6 |
7 |
8 | sse测试
9 |
10 |
11 |
12 |
31 |
--------------------------------------------------------------------------------
/bootstrap/src/test/java/impl/KnowledgeBaseApplicationServiceImplTest.java:
--------------------------------------------------------------------------------
1 | package impl;
2 |
3 | import com.honvay.flychat.application.FlychatApplication;
4 | import com.honvay.flychat.knowledge.application.KnowledgeApplicationService;
5 | import com.honvay.flychat.knowledge.domain.model.KnowledgeBase;
6 | import com.honvay.flychat.knowledge.domain.model.Owner;
7 | import com.honvay.flychat.knowledge.domain.model.SplitType;
8 | import org.junit.jupiter.api.Test;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.boot.test.context.SpringBootTest;
11 |
12 | import java.io.File;
13 |
14 | @SpringBootTest(classes = FlychatApplication.class)
15 | class KnowledgeBaseApplicationServiceImplTest {
16 |
17 | @Autowired
18 | private KnowledgeApplicationService knowledgeApplicationService;
19 |
20 |
21 | @Test
22 | void create(){
23 | KnowledgeBase knowledgeBase = new KnowledgeBase();
24 | knowledgeBase.setName("测试知识库");
25 | Owner knowledgeOwner = new Owner();
26 | knowledgeOwner.setId(1L);
27 | knowledgeBase.setOwner(knowledgeOwner);
28 | knowledgeApplicationService.create(knowledgeBase);
29 | }
30 |
31 | @Test
32 | void addItem(){
33 | File file = new File("/Users/user/workspace/ChatGPT/flychat/chat/src/test/resources/story-about-happy-carrot.pdf");
34 | KnowledgeBase knowledgeBase = new KnowledgeBase();
35 | knowledgeBase.setId(2L);
36 | knowledgeApplicationService.addFile(knowledgeBase,file, SplitType.SENTENCE);
37 | }
38 |
39 | }
--------------------------------------------------------------------------------
/bootstrap/src/test/java/impl/KnowledgeChatServiceImplTest.java:
--------------------------------------------------------------------------------
1 | package impl;
2 |
3 | import com.honvay.flychat.application.FlychatApplication;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest(classes = FlychatApplication.class)
7 | class KnowledgeChatServiceImplTest {
8 |
9 | /* @Autowired
10 | private KnowledgeChatService knowledgeChatService;
11 |
12 | @Test
13 | void chat(){
14 | String chat = knowledgeChatService.chat("Who is Charlie? Answer in 10 words.", Collections.singletonList(1L),0.8f,5);
15 | System.out.println(chat);
16 | }
17 | */
18 | }
--------------------------------------------------------------------------------
/chat/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.honvay
8 | flychat
9 | 0.0.1-SNAPSHOT
10 |
11 |
12 | chat
13 |
14 |
15 | 17
16 | 17
17 | UTF-8
18 |
19 |
20 |
21 |
22 | org.projectlombok
23 | lombok
24 |
25 |
26 | com.baomidou
27 | mybatis-plus-boot-starter
28 | 3.5.3.1
29 |
30 |
31 | com.honvay
32 | framework-web
33 | 0.0.1-SNAPSHOT
34 |
35 |
36 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/application/ChatApplicationService.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.application;
2 |
3 | import com.honvay.flychat.chat.domain.model.Chat;
4 |
5 | public interface ChatApplicationService{
6 |
7 | void create(Chat chat);
8 |
9 | void saveMessages(Chat chat);
10 | }
11 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/application/impl/ChatApplicationServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.application.impl;
2 |
3 | import com.honvay.flychat.chat.application.ChatApplicationService;
4 | import com.honvay.flychat.chat.domain.model.Chat;
5 | import com.honvay.flychat.chat.domain.repository.ChatRepository;
6 | import com.honvay.flychat.chat.domain.service.impl.ChatDomainService;
7 | import org.springframework.stereotype.Service;
8 |
9 | @Service
10 | public class ChatApplicationServiceImpl implements ChatApplicationService {
11 |
12 | private final ChatRepository chatRepository;
13 |
14 | public ChatApplicationServiceImpl(ChatRepository chatRepository) {
15 | this.chatRepository = chatRepository;
16 | }
17 |
18 | @Override
19 | public void create(Chat chat){
20 | this.chatRepository.create(chat);
21 | }
22 |
23 | @Override
24 | public void saveMessages(Chat chat){
25 | this.chatRepository.saveMessages(chat);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/domain/model/Chat.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.domain.model;
2 |
3 | import lombok.Data;
4 |
5 | import java.util.ArrayList;
6 | import java.util.Date;
7 | import java.util.List;
8 |
9 | @Data
10 | public class Chat {
11 |
12 | private Long id;
13 |
14 | private String name;
15 |
16 | private User user;
17 |
18 | private List messages;
19 |
20 | private Date createTime;
21 |
22 | public void addMessage(ChatMessage message){
23 | if(this.messages == null){
24 | this.messages = new ArrayList<>();
25 | }
26 | this.messages.add(message);
27 | }
28 |
29 | public boolean isExists(){
30 | return this.id != null;
31 | }
32 |
33 | public void create(){
34 | this.createTime = new Date();
35 | }
36 |
37 | public Long getApplicationId(){
38 | return null;
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/domain/model/ChatMessage.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.domain.model;
2 |
3 | import lombok.Data;
4 |
5 | import java.math.BigDecimal;
6 | import java.util.Date;
7 | import java.util.List;
8 |
9 | @Data
10 | public class ChatMessage {
11 |
12 | private Long id;
13 |
14 | private String conversationId;
15 |
16 | private String content;
17 |
18 | private String prompt;
19 |
20 | private String role;
21 |
22 | private List quotes;
23 |
24 | private Integer tokenSize;
25 |
26 | private BigDecimal cost;
27 |
28 | private Date createTime;
29 |
30 | public boolean hasQuotes() {
31 | return this.quotes != null && this.quotes.size() > 0;
32 | }
33 |
34 | public static ChatMessage ofUser(String conversationId, String content, String prompt, Integer tokenSize) {
35 | ChatMessage chatMessage = ChatMessage.of(conversationId, content, prompt, null, tokenSize);
36 | chatMessage.setRole("user");
37 | return chatMessage;
38 | }
39 |
40 | public static ChatMessage ofAi(String conversationId, String content, List quotes, Integer tokenSize) {
41 | ChatMessage chatMessage = ChatMessage.of(conversationId, content, null, quotes, tokenSize);
42 | chatMessage.setRole("ai");
43 | return chatMessage;
44 | }
45 |
46 | private static ChatMessage of(String conversationId, String content, String prompt, List quotes, Integer tokenSize) {
47 | ChatMessage chatMessage = new ChatMessage();
48 | chatMessage.setConversationId(conversationId);
49 | chatMessage.setQuotes(quotes);
50 | chatMessage.setContent(content);
51 | chatMessage.setTokenSize(tokenSize);
52 | chatMessage.setPrompt(prompt);
53 | return chatMessage;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/domain/model/ChatQuote.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.domain.model;
2 |
3 | import lombok.Data;
4 |
5 | @Data
6 | public class ChatQuote {
7 |
8 | private Double similarity;
9 |
10 | private Long sourceId;
11 |
12 | private String segment;
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/domain/model/User.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.domain.model;
2 |
3 | import lombok.Data;
4 |
5 | @Data
6 | public class User {
7 |
8 | private Long id;
9 |
10 | private String name;
11 |
12 | private String avatar;
13 |
14 | }
15 |
16 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/domain/repository/ChatRepository.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.domain.repository;
2 |
3 |
4 | import com.honvay.flychat.chat.domain.model.Chat;
5 | import com.honvay.flychat.chat.domain.model.ChatMessage;
6 |
7 | import java.util.List;
8 |
9 | public interface ChatRepository {
10 |
11 | void create(Chat knowledgeChat);
12 |
13 | List findMessage(Chat chat, int start, int size);
14 |
15 | void saveMessages(Chat knowledgeChat);
16 |
17 | List find();
18 |
19 | List findMessage(Chat chat);
20 | }
21 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/domain/service/ChatDomainServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.domain.service;
2 |
3 | import com.honvay.flychat.chat.domain.model.Chat;
4 | import com.honvay.flychat.chat.domain.repository.ChatRepository;
5 | import com.honvay.flychat.chat.domain.service.impl.ChatDomainService;
6 | import org.springframework.stereotype.Service;
7 |
8 | @Service
9 | public class ChatDomainServiceImpl implements ChatDomainService {
10 |
11 | private final ChatRepository chatRepository;
12 |
13 | public ChatDomainServiceImpl(ChatRepository chatRepository) {
14 | this.chatRepository = chatRepository;
15 | }
16 |
17 | @Override
18 | public void create(Chat chat){
19 | this.chatRepository.create(chat);
20 | }
21 |
22 | @Override
23 | public void saveMessages(Chat chat){
24 | this.chatRepository.saveMessages(chat);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/domain/service/impl/ChatDomainService.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.domain.service.impl;
2 |
3 |
4 | import com.honvay.flychat.chat.domain.model.Chat;
5 |
6 | public interface ChatDomainService {
7 | void create(Chat chat);
8 |
9 | void saveMessages(Chat chat);
10 | }
11 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/infra/mapper/ChatMapper.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.infra.mapper;
2 |
3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 | import com.honvay.flychat.chat.infra.po.ChatPo;
5 | import org.apache.ibatis.annotations.Mapper;
6 |
7 | @Mapper
8 | public interface ChatMapper extends BaseMapper {
9 | }
10 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/infra/mapper/ChatMessageMapper.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.infra.mapper;
2 |
3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 | import com.honvay.flychat.chat.infra.po.ChatMessagePo;
5 | import org.apache.ibatis.annotations.Mapper;
6 |
7 | @Mapper
8 | public interface ChatMessageMapper extends BaseMapper {
9 | }
10 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/infra/mapper/ChatQuoteMapper.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.infra.mapper;
2 |
3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 | import com.honvay.flychat.chat.infra.po.ChatQuotePo;
5 | import org.apache.ibatis.annotations.Mapper;
6 |
7 | @Mapper
8 | public interface ChatQuoteMapper extends BaseMapper {
9 | }
10 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/infra/po/ChatMessagePo.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.infra.po;
2 |
3 | import com.baomidou.mybatisplus.annotation.IdType;
4 | import com.baomidou.mybatisplus.annotation.TableId;
5 | import com.baomidou.mybatisplus.annotation.TableName;
6 | import lombok.Data;
7 |
8 | import java.math.BigDecimal;
9 | import java.util.Date;
10 |
11 | @Data
12 | @TableName("chat_message")
13 | public class ChatMessagePo {
14 |
15 | @TableId(type = IdType.AUTO)
16 | private Long id;
17 |
18 | private String conversationId;
19 |
20 | private Long chatId;
21 |
22 | private String content;
23 |
24 | private String prompt;
25 |
26 | private String role;
27 |
28 | private Integer tokenSize;
29 |
30 | private BigDecimal cost;
31 |
32 | private Date createTime;
33 | }
34 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/infra/po/ChatPo.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.infra.po;
2 |
3 |
4 | import com.baomidou.mybatisplus.annotation.IdType;
5 | import com.baomidou.mybatisplus.annotation.TableId;
6 | import com.baomidou.mybatisplus.annotation.TableName;
7 | import lombok.Data;
8 |
9 | import java.util.Date;
10 |
11 | @Data
12 | @TableName("chat")
13 | public class ChatPo {
14 |
15 | @TableId(type = IdType.AUTO)
16 | private Long id;
17 |
18 | private String name;
19 |
20 | private Long applicationId;
21 |
22 | private Long userId;
23 |
24 | private Date createTime;
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/infra/po/ChatQuotePo.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.infra.po;
2 |
3 | import com.baomidou.mybatisplus.annotation.IdType;
4 | import com.baomidou.mybatisplus.annotation.TableId;
5 | import com.baomidou.mybatisplus.annotation.TableName;
6 | import lombok.Data;
7 |
8 | @Data
9 | @TableName("chat_message_relevant")
10 | public class ChatQuotePo {
11 |
12 | @TableId(type = IdType.AUTO)
13 | private Long id;
14 |
15 | private Long messageId;
16 |
17 | private String segment;
18 |
19 | private Double similarity;
20 |
21 | private Long sourceId;
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/web/ChatController.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.web;
2 |
3 | import com.honvay.cola.framework.web.ResponsePayload;
4 | import com.honvay.flychat.chat.domain.model.Chat;
5 | import com.honvay.flychat.chat.domain.model.ChatMessage;
6 | import com.honvay.flychat.chat.domain.repository.ChatRepository;
7 | import com.honvay.flychat.chat.web.model.ChatMessageVo;
8 | import com.honvay.flychat.chat.web.model.ChatVo;
9 | import org.springframework.web.bind.annotation.GetMapping;
10 | import org.springframework.web.bind.annotation.PathVariable;
11 | import org.springframework.web.bind.annotation.RequestMapping;
12 | import org.springframework.web.bind.annotation.RestController;
13 |
14 | import java.util.List;
15 |
16 | @ResponsePayload
17 | @RestController
18 | @RequestMapping("/chat")
19 | public class ChatController {
20 |
21 | private final ChatRepository chatRepository;
22 |
23 | public ChatController(ChatRepository chatRepository) {
24 | this.chatRepository = chatRepository;
25 | }
26 |
27 | @GetMapping
28 | public List list(){
29 | List chats = this.chatRepository.find();
30 | return chats.stream()
31 | .map(ChatVo::from)
32 | .toList();
33 | }
34 |
35 | @GetMapping("/{id}/messages")
36 | public List findMessage(@PathVariable Long id){
37 | Chat chat = new Chat();
38 | chat.setId(id);
39 | List chatMessages = this.chatRepository.findMessage(chat);
40 | return chatMessages.stream()
41 | .map(ChatMessageVo::from)
42 | .toList();
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/web/model/ChatMessageVo.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.web.model;
2 |
3 | import com.honvay.flychat.chat.domain.model.ChatMessage;
4 | import lombok.Data;
5 |
6 | @Data
7 | public class ChatMessageVo {
8 |
9 | private Long id;
10 |
11 | private String content;
12 |
13 | private Long createTime;
14 |
15 | private String role;
16 |
17 | public static ChatMessageVo from(ChatMessage chatMessage){
18 | ChatMessageVo chatMessageVo = new ChatMessageVo();
19 | chatMessageVo.setId(chatMessage.getId());
20 | chatMessageVo.setContent(chatMessage.getContent());
21 | chatMessageVo.setCreateTime(chatMessage.getCreateTime().getTime());
22 | chatMessageVo.setRole(chatMessage.getRole());
23 | return chatMessageVo;
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/chat/src/main/java/com/honvay/flychat/chat/web/model/ChatVo.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.chat.web.model;
2 |
3 | import com.honvay.flychat.chat.domain.model.Chat;
4 | import lombok.Data;
5 |
6 | import java.util.Date;
7 |
8 | @Data
9 | public class ChatVo {
10 |
11 | private Long id;
12 |
13 | private String name;
14 |
15 | private Date createTime;
16 |
17 | public static ChatVo from(Chat chat){
18 | ChatVo chatVo = new ChatVo();
19 | chatVo.setId(chat.getId());
20 | chatVo.setName(chat.getName());
21 | chatVo.setCreateTime(chat.getCreateTime());
22 | return chatVo;
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/conversation/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.honvay
8 | flychat
9 | 0.0.1-SNAPSHOT
10 |
11 |
12 | conversation
13 |
14 |
15 | 17
16 | 17
17 | UTF-8
18 |
19 |
20 |
21 |
22 | com.honvay
23 | chat
24 | 0.0.1-SNAPSHOT
25 |
26 |
27 | com.honvay
28 | langchain
29 | 0.0.1-SNAPSHOT
30 |
31 |
32 | com.honvay
33 | knowledge
34 | 0.0.1-SNAPSHOT
35 |
36 |
37 | org.apache.commons
38 | commons-lang3
39 |
40 |
41 | com.github.javafaker
42 | javafaker
43 | 1.0.2
44 |
45 |
46 | snakeyaml
47 | org.yaml
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/conversation/src/main/java/com/honvay/flychat/conversation/application/ConversationApplicationService.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.conversation.application;
2 |
3 | import com.honvay.flychat.conversation.domain.Conversation;
4 |
5 | import java.util.function.Consumer;
6 |
7 | public interface ConversationApplicationService {
8 | void converse(Conversation conversation);
9 |
10 | void converse(Conversation conversation,
11 | Consumer onResult,
12 | Consumer onComplete,
13 | Consumer onError);
14 | }
15 |
--------------------------------------------------------------------------------
/conversation/src/main/java/com/honvay/flychat/conversation/application/ConversationModel.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.conversation.application;
2 |
3 | import com.honvay.flychat.langchain.chat.ChatSetup;
4 | import lombok.Data;
5 |
6 | @Data
7 | public class ConversationModel {
8 |
9 | private String modelName;
10 |
11 | private Double temperature;
12 |
13 | private Integer maxTokenSize;
14 |
15 | public ChatSetup toModelSetup(){
16 | ChatSetup chatSetup = new ChatSetup();
17 | chatSetup.setModelName(this.modelName);
18 | chatSetup.setTemperature(this.temperature);
19 | return chatSetup;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/conversation/src/main/java/com/honvay/flychat/conversation/controller/ConversationVo.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.conversation.controller;
2 |
3 | import com.honvay.flychat.chat.domain.model.Chat;
4 | import com.honvay.flychat.chat.web.model.ChatVo;
5 | import com.honvay.flychat.conversation.domain.Conversation;
6 | import lombok.Data;
7 |
8 | @Data
9 | public class ConversationVo {
10 |
11 | private String id;
12 |
13 | public ChatVo chat;
14 |
15 | private String message;
16 |
17 | public static ConversationVo from(Conversation conversation){
18 | ConversationVo conversationVo = new ConversationVo();
19 | conversationVo.setMessage(conversation.getResult());
20 | Chat chat = conversation.getChat();
21 | conversationVo.setChat(ChatVo.from(chat));
22 | conversationVo.setId(conversation.getId());
23 | return conversationVo;
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/conversation/src/main/java/com/honvay/flychat/conversation/domain/Knowledge.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.conversation.domain;
2 |
3 | import com.honvay.flychat.chat.domain.model.ChatQuote;
4 | import lombok.Data;
5 |
6 | import java.util.List;
7 |
8 | @Data
9 | public class Knowledge {
10 |
11 | /**
12 | * 知识库列表
13 | */
14 | private List knowledgeBases;
15 |
16 | /**
17 | * 相似度
18 | */
19 | private Double similarity;
20 |
21 | /**
22 | * 关联数量
23 | */
24 | private Integer relevantSize;
25 |
26 | /**
27 | * 引用
28 | */
29 | private List quotes;
30 |
31 | private boolean blockOnEmpty;
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/conversation/src/main/java/com/honvay/flychat/conversation/domain/Model.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.conversation.domain;
2 |
3 | import com.honvay.flychat.conversation.types.ChatModel;
4 | import com.honvay.flychat.langchain.chat.ChatSetup;
5 | import lombok.Data;
6 |
7 | @Data
8 | public class Model {
9 |
10 | private String apiKey;
11 |
12 | private String modelName;
13 |
14 | private Double temperature;
15 |
16 | private Integer maxTokens;
17 |
18 | private Double topP;
19 |
20 | private Double presencePenalty;
21 |
22 | private Double frequencyPenalty;
23 |
24 | public void init(){
25 | if(modelName == null){
26 | this.modelName = ChatModel.GPT_35_TURBO.getModelName();
27 | this.maxTokens = ChatModel.GPT_35_TURBO.getMaxTokens();
28 | }
29 | }
30 |
31 | public ChatSetup toModelSetup(){
32 | ChatSetup chatSetup = new ChatSetup();
33 | chatSetup.setModelName(this.modelName);
34 | chatSetup.setTemperature(this.temperature);
35 | chatSetup.setApiKey(this.apiKey);
36 | chatSetup.setMaxTokens(this.maxTokens);
37 | chatSetup.setTopP(this.topP);
38 | chatSetup.setPresencePenalty(this.presencePenalty);
39 | chatSetup.setFrequencyPenalty(this.frequencyPenalty);
40 | return chatSetup;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/conversation/src/main/java/com/honvay/flychat/conversation/domain/Relation.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.conversation.domain;
2 |
3 | import lombok.Data;
4 |
5 | /**
6 | * 对话上下文
7 | */
8 | @Data
9 | public class Relation {
10 |
11 | /**
12 | * 历史消息数量
13 | */
14 | private Integer size;
15 |
16 | /**
17 | * 是否为清洁模式,不关联历史消息
18 | */
19 | private boolean clean;
20 | }
21 |
--------------------------------------------------------------------------------
/conversation/src/main/java/com/honvay/flychat/conversation/infra/MessageProvider.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.conversation.infra;
2 |
3 | import com.honvay.flychat.chat.domain.model.Chat;
4 | import com.honvay.flychat.chat.domain.model.ChatMessage;
5 | import com.honvay.flychat.conversation.domain.Conversation;
6 | import org.jetbrains.annotations.NotNull;
7 |
8 | import java.util.List;
9 |
10 | public interface MessageProvider {
11 |
12 | String getPrompt(Conversation conversation);
13 |
14 | @NotNull
15 | List getRelationMessages(Conversation conversation, Chat chat);
16 |
17 | String getName();
18 | }
19 |
--------------------------------------------------------------------------------
/conversation/src/main/java/com/honvay/flychat/conversation/infra/MessageProviderDelegator.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.conversation.infra;
2 |
3 | import com.honvay.flychat.chat.domain.model.Chat;
4 | import com.honvay.flychat.chat.domain.model.ChatMessage;
5 | import com.honvay.flychat.conversation.domain.Conversation;
6 | import org.apache.commons.collections4.CollectionUtils;
7 | import org.springframework.stereotype.Component;
8 |
9 | import java.util.List;
10 | import java.util.Map;
11 | import java.util.stream.Collectors;
12 |
13 | @Component
14 | public class MessageProviderDelegator {
15 |
16 | private final Map providers;
17 |
18 | public MessageProviderDelegator(List providers) {
19 | this.providers = providers.stream()
20 | .collect(Collectors.toMap(MessageProvider::getName,
21 | provider -> provider));
22 | }
23 |
24 | public String getPrompt(Conversation conversation){
25 | return this.getMessageProvider(conversation).getPrompt(conversation);
26 | }
27 |
28 | public List getRelation(Conversation conversation, Chat chat){
29 | return this.getMessageProvider(conversation).getRelationMessages(conversation,chat);
30 | }
31 |
32 | private MessageProvider getMessageProvider(Conversation conversation) {
33 | return CollectionUtils.isNotEmpty(conversation.getKnowledge().getKnowledgeBases()) ? providers.get("knowledge") : providers.get("conversation");
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/conversation/src/main/java/com/honvay/flychat/conversation/types/ChatModel.java:
--------------------------------------------------------------------------------
1 | package com.honvay.flychat.conversation.types;
2 |
3 | import lombok.Getter;
4 |
5 | public enum ChatModel {
6 | GPT_35_TURBO("gpt-3.5-turbo",4069),
7 | GPT_35_TURBO_0613("gpt-3.5-turbo-0613",4069),
8 | GPT_35_TURBO_16K("gpt-3.5-turbo-16k",16384),
9 | GPT_35_TURBO_16K_0613("gpt-3.5-turbo-16k-0613",16384),
10 | GPT_4("gpt-4",8192),
11 | GPT_4_0613("gpt-4-0613",8192),
12 | GPT_4_32k("gpt-4-32k",8192),
13 | GPT_4_32k_0613("gpt-4-32k-0613",8192);
14 |
15 | @Getter
16 | private final String modelName;
17 |
18 | @Getter
19 | private final Integer maxTokens;
20 |
21 |
22 | ChatModel(String modelName, Integer maxTokens) {
23 | this.modelName = modelName;
24 | this.maxTokens = maxTokens;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/framework/framework-core/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.honvay
8 | framework
9 | 0.0.1-SNAPSHOT
10 |
11 |
12 | framework-core
13 |
14 |
15 | 17
16 | 17
17 | UTF-8
18 |
19 |
20 |
--------------------------------------------------------------------------------
/framework/framework-core/src/main/java/com/honvay/cola/framework/core/ErrorConstants.java:
--------------------------------------------------------------------------------
1 | package com.honvay.cola.framework.core;
2 |
3 | /**
4 | * @author LIQIU
5 | * @date 2018-1-12
6 | **/
7 | public class ErrorConstants implements ErrorMessage {
8 |
9 |
10 | // 1xx Informational
11 |
12 | /**
13 | * 系统内部错误
14 | */
15 | public static final ErrorConstants INTERNAL_SERVER_ERROR = new ErrorConstants("10000", "系统错误");
16 | /**
17 | * 参数错误
18 | */
19 | public static final ErrorConstants ILLEGAL_ARGUMENT = new ErrorConstants("10001", "参数错误");
20 | /**
21 | * 业务错误
22 | */
23 | public static final ErrorConstants SERVICE_EXCEPTION = new ErrorConstants("10002", "业务错误");
24 | /**
25 | * 非法的数据格式,参数没有经过校验
26 | */
27 | public static final ErrorConstants ILLEGAL_DATA = new ErrorConstants("10003", "数据错误");
28 | /**
29 | * 非法状态
30 | */
31 | public static final ErrorConstants ILLEGAL_STATE = new ErrorConstants("10005", "非法状态");
32 | /**
33 | * 缺少参数
34 | */
35 | public static final ErrorConstants MISSING_ARGUMENT = new ErrorConstants("10006", "缺少参数");
36 | /**
37 | * 非法访问
38 | */
39 | public static final ErrorConstants ACCESS_DEFINED = new ErrorConstants("10007", "非法访问,没有认证");
40 | /**
41 | * 权限不足
42 | */
43 | public static final ErrorConstants UNAUTHORIZED = new ErrorConstants("10008", "权限不足");
44 |
45 | /**
46 | * 错误的请求
47 | */
48 | public static final ErrorConstants METHOD_NOT_ALLOWED = ErrorConstants.of("10009", "不支持的方法");
49 |
50 | /**
51 | * 参数错误
52 | */
53 | public static final ErrorConstants ILLEGAL_ARGUMENT_TYPE = ErrorConstants.of("10010", "参数类型错误");
54 |
55 |
56 | private final String code;
57 |
58 | private final String message;
59 |
60 |
61 | ErrorConstants(String value, String message) {
62 | this.code = value;
63 | this.message = message;
64 | }
65 |
66 | public static ErrorConstants of(String code, String message) {
67 | return new ErrorConstants(code, message);
68 | }
69 |
70 |
71 | @Override
72 | public String getCode() {
73 | return this.code;
74 | }
75 |
76 | /**
77 | * Return the reason phrase of this status credential.
78 | */
79 | @Override
80 | public String getMessage() {
81 | return this.message;
82 | }
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/framework/framework-core/src/main/java/com/honvay/cola/framework/core/ErrorMessage.java:
--------------------------------------------------------------------------------
1 | package com.honvay.cola.framework.core;
2 |
3 | /**
4 | * @author LIQIU
5 | * created on 2018/12/24
6 | **/
7 | public interface ErrorMessage {
8 |
9 | /**
10 | * 获取错误码
11 | *
12 | * @return 错误码
13 | */
14 | String getCode();
15 |
16 | /**
17 | * 获取错误信息
18 | *
19 | * @return 错误信息
20 | */
21 | String getMessage();
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/framework/framework-core/src/main/java/com/honvay/cola/framework/core/enums/GeneralEnum.java:
--------------------------------------------------------------------------------
1 | package com.honvay.cola.framework.core.enums;
2 |
3 | public interface GeneralEnum {
4 |
5 | T getCode();
6 | String getName();
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/framework/framework-core/src/main/java/com/honvay/cola/framework/core/exception/ServiceException.java:
--------------------------------------------------------------------------------
1 | package com.honvay.cola.framework.core.exception;
2 |
3 |
4 | import com.honvay.cola.framework.core.ErrorMessage;
5 |
6 | /**
7 | * @author LIQIU
8 | * created on 2018/12/24
9 | **/
10 | public class ServiceException extends RuntimeException {
11 |
12 | private String code;
13 |
14 | public ServiceException(String code, String message, Throwable throwable) {
15 | super(message, throwable);
16 | this.code = code;
17 | }
18 |
19 | public ServiceException(String code, String message) {
20 | this(code, message, null);
21 | }
22 |
23 | public ServiceException(String message) {
24 | this("0", message, null);
25 | }
26 |
27 | public ServiceException(ErrorMessage errorMessage) {
28 | this(errorMessage.getCode(), errorMessage.getMessage(), null);
29 | }
30 |
31 | public ServiceException(ErrorMessage errorMessage, Throwable throwable) {
32 | this(errorMessage.getCode(), errorMessage.getMessage(), throwable);
33 | }
34 |
35 | public ServiceException(ErrorMessage errorMessage, String message) {
36 | this(errorMessage.getCode(), message, null);
37 | }
38 |
39 | public ServiceException(ErrorMessage errorMessage, String message, Throwable throwable) {
40 | this(errorMessage.getCode(), message, throwable);
41 | }
42 |
43 | public String getCode() {
44 | return code;
45 | }
46 |
47 | public void setCode(String code) {
48 | this.code = code;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/framework/framework-web/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.honvay
8 | framework
9 | 0.0.1-SNAPSHOT
10 |
11 |
12 | framework-web
13 |
14 |
15 | 17
16 | 17
17 | UTF-8
18 |
19 |
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter-web
24 |
25 |
26 | com.honvay
27 | framework-core
28 |
29 |
30 |
--------------------------------------------------------------------------------
/framework/framework-web/src/main/java/com/honvay/cola/framework/web/ResponsePayload.java:
--------------------------------------------------------------------------------
1 | package com.honvay.cola.framework.web;
2 |
3 |
4 | import java.lang.annotation.*;
5 |
6 | /**
7 | * @author LIQIU
8 | * created on 2020/9/15
9 | **/
10 | @Target({ElementType.METHOD,ElementType.TYPE})
11 | @Retention(RetentionPolicy.RUNTIME)
12 | @Documented
13 | public @interface ResponsePayload {
14 | }
15 |
--------------------------------------------------------------------------------
/framework/framework-web/src/main/java/com/honvay/cola/framework/web/ResponsePayloadAdvise.java:
--------------------------------------------------------------------------------
1 | package com.honvay.cola.framework.web;
2 |
3 | import com.fasterxml.jackson.core.JsonProcessingException;
4 | import com.fasterxml.jackson.databind.ObjectMapper;
5 | import com.honvay.cola.framework.core.protocol.Response;
6 | import jakarta.annotation.Resource;
7 | import org.springframework.core.MethodParameter;
8 | import org.springframework.http.MediaType;
9 | import org.springframework.http.converter.HttpMessageConverter;
10 | import org.springframework.http.server.ServerHttpRequest;
11 | import org.springframework.http.server.ServerHttpResponse;
12 | import org.springframework.web.bind.annotation.RestControllerAdvice;
13 | import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
14 |
15 |
16 | /**
17 | * @author LIQIU
18 | * created on 2019/1/14
19 | **/
20 | @RestControllerAdvice
21 | public class ResponsePayloadAdvise implements ResponseBodyAdvice