Commit
·
152cd46
1
Parent(s):
fbdbb30
initial code
Browse files- AcademicPolicy/index.faiss +0 -0
- AcademicPolicy/index.pkl +3 -0
- app.py +78 -0
- firestore_util.py +11 -0
- requirements.txt +7 -0
AcademicPolicy/index.faiss
ADDED
Binary file (713 kB). View file
|
|
AcademicPolicy/index.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:32aa70f7c41c559eeb80b655a585ec915fb8dfc4f63147412a3b0ebc1c077f0b
|
3 |
+
size 216664
|
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain_community.llms import OpenAI
|
3 |
+
import configparser
|
4 |
+
|
5 |
+
import os
|
6 |
+
from langchain_openai import OpenAIEmbeddings
|
7 |
+
from langchain_community.vectorstores import FAISS
|
8 |
+
from langchain_openai import ChatOpenAI
|
9 |
+
from langchain_core.prompts import ChatPromptTemplate
|
10 |
+
from langchain.chains import create_retrieval_chain
|
11 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
12 |
+
|
13 |
+
|
14 |
+
Config = configparser.ConfigParser()
|
15 |
+
Config.read("config.ini")
|
16 |
+
st.title('⚖️🔗 Поисковик по академической политике университета Narxoz')
|
17 |
+
st.markdown('Наш чатбот ответ на вопросы по правилам из академической политики университета Narxoz')
|
18 |
+
|
19 |
+
|
20 |
+
#openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password')
|
21 |
+
#openai_api_key = Config.get('Pass','OpenaiKey')
|
22 |
+
openai_api_key = st.secrets['openai']
|
23 |
+
|
24 |
+
os.environ["OPENAI_API_KEY"] = openai_api_key
|
25 |
+
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
|
26 |
+
new_db = FAISS.load_local("AcademicPolicy", embeddings,allow_dangerous_deserialization= True)
|
27 |
+
retriever = new_db.as_retriever(search_type="similarity", search_kwargs={"k": 6})
|
28 |
+
llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
|
29 |
+
|
30 |
+
system_prompt = (
|
31 |
+
"Вы бот который отвечает на вопросы по внутренним правилам университета Narxoz."
|
32 |
+
"Используйте следующий набор документов. "
|
33 |
+
"Если не знаете ответ, ответьте что вы не знаете. "
|
34 |
+
"Постарайте уложиться в три предложения."
|
35 |
+
"Университет Narxoz также подчиняется правилам Республики Казахстан"
|
36 |
+
"И входит в Болонскую систему образования"
|
37 |
+
"\n\n"
|
38 |
+
"{context}"
|
39 |
+
)
|
40 |
+
prompt = ChatPromptTemplate.from_messages(
|
41 |
+
[
|
42 |
+
("system", system_prompt),
|
43 |
+
("human", "{input}"),
|
44 |
+
]
|
45 |
+
)
|
46 |
+
|
47 |
+
question_answer_chain = create_stuff_documents_chain(llm, prompt)
|
48 |
+
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
49 |
+
|
50 |
+
import firestore_util
|
51 |
+
@st.cache_resource
|
52 |
+
def load_firestore_connection_ref():
|
53 |
+
db = firestore_util.load_firestore()
|
54 |
+
doc_ref = db.collection('requests').document("client_queries")
|
55 |
+
return doc_ref
|
56 |
+
|
57 |
+
doc_ref = load_firestore_connection_ref()
|
58 |
+
|
59 |
+
@st.cache_data
|
60 |
+
def generate_response(input_text):
|
61 |
+
response = rag_chain.invoke({"input": text})
|
62 |
+
|
63 |
+
doc_ref.set({"query":input_text,"response":response['answer']})
|
64 |
+
return response
|
65 |
+
|
66 |
+
|
67 |
+
with st.form('my_form'):
|
68 |
+
text = st.text_area('Напишите свой вопрос:', '')
|
69 |
+
submitted = st.form_submit_button('Submit')
|
70 |
+
if submitted:
|
71 |
+
response = generate_response(text)
|
72 |
+
st.markdown(response['answer'])
|
73 |
+
|
74 |
+
#print(response['context'])
|
75 |
+
for context in response['context']:
|
76 |
+
with st.expander(context.metadata['codex']+" "+context.metadata['title']):
|
77 |
+
st.write(context.page_content)
|
78 |
+
#generate_response(text)
|
firestore_util.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import firebase_admin
|
2 |
+
from firebase_admin import credentials
|
3 |
+
from firebase_admin import firestore
|
4 |
+
|
5 |
+
|
6 |
+
def load_firestore():
|
7 |
+
# Use a service account.
|
8 |
+
cred = credentials.Certificate('firestore_keys/lawllm-427510-582b20c51958.json')
|
9 |
+
app = firebase_admin.initialize_app(cred)
|
10 |
+
db = firestore.client()
|
11 |
+
return db
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
configparser
|
3 |
+
langchain-community
|
4 |
+
langchain
|
5 |
+
faiss-cpu
|
6 |
+
langchain-openai
|
7 |
+
firebase-admin
|