Skip to content

Commit a8e298a

Browse files
committed
Initial commit
0 parents  commit a8e298a

File tree

5 files changed

+211
-0
lines changed

5 files changed

+211
-0
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Lightning ⚡️ AI Style Guide Assistant
2+
3+
<p align="center">
4+
<img src="assets/logo.png" alt="AI Style Guide Assistant Logo" width="200" height="200">
5+
</p>
6+
7+
[![GitHub Stars](https://img.shields.io/github/stars/MaxMLang/lightningAI-styleguide-assistant?style=social)](https://github.com/MaxMLang/lightningAI-styleguide-assistant/stargazers)
8+
[![GitHub Issues](https://img.shields.io/github/issues/MaxMLang/lightningAI-styleguide-assistant)](https://github.com/MaxMLang/lightningAI-styleguide-assistant/issues)
9+
[![GitHub Pull Requests](https://img.shields.io/github/issues-pr/MaxMLang/lightningAI-styleguide-assistant)](https://github.com/MaxMLang/lightningAI-styleguide-assistant/pulls)
10+
[![License](https://img.shields.io/github/license/MaxMLang/lightningAI-styleguide-assistant)](https://github.com/MaxMLang/lightningAI-styleguide-assistant/blob/main/LICENSE)
11+
12+
The Lightning ⚡️ AI Style Guide Assistant is a specialized adaptation of the [Lightning Chatbot](https://github.com/MaxMLang/lightningfast-ai-chat), designed to assist in maintaining consistency and accuracy in style guides. Leveraging Groq LPUs, it provides real-time style and grammar recommendations, making it an essential tool for editors and writers. This AI-powered assistant enhances productivity by automating the enforcement of writing standards and style consistency.
13+
14+
## Features
15+
16+
- ⚡ Real-time style and grammar recommendations powered by Groq LPUs.
17+
- 📘 Supports custom style guide integration for personalized recommendations.
18+
- 💬 Interactive editing experience with instant feedback.
19+
- 🌐 Deploy easily using the Streamlit web framework.
20+
21+
## Installation
22+
23+
1. Clone the repository:
24+
```
25+
git clone https://github.com/MaxMLang/lightningAI-styleguide-assistant.git
26+
```
27+
2. Navigate to the project directory:
28+
```
29+
cd lightningAI-styleguide-assistant
30+
```
31+
3. Install the required dependencies:
32+
```
33+
pip install -r requirements.txt
34+
```
35+
4. Configure the Groq API key:
36+
- Create a `.env` file in the project directory.
37+
- Add the following line, replacing `your_api_key` with your actual Groq API key:
38+
```
39+
GROQ_API_KEY=your_api_key
40+
```
41+
42+
5. Launch the assistant:
43+
```
44+
streamlit run app.py
45+
```
46+
47+
6. Open the provided URL in your browser to access the AI Style Guide Assistant interface.
48+
49+
## Usage
50+
51+
1. Select your custom style guide settings from the sidebar.
52+
2. Type a block of text into the input field and submit.
53+
3. Receive style and grammar recommendations based on your settings.
54+
4. Apply or dismiss suggestions and see the edits in real-time.
55+
5. Continue refining your text with further input and revisions.
56+
57+
## License
58+
59+
This project is licensed under the [MIT License](LICENSE).
60+
61+
## Acknowledgements
62+
63+
- [Groq](https://groq.com) for the cutting-edge LPUs.
64+
- [Langchain](https://github.com/hwchase17/langchain) for robust language modeling tools.
65+
- [Streamlit](https://streamlit.io) for the user-friendly web application framework.
66+
67+

app.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import os
2+
3+
import streamlit as st
4+
from langchain.chains import ConversationChain
5+
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
6+
from langchain_groq import ChatGroq
7+
import dotenv
8+
9+
dotenv.load_dotenv(dotenv.find_dotenv())
10+
11+
12+
def initialize_session_state():
13+
"""
14+
Initialize the session state variables if they don't exist.
15+
"""
16+
if 'chat_history' not in st.session_state:
17+
st.session_state.chat_history = []
18+
if 'model' not in st.session_state:
19+
st.session_state.model = 'mixtral-8x7b-32768'
20+
if 'programming_language' not in st.session_state:
21+
st.session_state.programming_language = 'Python'
22+
if 'style_guide' not in st.session_state:
23+
st.session_state.style_guide = 'PEP 8'
24+
25+
26+
def display_customization_options():
27+
"""
28+
Add customization options to the sidebar for model selection, memory length, programming language, and style guide.
29+
"""
30+
st.sidebar.title('Customization')
31+
model = st.sidebar.selectbox(
32+
'Choose a model',
33+
['mixtral-8x7b-32768', 'llama2-70b-4096'],
34+
key='model_selectbox'
35+
)
36+
programming_language = st.sidebar.selectbox(
37+
'Programming Language',
38+
['Python', 'R', 'Java', 'JavaScript']
39+
)
40+
41+
style_guide_options = {
42+
'Python': ['PEP 8', 'Google Python Style Guide'],
43+
'R': ['Google R Style Guide', 'Tidyverse Style Guide'],
44+
'Java': ['Google Java Style Guide', 'Oracle Java Code Conventions'],
45+
'JavaScript': ['Airbnb JavaScript Style Guide', 'Google JavaScript Style Guide']
46+
}
47+
style_guide = st.sidebar.selectbox(
48+
'Style Guide',
49+
style_guide_options[programming_language]
50+
)
51+
52+
return model, programming_language, style_guide
53+
54+
55+
def initialize_groq_chat(groq_api_key, model):
56+
"""
57+
Initialize the Groq Langchain chat object.
58+
"""
59+
return ChatGroq(
60+
groq_api_key=groq_api_key,
61+
model_name=model
62+
)
63+
64+
65+
def initialize_conversation(groq_chat, memory):
66+
"""
67+
Initialize the conversation chain with the Groq chat object and memory.
68+
"""
69+
return ConversationChain(
70+
llm=groq_chat,
71+
memory=memory
72+
)
73+
74+
75+
def process_user_code(user_code, conversation, programming_language, style_guide):
76+
"""
77+
Process the user's code and generate a response using the conversation chain.
78+
"""
79+
prompt = f"Please rewrite the provided {programming_language} code to adhere strictly to the {style_guide} standards. Ensure the output consists solely of the revised code, ready for copy-paste:\n\n{user_code}. Please ensure to that the output only consists of the revised code this is very important!"
80+
response = conversation(prompt)
81+
message = {'human': prompt, 'AI': response['response']}
82+
st.session_state.chat_history.append(message)
83+
84+
85+
def main():
86+
"""
87+
The main entry point of the application.
88+
"""
89+
groq_api_key = os.environ['GROQ_API_KEY']
90+
initialize_session_state()
91+
st.title("Lightning ⚡️ Code Style Guide Assistant")
92+
st.markdown("Get your code rewritten according to popular style guides by Lightning, an ultra-fast AI chatbot powered by Groq LPUs!!")
93+
94+
model, programming_language, style_guide = display_customization_options()
95+
96+
if st.session_state.model != model or st.session_state.programming_language != programming_language or st.session_state.style_guide != style_guide:
97+
# Reset chat history and session state when the model, programming language, or style guide is switched
98+
st.session_state.chat_history = []
99+
st.session_state.model = model
100+
st.session_state.programming_language = programming_language
101+
st.session_state.style_guide = style_guide
102+
st.rerun()
103+
104+
memory = ConversationBufferWindowMemory(k=1)
105+
106+
st.divider()
107+
108+
user_code = st.text_area("Enter your code:")
109+
if user_code:
110+
st.session_state.chat_history.append({"human": user_code, "AI": ""})
111+
112+
with st.expander("Original Code"):
113+
st.code(user_code, language=programming_language.lower())
114+
115+
for message in st.session_state.chat_history:
116+
memory.save_context({'input': message['human']}, {'output': message['AI']})
117+
118+
groq_chat = initialize_groq_chat(groq_api_key, model)
119+
conversation = initialize_conversation(groq_chat, memory)
120+
process_user_code(user_code, conversation, programming_language, style_guide)
121+
122+
with st.expander("Rewritten Code"):
123+
response = conversation(user_code)
124+
st.code(response['response'], language=programming_language.lower())
125+
st.session_state.chat_history[-1]["AI"] = response['response']
126+
127+
128+
if __name__ == "__main__":
129+
main()

assets/logo.png

239 KB
Loading

requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
langchain~=0.1.12
2+
langchain-community==0.0.28
3+
langchain-core==0.1.32
4+
langchain-openai==0.0.3
5+
langchain-groq==0.1.2
6+
langchain-text-splitters==0.0.1
7+
python-dotenv==1.0.1

0 commit comments

Comments
 (0)