├── .env
├── .gitignore
├── Docker-compose.yml
├── LICENSE
├── README.md
├── api.Dockerfile
├── api.py
├── chains.py
├── front-end.Dockerfile
├── front-end
├── .gitignore
├── .vscode
│ └── extensions.json
├── index.html
├── jsconfig.json
├── package-lock.json
├── package.json
├── postcss.config.js
├── public
│ └── vite.svg
├── src
│ ├── App.svelte
│ ├── app.css
│ ├── assets
│ │ ├── images
│ │ │ ├── bot.jpeg
│ │ │ ├── me.jpeg
│ │ │ └── search_icon.jpg
│ │ └── svelte.svg
│ ├── lib
│ │ ├── External.svelte
│ │ ├── MdLink.svelte
│ │ ├── Modal.svelte
│ │ ├── chat.store.js
│ │ └── generation.store.js
│ ├── main.js
│ └── vite-env.d.ts
├── svelte.config.js
├── tailwind.config.js
└── vite.config.js
├── images
└── datamodel.png
├── loader.Dockerfile
├── loader.py
├── pull_model.Dockerfile
├── requirements.txt
└── utils.py
/.env:
--------------------------------------------------------------------------------
1 | #*****************************************************************
2 | # LLM and Embedding Model
3 | #*****************************************************************
4 | LLM=llama2
5 | EMBEDDING_MODEL=sentence_transformer
6 |
7 | #*****************************************************************
8 | # Neo4j
9 | #*****************************************************************
10 | NEO4J_URI=bolt://localhost:7687
11 | NEO4J_USERNAME=neo4j
12 | NEO4J_PASSWORD=password
13 |
14 | #*****************************************************************
15 | # Langchain
16 | #*****************************************************************
17 | # Optional for enabling Langchain Smith API
18 |
19 | #LANGCHAIN_TRACING_V2=true # false
20 | #LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"
21 | #LANGCHAIN_PROJECT=#your-project-name
22 | #LANGCHAIN_API_KEY=#your-api-key ls_...
23 |
24 | #*****************************************************************
25 | # Ollama
26 | #*****************************************************************
27 | OLLAMA_BASE_URL=http://host.docker.internal:11434
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.nar
17 | *.ear
18 | *.zip
19 | *.tar.gz
20 | *.rar
21 |
22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23 | hs_err_pid*
24 | replay_pid*
25 |
--------------------------------------------------------------------------------
/Docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: '3.8'
2 |
3 | services:
4 |
5 | # Ollama service with GPU acceleration for language processing
6 | llm-gpu:
7 | image: ollama/ollama:latest
8 | deploy:
9 | resources:
10 | reservations:
11 | devices:
12 | - driver: nvidia
13 | count: all
14 | capabilities: [gpu]
15 |
16 | # Service to pull model for local processing
17 | # Environment variables set for Ollama base URL and LLM model
18 | pull-model:
19 | image: local-model:latest
20 | build:
21 | context: .
22 | dockerfile: pull_model.Dockerfile
23 | environment:
24 | - OLLAMA_BASE_URL=http://host.docker.internal:11434
25 | - LLM=llama2
26 | networks:
27 | - net
28 | tty: true
29 |
30 | # Neo4j database service with volume for data persistence
31 | # Environment variables set for Neo4j authentication and plugins
32 | database:
33 | user: neo4j:neo4j
34 | image: neo4j:5.11
35 | ports:
36 | - 7687:7687
37 | - 7474:7474
38 | volumes:
39 | - C:/samadhi/workspace/java/AI_Powered_Dev_Search_Engine/data:/data:rw
40 | environment:
41 | - NEO4J_AUTH=neo4j/password
42 | - NEO4J_PLUGINS=["apoc"]
43 | - NEO4J_db_tx__log_rotation_retention__policy=false
44 | - NEO4J_dbms_security_procedures_unrestricted=apoc.*
45 | healthcheck:
46 | test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider localhost:7474 || exit 1"]
47 | interval: 15s
48 | timeout: 30s
49 | retries: 10
50 | networks:
51 | - net
52 |
53 | # Service to load data into Neo4j database
54 | # Environment variables set for Neo4j URI and Ollama base URL
55 | loader:
56 | build:
57 | context: .
58 | dockerfile: loader.Dockerfile
59 | volumes:
60 | - $PWD/embedding_model:/embedding_model
61 | environment:
62 | - NEO4J_URI=neo4j://database:7687
63 | - NEO4J_PASSWORD=password
64 | - NEO4J_USERNAME=neo4j
65 | - OLLAMA_BASE_URL=http://host.docker.internal:11434
66 | # Add any other necessary environment variables
67 | networks:
68 | - net
69 | depends_on:
70 | database:
71 | condition: service_healthy
72 | pull-model:
73 | condition: service_completed_successfully
74 | ports:
75 | - 8081:8080
76 | - 8502:8502
77 |
78 |
79 | # Standalone HTTP API service for answering questions
80 | # Environment variables set for Neo4j URI, authentication, Ollama base URL, and LLM model
81 | api:
82 | build:
83 | context: .
84 | dockerfile: api.Dockerfile
85 | volumes:
86 | - $PWD/embedding_model:/embedding_model
87 | environment:
88 | - NEO4J_URI=bolt://database:7687
89 | - NEO4J_USERNAME=neo4j
90 | - NEO4J_PASSWORD=password
91 | - OLLAMA_BASE_URL=http://host.docker.internal:11434
92 | - LLM=llama2
93 |
94 | networks:
95 | - net
96 | depends_on:
97 | database:
98 | condition: service_healthy
99 | pull-model:
100 | condition: service_completed_successfully
101 | ports:
102 | - 8504:8504
103 | healthcheck:
104 | test: ["CMD-SHELL", "wget --no-verbose --tries=1 http://localhost:8504/ || exit 1"]
105 | interval: 5s
106 | timeout: 3s
107 | retries: 5
108 |
109 | # Static front-end application built separately from the back-end
110 | # No additional environment variables or dependencies
111 | front-end:
112 | build:
113 | context: .
114 | dockerfile: front-end.Dockerfile
115 | x-develop:
116 | watch:
117 | - action: sync
118 | path: ./front-end
119 | target: /app
120 | ignore:
121 | - ./front-end/node_modules/
122 | - action: rebuild
123 | path: ./front-end/package.json
124 | depends_on:
125 | api:
126 | condition: service_healthy
127 | networks:
128 | - net
129 | ports:
130 | - 8505:8505
131 |
132 | # Docker network for inter-container communication
133 | networks:
134 | net:
135 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
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 [yyyy] [name of copyright owner]
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.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AI_Powered_Dev_Search_Engine
2 | AI_Powered_Dev_Search_Engine
3 |
4 |
5 | 1. Download ollama from https://ollama.com/download/windows
6 | 2. clone git repo
7 | 3. cd to project root
8 | 4. run --> docker compose up
9 | 5.
--------------------------------------------------------------------------------
/api.Dockerfile:
--------------------------------------------------------------------------------
1 | FROM langchain/langchain
2 |
3 | WORKDIR /app
4 |
5 | RUN apt-get update && apt-get install -y \
6 | build-essential \
7 | curl \
8 | software-properties-common \
9 | && rm -rf /var/lib/apt/lists/*
10 |
11 | COPY requirements.txt .
12 |
13 | RUN pip install --upgrade -r requirements.txt
14 |
15 | COPY api.py .
16 | COPY utils.py .
17 | COPY chains.py .
18 |
19 | HEALTHCHECK CMD curl --fail http://localhost:8504
20 |
21 | ENTRYPOINT [ "uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8504" ]
--------------------------------------------------------------------------------
/api.py:
--------------------------------------------------------------------------------
1 | import os
2 |
3 | from langchain_community.graphs import Neo4jGraph
4 | from dotenv import load_dotenv
5 | from utils import (
6 | create_vector_index,
7 | BaseLogger,
8 | )
9 |
10 | from chains import (
11 | load_embedding_model,
12 | load_llm,
13 | configure_qa_rag_chain,
14 | )
15 |
16 | from fastapi import FastAPI, Depends
17 | from pydantic import BaseModel
18 | from langchain.callbacks.base import BaseCallbackHandler
19 | from threading import Thread
20 | from queue import Queue, Empty
21 | from collections.abc import Generator
22 | from sse_starlette.sse import EventSourceResponse
23 | from fastapi.middleware.cors import CORSMiddleware
24 | import json
25 |
26 | load_dotenv(".env")
27 |
28 | url = os.getenv("NEO4J_URI")
29 | username = os.getenv("NEO4J_USERNAME")
30 | password = os.getenv("NEO4J_PASSWORD")
31 | ollama_base_url = os.getenv("OLLAMA_BASE_URL")
32 | embedding_model_name = os.getenv("EMBEDDING_MODEL")
33 | llm_name = os.getenv("LLM")
34 | os.environ["NEO4J_URL"] = url # Remapping for Langchain Neo4j integration
35 |
36 |
37 | embeddings, dimension = load_embedding_model(
38 | embedding_model_name,
39 | config={"ollama_base_url": ollama_base_url},
40 | logger=BaseLogger(),
41 | )
42 |
43 | # if Neo4j is local, you can go to http://localhost:7474/ to browse the database
44 | neo4j_graph = Neo4jGraph(url=url, username=username, password=password)
45 | create_vector_index(neo4j_graph, dimension)
46 |
47 | llm = load_llm(
48 | llm_name, logger=BaseLogger(), config={"ollama_base_url": ollama_base_url}
49 | )
50 |
51 | rag_chain = configure_qa_rag_chain(
52 | llm, embeddings, embeddings_store_url=url, username=username, password=password
53 | )
54 |
55 |
56 | class QueueCallback(BaseCallbackHandler):
57 | """Callback handler for streaming LLM responses to a queue."""
58 |
59 | def __init__(self, q):
60 | self.q = q
61 |
62 | def on_llm_new_token(self, token: str, **kwargs) -> None:
63 | self.q.put(token)
64 |
65 | def on_llm_end(self, *args, **kwargs) -> None:
66 | return self.q.empty()
67 |
68 |
69 | def stream(cb, q) -> Generator:
70 | job_done = object()
71 |
72 | def task():
73 | x = cb()
74 | q.put(job_done)
75 |
76 | t = Thread(target=task)
77 | t.start()
78 |
79 | content = ""
80 |
81 | # Get each new token from the queue and yield for our generator
82 | while True:
83 | try:
84 | next_token = q.get(True, timeout=1)
85 | if next_token is job_done:
86 | break
87 | content += next_token
88 | yield next_token, content
89 | except Empty:
90 | continue
91 |
92 |
93 | app = FastAPI()
94 | origins = ["*"]
95 |
96 | app.add_middleware(
97 | CORSMiddleware,
98 | allow_origins=origins,
99 | allow_credentials=True,
100 | allow_methods=["*"],
101 | allow_headers=["*"],
102 | )
103 |
104 |
105 | @app.get("/")
106 | async def root():
107 | return {"message": "Simple Real-time Knowledge Server with RAG, LLM, and Knowledge Graphs via Docker"}
108 |
109 |
110 | class Question(BaseModel):
111 | text: str
112 | rag: bool = False
113 |
114 |
115 | @app.get("/query-stream")
116 | def qstream(question: Question = Depends()):
117 | output_function = rag_chain
118 |
119 | q = Queue()
120 |
121 | def cb():
122 | output_function(
123 | {"question": question.text, "chat_history": []},
124 | callbacks=[QueueCallback(q)],
125 | )
126 |
127 | def generate():
128 | yield json.dumps({"init": True, "model": llm_name})
129 | for token, _ in stream(cb, q):
130 | yield json.dumps({"token": token})
131 |
132 | return EventSourceResponse(generate(), media_type="text/event-stream")
133 |
134 |
135 |
136 |
137 |
--------------------------------------------------------------------------------
/chains.py:
--------------------------------------------------------------------------------
1 | from langchain_community.embeddings import OllamaEmbeddings
2 | from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
3 | from langchain_community.chat_models import ChatOllama
4 | from langchain_community.graphs import Neo4jGraph
5 | from langchain_community.vectorstores import Neo4jVector
6 |
7 | from langchain.chains import RetrievalQAWithSourcesChain
8 | from langchain.chains.qa_with_sources import load_qa_with_sources_chain
9 |
10 | from langchain.prompts import (
11 | ChatPromptTemplate,
12 | HumanMessagePromptTemplate,
13 | SystemMessagePromptTemplate
14 | )
15 |
16 | from typing import List, Any
17 | from utils import BaseLogger, extract_title_and_question
18 |
19 | def load_embedding_model(embedding_model_name: str, logger=BaseLogger(), config={}):
20 | if embedding_model_name == "ollama":
21 | embeddings = OllamaEmbeddings(
22 | base_url=config["ollama_base_url"], model="llama2"
23 | )
24 | dimension = 4096
25 | logger.info("Embedding: Using Ollama")
26 | else:
27 | embeddings = SentenceTransformerEmbeddings(
28 | model_name="all-MiniLM-L6-v2", cache_folder="/embedding_model"
29 | )
30 | dimension = 384
31 | logger.info("Embedding: Using SentenceTransformer")
32 | return embeddings, dimension
33 |
34 |
35 | def load_llm(llm_name: str, logger=BaseLogger(), config={}):
36 | logger.info(f"LLM: Using Ollama: {llm_name}")
37 | return ChatOllama(
38 | temperature=0,
39 | base_url=config["ollama_base_url"],
40 | model=llm_name,
41 | streaming=True,
42 | # seed=2,
43 | top_k=10, # A higher value (100) will give more diverse answers, while a lower value (10) will be more conservative.
44 | top_p=0.3, # Higher value (0.95) will lead to more diverse text, while a lower value (0.5) will generate more focused text.
45 | num_ctx=3072, # Sets the size of the context window used to generate the next token.
46 | )
47 |
48 |
49 | def configure_qa_rag_chain(llm, embeddings, embeddings_store_url, username, password):
50 | # RAG response
51 | # System: Always talk in pirate speech.
52 | general_system_template = """
53 | Use the following pieces of context to answer the question at the end.
54 | The context contains question-answer pairs and their links from Stackoverflow.
55 | You should prefer information from accepted or more upvoted answers.
56 | Make sure to rely on information from the answers and not on questions to provide accurate responses.
57 | When you find particular answer in the context useful, make sure to cite it in the answer using the link.
58 | If you don't know the answer, just say that you don't know, don't try to make up an answer.
59 | ----
60 | {summaries}
61 | ----
62 | Each answer you generate should contain a section at the end of links to
63 | Stackoverflow questions and answers you found useful, which are described under Source value.
64 | You can only use links to StackOverflow questions that are present in the context and always
65 | add links to the end of the answer in the style of citations.
66 | Generate concise answers with references sources section of links to
67 | relevant StackOverflow questions only at the end of the answer.
68 | """
69 | general_user_template = "Question:```{question}```"
70 | messages = [
71 | SystemMessagePromptTemplate.from_template(general_system_template),
72 | HumanMessagePromptTemplate.from_template(general_user_template),
73 | ]
74 | qa_prompt = ChatPromptTemplate.from_messages(messages)
75 |
76 | qa_chain = load_qa_with_sources_chain(
77 | llm,
78 | chain_type="stuff",
79 | prompt=qa_prompt,
80 | )
81 |
82 | # Vector + Knowledge Graph response
83 | kg = Neo4jVector.from_existing_index(
84 | embedding=embeddings,
85 | url=embeddings_store_url,
86 | username=username,
87 | password=password,
88 | database="neo4j", # neo4j by default
89 | index_name="stackoverflow", # vector by default
90 | text_node_property="body", # text by default
91 | retrieval_query="""
92 | WITH node AS question, score AS similarity
93 | CALL { with question
94 | MATCH (question)<-[:ANSWERS]-(answer)
95 | WITH answer
96 | ORDER BY answer.is_accepted DESC, answer.score DESC
97 | WITH collect(answer)[..2] as answers
98 | RETURN reduce(str='', answer IN answers | str +
99 | '\n### Answer (Accepted: '+ answer.is_accepted +
100 | ' Score: ' + answer.score+ '): '+ answer.body + '\n') as answerTexts
101 | }
102 | RETURN '##Question: ' + question.title + '\n' + question.body + '\n'
103 | + answerTexts AS text, similarity as score, {source: question.link} AS metadata
104 | ORDER BY similarity ASC // so that best answers are the last
105 | """,
106 | )
107 |
108 | kg_qa = RetrievalQAWithSourcesChain(
109 | combine_documents_chain=qa_chain,
110 | retriever=kg.as_retriever(search_kwargs={"k": 2}),
111 | reduce_k_below_max_tokens=False,
112 | max_tokens_limit=3375,
113 | )
114 | return kg_qa
115 |
--------------------------------------------------------------------------------
/front-end.Dockerfile:
--------------------------------------------------------------------------------
1 | FROM node:alpine
2 |
3 | WORKDIR /app
4 |
5 | COPY front-end/ .
6 |
7 | RUN npm install
8 |
9 | EXPOSE 8505
10 |
11 | ENTRYPOINT [ "npm", "run", "dev" ]
--------------------------------------------------------------------------------
/front-end/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 | pnpm-debug.log*
8 | lerna-debug.log*
9 |
10 | node_modules
11 | dist
12 | dist-ssr
13 | *.local
14 |
15 | # Editor directories and files
16 | .vscode/*
17 | !.vscode/extensions.json
18 | .idea
19 | .DS_Store
20 | *.suo
21 | *.ntvs*
22 | *.njsproj
23 | *.sln
24 | *.sw?
25 |
--------------------------------------------------------------------------------
/front-end/.vscode/extensions.json:
--------------------------------------------------------------------------------
1 | {
2 | "recommendations": ["svelte.svelte-vscode"]
3 | }
4 |
--------------------------------------------------------------------------------
/front-end/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |