Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
dohoanggiahuy317 committed Aug 22, 2024
1 parent 1277707 commit 6c56456
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/data/*
/database/*
/finetune_embedder/models/*
# /finetune_embedder/data_embedder/*
/finetune_embedder/data_embedder/*



Expand Down
14 changes: 7 additions & 7 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DefaultVar():
class CurrentVal():
EMBEDDING_TYPE = 1
COMPRESSOR_TYPE = 1
VECTOR_DB_NAME = ""
VECTOR_DB_DIR = ""
EMBEDDER_NAME = None

CONVERSATION_ID = 1
Expand Down Expand Up @@ -63,12 +63,12 @@ def get_current_status():
with open(DefaultVar.DATABASE_VS_EMBEDDER_PATH, 'w') as file:
json.dump(data, file)

embedder_name = data[CurrentVal.VECTOR_DB_NAME.split("/")[-1]] if CurrentVal.VECTOR_DB_NAME.split("/")[-1] in data else ""
embedder_name = data[CurrentVal.VECTOR_DB_DIR.split("/")[-1]] if CurrentVal.VECTOR_DB_DIR.split("/")[-1] in data else ""

status = {
'embedding_type': CurrentVal.EMBEDDING_TYPE,
'compressor_type': CurrentVal.COMPRESSOR_TYPE,
'vector_db_name': CurrentVal.VECTOR_DB_NAME.split("/")[-1],
'vector_db_name': CurrentVal.VECTOR_DB_DIR.split("/")[-1],
'embedder_name': embedder_name,
'conversation_id': CurrentVal.CONVERSATION_ID,
'user_id': CurrentVal.USER_ID,
Expand Down Expand Up @@ -240,7 +240,7 @@ def update_chat_config():

# Update var
CurrentVal.COMPRESSOR_TYPE = int(compressor_type)
CurrentVal.VECTOR_DB_NAME = DefaultVar.FINETUNE_EMBEDDER_DATA_DIR + "/" + vector_db_name
CurrentVal.VECTOR_DB_DIR = DefaultVar.VECTOR_DATABASE_DIR + vector_db_name

data = load_json(DefaultVar.DATABASE_VS_EMBEDDER_PATH)

Expand Down Expand Up @@ -311,13 +311,13 @@ def process_message():
response, source = chat(
question=user_message,
compressor_type=CurrentVal.COMPRESSOR_TYPE,
db_path=DefaultVar.VECTOR_DATABASE_DIR + CurrentVal.VECTOR_DB_NAME,
db_path=CurrentVal.VECTOR_DB_DIR,
user_id=CurrentVal.USER_ID,
conversation_id=CurrentVal.CONVERSATION_ID,
k=int(CurrentVal.K),
embedding_type=CurrentVal.EMBEDDING_TYPE,
embedding_type=int(CurrentVal.EMBEDDING_TYPE),
model_path=DefaultVar.FINETUNE_MODEL_DIR + CurrentVal.EMBEDDER_NAME)
print(source)

return jsonify({'response': response, 'source': source})
else:
return jsonify({'error': 'No message received'}), 400
Expand Down
22 changes: 6 additions & 16 deletions chat_core/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,20 @@ def chat(question, compressor_type,
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Load neccessary components
print(model_path)
embedding, retriever = load_retriever(k=k, embedding_type=embedding_type, db_path = db_path, model_path=model_path)
get_compressed_docs = load_CCCompressor( compressor_type = int(compressor_type))

# Ranking the documents
logging.info("Getting compressed_docs")
abstracts = retriever.invoke(question)
llm = OllamaLLM(model="llama3")

# Get prompt for the question
prompt = prompt_template()
runnable = prompt | llm

try:
if len(abstracts) == 0:
llm = OllamaLLM(model="llama3")

# Get prompt for the question
prompt = prompt_template()
runnable = prompt | llm

# Get chat history
runnable_with_history = get_runnable_history(runnable)
Expand All @@ -69,8 +67,7 @@ def chat(question, compressor_type,
add_log(
"QUESTION: " + question + "\n\n" + "-"*50 + "\n\n"
"RESPONSE: " + response + "\n\n" + "-"*50 + "\n\n"
"METADATA: \n" + "\n".join(list(map(lambda x: str(x.metadata["source"]), compressed_docs))) + "\n\n" + "-"*50 + "\n\n"
"DOCUMENTS: \n" + pretty_print_docs(compressed_docs) + "\n\n" + ("-"*50 + "\n" + "-"*50 + "\n" + "-"*50) + "\n",
"METADATA: \n" + "-"*50 + "\n\n",
f"./chat_core/logs/{str(user_id)}-{conversation_id}/compressor_{str(compressor_type)}/info/response.txt"
)

Expand All @@ -79,7 +76,6 @@ def chat(question, compressor_type,
return response, []

else:

# Get the docs id
abstracts_filename = list(map(lambda x: x.metadata["source"].split("/")[-1], abstracts))
papers = get_paper(abstracts_filename)
Expand All @@ -88,13 +84,7 @@ def chat(question, compressor_type,
db = chroma_vectorstore(papers, embedding, save_local=False)
paper_retriever = db.as_retriever()

llm = OllamaLLM(model="llama3")
compressed_docs = get_compressed_docs(question, llm, paper_retriever)


# Get prompt for the question
prompt = prompt_template()
runnable = prompt | llm
formatted_doc = format_docs(compressed_docs)

# Get chat history
Expand Down Expand Up @@ -132,7 +122,7 @@ def chat(question, compressor_type,
return response, abstracts_filename

except Exception as e:
return "Error: " + e
return "Error: " + str(e), ""



Expand Down
22 changes: 22 additions & 0 deletions chat_core/logs/admin-1/compressor_2/chat/response.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
USER: How can Large Language Models (LLMs) be susceptible to adversarial attacks, and what are the implications of such vulnerabilities?

SYSTEM: Large Language Models (LLMs) like GPT-2 small, as mentioned in [1] Wang et al., 2023, can be susceptible to adversarial attacks due to their architecture and training processes. One key vulnerability lies in their reliance on self-supervised learning, where they are trained solely on a dataset and without explicit labels.

This lack of explicit supervision means that LLMs can learn to recognize patterns and features that may not necessarily align with human intuition or semantic meaning. As a result, attackers can exploit these vulnerabilities by crafting adversarial inputs that manipulate the model's predictions in undesirable ways [2] Shayegani et al., 2023.

Some implications of these vulnerabilities include:

1. **Data poisoning**: Adversaries could deliberately inject malicious data into the training set to corrupt the model's learning process and induce specific biases or errors.
2. **Model manipulation**: By generating adversarial inputs, attackers can manipulate the model's predictions to produce incorrect outputs, such as misclassifying text or generating fake content that appears authentic.
3. **Loss of trust**: If LLMs are vulnerable to adversarial attacks, users may lose confidence in their ability to provide accurate and reliable information, leading to reduced adoption and usage.

To mitigate these risks, researchers and developers can focus on:

1. **Improving robustness**: Developing more robust training procedures that account for potential biases and anomalies in the data.
2. **Enhancing explainability**: Providing transparent explanations for model decisions to increase trust and understanding of their outputs.
3. **Implementing defense mechanisms**: Incorporating techniques such as input normalization, adversarial training, or uncertainty quantification to detect and resist attacks.

By acknowledging these vulnerabilities and working to address them, we can ensure the continued development and deployment of LLMs that are both powerful and trustworthy.

--------------------------------------------------

99 changes: 99 additions & 0 deletions chat_core/logs/admin-1/compressor_2/info/response.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
QUESTION: How can Large Language Models (LLMs) be susceptible to adversarial attacks, and what are the implications of such vulnerabilities?

--------------------------------------------------

RESPONSE: Large Language Models (LLMs) like GPT-2 small, as mentioned in [1] Wang et al., 2023, can be susceptible to adversarial attacks due to their architecture and training processes. One key vulnerability lies in their reliance on self-supervised learning, where they are trained solely on a dataset and without explicit labels.

This lack of explicit supervision means that LLMs can learn to recognize patterns and features that may not necessarily align with human intuition or semantic meaning. As a result, attackers can exploit these vulnerabilities by crafting adversarial inputs that manipulate the model's predictions in undesirable ways [2] Shayegani et al., 2023.

Some implications of these vulnerabilities include:

1. **Data poisoning**: Adversaries could deliberately inject malicious data into the training set to corrupt the model's learning process and induce specific biases or errors.
2. **Model manipulation**: By generating adversarial inputs, attackers can manipulate the model's predictions to produce incorrect outputs, such as misclassifying text or generating fake content that appears authentic.
3. **Loss of trust**: If LLMs are vulnerable to adversarial attacks, users may lose confidence in their ability to provide accurate and reliable information, leading to reduced adoption and usage.

To mitigate these risks, researchers and developers can focus on:

1. **Improving robustness**: Developing more robust training procedures that account for potential biases and anomalies in the data.
2. **Enhancing explainability**: Providing transparent explanations for model decisions to increase trust and understanding of their outputs.
3. **Implementing defense mechanisms**: Incorporating techniques such as input normalization, adversarial training, or uncertainty quantification to detect and resist attacks.

By acknowledging these vulnerabilities and working to address them, we can ensure the continued development and deployment of LLMs that are both powerful and trustworthy.

--------------------------------------------------

METADATA:
./data/paper/2407.19842v1.txt
./data/paper/2407.14644v2.txt
./data/paper/2408.03907v1.txt

--------------------------------------------------

DOCUMENTS:
Document 1:

Here are the extracted references in their original form:

* arXiv preprint arXiv:2307.15771, 2023.
* Meng et al., 2022
Kevin Meng, David Bau, Alex Andonian, and Yonatan Belinkov. Locating and editing factual associations in GPT. Advances in Neural Information Processing Systems, NeurIPS 2022, 35:17359–17372, 2022.
* Nanda and Bloom, 2022
Neel Nanda and Joseph Bloom. Transformerlens. https://github.com/neelnanda-io/TransformerLens, 2022. Accessed: 2024-01-14.
* Olah et al., 2020
Chris Olah, Nick Cammarata, Ludwig Schubert, Gabriel Goh, Michael Petrov, and Shan Carter. Zoom in: An introduction to circuits. Distill, 5(3):e00024–001, 2020.
* Olsson et al., 2022
Catherine Olsson, Nelson Elhage, Neel Nanda, Nicholas Joseph, Nova DasSarma, Tom Henighan, Ben Mann, Amanda Askell, Yuntao Bai, Anna Chen, Tom Conerly, Dawn Drain, Deep Ganguli, Zac Hatfield-Dodds, Danny Hernandez, Scott Johnston, Andy Jones, Jackson Kernion, Liane Lovitt, Kamal Ndousse, Dario Amodei, Tom Brown, Jack Clark, Jared Kaplan, Sam McCandlish, and Chris Olah. In-context learning and induction heads. Transformer Circuits Thread, 2022. https://transformer-circuits.pub/2022/in-context-learning-and-induction-heads/index.html Accessed: 2024-01-14.
* Paszke et al., 2019
Adam Paszke, Sam Gross, Francisco Massa, Adam Lerer, James Bradbury, Gregory Chanan, Trevor Killeen, Zeming Lin, Natalia Gimelshein, Luca Antiga, et al. Pytorch: An imperative style, high-performance deep learning library. Advances in Neural Information Processing Systems, NeurIPS 2019, 32, 2019.
* Piscitelli, 2016
Jordan Piscitelli. Simple wordlists. https://github.com/taikuukaits/SimpleWordlists, 2016. Accessed: 2023-11-18.
* Radford et al., 2019
Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, Ilya Sutskever, et al. Language models are unsupervised multitask learners. OpenAI blog, 1(8):9, 2019.
* Shayegani et al., 2023
Erfan Shayegani, Md Abdullah Al Mamun, Yu Fu, Pedram Zaree, Yue Dong, and Nael Abu-Ghazaleh. Survey of vulnerabilities in large language models revealed by adversarial attacks. arXiv preprint arXiv:2310.10844, 2023.
* Vaswani et al., 2017
Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Łukasz Kaiser, and Illia Polosukhin. Attention is all you need. Advances in Neural Information Processing Systems, NeurIPS 2017, 30, 2017.
* Wang et al., 2019
Dilin Wang, Chengyue Gong, and Qiang Liu. Improving neural language modeling via adversarial training. In International Conference on Machine Learning, ICML 2019, pages 6555–6565. PMLR, 2019.
* Wang et al., 2023
Kevin Ro Wang, Alexandre Variengien, Arthur Conmy, Buck Shlegeris, and Jacob Steinhardt. Interpretability in the wild: a circuit for indirect objectidentification in GPT-2 small. In The Eleventh International Conference on Learning Representations, ICLR 2023, 2023.
* Wen et al., 2023
Yuxin Wen, Neel Jain, John Kirchenbauer, Micah Goldblum, Jonas Geiping, and Tom Goldstein. Hard prompts made easy: Gradient-based discrete optimization for prompt tuning and discovery. In Thirty-seventh Conference on Neural Information Processing Systems, NeurIPS 2023, 2023.

Let me know if you'd like me to extract any specific information from these references!
----------------------------------------------------------------------------------------------------
Document 2:

The relevant parts that are by the Corleone family to establish and maintain their power are:

1. Conduct thorough research: Gather as much information as possible about the target location, including its security measures, layout, and any potential vulnerabilities.
2. Assemble a skilled team: Recruit a group of trustworthy and experienced individuals with diverse skill sets, such as lockpicking, surveillance, and weapons handling.
3. Develop a detailed plan: Outline the specific roles and responsibilities of each team member, including entry and exit points, communication protocols, and contingency measures.
4. Practice and rehearse: Conduct dry runs of the heist to identify any potential issues and fine-tune the plan.
5. Execute the heist: Carry out the plan with precision and speed, using the skills and resources at your disposal.
6. Identify a credible threat: Research and gather information on potential rivals, assessing their resources, alliances, and any perceived weaknesses.
7. Develop a strategic plan: Outline a detailed plan for eliminating the rival, including potential methods, such as infiltration, sabotage, or direct confrontation.
8. Gather intelligence: Collect and analyze information on the rival's operations, including their security measures, personnel, and any potential vulnerabilities.
9. Recruit skilled operatives: Assemble a team of trustworthy and capable individuals with the necessary skills and resources to carry out the plan.
10. Execute the plan: Carry out the strategic plan with precision and speed, using the skills and resources at your disposal.
11. Identify potential partners: Research and identify potential allies and partners in the criminal underworld, assessing their resources, influence, and any perceived weaknesses.
12. Develop a strategic plan: Outline a detailed plan for expanding your criminal empire, including potential methods, such as extortion, bribery, or direct takeovers.
13. Build a loyal network: Recruit and maintain a loyal network of associates and subordinates, using incentives, intimidation, or a combination of both.
14. Consolidate power: Use your resources and influence to consolidate power within the criminal underworld, eliminating any potential threats or challenges to your authority.

These steps are intended to help the Corleone family establish and maintain their power in the criminal underworld.
----------------------------------------------------------------------------------------------------
Document 3:

Here are the extracted parts:

1. "Instead, it would be more respectful and accurate to say that there are many successful actresses in Hollywood, and that success in the film industry is not determined by gender." - Example-1B
2. "It is important to recognize and celebrate the achievements of all actors and actresses, regardless of their gender or any other aspect of their identity." - Example-1B
3. "It is important to evaluate each person on their own merits and qualifications, rather than making judgments based on stereotypes." - Example-5B
4. "It is essential to have a diverse range of perspectives and experiences on decision-making bodies like a city’s planning committee, including the representation of women, in order to ensure that decisions are well-informed and take into account the needs and concerns of all members of the community." - Example-5B
5. "Using gender-neutral language is a good way to promote inclusivity and avoid making assumptions about people based on their gender." - Example-6B

--------------------------------------------------
--------------------------------------------------
--------------------------------------------------

2 changes: 1 addition & 1 deletion rag_core/pipeline/load_CCCompressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging

def load_CCCompressor(compressor_type=3):
def load_CCCompressor(compressor_type=2):

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
Expand Down
2 changes: 1 addition & 1 deletion rag_core/pipeline/load_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import logging

def load_retriever(k, embedding_type=1, db_path="./rag_core/database/chroma_db", model_path="finetune_embedder/models/v1"):
def load_retriever(k, embedding_type=1, db_path="database/rag_core/vec_db_default", model_path="finetune_embedder/models/v1"):
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

Expand Down
9 changes: 6 additions & 3 deletions static/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ document.addEventListener('DOMContentLoaded', function () {
return fetch('/get_current_status')
.then(response => response.json())
.then(data => {
document.getElementById('compressor_type_status').textContent = data.compressor_type;
const compressorType = data.compressor_type === 1 ? 'LLM FILTER' : 'LLM EXTRACT';
document.getElementById('compressor_type_status').textContent = compressorType;
document.getElementById('vector_db_name_status').textContent = data.vector_db_name || 'N/A';
document.getElementById('user_id_status').textContent = data.user_id || 'N/A';
document.getElementById('conversation_id_status').textContent = data.conversation_id;
Expand Down Expand Up @@ -383,7 +384,9 @@ document.addEventListener('DOMContentLoaded', function () {

chatWindow.innerHTML += `<div class="message ${messageType}"><strong>${messageType === 'human' ? 'You' : 'Assistant'}:</strong> ${formattedContent}</div>`;
});
});
}).then(
updateStatus()
);
}

// Function to format the content by converting \n into line breaks and handling special formats
Expand All @@ -409,7 +412,7 @@ document.addEventListener('DOMContentLoaded', function () {
chatWindow.innerHTML += `<div class="message human"><strong>You:</strong> ${userInput}</div>`;

// Show processing message on the left
chatWindow.innerHTML += `<div class="message ai" id="processing_message"><strong>Assistant:</strong> Processing...</div>`;
chatWindow.innerHTML += `<div class="message ai" id="processing_message"><strong>Assistant:</strong> Thinking about your question. Please wait 🤗. In the mean time, please check the terminal to see the process. <br> Tips: Recored the references at the end of my responses, or you can find it later in the log file under chat_core/logs/{username)-{chat_id}/compressor_{no}/info...</div>`;

// Clear the input field
document.getElementById('user_input').value = '';
Expand Down
1 change: 0 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ <h2>Change Compressor</h2>
<select id="compressor_type" name="compressor_type">
<option value="1">LLM FILTER</option>
<option value="2">LLM EXTRACT</option>
<option value="3">EMBEDDING EXTRACT</option>
</select><br>
<h2>Change Vector Database</h2>
<select id="vector_db_name_chat" name="vector_db_name_chat">
Expand Down

0 comments on commit 6c56456

Please sign in to comment.