Update app.py
Browse files
app.py
CHANGED
|
@@ -15,4 +15,56 @@ st.title("Aplikacja z LlamaIndex")
|
|
| 15 |
documents = SimpleDirectoryReader('./data/').load_data()
|
| 16 |
db = chromadb.PersistentClient(path="./data")
|
| 17 |
chroma_collection = db.get_or_create_collection("zalacznik_nr12")
|
| 18 |
-
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
documents = SimpleDirectoryReader('./data/').load_data()
|
| 16 |
db = chromadb.PersistentClient(path="./data")
|
| 17 |
chroma_collection = db.get_or_create_collection("zalacznik_nr12")
|
| 18 |
+
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Utw贸rz pipeline do przetwarzania dokument贸w
|
| 22 |
+
pipeline = IngestionPipeline(
|
| 23 |
+
transformations=[
|
| 24 |
+
SentenceSplitter(),
|
| 25 |
+
embed_model,
|
| 26 |
+
],
|
| 27 |
+
vector_store=vector_store
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Utw贸rz indeks
|
| 31 |
+
index = VectorStoreIndex.from_vector_store(vector_store, embed_model=embed_model)
|
| 32 |
+
|
| 33 |
+
# Utw贸rz silnik zapyta艅
|
| 34 |
+
llm = Ollama(model="qwen2:7b")
|
| 35 |
+
query_engine = index.as_query_engine(
|
| 36 |
+
llm=llm,
|
| 37 |
+
response_mode='compact')
|
| 38 |
+
|
| 39 |
+
# Store LLM generated responses
|
| 40 |
+
if "messages" not in st.session_state.keys():
|
| 41 |
+
st.session_state.messages = [{"role": "assistant", "content": "Zadaj mi pytanie..."}]
|
| 42 |
+
|
| 43 |
+
# Display chat messages
|
| 44 |
+
for message in st.session_state.messages:
|
| 45 |
+
with st.chat_message(message["role"]):
|
| 46 |
+
st.write(message["content"])
|
| 47 |
+
|
| 48 |
+
# User-provided prompt
|
| 49 |
+
if input := st.chat_input():
|
| 50 |
+
st.session_state.messages.append({"role": "user", "content": input})
|
| 51 |
+
with st.chat_message("user"):
|
| 52 |
+
st.write(input)
|
| 53 |
+
|
| 54 |
+
# Generate a new response if last message is not from assistant
|
| 55 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 56 |
+
with st.chat_message("assistant"):
|
| 57 |
+
with st.spinner("Czekaj, odpowied藕 jest generowana.."):
|
| 58 |
+
response = query_engine.query(input)
|
| 59 |
+
|
| 60 |
+
# Zbuduj tre艣膰 wiadomo艣ci z odpowiedzi膮 i score
|
| 61 |
+
content = str(response.response) # Upewnij si臋, 偶e response jest stringiem
|
| 62 |
+
if hasattr(response, 'source_nodes') and response.source_nodes: # Sprawd藕, czy source_nodes istnieje
|
| 63 |
+
# Dodaj score pierwszego w臋z艂a (je艣li istnieje)
|
| 64 |
+
content += f"\nScore: {response.source_nodes[0].score:.4f}" # Dodaj score
|
| 65 |
+
|
| 66 |
+
st.write(content) # Wy艣wietl ca艂膮 tre艣膰 w Streamlit
|
| 67 |
+
|
| 68 |
+
message = {"role": "assistant", "content": content} # Zapisz ca艂膮 tre艣膰 w wiadomo艣ci
|
| 69 |
+
st.session_state.messages.append(message)
|
| 70 |
+
|