forked from AllAboutAI-YT/easy-local-rag
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d87cf1
commit 29268db
Showing
3 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import torch | ||
from sentence_transformers import SentenceTransformer, util | ||
import os | ||
from openai import OpenAI | ||
|
||
# ANSI escape codes for colors | ||
PINK = '\033[95m' | ||
CYAN = '\033[96m' | ||
YELLOW = '\033[93m' | ||
NEON_GREEN = '\033[92m' | ||
RESET_COLOR = '\033[0m' | ||
|
||
# Configuration for the Ollama API client | ||
client = OpenAI( | ||
base_url='http://localhost:11434/v1', | ||
api_key='mistral' | ||
) | ||
|
||
# Function to open a file and return its contents as a string | ||
def open_file(filepath): | ||
with open(filepath, 'r', encoding='utf-8') as infile: | ||
return infile.read() | ||
|
||
# Function to get relevant context from the vault based on user input | ||
def get_relevant_context(user_input, vault_embeddings, vault_content, model, top_k=3): | ||
if vault_embeddings.nelement() == 0: # Check if the tensor has any elements | ||
return [] | ||
# Encode the user input | ||
input_embedding = model.encode([user_input]) | ||
# Compute cosine similarity between the input and vault embeddings | ||
cos_scores = util.cos_sim(input_embedding, vault_embeddings)[0] | ||
# Adjust top_k if it's greater than the number of available scores | ||
top_k = min(top_k, len(cos_scores)) | ||
# Sort the scores and get the top-k indices | ||
top_indices = torch.topk(cos_scores, k=top_k)[1].tolist() | ||
# Get the corresponding context from the vault | ||
relevant_context = [vault_content[idx].strip() for idx in top_indices] | ||
return relevant_context | ||
|
||
|
||
# Function to interact with the Ollama model | ||
def ollama_chat(user_input, system_message, vault_embeddings, vault_content, model): | ||
# Get relevant context from the vault | ||
relevant_context = get_relevant_context(user_input, vault_embeddings, vault_content, model) | ||
if relevant_context: | ||
# Convert list to a single string with newlines between items | ||
context_str = "\n".join(relevant_context) | ||
print("Context Pulled from Documents: \n\n" + CYAN + context_str + RESET_COLOR) | ||
else: | ||
print(CYAN + "No relevant context found." + RESET_COLOR) | ||
|
||
# Prepare the user's input by concatenating it with the relevant context | ||
user_input_with_context = user_input | ||
if relevant_context: | ||
user_input_with_context = context_str + "\n\n" + user_input | ||
|
||
# Create a message history including the system message and the user's input with context | ||
messages = [ | ||
{"role": "system", "content": system_message}, | ||
{"role": "user", "content": user_input_with_context} | ||
] | ||
# Send the completion request to the Ollama model | ||
response = client.chat.completions.create( | ||
model="mistral", | ||
messages=messages | ||
) | ||
# Return the content of the response from the model | ||
return response.choices[0].message.content | ||
|
||
|
||
# How to use: | ||
# Load the model and vault content | ||
model = SentenceTransformer("all-MiniLM-L6-v2") | ||
vault_content = [] | ||
if os.path.exists("vault.txt"): | ||
with open("vault.txt", "r", encoding='utf-8') as vault_file: | ||
vault_content = vault_file.readlines() | ||
|
||
vault_embeddings = model.encode(vault_content) if vault_content else [] | ||
|
||
# Convert to tensor and print embeddings | ||
vault_embeddings_tensor = torch.tensor(vault_embeddings) | ||
print("Embeddings for each line in the vault:") | ||
print(vault_embeddings_tensor) | ||
|
||
# Example usage | ||
user_input = input(YELLOW + "Ask a question about your documents: " + RESET_COLOR) | ||
system_message = "You are a helpful assistat that is an expert at extracting the most useful information from a given text" | ||
response = ollama_chat(user_input, system_message, vault_embeddings_tensor, vault_content, model) | ||
print(NEON_GREEN + "Mistral Response: \n\n" + response + RESET_COLOR) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
import tkinter as tk | ||
from tkinter import filedialog | ||
import PyPDF2 | ||
import re | ||
|
||
# Function to convert PDF to text and append to vault.txt | ||
def convert_pdf_to_text(): | ||
file_path = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf")]) | ||
if file_path: | ||
with open(file_path, 'rb') as pdf_file: | ||
pdf_reader = PyPDF2.PdfReader(pdf_file) | ||
num_pages = len(pdf_reader.pages) | ||
text = '' | ||
for page_num in range(num_pages): | ||
page = pdf_reader.pages[page_num] | ||
if page.extract_text(): | ||
text += page.extract_text() + " " | ||
|
||
# Normalize whitespace and clean up text | ||
text = re.sub(r'\s+', ' ', text).strip() | ||
|
||
# Split text into chunks by sentences, respecting a maximum chunk size | ||
sentences = re.split(r'(?<=[.!?]) +', text) # split on spaces following sentence-ending punctuation | ||
chunks = [] | ||
current_chunk = "" | ||
for sentence in sentences: | ||
# Check if the current sentence plus the current chunk exceeds the limit | ||
if len(current_chunk) + len(sentence) + 1 < 1000: # +1 for the space | ||
current_chunk += (sentence + " ").strip() | ||
else: | ||
# When the chunk exceeds 1000 characters, store it and start a new one | ||
chunks.append(current_chunk) | ||
current_chunk = sentence + " " | ||
if current_chunk: # Don't forget the last chunk! | ||
chunks.append(current_chunk) | ||
|
||
with open("vault.txt", "a", encoding="utf-8") as vault_file: | ||
for chunk in chunks: | ||
# Write each chunk to its own line | ||
vault_file.write(chunk.strip() + "\n\n") # Two newlines to separate chunks | ||
print(f"PDF content appended to vault.txt with each chunk on a separate line.") | ||
|
||
# Create the main window | ||
root = tk.Tk() | ||
root.title("Upload .pdf") | ||
|
||
# Create a button to open the file dialog | ||
button = tk.Button(root, text="Upload PDF", command=convert_pdf_to_text) | ||
button.pack(pady=20) | ||
|
||
# Run the main event loop | ||
root.mainloop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
US President Joe Biden has praised American forces who he said "helped Israel take down nearly all" the drones and missiles launched by Iran on Sunday .In a statement, he said the US had moved aircraft and warships to the region before the unprecedented attack."I condemn these attacks in the strongest possible terms," he added.Israel said Iran launched hundreds of drones and missiles in its direction, the first time it has attacked Israel directly from its own territory .It said the "vast majority" were intercepted, but there were a small number of hits including at an IDF base in southern Israel.At least one person, reported to be a young girl, was injured.Iran earlier warned that Israel would be "punished" for a strike on its consulate in Syria on 1 April, which killed seven Iranian officers including a top commander .Israel has not confirmed or denied whether it was responsible. | ||
|
||
"I've just spoken with Prime Minister [Benjamin] Netanyahu to reaffirm America's ironclad commitment to the security of Israel," Mr Biden said shortly after the pair held a call. "I told him that Israel demonstrated a remarkable capacity to defend against and defeat even unprecedented attacks," he added.● LIVE UPDA TES: Iran launches drones at Israel in retaliatory attacks ● Iran launches aerial attack on Israel in major escalation Mr Biden also said he plans to convene G7 leaders on Sunday "to co-ordinate a united diplomatic response to Iran's brazen attack".He warned Iran against attacking any US assets, adding while Iran has not done so, America "remains vigilant to all threats".President Biden cut short a planned visit to his home state of Delaware on Saturday , travelling back to the White House to be briefed by national security officials hours before the attack. | ||
|
||
White House National Security Council spokeswoman Adrienne Watson said Mr Biden was "in constant communication with Israeli officials, as well as other partners and allies". Republicans in the House of Representatives, meanwhile, said they were drafting legislation to provide more aid to Israel and sanction Iran.What was in wave of Iranian attacks and how were they thwarted?For the first time ever , Iran has carried out strikes against Israeli territory .In the middle of Saturday night, air raid alerts went off in Israel, residents were urged to seek shelter while explosions were heard as air defences were activated.Interceptions lit up the night sky in several places across the country , while many drones and missiles were shot down by Israel's allies before they reached Israeli territory .At least nine countries were involved in the military escalation - with projectiles fired from Iran, Iraq, Syria and Yemen and downed by Israel, the US, the UK and France as well as Jordan. | ||
|
||
Here's what we know about the attack so far. ● LIVE UPDA TES: Follow the latest ● What is Israel's Iron Dome missile system?● Watch: Sirens activated as objects shot down from sky Attack involved drones, cruise missiles and ballistic missiles Iran launched more than 300 drones and missiles towards Israel, the Israeli military said on Sunday .The attack included 170 drones and 30 cruise missiles, none of which entered Israeli territory , and 110 ballistic missiles of which a small number reached Israel, military spokesman Rear Admiral Daniel Hagari said in a televised statement.The BBC has not independently verified those figures.The shortest distance from Iran to Israel is about 1,000km (620 miles) across Iraq, Syria and Jordan.Bombardment launched from several countries On Saturday night Iran's Revolutionary Guards Corps (IRGC) said it had launched drones and missiles.Iraqi security sources told Reuters that projectiles were seen flying over Iraq in the direction of Israel. | ||
|
||
The IRGC said ballistic missiles were fired almost an hour after the slower moving drones so that they would strike Israel at roughly the same time. The US defence department said US forces intercepted dozens of missiles and drones launched from Iran, Iraq, Syria and Yemen.The Iran-backed Hezbollah group in Lebanon also said it had fired two barrages of rockets at an Israeli military base in the occupied Golan Heights, a plateau which Israel has annexed from Syria in a move not recognised by most of the international community .Israel and allies intercept majority of drones and missiles Some 99% of the incoming barrage was intercepted either outside Israeli airspace or over the country itself, Rear Adm Hagari said.They included all the drones and cruise missiles, which follow a flat trajectory , and most of the ballistic missiles, which are fired on an arcing trajectory that uses gravity to reach very high speeds. | ||
|
||
US President Joe Biden said US forces "helped Israel take down nearly all" drones and missiles launched by Iran on Sunday . In a statement, he said the US had moved aircraft and warships to the region before the unprecedented attack.Operating from undisclosed bases in the region, US forces shot down a number of Iranian drones over southern Syria near the border with Jordan, security sources told Reuters.UK Prime Minister Rishi Sunak has confirmed that UK RAF Typhoon jets also shot down a number of Iranian attack drones.Mr Sunak said the Iranian attack was a "dangerous and unnecessary escalation which I've condemned in strongest terms".Jordan - which has a peace treaty with Israel but has been highly critical of the way it has carried out its war against the Palestinian group Hamas in Gaza - also intercepted flying objects that entered its airspace to secure the safety of its citizens, a Jordanian cabinet statement said. | ||
|
||
France helped to patrol airspace but it was unclear if they had shot down any drones or missiles, the Israeli military said. How many missiles got through and what damage did they cause?In Jerusalem BBC correspondents reported hearing sirens and seeing Israel's Iron Dome missile defence system in operation , which uses radar to track rockets and can differentiate between those that are likely to hit built-up areas and those that are not.Interceptor missiles are only fired at rockets expected to strike populated areas.A few of the ballistic missiles got through and struck Israeli territory , Rear Adm Hagari said.One of them "lightly hit" the Nevatim air force base in the Negev desert in southern Israel.Rear Adm Hagari said the base was "still functioning".Iran's official IRNA news agency said the attack had dealt "heavy blows" to the air base.A 10-year-old girl was severely injured by shrapnel, Rear Adm Hagari said. | ||
|
||
The girl from a Bedouin Arab community near the southern town of Arad, was reported to have been injured after an Iranian drone was intercepted overhead. She was in intensive care.Jordan also said that some shrapnel had fallen on its territory "without causing any significant damage or any injuries to citizens".What happens now?Israel's Channel 12 TV cited an unnamed Israeli official as saying there would be a "significant response" to the attack.Israeli airspace has been reopened as has that of neighbouring countries, but Defence Minister Yoav Gallant said the confrontation with Iran was "not over yet".Meanwhile Iran has warned Israel its response "will be much larger than tonight's military action if Israel retaliates against Iran", armed forces chief of staff Major General Mohammad Bagheri told state TV.He said US bases would also be attacked if the US took part in any Israeli retaliation. | ||
|
||
IRGC commander Hossein Salami also said Tehran would retaliate against any Israeli attack on its interests, officials or citizens. The UN Security Council is due to meet at around 20:00 GMT to the latest crisis at Israel's request.Mr Biden said he would also convene leaders of the G7 group of wealthy nations on Sunday to coordinate a "united diplomatic response" to Iran's "brazen" attack.Iran has warned Israel that any "reckless" retaliation to its unprecedented aerial attack would receive a "much stronger response".More than 300 drones and missiles were launched at Israel by Iran overnight, following the 1 April Israeli strike on Iran's consulate in Syria.Israel said it and allies had intercepted 99% of the weapons.It marked Iran's first direct attack on Israel, with the two countries having waged a years-long shadow war.World leaders have urged restraint amid concerns about a major escalation in tensions in the Middle East. | ||
|
||
Following the attack, Israel's Prime Minister Benjamin Netanyahu vowed "together we will win", but it is unclear how his country plans to respond. Last week, Israel's defence and foreign ministers warned that if Iran attacked Israel, Israel would strike back inside Iran.● LIVE UPDA TES: Follow the latest on the Iran-Israel attack ● EXPLAINED: Everything we know so far about wave of Iranian attacks ● LEARN: What is Israel's Iron Dome missile system?● UPDA TE ME: Israel on high alert after unprecedented Iranian attack ● WATCH: Explosions in sky over Jerusalem ● WATCH: Iran drone attack 'major escalation' - Israeli military An attack had been anticipated after the strike on the Damascus consulate killed seven Islamic Revolutionary Guard Corps (IRGC) officers.In a statement reported by AFP, Iran's President Ebrahim Raisi said "if the Zionist regime [Israel] or its supporters demonstrate reckless behaviour , they will receive a decisive and much stronger response". | ||
|
||
Iran's IRGC - the most powerful branch of its armed forces - said it had launched the attack "in retaliation against the Zionist regime's [Israel] repeated crimes, including the attack on the Iranian embassy's consulate in Damascus". Following the strikes the Iranian mission to the UN said "the matter can be deemed concluded".Iranian armed forces chief of staff Maj Gen Mohammad Bagheri told state TV the US had been warned - via Switzerland - that American backing of an Israeli retaliation would result in US regional bases being targeted.Iranian Foreign Minister Hossein Amir-Abdollahian said he had told the US attacks against Israel will be "limited" and for self-defence, Reuters news agency reported.US President Joe Biden spoke to Mr Netanyahu following the launch of the Iranian attack and reaffirmed "America's ironclad commitment to the security of Israel". | ||
|
||
He condemned the "unprecedented" attack on Israel and said the US had helped Israel and other allies to "take down nearly all" of the missiles and drones. Sirens sounded across Israel and loud explosions were heard over Jerusalem, with air defence systems shooting down objects over the city.The New York Times, citing Israeli intelligence sources, reported that the main targets appeared to be military instillations in the occupied Golan Heights.An Israeli military spokesman said around 360 munitions had been fired - including 170 explosive drones, 30 cruise missiles and 120 ballistic missiles - but Israel has said very little damage had been done.Israel Defense Forces (IDF) spokesman Rear Adm Daniel Hagari said some Iranian missiles had hit inside Israel, causing minor damage to a military base but no casualties.He said a 10-year-old Bedouin girl had been severely injured by shrapnel from falling debris in the southern Arad region. | ||
|
||
World leaders will be considering their response to the major escalation in tensions between Israel and Iran, with many condemning the attack or warning of the dangers of escalation. Mr Biden said he would convene "my fellow G7 leaders to co-ordinate a united diplomatic response to Iran's brazen attack".The UN Security Council will also hold an emergency meeting later , its president Vanessa Frazier said.UN Secretary General António Guterres issued a statement saying he "strongly condemn[ed] the serious escalation represented by the large-scale attack launched on Israel" by Iran.He called for "an immediate cessation of these hostilities" and for all sides to exercise maximum restraint.UK Prime Minister Rishi Sunak called the strikes "reckless", while the European Union's foreign affairs chief Josep Borrell said it was a "grave threat to regional security". | ||
|
||
China's foreign ministry urged restraint, characterising it as "the latest spillover of the Gaza conflict", while Russia's foreign ministry expressed "extreme concern over another dangerous escalation". On Sunday , France recommended its citizens in Iran should temporarily leave the country due to the risk of military escalation.Speaking to crowds at the Vatican on Sunday , Pope Francis made a "heartfelt appeal for a halt to any action that could fuel a spiral of violence with the risk of dragging the Middle East into an even greater conflict".There have been increased tensions in the Middle East since the 7 October Hamas attacks on Israel, in which about 1,200 people, mostly civilians, were killed and more than 250 others were taken hostage.The subsequent Israeli military operation in Gaza has killed 33,729 people, mostly civilians, according to the Hamas-run health ministry . | ||
|
||
Meanwhile Israel's spy agency Mossad said Hamas negotiators had rejected the most recent proposal put forward by mediators in peace talks. In a statement it said this proved Hamas' Gaza leader Yahya Sinwar "does not want a humanitarian deal and the return of the hostages" and was "continuing to exploit the tension with Iran".On Saturday , Hamas said it stood by its demand for a permanent ceasefire in the Gaza war, a complete Israeli withdrawal from the Gaza Strip, a return of Palestinians displaced by the fighting to their homes, and increased humanitarian aid to the territory . | ||
|