maximka608 commited on
Commit
5748c97
·
1 Parent(s): 7b5db49
__pycache__/app.cpython-310.pyc ADDED
Binary file (3.51 kB). View file
 
__pycache__/config.cpython-310.pyc ADDED
Binary file (532 Bytes). View file
 
app.py CHANGED
@@ -10,8 +10,8 @@ def get_emdedding_model():
10
  return Embeddings()
11
 
12
 
13
- def get_llm(url, api_key):
14
- return LLM(url, api_key)
15
 
16
 
17
  def get_metadata(path):
@@ -23,7 +23,6 @@ def get_metadata(path):
23
  texts.append(data['text'])
24
  return texts, titles
25
 
26
-
27
  def combine_docs(indexes, texts):
28
  result = ""
29
  for i, index in enumerate(indexes):
@@ -32,64 +31,69 @@ def combine_docs(indexes, texts):
32
 
33
 
34
  def create_prompt(query, docs):
35
- system_prompt = f""" You are a language model integrated into a search and
36
- generation system based on relevant documents (RAG system).
37
- Your task is to provide answers to the user's queries based on the provided
38
- documents. Respond only based on the provided documents. Do not make up
39
- information that is not in the sources. If you use data from a document,
40
- indicate the document number in square brackets. For example: "This term
41
- means such-and-such [1]." If there is no information in the documents,
42
- politely explain that the information is not available. Do not alter the
43
- content of the sources, convey the information accurately
44
- Structure the text in a clear way whenever possible, even if formatting is
45
- limited.
46
- For example:
47
- User query: ML.
48
- Documents:
49
- [1] es of ML models.
50
- [2] The rapid escalation of applying Machine Learning (ML) in various domains has led to paying more attention to the quality of ML components. There is then a growth of techniques and tools aiming at improving the quality of ML components and integrating them.
51
-
52
- Machine Learning (ML) is increasingly applied across various domains, leading to a focus on the quality of ML components and the development of techniques to improve and integrate them [2]
53
-
54
- Follow this format in your responses and print all documents. User query: {query}. Documents: {docs}
55
  """
56
  return system_prompt
57
 
58
 
59
- st.title("PaperRAG")
60
- st.write("RAG system for scientific papers with selectable search types")
 
 
 
 
 
 
61
 
62
- query = st.text_input("Enter your query", "")
63
- search_types = st.multiselect(
64
- "Search Types", options=["Vector", "BM25"], default=["Vector", "BM25"]
65
- )
66
- llm_url = st.text_input("LLM URL", "", placeholder="Enter LLM ENDPOINT")
67
- llm_api_key = st.text_input("LLM API Key", "", placeholder="Enter LLM API Key", type="password")
68
 
69
- if st.button("Search"):
70
- if query and llm_url and llm_api_key:
71
- model = get_emdedding_model()
72
- llm = get_llm(llm_url, llm_api_key)
73
 
74
- texts, titles = get_metadata(config.PATH_METADATA)
75
- embedding = model.get_query_embedding(query)
76
 
77
- knowledge_base = KnowledgeBase(config.PATH_FAISS, config.PATH_PREPROCESSING_TEXT)
78
 
79
- vector_search = []
80
- bm25_search = []
 
 
81
 
82
- if "Vector" in search_types:
83
- vector_search = knowledge_base.search_by_embedding(embedding, 5)[0].tolist()
84
- if "BM25" in search_types:
85
- bm25_search = knowledge_base.search_by_BM25(query, 5)
 
 
 
 
86
 
87
- docs = combine_docs(vector_search + bm25_search, texts)
88
- prompt = create_prompt(query, docs)
 
 
89
 
90
- response = llm.generate_response(prompt)
 
 
91
 
92
- st.subheader("Response")
93
- st.write(response)
94
- else:
95
- st.error("Please fill in all the required fields.")
 
 
10
  return Embeddings()
11
 
12
 
13
+ def get_llm(api_key):
14
+ return LLM(api_key)
15
 
16
 
17
  def get_metadata(path):
 
23
  texts.append(data['text'])
24
  return texts, titles
25
 
 
26
  def combine_docs(indexes, texts):
27
  result = ""
28
  for i, index in enumerate(indexes):
 
31
 
32
 
33
  def create_prompt(query, docs):
34
+ system_prompt = f"""You are a language model integrated into a search and generation system based on relevant documents (RAG system).
35
+ Your task is to provide answers to the user's queries based solely on the provided documents.
36
+ If the information required to answer the user's question is available in the documents, use it, and refer to the document from which it was sourced by indicating its number in square brackets. For example:
37
+ "This term means such-and-such [1]."
38
+ Ensure that the citation clearly refers to the relevant document and is placed directly after the information from the source.
39
+
40
+ If the information is not present in the documents, kindly explain that the information is not available, and do not speculate or make up information.
41
+
42
+ Do not alter the content or meaning of the sources. Convey the information accurately and structure your response clearly, even if the formatting options are limited.
43
+
44
+ User query: {query}
45
+ Documents:
46
+ {docs}
 
 
 
 
 
 
 
47
  """
48
  return system_prompt
49
 
50
 
51
+ def main(query, search_types, llm_api_key):
52
+ model, llm = get_emdedding_model(), get_llm(llm_api_key)
53
+ texts, titles = get_metadata(config.PATH_METADATA)
54
+ embedding = model.get_query_embedding(query)
55
+
56
+ knowledge_base = KnowledgeBase(config.PATH_FAISS, config.PATH_PREPROCESSING_TEXT)
57
+ vector_search = []
58
+ bm25_search = []
59
 
60
+ if "Vector" in search_types:
61
+ vector_search = knowledge_base.search_by_embedding(embedding, 5)[0].tolist()
62
+ if "BM25" in search_types:
63
+ bm25_search = knowledge_base.search_by_BM25(query, 5)
 
 
64
 
65
+ docs = combine_docs(vector_search + bm25_search, texts)
66
+ prompt = create_prompt(query, docs)
 
 
67
 
68
+ response = llm.generate_response(prompt)
69
+ return response, docs
70
 
 
71
 
72
+ # Streamlit Interface
73
+ if __name__ == '__main__':
74
+ st.title("PaperRAG")
75
+ st.subheader("RAG system for scientific papers with selectable search types")
76
 
77
+ # User inputs
78
+ query = st.text_input("Enter your query")
79
+ search_types = st.multiselect(
80
+ "Select search types",
81
+ options=["Vector", "BM25"],
82
+ default=["Vector", "BM25"]
83
+ )
84
+ llm_api_key = st.text_input("Cohere API Key", type="password")
85
 
86
+ if st.button("Get Response"):
87
+ if query and llm_api_key:
88
+ # Call the main function
89
+ response, docs = main(query, search_types, llm_api_key)
90
 
91
+ # Show the LLM response
92
+ st.subheader("LLM Response:")
93
+ st.text_area("Response", value=response, height=300)
94
 
95
+ # Show combined documents
96
+ st.subheader("Citations:")
97
+ st.text_area("Documents", value=docs, height=300)
98
+ else:
99
+ st.error("Please enter both a query and an API key.")
requirements.txt CHANGED
@@ -110,3 +110,4 @@ uvicorn==0.32.1
110
  websockets==12.0
111
  xxhash==3.5.0
112
  yarl==1.17.2
 
 
110
  websockets==12.0
111
  xxhash==3.5.0
112
  yarl==1.17.2
113
+ litellm~=1.52.16
script/__pycache__/preprocessing_text.cpython-310.pyc ADDED
Binary file (1.99 kB). View file
 
utils/__pycache__/embedding.cpython-310.pyc ADDED
Binary file (995 Bytes). View file
 
utils/__pycache__/llm.cpython-310.pyc ADDED
Binary file (840 Bytes). View file
 
utils/__pycache__/vector_base.cpython-310.pyc ADDED
Binary file (1.47 kB). View file
 
utils/llm.py CHANGED
@@ -1,28 +1,15 @@
1
- import requests
2
- from dotenv import load_dotenv
3
-
4
- load_dotenv()
5
 
6
  class LLM:
7
- def __init__(self, url, api_key):
8
- self.endpoint = url
9
- self.api_key = api_key
10
-
11
- def generate_response(self, prompt):
12
- headers = {
13
- "Content-Type": "application/json",
14
- "api-key": self.api_key,
15
- }
16
-
17
- data = {
18
- "messages": [{"role": "user", "content": prompt}],
19
- "max_tokens": 3000,
20
- "temperature": 0.5,
21
- }
22
-
23
- response = requests.post(self.endpoint, headers=headers, json=data)
24
-
25
- if response.status_code == 200:
26
- return response.json()["choices"][0]["message"]["content"]
27
- else:
28
- return ValueError(response.text)
 
1
+ import os
2
+ from litellm import completion
 
 
3
 
4
  class LLM:
5
+ def __init__(self, api_key):
6
+ os.environ["COHERE_API_KEY"] = api_key
7
+
8
+ def generate_response(self, prompt, temperature=0.5, max_tokens=1500):
9
+ response = completion(
10
+ model="command-r",
11
+ messages=[{"content": prompt, "role": "user"}],
12
+ temperature=temperature,
13
+ max_tokens=max_tokens
14
+ )
15
+ return response.choices[0].message.content