File size: 2,801 Bytes
ad5e7c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e9c64b
 
 
 
 
 
 
 
 
 
 
 
ad5e7c5
 
 
 
 
 
 
 
2b674bb
 
 
 
 
 
 
 
 
 
 
0e9c64b
 
 
2b674bb
 
 
 
ad5e7c5
 
737c9f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
from groq import Groq
import streamlit as st
from dotenv import load_dotenv

# Load API key from .env file
load_dotenv()
api_key = os.getenv("GROQ_API_KEY")

# Initialize the Groq client
client = Groq(api_key=api_key)

# Define the programming development topics for the chatbot
developer_topics = [
    "best programming languages", "web development frameworks", "version control with Git", 
    "debugging tips", "data structures and algorithms", "object-oriented programming", 
    "functional programming", "software design patterns", "API design and development", 
    "devops practices", "cloud computing", "front-end development", "back-end development", 
    "machine learning", "deep learning", "software testing and QA", "agile methodologies", 
    "CI/CD pipelines", "database design", "programming best practices", "security in development", 
    "mobile app development", "project management for developers", "open source contribution", 
    "developer tools and IDEs", "documentation and code commenting", "coding interview preparation"
]

# Function to fetch chatbot completion from Groq API
def get_response(query):
    try:
        completion = client.chat.completions.create(
            model="llama-3.3-70b-versatile",
            messages=[{"role": "user", "content": query}],
            temperature=0.7,
            max_completion_tokens=2024,
            top_p=1,
        )
        response = completion.choices[0].message.content
        return response
    except Exception as e:
        return f"Error: {str(e)}"

def main():
    st.title("Programming Developer Advisor Chatbot")

    # Let the user choose a developer-related topic or type a custom query
    topic = st.selectbox("Choose a programming topic", developer_topics)
    user_input = st.text_area("Or ask a programming-related question:", "")

    # Add a submit button to trigger the response
    submit_button = st.button("Submit")

    # If the user clicks the submit button, process the query
    if submit_button:
        if user_input:
            query = user_input
            # Check if the user input is related to a programming topic
            if any(topic.lower() in user_input.lower() for topic in developer_topics):
                response = get_response(query)
                st.write("### Response:")
                # Display the response with proper formatting, and if it is long, we can show it in a scrollable container
                st.markdown(f"#### Query: {query}")
                st.text_area("Response:", response, height=300)
            else:
                st.write("Sorry, I can only answer programming-related questions.")
        else:
            st.write("Please enter a programming-related question or choose a topic.")

if __name__ == "__main__":
    main()