Skip to content

Commit e374baa

Browse files
committed
Refactor & update
Signed-off-by: Jael Gu <[email protected]>
1 parent f9e77fe commit e374baa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+4138
-102
lines changed

README.md

+104-47
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
**Important Notice**:
2-
3-
- This repository has been archived.
4-
Please visit [akcio2023/akcio](https://github.com/akcio2023/akcio) for updates after June 14, 2023.
5-
- To get invitation for the new repository, please send email to [jaelgu]([email protected]).
6-
7-
---
8-
91
# Akcio: Enhancing LLM-Powered ChatBot with CVP Stack
102

113
[OSSChat](https://osschat.io) |
@@ -24,34 +16,77 @@ ChatGPT has constraints due to its limited knowledge base, sometimes resulting i
2416

2517
We have built [OSSChat](https://osschat.io) as a working demonstration of the CVP stack. Now we are presenting the technology behind OSSChat in this repository with a code name of Akcio.
2618

19+
<img src='pics/osschat_web.png' width='100%' alignment='centre'>
20+
2721
With this project, you are able to build a knowledge-enhanced ChatBot using LLM service like ChatGPT.
2822
By the end, you will learn how to start a backend service using FastAPI, which provides standby APIs to support further applications. Alternatively, we show how to use Gradio to build an online demo with user interface.
2923

3024
## Overview
3125

32-
The system is built on top of LangChain Agent using vector database for semantic search and memory storage for context support.
33-
You can find more details and instructions at our [documentation](https://github.com/zilliztech/akcio/wiki).
34-
3526
<img src='pics/osschat.png' width='75%' alignment='centre'>
3627

37-
### Modules
28+
Akcio allows you to create a ChatGPT-like system with added intelligence obtained through semantic search of customized knowledge base.
29+
Instead of sending the user query directly to LLM service, our system firstly retrieves relevant information from stores by semantic search or keyword match. Then it feeds both user needs and helpful information into LLM. This allows LLM to better tailor its response to the user's needs and provide more accurate and helpful information.
30+
31+
You can find more details and instructions at our [documentation](https://github.com/zilliztech/akcio/wiki).
3832

39-
- [Agent](./src/agent)
40-
- ChatAgent: agent ensembles all modules together to build up qa system.
33+
Akcio offers two AI platforms to choose from: [Towhee](https://towhee.io) or [LangChain](https://langchain.com).
34+
It also supports different integrations of LLM service and databases:
35+
36+
| | | **Towhee** | **LangChain** |
37+
|:-----------------------:|:------------:|:------:|:-----:|
38+
| **LLM** | OpenAI |||
39+
| | Dolly |||
40+
| | Ernie |||
41+
| | MiniMax |||
42+
| | DashScope || |
43+
| | ChatGLM || |
44+
| | SkyChat || |
45+
| **Embedding** | OpenAI |||
46+
| | HuggingFace |||
47+
| **Vector Store** | Zilliz Cloud |||
48+
| | Milvus |||
49+
| **Scalar Store (Optional)** | Elastic |||
50+
| **Memory Store** | Postgresql |||
51+
| | MySQL and MariaDB || |
52+
| | SQLite || |
53+
| | Oracle || |
54+
| | Microsoft SQL Server || |
55+
| **Rerank** | MS MARCO Cross-Encoders || |
56+
57+
### Option 1: Towhee
58+
59+
The option using Towhee simplifies the process of building a system by providing [pre-defined pipelines](https://towhee.io/tasks/pipeline). These built-in pipelines require less coding and make system building much easier. If you require customization, you can either simply modify configuration or create your own pipeline with rich options of [Towhee Operators](https://towhee.io/tasks/operator).
60+
61+
- [Pipelines](./towhee_src/pipelines)
62+
- **Insert:**
63+
The insert pipeline builds a knowledge base by saving documents and corresponding data in database(s).
64+
- **Search:**
65+
The search pipeline enables the question-answering capability powered by information retrieval (semantic search and optional keyword match) and LLM service.
66+
- **Prompt:** a prompt operator prepares messages for LLM by assembling system message, chat history, and the user's query processed by template.
67+
68+
- [Memory](./towhee_src/memory):
69+
The memory storage stores chat history to support context in conversation. (available: [most SQL](./towhee_src/memory/sql.py))
70+
71+
72+
### Option 2: LangChain
73+
74+
The option using LangChain employs the use of [Agent](https://python.langchain.com/docs/modules/agents) in order to enable LLM to utilize specific tools, resulting in a greater demand for LLM's ability to comprehend tasks and make informed decisions.
75+
76+
- [Agent](./langchain_src/agent)
77+
- **ChatAgent:** agent ensembles all modules together to build up qa system.
4178
- Other agents (todo)
42-
- [LLM](./src/llm)
43-
- ChatLLM: large language model or service to generate answers (available: [OpenAI](src/llm/openai_chat.py), [Dolly](src/llm/dolly_chat.py))
44-
- [Embedding](./src/embedding/)
45-
- TextEncoder: encoder converts each text input to a vector (available: [OpenAI embedding](src/embedding/openai_embedding.py), [HuggingFace Hub](src/embedding/langchain_huggingface.py))
79+
- [LLM](./langchain_src/llm)
80+
- **ChatLLM:** large language model or service to generate answers.
81+
- [Embedding](./langchain_src/embedding/)
82+
- **TextEncoder:** encoder converts each text input to a vector.
4683
- Other encoders (todo)
47-
- [Store](./src/store)
48-
- VectorStore: vector database stores document chunks in embeddings, and performs document retrieval via semantic search. (available: [Milvus/Zilliz Cloud](src/store/vector_store/milvus.py))
49-
- ScalarStore: optional, database stores metadata for each document chunk, which supports additional information retrieval. (available: [Elastic](src/store/scalar_store/es.py))
50-
- MemoryStore: memory storage stores chat history to support context in conversation. (available: [Postgresql](src/store/memory_store/pg.py))
51-
- Other stores (todo)
52-
- [DataLoader](./src/data_loader/)
53-
- DataParser: tool loads data from given source and then splits documents into processed doc chunks.
54-
- QuestionGenerator: tool generates a list of potential questions for each document chunk.
84+
- [Store](./langchain_src/store)
85+
- **VectorStore:** vector database stores document chunks in embeddings, and performs document retrieval via semantic search.
86+
- **ScalarStore:** optional, database stores metadata for each document chunk, which supports additional information retrieval. (available: [Elastic](langchain_src/store/scalar_store/es.py))
87+
- **MemoryStore:** memory storage stores chat history to support context in conversation.
88+
- [DataLoader](./langchain_src/data_loader/)
89+
- **DataParser:** tool loads data from given source and then splits documents into processed doc chunks.
5590

5691
## Deployment
5792

@@ -63,56 +98,77 @@ You can find more details and instructions at our [documentation](https://github
6398

6499
2. Install dependencies
65100
```shell
66-
$ pip -r requirements.txt
101+
$ pip install -r requirements.txt
67102
```
68103

69104
3. Configure modules
70-
- Agent
71105

72-
It will use default agents and prompts.
73-
If you want to configure prompts or customize agent modules, refer to [agent](./src/agent) for guide.
106+
You can configure all arguments by modifying [config.py](./config.py) to set up your system with default modules.
74107

75108
- LLM
76109

77-
The default ChatAI module uses OpenAI service, which requires an [OpenAI API key](https://platform.openai.com/account/api-keys).
110+
By default, the system will use OpenAI service as the LLM option.
111+
To set your OpenAI API key without modifying the configuration file, you can pass it as environment variable.
78112

79113
```shell
80114
$ export OPENAI_API_KEY=your_keys_here
81115
```
82116

83-
If you want to customize llm modules, you can refer to [llm](./src/llm) for guide.
117+
<details>
118+
119+
<summary> Check how to <strong>SWITCH LLM</strong>. </summary>
120+
If you want to use another supported LLM service, you can change the LLM option and set up for it.
121+
Besides directly modifying the configuration file, you can also set up via environment variables.
122+
For example, to use Ernie instead of OpenAI, you need to change the option and set up Ernie API key & secret key:
123+
124+
```shell
125+
$ export LLM_OPTION=ernie
126+
$ export ERNIE_API_KEY=your_ernie_api_key
127+
$ export ERNIE_SECRET_KEY=your_ernie_secret_key
128+
```
129+
</details>
84130

85131
- Embedding
86132

87-
By default, the embedding module uses LangChain HuggingFaceEmbeddings to convert text inputs to vectors. Here are some information about the default embedding method:
133+
By default, the embedding module uses methods from [Sentence Transformers](https://www.sbert.net/) to convert text inputs to vectors. Here are some information about the default embedding method:
88134
- model: [multi-qa-mpnet-base-cos-v1](https://huggingface.co/sentence-transformers/multi-qa-mpnet-base-cos-v1)(420MB)
89135
- dim: 768
90136
- normalization: True
91137

92-
If you want to customize embedding method, you can refer to [embedding](./src/embedding) for guide.
93-
94138
- Store
95139

140+
Before getting started, all database services used for store must be running and be configured with write and create access.
141+
96142
- Vector Store: You need to prepare the service of vector database in advance. For example, you can refer to [Milvus Documents](https://milvus.io/docs) or [Zilliz Cloud](https://zilliz.com/doc/quick_start) to learn about how to start a Milvus service.
97-
- Scalar Store (Optional): This is optional, only work when `USE_SCALAR` is true in [configuration](./src/store/config.py). If this is enabled (i.e. USE_SCALAR=True), the default scalar store will use [Elastic](https://www.elastic.co/). In this case, you need to prepare the Elasticsearch service in advance.
98-
- Memory Store: You need to prepare the database for memory storage as well. By default, the memory store uses [Postgresql](https://www.postgresqltutorial.com) which requires installation.
143+
- Scalar Store (Optional): This is optional, only work when `USE_SCALAR` is true in [configuration](config.py). If this is enabled (i.e. USE_SCALAR=True), the default scalar store will use [Elastic](https://www.elastic.co/). In this case, you need to prepare the Elasticsearch service in advance.
144+
- Memory Store: You need to prepare the database for memory storage as well. By default, LangChain mode supports [Postgresql](https://www.postgresql.org/) and Towhee mode allows interaction with any database supported by [SQLAlchemy 2.0](https://docs.sqlalchemy.org/en/20/dialects/).
99145

100-
The store will use [default store configs](./src/store/config.py).
101-
You can modify the the [config file](./src/store/config.py).
102-
To set up your special connections for each database, you can also export environment variables instead of modifying:
146+
The system will use default store configs.
147+
To set up your special connections for each database, you can also export environment variables instead of modifying the configuration file.
103148

149+
For the Vector Store, set **MILVUS_URI**:
104150
```shell
105151
$ export MILVUS_URI=https://localhost:19530
106-
$ export PG_URI=postgresql://postgres:postgres@localhost/chat_history
107152
```
108153

154+
For the Memory Store, set **SQL_URI**:
155+
```shell
156+
$ export SQL_URI={database_type}://{user}:{password}@{host}/{database_name}
157+
```
158+
> LangChain mode only supports [Postgresql](https://www.postgresql.org/) as database type.
159+
109160
4. Start service
110161

111162
The main script will run a FastAPI service with default address `localhost:8900`.
112163

113-
```shell
114-
$ python main.py
115-
```
164+
- Option 1: Towhee
165+
```shell
166+
$ python main.py --towhee
167+
```
168+
- Option 2: Towhee
169+
```shell
170+
$ python main.py --langchain
171+
```
116172

117173
4. Access via browser
118174

@@ -131,15 +187,15 @@ You can find more details and instructions at our [documentation](https://github
131187

132188
## Load data
133189

134-
The `insert` function in [operations](./src/operations.py) loads project data from url(s) or file(s).
190+
The `insert` function in [operations](./langchain_src/operations.py) loads project data from url(s) or file(s).
135191

136192
There are 2 options to load project data:
137193

138194
### Option 1: Offline
139195

140196
We recommend this method, which loads data in separate steps.
141-
There is also advanced options to load document with advanced options.
142-
Refer to [offline_tools](./src/offline_tools) for instructions.
197+
There is also advanced options to load document, for example, generating and inserting potential questions for each doc chunk.
198+
Refer to [offline_tools](./offline_tools) for instructions.
143199

144200
### Option 2. Online
145201

@@ -170,5 +226,6 @@ This method is only recommended to load a small amount of data, but **not for a
170226
---
171227

172228
## Notice
229+
Akcio is a project developed by Zilliz, All rights reserved.
173230

174231
By using our project or any part of our project, you agree to the [Terms of Service](./ToS.md).

config.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import os
2+
3+
################## LLM ##################
4+
LLM_OPTION = os.getenv('LLM_OPTION', 'openai') # select your LLM service
5+
CHAT_CONFIG = {
6+
'openai': {
7+
'openai_model': 'gpt-3.5-turbo',
8+
'openai_api_key': None, # will use environment value 'OPENAI_API_KEY' if None
9+
'llm_kwargs': {
10+
'temperature': 0.8,
11+
# 'max_tokens': 200,
12+
}
13+
},
14+
'ernie': {
15+
'ernie_api_key': None, # If None, use environment value 'ERNIE_API_KEY'
16+
'ernie_secret_key': None, # If None, use environment value 'ERNIE_SECRET_KEY'
17+
'llm_kwargs': {}
18+
},
19+
'minimax': {
20+
'minimax_model': 'abab5-chat',
21+
'minimax_api_key': None, # If None, use environment value 'MINIMAX_API_KEY'
22+
'minimax_group_id': None, # If None, use environment value 'MINIMAX_GROUP_ID'
23+
'llm_kwargs': {}
24+
},
25+
'dolly': {
26+
'dolly_model': 'databricks/dolly-v2-3b',
27+
'llm_kwargs': {'device': 'auto'}
28+
},
29+
'skychat': {
30+
'skychat_api_host': None, # If None, use default value 'sky-api.singularity-ai.com'
31+
'skychat_app_key': None, # If None, use environment value 'SKYCHAT_APP_KEY'
32+
'skychat_app_secret': None # If None, use environment value 'SKYCHAT_APP_SECRET'
33+
},
34+
'dashscope': {
35+
'dashscope_model': 'qwen-plus-v1',
36+
'dashscope_api_key': None # If None, use environment value 'DASHSCOPE_API_KEY'
37+
},
38+
'chatglm':{
39+
'chatglm_model': 'chatglm_130b',
40+
'chatglm_api_key': None # If None, use environment value 'ZHIPUAI_API_KEY'
41+
}
42+
}
43+
44+
45+
################## Embedding ##################
46+
TEXTENCODER_CONFIG = {
47+
'model': 'multi-qa-mpnet-base-cos-v1',
48+
'norm': True,
49+
'dim': 768
50+
}
51+
52+
53+
################## Store ##################
54+
USE_SCALAR = os.getenv('USE_SCALAR', False)
55+
56+
# Vector db configs
57+
VECTORDB_CONFIG = {
58+
'connection_args': {
59+
'uri': os.getenv('MILVUS_URI', 'https://localhost:19530'),
60+
'user': os.getenv('MILVUS_USER', ''),
61+
'password': os.getenv('MILVUS_PASSWORD', ''),
62+
'secure': True if os.getenv('MILVUS_SECURE', 'False').lower() == 'true' else False
63+
},
64+
'top_k': 10,
65+
'threshold': 0.6,
66+
'index_params': {
67+
'metric_type': 'IP',
68+
'index_type': 'IVF_FLAT',
69+
'params': {'nlist': 1024}
70+
}
71+
}
72+
73+
# Scalar db configs
74+
SCALARDB_CONFIG = {
75+
'connection_args': {
76+
'hosts': os.getenv('ES_HOSTS', 'https://localhost:9200'),
77+
'ca_certs': os.getenv('ES_CA_CERTS', None),
78+
'basic_auth': (os.getenv('ES_USER', 'user_name'), os.getenv('ES_PASSWORD', 'es_password'))
79+
},
80+
}
81+
82+
# Memory db configs
83+
MEMORYDB_CONFIG = {
84+
'connect_str': os.getenv('SQL_URI', 'postgresql://postgres:postgres@localhost/chat_history')
85+
}
86+
87+
88+
############### Rerank configs ##################
89+
RERANK_CONFIG = {
90+
'rerank': True,
91+
'rerank_model': 'cross-encoder/ms-marco-MiniLM-L-12-v2',
92+
'threshold': 0.6
93+
}
94+
95+
################## Data loader ##################
96+
DATAPARSER_CONFIG = {
97+
'chunk_size': 300
98+
}
99+
100+
QUESTIONGENERATOR_CONFIG = {
101+
'model_name': 'gpt-3.5-turbo',
102+
'temperature': 0,
103+
}

0 commit comments

Comments
 (0)