Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Advanced Mistral 7 b based LLM chatbot #1190

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Mistral 7B LLM based advanced Chatbot/Flask/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from flask import Flask, render_template, request, jsonify
from chat import chatbot

app = Flask(__name__)


@app.route("/")
def hello():
return render_template('chat.html')

@app.route("/ask", methods=['POST'])
def ask():

message = str(request.form['messageText'])

bot_response = chatbot(message)

return jsonify({'status':'OK','answer':bot_response})


if __name__ == "__main__":
app.run()
26 changes: 26 additions & 0 deletions Mistral 7B LLM based advanced Chatbot/Flask/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from peft import AutoPeftModelForCausalLM
from transformers import GenerationConfig
from transformers import AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("Vasanth/mistral-finetuned-alpaca")

model = AutoPeftModelForCausalLM.from_pretrained(
"Vasanth/mistral-finetuned-alpaca",
low_cpu_mem_usage=True,
return_dict=True,
torch_dtype=torch.float16,
device_map="cuda")

generation_config = GenerationConfig(
do_sample=True,
top_k=1,
temperature=0.1,
max_new_tokens=100,
pad_token_id=tokenizer.eos_token_id
)

def chatbot(message):
input_str = "###Human: " + message + " ###Assistant: "
inputs = tokenizer(input_str, return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, generation_config=generation_config)
return tokenizer.decode(outputs[0], skip_special_tokens=True).replace(input_str, '')
2 changes: 2 additions & 0 deletions Mistral 7B LLM based advanced Chatbot/Flask/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
##OPEN API STUFF
OPENAI_API_KEY = "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"
160 changes: 160 additions & 0 deletions Mistral 7B LLM based advanced Chatbot/Flask/templates/chat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<!DOCTYPE html>
<html>

<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mistral Chatbot</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
<style type="text/css">
.fixed-panel {
min-height: 400px;
max-height: 400px;
background-color: #19313c;
color: white;
overflow: auto;
}

.media-list {
overflow: auto;
clear: both;
display: table;
overflow-wrap: break-word;
word-wrap: break-word;
word-break: normal;
line-break: strict;
}

.panel {
margin-bottom: 20px;
background-color: #fff;
border: 6px solid transparent;
border-radius: 25px;
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
}

.panel-info {
border-color: #0c2735;
}

.panel-info>.panel-heading {
color: white;
background-color: #0c2735;
border-color: #0c2735;
}

.panel-footer {
padding: 10px 15px;
background-color: #0c2735;
border-top: 1px solid #0c2735;
border-bottom-right-radius: 3px;
border-bottom-left-radius: 3px
}

body {
background: rgb(96, 143, 149);
/* Old browsers */
background: -moz-linear-gradient(-45deg, rgba(96, 143, 149, 1) 0%, rgba(0, 133, 136, 1) 9%, rgba(12, 36, 73, 1) 52%, rgba(26, 30, 59, 1) 100%);
/* FF3.6-15 */
background: -webkit-linear-gradient(-45deg, rgba(96, 143, 149, 1) 0%, rgba(0, 133, 136, 1) 9%, rgba(12, 36, 73, 1) 52%, rgba(26, 30, 59, 1) 100%);
/* Chrome10-25,Safari5.1-6 */
background: linear-gradient(135deg, rgba(96, 143, 149, 1) 0%, rgba(0, 133, 136, 1) 9%, rgba(12, 36, 73, 1) 52%, rgba(26, 30, 59, 1) 100%);
/* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#608f95', endColorstr='#1a1e3b', GradientType=1);
/* IE6-9 fallback on horizontal gradient */
}

body {
height: 100vh;
}

.container {
height: 100%;
}
/* powered by https://sohipm.com */
</style>
</head>

<body>
<div class="container background-color: rgb(255,0,255);">
<br />
<br />
<br />
<div class="row">
<!-- <h1 style="text-align: center; color: #f2f2f2;" >Welcome To Climate Change Chatbot</h1> -->
<!-- class="col-md-8 col-md-offset-2" -->
<!-- d-flex align-items-center justify-content-center -->
<div class="col-md-8 col-md-offset-2">
<div id="chatPanel" class="panel panel-info">
<div class="panel-heading">
<strong><span class="glyphicon glyphicon-globe"></span> Welcome To Mistral Chatbot !!!
(You: Green / Bot:
White) </strong>
</div>
<div class="panel-body fixed-panel">
<ul class="media-list">
</ul>
</div>
<div class="panel-footer">
<form method="post" id="chatbot-form">
<div class="input-group">
<input type="text" class="form-control" placeholder="Enter Message" name="messageText"
id="messageText" autofocus />
<span class="input-group-btn">
<button class="btn btn-info" type="button" id="chatbot-form-btn">Send</button>
</span>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

Check warning

Code scanning / CodeQL

Inclusion of functionality from an untrusted source Medium

Script loaded from content delivery network with no integrity check.
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script>
var exports = {};
</script>
<script>
$(function () {

$('#chatbot-form-btn').click(function (e) {
e.preventDefault();
$('#chatbot-form').submit();
});

$('#chatbot-form').submit(function (e) {
e.preventDefault();
var message = $('#messageText').val();
$(".media-list").append(
'<li class="media"><div class="media-body"><div class="media"><div style = "text-align:right; color : #2EFE2E" class="media-body">' +
message + '<hr/></div></div></div></li>');
Comment on lines +133 to +134

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.

$.ajax({
type: "POST",
url: "/ask",
data: $(this).serialize(),
success: function (response) {
$('#messageText').val('');
var answer = response.answer;
const chatPanel = document.getElementById("chatPanel");
$(".media-list").append(
'<li class="media"><div class="media-body"><div class="media"><div style = "color : white" class="media-body">' +
answer + '<hr/></div></div></div></li>');
$(".fixed-panel").stop().animate({
scrollTop: $(".fixed-panel")[0].scrollHeight
}, 1000);
},
error: function (error) {
console.log(error);
}
});
});
});
</script>
</body>

</html>
23 changes: 23 additions & 0 deletions Mistral 7B LLM based advanced Chatbot/Flask/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Questions,Answers
What is global warming?,Global warming is a long-term increase in Earth's average surface temperature due to human-made emissions of greenhouse gases.
What causes climate change?,"Climate change is caused by factors such as biotic processes, variations in solar radiation received by Earth, plate tectonics, and volcanic eruptions. Certain human activities have also been identified as significant causes of recent climate change, often referred to as global warming."
How can we mitigate climate change?,"Climate change can be mitigated through a variety of means such as reducing greenhouse gas emissions, promoting the use of renewable energy sources, improving insulation in buildings, and adopting sustainable farming practices."
What is the effect of climate change on biodiversity?,"Climate change affects biodiversity by altering the timing of natural events, shifting habitats, and increasing the rate of disease. In some cases, species may not be able to adapt to these changes quickly enough, which can result in reduced populations and even extinction."
What are greenhouse gases?,"Greenhouse gases are gases in Earth's atmosphere that trap heat. They let sunlight pass through the atmosphere, but they prevent the heat that the sunlight brings from leaving the atmosphere. The main greenhouse gases are carbon dioxide, methane, nitrous oxide, and fluorinated gases."
What is carbon footprint?,"A carbon footprint is the total greenhouse gas (GHG) emissions caused by an individual, event, organization, or product, expressed as carbon dioxide equivalent."
What is climate change adaptation?,"Climate change adaptation is the process of adjusting to current or expected climate change and its effects. It is one of the ways to respond to climate change, along with climate change mitigation."
What is the Paris Agreement?,"The Paris Agreement is a legally binding international treaty on climate change. It was adopted by 196 Parties at COP 21 in Paris, on 12 December 2015. Its goal is to limit global warming to well below 2, preferably to 1.5 degrees Celsius, compared to pre-industrial levels."
What is the impact of deforestation on climate change?,"Deforestation affects climate change on a large scale, as trees absorb CO2 when they grow. When they are cut down and burned or allowed to rot, their stored carbon is released back into the air."
Can renewable energy sources help combat climate change?,"Yes, shifting to renewable energy sources like wind, solar, and hydro can significantly help in reducing the emission of greenhouse gases and thus combat climate change."
What is the role of individuals in combating climate change?,"Individuals can play a significant role in combating climate change. This can be done by reducing personal carbon footprints, through measures like using energy efficiently, reducing waste, choosing sustainable products, using public transportation, and advocating for policies that support renewable energy and other sustainable practices."
How does climate change affect human health?,"Climate change affects human health in several ways. It can increase heat-related illnesses, exacerbate respiratory disorders due to poor air quality, alter the spread of vector-borne diseases, and increase the risk of illnesses caused by unsafe or insufficient water or lack of food."
What is GR491?,"GR491 is a Handbook of Sustainable Design of Digital Services, created by Institutes for Sustainable IT, learn more at https://gr491.isit-europe.org/en/#main-container"
What is a Carbon footprint?,"The Carbon footprint is a measure of the amount of carbon dioxide released into the atmosphere as a result of the activities of a particular individual, organization, or community."
Why is important to measure the Digital Services' carbon footprint?,"Calculating your Digital Solution’s carbon footprint is a necessary step to understanding your company’s contribution to global warming so you can identify ways to reduce it. Plus, consumers are increasingly interested in transparency around the environmental impacts of the products they use"
How to assess the carbon footprint of my digital solution?,"Identify the Digital Solutions that are your sources of carbon dioxide emissions
Collect data and quantify the amount of energy that is used
Look for potential reduction opportunities
Go to Carbon assessment to start your journey"
What can I do to reduce my digital solution carbon footprint?,"Once you have the results of the Carbon assessment, deepen into the results to identify disproportionate emissions sources and think about where there might be opportunities to reduce your footprint, if your main source of emissions is a website that lots of people visit, some small tweaks to the site design can increase its energy efficiency and reduce its carbon footprint."
What is SDG?,"The Sustainable Development Goals (SDGs), also known as the Global Goals, were adopted by the United Nations in 2015 as a universal call to action to end poverty, protect the planet, and ensure that by 2030 all people enjoy peace and prosperity."
How to learn more about sustainability?,Go to the Awareness section and take one of the quizzes catered for you or create a custom quiz based on your interests

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions Mistral 7B LLM based advanced Chatbot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Mistral LLM Chatbot

Mistral LLM is a language model developed by Mistral AI. It is a large language model designed to understand and generate human-like text. LLM stands for "Large Language Model," indicating its size and capabilities.

Mistral 7B is a specific model within the Mistral LLM family. The "7B" refers to the number of parameters in the model, with larger numbers generally indicating more powerful and capable models. Mistral 7B is one of the latest models from Mistral AI and is designed to outperform the previous Llama 2 13B model on various benchmarks.

Mistral 7B is trained on a large amount of text data and can be used for a wide range of natural language processing tasks, such as text generation, question answering, language translation, and more. It is designed to understand and generate text in a way that is coherent and contextually relevant.

In this repository, we have used Mistral 7B to create a LLM chatbot:


## Introduction
Mistral LLM is a state-of-the-art language model developed by Mistral AI. It is part of the Large Language Model (LLM) family, designed to generate human-like text based on the input it receives. The Mistral 7B model is specifically optimized for high performance, outperforming previous models like Llama 2 13B in various benchmarks.

This project utilizes Mistral 7B, a cutting-edge language model with 7 billion parameters, to create a highly capable conversational chatbot. The Mistral 7B model can generate natural, contextually relevant responses, making it ideal for a wide range of natural language processing (NLP) tasks, including text generation, question answering, and language translation.

## Key Features:
Text Generation: Generate coherent and contextually accurate responses.
Question Answering: Answer questions based on context.
Multilingual Support: Understand and respond in multiple languages.
Contextual Understanding: Maintain conversation context over multiple interactions.


Pre-trained Mistral 7B model for natural language processing tasks.
Real-time text generation using the language model.
Contextual memory: Remembers previous conversation history.
Fast and scalable deployment, capable of handling multiple user queries.

## Requirements
To run the Mistral LLM Chatbot, you need to install the following dependencies:

Python 3.8+
PyTorch (for using the Mistral 7B model)
Transformers library from Hugging Face
CUDA (optional, for GPU acceleration)

![mistral1](https://github.com/user-attachments/assets/cfb19f50-5438-4c77-b84b-9e30febfe91e)



Loading