soroushsrd commited on
Commit
f2a8840
1 Parent(s): 6ab6715

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +78 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.document_loaders import PyPDFLoader
2
+ from langchain_openai import OpenAIEmbeddings, ChatOpenAI
3
+ from langchain_community.embeddings import OllamaEmbeddings
4
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
5
+ from langchain_community.vectorstores import Chroma
6
+ import os
7
+ from langchain.retrievers.multi_query import MultiQueryRetriever
8
+ from langchain_core.runnables import RunnablePassthrough
9
+ from langchain_core.output_parsers import StrOutputParser
10
+ from langchain.prompts import ChatPromptTemplate, PromptTemplate
11
+ import streamlit as st
12
+
13
+ os.environ["OPENAI_API_KEY"] =st.secrets["OPENAI_API_KEY"]
14
+
15
+ llm = ChatOpenAI(model='gpt-4o', temperature=0.2)
16
+
17
+ embeddings = OpenAIEmbeddings()
18
+
19
+ vector_store = Chroma(embedding_function=embeddings, persist_directory="mining-rag")
20
+ print("Vector store loaded successfully.")
21
+
22
+
23
+ question=st.text_input('whats your question?')
24
+ key=st.button('enter')
25
+
26
+ if key:
27
+ QUERY_PROMPT = PromptTemplate(
28
+ input_variables=["question"],
29
+ template="""You are an AI language model assistant. Your task is to generate three
30
+ different versions of the given user question to retrieve relevant documents from
31
+ a vector database. By generating multiple perspectives on the user question, your
32
+ goal is to help the user overcome some of the limitations of the distance-based
33
+ similarity search. Provide these alternative questions separated by newlines.
34
+ Original question: {question}""",
35
+ )
36
+
37
+ retriever = MultiQueryRetriever.from_llm(
38
+ vector_store.as_retriever(),
39
+ llm,
40
+ prompt=QUERY_PROMPT
41
+ )
42
+
43
+ WRITER_SYSTEM_PROMPT = "You are an AI critical thinker research assistant. Your sole purpose is to write well written, critically acclaimed, objective and structured reports on given text." # noqa: E501
44
+ # Report prompts from https://github.com/assafelovic/gpt-researcher/blob/master/gpt_researcher/master/prompts.py
45
+ RESEARCH_REPORT_TEMPLATE = """Information:
46
+ --------
47
+ {text}
48
+ --------
49
+ Using the above information, answer the following question or topic: "{question}" in a short manner-- \
50
+ The answer should focus on the answer to the question, should be well structured, informative, \
51
+ in depth, with facts and numbers if available and a minimum of 150 words and a maximum of 300 words.
52
+ You should strive to write the report using all relevant and necessary information provided.
53
+ You must write the report with markdown syntax.
54
+ You MUST determine your own concrete and valid opinion based on the given information. Do NOT deter to general and meaningless conclusions.
55
+ You must write the sources used in the context. if any article is used, mentioned in the end.
56
+ Please do your best, this is very important to my career.""" # noqa: E501
57
+
58
+ prompt = ChatPromptTemplate.from_messages(
59
+ [
60
+ ("system", WRITER_SYSTEM_PROMPT),
61
+ ("user", RESEARCH_REPORT_TEMPLATE),
62
+ ]
63
+ )
64
+
65
+ chain = (
66
+ {"text": retriever, "question": RunnablePassthrough()}
67
+ | prompt
68
+ | llm
69
+ | StrOutputParser()
70
+ )
71
+
72
+ answer = chain.invoke(
73
+ {
74
+ "question": question
75
+ }
76
+ )
77
+ st.write(answer)
78
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ langchain==0.2.0
2
+ langchain_community==0.2.0
3
+ langchain_core==0.2.0
4
+ langchain_openai==0.1.7
5
+ langchain_text_splitters==0.2.0
6
+ streamlit==1.33.0