-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.py
61 lines (52 loc) · 2.65 KB
/
vector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import streamlit as st
from llm import llm, embeddings
from graph import graph
from langchain_community.vectorstores.neo4j_vector import Neo4jVector
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains import create_retrieval_chain
from langchain_core.prompts import ChatPromptTemplate
neo4jvector = Neo4jVector.from_existing_index(
embeddings,
graph=graph,
index_name="ChunkVectorIndex",
node_label="Chunk",
text_node_property="content",
embedding_node_property="embedding",
retrieval_query="""
RETURN
node.content AS text,
score,
{
title: [(node)<-[:HAS_CHUNK]-(content:Content)<-[:HAS_CONTENT]-(article:Article) | article.name],
content: [(node)<-[:HAS_CHUNK]-(content:Content) | content.content],
sofwares: COALESCE([(node)<-[:HAS_CHUNK]-(content:Content)<-[:HAS_CONTENT]-(article:Article)-[:REFERS_TO_SOFTWARE]->(software:Software) | software.name],''),
operations: COALESCE([(node)<-[:HAS_CHUNK]-(content:Content)<-[:HAS_CONTENT]-(article:Article)-[:HAS_OPERATION]->(operation:Operation) | operation.name],'')
} AS metadata
"""
)
retriever = neo4jvector.as_retriever(search_kwargs={'k': 10})
print('retriever', retriever)
instructions = (
"Use the given context to answer the question."
"If you don't know the answer, say you don't know."
"Explicitly return the title of the documents/articles found and include all document names as sources in the final response."
"If a document/article is requested, explicitly return the content (from the metadata) pertaining to the document/article."
"If a summary of a document/article is requested, return a summary of the content (from the metadata) pertaining to the document/article."
"If the software of a document/article is requested, return the softwares (from the metadata) pertaining to the document/article."
"If the operation of a document/article is requested, return the operations (from the metadata) pertaining to the document/article."
"Context: {context}"
)
prompt = ChatPromptTemplate.from_messages(
[
("system", instructions),
("human", "{input}"),
]
)
question_answer_chain = create_stuff_documents_chain(llm, prompt)
plot_retriever = create_retrieval_chain(
retriever,
question_answer_chain
)
def get_article_content(input):
return plot_retriever.invoke({"input": input})
# return neo4jvector.similarity_search_with_score(input, k=20)