chATLAS_Chains 0.1.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chatlas_chains-0.1.3/LICENSE +201 -0
- chatlas_chains-0.1.3/PKG-INFO +113 -0
- chatlas_chains-0.1.3/README.md +92 -0
- chatlas_chains-0.1.3/benchmark/basic.py +34 -0
- chatlas_chains-0.1.3/benchmark/conversational.py +0 -0
- chatlas_chains-0.1.3/chATLAS_Chains/__init__.py +0 -0
- chatlas_chains-0.1.3/chATLAS_Chains/chains/__init__.py +0 -0
- chatlas_chains-0.1.3/chATLAS_Chains/chains/basic.py +42 -0
- chatlas_chains-0.1.3/chATLAS_Chains/chains/basic_graph.py +163 -0
- chatlas_chains-0.1.3/chATLAS_Chains/chains/conversational.py +0 -0
- chatlas_chains-0.1.3/chATLAS_Chains/chains/websearch_retrieval_chain.py +447 -0
- chatlas_chains-0.1.3/chATLAS_Chains/llm/__init__.py +0 -0
- chatlas_chains-0.1.3/chATLAS_Chains/llm/model_selection.py +47 -0
- chatlas_chains-0.1.3/chATLAS_Chains/log.py +20 -0
- chatlas_chains-0.1.3/chATLAS_Chains/prompt/__init__.py +0 -0
- chatlas_chains-0.1.3/chATLAS_Chains/prompt/doc_joiners.py +5 -0
- chatlas_chains-0.1.3/chATLAS_Chains/prompt/starters.py +151 -0
- chatlas_chains-0.1.3/chATLAS_Chains/search/__init__.py +0 -0
- chatlas_chains-0.1.3/chATLAS_Chains/search/basic.py +45 -0
- chatlas_chains-0.1.3/chATLAS_Chains/utils/__init__.py +0 -0
- chatlas_chains-0.1.3/chATLAS_Chains/utils/doc_utils.py +28 -0
- chatlas_chains-0.1.3/chATLAS_Chains/vectorstore.py +94 -0
- chatlas_chains-0.1.3/chATLAS_Chains.egg-info/PKG-INFO +113 -0
- chatlas_chains-0.1.3/chATLAS_Chains.egg-info/SOURCES.txt +33 -0
- chatlas_chains-0.1.3/chATLAS_Chains.egg-info/dependency_links.txt +1 -0
- chatlas_chains-0.1.3/chATLAS_Chains.egg-info/requires.txt +5 -0
- chatlas_chains-0.1.3/chATLAS_Chains.egg-info/top_level.txt +4 -0
- chatlas_chains-0.1.3/pyproject.toml +32 -0
- chatlas_chains-0.1.3/setup.cfg +4 -0
- chatlas_chains-0.1.3/tests/__init__.py +0 -0
- chatlas_chains-0.1.3/tests/conftest.py +274 -0
- chatlas_chains-0.1.3/tests/test_chains.py +54 -0
- chatlas_chains-0.1.3/tests/test_llm.py +81 -0
- chatlas_chains-0.1.3/tests/test_search.py +48 -0
- chatlas_chains-0.1.3/tests/test_utils.py +27 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright (C) 2023 CERN for the benefit of the ATLAS collaboration
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: chATLAS_Chains
|
|
3
|
+
Version: 0.1.3
|
|
4
|
+
Summary: A modular Python package for implementing Retrieval Augmented Generation chains for the chATLAS project.
|
|
5
|
+
Author-email: Joe Egan <joseph.caimin.egan@cern.ch>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://gitlab.cern.ch/belliot/chatlas-packages/
|
|
8
|
+
Project-URL: Documentation, https://chatlas-packages.docs.cern.ch/chATLAS_Chain/
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.11
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: chATLAS_Benchmark>=0.0.9
|
|
16
|
+
Requires-Dist: chATLAS_Embed>=0.1.14
|
|
17
|
+
Requires-Dist: langchain~=0.3.3
|
|
18
|
+
Requires-Dist: langchain_core
|
|
19
|
+
Requires-Dist: langchain_openai
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# chATLAS_Chains
|
|
24
|
+
|
|
25
|
+
This package implements and benchmarks various Retrieval Augmented Generation (RAG) chains for use in the [chATLAS](https://chatlas-flask-chatlas.app.cern.ch) project.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
conda create -n venv chatlas_chains_env python=3.10
|
|
31
|
+
conda activate chatlas_chains_env
|
|
32
|
+
pip install chatlas-chains
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Environment variables
|
|
36
|
+
|
|
37
|
+
These are required for the following use cases
|
|
38
|
+
|
|
39
|
+
1. Using an OpenAI LLM
|
|
40
|
+
```bash
|
|
41
|
+
export CHATLAS_OPENAI_KEY='your api key'
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
2. Benchmarking, set the path to the question set
|
|
45
|
+
```bash
|
|
46
|
+
export CHATLAS_BENCHMARK_QUESTIONS=/path/to/questions.josn
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Available Chains
|
|
50
|
+
- chains.basic.basic_retrieval_chain
|
|
51
|
+
- chains.basic_graph.basic_retrieval_graph
|
|
52
|
+
|
|
53
|
+
## Benchmarking
|
|
54
|
+
|
|
55
|
+
To benchmark e.g. the chains in `chATLAS_Chains.chains.basic` run this from the project root
|
|
56
|
+
```bash
|
|
57
|
+
python benchmark/basic.py
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Testing
|
|
61
|
+
|
|
62
|
+
The tests require a running postgres server to work. If on lxplus you can modify `TEST_DB_CONFIG` in [tests/conftest.py](tests/conftest.py) to connect to the chATLAS server.
|
|
63
|
+
|
|
64
|
+
If you want to create a local dummy postgres server, you need to install `psql`. This can be done on macOS using [homebrew](https://brew.sh):
|
|
65
|
+
|
|
66
|
+
Software install
|
|
67
|
+
```bash
|
|
68
|
+
brew install postgresql
|
|
69
|
+
brew services start postgresql
|
|
70
|
+
brew install pgvector
|
|
71
|
+
brew unlink pgvector && brew link pgvector
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Create a user
|
|
75
|
+
```bash
|
|
76
|
+
psql -h localhost -U postgres
|
|
77
|
+
ALTER USER postgres WITH PASSWORD 'Set_your_password_here';
|
|
78
|
+
CREATE EXTENSION IF NOT EXISTS vector;
|
|
79
|
+
```
|
|
80
|
+
## CHANGELOG
|
|
81
|
+
|
|
82
|
+
#### 0.1.3
|
|
83
|
+
|
|
84
|
+
Fixing imports
|
|
85
|
+
|
|
86
|
+
Changed output format of `basic_retrieval_chain` (`docs` key is now a list of `Document` objects, rather than a dict)
|
|
87
|
+
|
|
88
|
+
Unit tests for `basic_retrieval_chain`
|
|
89
|
+
|
|
90
|
+
#### 0.1.2
|
|
91
|
+
|
|
92
|
+
Unit tests
|
|
93
|
+
|
|
94
|
+
First Langgraph chain
|
|
95
|
+
|
|
96
|
+
#### 0.1.1
|
|
97
|
+
|
|
98
|
+
Initial Release
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
## 📄 License
|
|
102
|
+
|
|
103
|
+
chATLAS_Benchmark is released under Apache v2.0 license.
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
<div align="center">
|
|
108
|
+
|
|
109
|
+
**Made with ❤️ by the ATLAS Collaboration**
|
|
110
|
+
|
|
111
|
+
*For questions and support, please [contact](mailto:joseph.caimin.egan@cern.ch)*
|
|
112
|
+
|
|
113
|
+
</div>
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
|
|
2
|
+
# chATLAS_Chains
|
|
3
|
+
|
|
4
|
+
This package implements and benchmarks various Retrieval Augmented Generation (RAG) chains for use in the [chATLAS](https://chatlas-flask-chatlas.app.cern.ch) project.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
conda create -n venv chatlas_chains_env python=3.10
|
|
10
|
+
conda activate chatlas_chains_env
|
|
11
|
+
pip install chatlas-chains
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Environment variables
|
|
15
|
+
|
|
16
|
+
These are required for the following use cases
|
|
17
|
+
|
|
18
|
+
1. Using an OpenAI LLM
|
|
19
|
+
```bash
|
|
20
|
+
export CHATLAS_OPENAI_KEY='your api key'
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
2. Benchmarking, set the path to the question set
|
|
24
|
+
```bash
|
|
25
|
+
export CHATLAS_BENCHMARK_QUESTIONS=/path/to/questions.josn
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Available Chains
|
|
29
|
+
- chains.basic.basic_retrieval_chain
|
|
30
|
+
- chains.basic_graph.basic_retrieval_graph
|
|
31
|
+
|
|
32
|
+
## Benchmarking
|
|
33
|
+
|
|
34
|
+
To benchmark e.g. the chains in `chATLAS_Chains.chains.basic` run this from the project root
|
|
35
|
+
```bash
|
|
36
|
+
python benchmark/basic.py
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Testing
|
|
40
|
+
|
|
41
|
+
The tests require a running postgres server to work. If on lxplus you can modify `TEST_DB_CONFIG` in [tests/conftest.py](tests/conftest.py) to connect to the chATLAS server.
|
|
42
|
+
|
|
43
|
+
If you want to create a local dummy postgres server, you need to install `psql`. This can be done on macOS using [homebrew](https://brew.sh):
|
|
44
|
+
|
|
45
|
+
Software install
|
|
46
|
+
```bash
|
|
47
|
+
brew install postgresql
|
|
48
|
+
brew services start postgresql
|
|
49
|
+
brew install pgvector
|
|
50
|
+
brew unlink pgvector && brew link pgvector
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Create a user
|
|
54
|
+
```bash
|
|
55
|
+
psql -h localhost -U postgres
|
|
56
|
+
ALTER USER postgres WITH PASSWORD 'Set_your_password_here';
|
|
57
|
+
CREATE EXTENSION IF NOT EXISTS vector;
|
|
58
|
+
```
|
|
59
|
+
## CHANGELOG
|
|
60
|
+
|
|
61
|
+
#### 0.1.3
|
|
62
|
+
|
|
63
|
+
Fixing imports
|
|
64
|
+
|
|
65
|
+
Changed output format of `basic_retrieval_chain` (`docs` key is now a list of `Document` objects, rather than a dict)
|
|
66
|
+
|
|
67
|
+
Unit tests for `basic_retrieval_chain`
|
|
68
|
+
|
|
69
|
+
#### 0.1.2
|
|
70
|
+
|
|
71
|
+
Unit tests
|
|
72
|
+
|
|
73
|
+
First Langgraph chain
|
|
74
|
+
|
|
75
|
+
#### 0.1.1
|
|
76
|
+
|
|
77
|
+
Initial Release
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
## 📄 License
|
|
81
|
+
|
|
82
|
+
chATLAS_Benchmark is released under Apache v2.0 license.
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
<div align="center">
|
|
87
|
+
|
|
88
|
+
**Made with ❤️ by the ATLAS Collaboration**
|
|
89
|
+
|
|
90
|
+
*For questions and support, please [contact](mailto:joseph.caimin.egan@cern.ch)*
|
|
91
|
+
|
|
92
|
+
</div>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from tqdm import tqdm
|
|
4
|
+
|
|
5
|
+
from chATLAS_Benchmark import BenchmarkTest
|
|
6
|
+
from chATLAS_Chains.chains.basic import basic_retrieval_chain
|
|
7
|
+
from chATLAS_Chains.prompt.starters import CHAT_PROMPT_TEMPLATE
|
|
8
|
+
from chATLAS_Chains.vectorstore import vectorstore
|
|
9
|
+
|
|
10
|
+
chain = basic_retrieval_chain(prompt=CHAT_PROMPT_TEMPLATE, vectorstore=vectorstore, model_name="gpt-4o-mini")
|
|
11
|
+
|
|
12
|
+
# Initialize the test set
|
|
13
|
+
questions_path = os.getenv("CHATLAS_BENCHMARK_QUESTIONS")
|
|
14
|
+
test = BenchmarkTest(questions_path)
|
|
15
|
+
# test = BenchmarkTest(questions_path, keys={"test_questions": "question", "test_documents":"documents", "test_answer": "answer"})
|
|
16
|
+
|
|
17
|
+
# --- Run the RAG on the questions ---
|
|
18
|
+
# Assuming RAG.run() returns an answer and list of docs for each question
|
|
19
|
+
gen_answers = []
|
|
20
|
+
gen_docs = []
|
|
21
|
+
for q in tqdm(test.questions):
|
|
22
|
+
result = chain.invoke(q)
|
|
23
|
+
|
|
24
|
+
gen_answers.append(result["answer"].content)
|
|
25
|
+
gen_docs.append(result["docs"])
|
|
26
|
+
|
|
27
|
+
# Set generated answers and documents on the test instance
|
|
28
|
+
test.set_generated_data(gen_answers, gen_docs)
|
|
29
|
+
|
|
30
|
+
# Run the scoring with any metrics you want
|
|
31
|
+
scores = test.score_test_set("LexicalMetrics", "SemanticSimilarity", "DocumentMatch")
|
|
32
|
+
|
|
33
|
+
# Save the results to the db
|
|
34
|
+
test.store_results(scores, db_name="results.db", name="basic_retrieval_chain")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from operator import itemgetter
|
|
2
|
+
|
|
3
|
+
from langchain_core.prompts import ChatPromptTemplate
|
|
4
|
+
from langchain_core.runnables import RunnableParallel
|
|
5
|
+
|
|
6
|
+
from chATLAS_Chains.llm.model_selection import get_chat_model
|
|
7
|
+
from chATLAS_Chains.search.basic import search_runnable
|
|
8
|
+
from chATLAS_Chains.utils.doc_utils import combine_documents
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def basic_retrieval_chain(prompt: str, vectorstore, model_name: str) -> RunnableParallel:
|
|
12
|
+
"""
|
|
13
|
+
Baseline RAG retrieval chain. Searches one or several vectorstores in parallel, passes retrieved documents to the model
|
|
14
|
+
|
|
15
|
+
:param prompt: The prompt template to use with the model.
|
|
16
|
+
:type prompt: str
|
|
17
|
+
:param vectorstore: The vectorstore or list of vectorstores to search over.
|
|
18
|
+
:type vectorstore: Any
|
|
19
|
+
:param model_name: The name of the chat model to use for generating responses.
|
|
20
|
+
:type model_name: str
|
|
21
|
+
|
|
22
|
+
:return: A LangChain RunnableParallel chain that performs retrieval and response generation.
|
|
23
|
+
:rtype: RunnableParallel
|
|
24
|
+
|
|
25
|
+
"""
|
|
26
|
+
prompt = ChatPromptTemplate.from_template(prompt)
|
|
27
|
+
model = get_chat_model(model_name)
|
|
28
|
+
|
|
29
|
+
search = search_runnable(vectorstore)
|
|
30
|
+
|
|
31
|
+
final_inputs = {
|
|
32
|
+
"context": lambda x: combine_documents(x["docs"]),
|
|
33
|
+
"question": itemgetter("question"),
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
answer = {
|
|
37
|
+
"answer": final_inputs | prompt | model,
|
|
38
|
+
"docs": lambda x: x["docs"],
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
chain = search | answer
|
|
42
|
+
return chain
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Example graph for running langgraph with this general setup and the postgres vector stores.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import TypedDict
|
|
6
|
+
|
|
7
|
+
import langgraph.graph as lg
|
|
8
|
+
from langchain_core.documents import Document
|
|
9
|
+
from langchain_core.prompts import ChatPromptTemplate
|
|
10
|
+
from langgraph.graph import END, StateGraph
|
|
11
|
+
|
|
12
|
+
from chATLAS_Chains.llm.model_selection import get_chat_model
|
|
13
|
+
from chATLAS_Chains.utils.doc_utils import combine_documents
|
|
14
|
+
from chATLAS_Embed import LangChainVectorStore
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# Define TypedDict for the state
|
|
18
|
+
class GraphState(TypedDict, total=False):
|
|
19
|
+
question: str
|
|
20
|
+
search_kwargs: dict
|
|
21
|
+
retrieved_docs: dict[str, list[Document]]
|
|
22
|
+
merged_docs: list[Document]
|
|
23
|
+
context: str
|
|
24
|
+
answer: str
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def basic_retrieval_graph(prompt: str, vectorstore, model_name: str) -> lg.Graph:
|
|
28
|
+
"""
|
|
29
|
+
Baseline RAG retrieval graph using LangGraph. Searches one or several vectorstores,
|
|
30
|
+
passes retrieved documents to the model, and returns the final answer.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
prompt: str, the prompt to use for the model
|
|
34
|
+
vectorstore: the vectorstore(s) to search
|
|
35
|
+
model_name: str, the name of the model to use for the response
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
A LangGraph graph that can be executed for RAG
|
|
39
|
+
"""
|
|
40
|
+
# Initialize the model and prompt template
|
|
41
|
+
model = get_chat_model(model_name)
|
|
42
|
+
prompt_template = ChatPromptTemplate.from_template(prompt)
|
|
43
|
+
|
|
44
|
+
# Create a list of retrievers from the vectorstore(s)
|
|
45
|
+
if isinstance(vectorstore, list):
|
|
46
|
+
retrievers = [LangChainVectorStore(vector_store=vs) for vs in vectorstore]
|
|
47
|
+
else:
|
|
48
|
+
retrievers = [LangChainVectorStore(vector_store=vectorstore)]
|
|
49
|
+
|
|
50
|
+
# Define the retrieval function
|
|
51
|
+
def retrieve_documents(state: GraphState) -> GraphState:
|
|
52
|
+
"""Retrieve documents from all vectorstores."""
|
|
53
|
+
question = state["question"]
|
|
54
|
+
search_kwargs = state.get("search_kwargs", {}) # Get search params from state
|
|
55
|
+
docs_dict = {}
|
|
56
|
+
|
|
57
|
+
for i, retriever in enumerate(retrievers):
|
|
58
|
+
docs_dict[f"docs_{i}"] = retriever.invoke(
|
|
59
|
+
question,
|
|
60
|
+
config={"metadata": {"search_kwargs": search_kwargs}}, # Pass search params to retriever
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
"question": question,
|
|
65
|
+
"search_kwargs": search_kwargs,
|
|
66
|
+
"retrieved_docs": docs_dict,
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
# Define the document merging function
|
|
70
|
+
def merge_docs(state: GraphState) -> GraphState:
|
|
71
|
+
"""Merge all retrieved documents into a single list."""
|
|
72
|
+
docs_dict = state["retrieved_docs"]
|
|
73
|
+
all_docs = []
|
|
74
|
+
|
|
75
|
+
for i in range(len(retrievers)):
|
|
76
|
+
all_docs.extend(docs_dict[f"docs_{i}"])
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
"question": state["question"],
|
|
80
|
+
"retrieved_docs": state["retrieved_docs"],
|
|
81
|
+
"merged_docs": all_docs,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
# Define the document processing function
|
|
85
|
+
def process_docs(state: GraphState) -> GraphState:
|
|
86
|
+
"""Process the merged documents into a context string."""
|
|
87
|
+
docs = state["merged_docs"]
|
|
88
|
+
context = combine_documents(docs)
|
|
89
|
+
return {
|
|
90
|
+
"question": state["question"],
|
|
91
|
+
"retrieved_docs": state["retrieved_docs"],
|
|
92
|
+
"merged_docs": state["merged_docs"],
|
|
93
|
+
"context": context,
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
# Define the answer generation function
|
|
97
|
+
def generate_answer(state: GraphState) -> GraphState:
|
|
98
|
+
"""Generate an answer using the LLM."""
|
|
99
|
+
question = state["question"]
|
|
100
|
+
context = state["context"]
|
|
101
|
+
|
|
102
|
+
prompt_input = {"context": context, "question": question}
|
|
103
|
+
|
|
104
|
+
chain = prompt_template | model
|
|
105
|
+
response = chain.invoke(prompt_input)
|
|
106
|
+
answer = response.content
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
"question": state["question"],
|
|
110
|
+
"retrieved_docs": state["retrieved_docs"],
|
|
111
|
+
"merged_docs": state["merged_docs"],
|
|
112
|
+
"context": state["context"],
|
|
113
|
+
"answer": answer,
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
# Build the graph with the defined state schema
|
|
117
|
+
graph = StateGraph(GraphState)
|
|
118
|
+
|
|
119
|
+
# Add nodes
|
|
120
|
+
graph.add_node("retrieve", retrieve_documents)
|
|
121
|
+
graph.add_node("merge", merge_docs)
|
|
122
|
+
graph.add_node("process", process_docs)
|
|
123
|
+
graph.add_node("generate", generate_answer)
|
|
124
|
+
|
|
125
|
+
# Define the edges
|
|
126
|
+
graph.add_edge("retrieve", "merge")
|
|
127
|
+
graph.add_edge("merge", "process")
|
|
128
|
+
graph.add_edge("process", "generate")
|
|
129
|
+
graph.add_edge("generate", END)
|
|
130
|
+
|
|
131
|
+
# Set the entry point
|
|
132
|
+
graph.set_entry_point("retrieve")
|
|
133
|
+
|
|
134
|
+
# Compile the graph
|
|
135
|
+
return graph.compile()
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
if __name__ == "__main__":
|
|
139
|
+
# Example of how to run the graph correctly
|
|
140
|
+
import os
|
|
141
|
+
|
|
142
|
+
os.environ["CHATLAS_EMBEDDING_MODEL_PATH"] = "<PATH TO YOUR EMBEDDING MODEL>"
|
|
143
|
+
os.environ["CHATLAS_OPENAI_KEY"] = "YOUR OPENAI API KEY"
|
|
144
|
+
os.environ["CHATLAS_DB_PASSWORD"] = "<>"
|
|
145
|
+
|
|
146
|
+
from ..prompt.starters import CHAT_PROMPT_TEMPLATE
|
|
147
|
+
from ..vectorstore import vectorstore
|
|
148
|
+
|
|
149
|
+
graph = basic_retrieval_graph(prompt=CHAT_PROMPT_TEMPLATE, vectorstore=vectorstore, model_name="gpt-4o-mini")
|
|
150
|
+
|
|
151
|
+
ans = graph.invoke(
|
|
152
|
+
{
|
|
153
|
+
"question": "How many onions are in ATLAS",
|
|
154
|
+
"search_kwargs": {
|
|
155
|
+
"k_text": 3,
|
|
156
|
+
"k": 10,
|
|
157
|
+
"date_filter": "01-01-2010",
|
|
158
|
+
"type": ["CDS", "twiki", "Indico"],
|
|
159
|
+
},
|
|
160
|
+
}
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
print(ans)
|
|
File without changes
|