File size: 1,483 Bytes
acdc1e9
 
ecee2b6
acdc1e9
 
 
 
 
 
 
 
 
 
 
ecee2b6
acdc1e9
 
ecee2b6
acdc1e9
 
ecee2b6
acdc1e9
 
ecee2b6
acdc1e9
ecee2b6
acdc1e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecee2b6
acdc1e9
 
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
# tesla_chatbot.py

import streamlit as st
import openai

# Set your OpenAI API key
openai.api_key = "YOUR_OPENAI_API_KEY"  # Replace with your API key

# Tesla knowledge base prompt
BASE_PROMPT = """
You are a helpful Tesla assistant.
You can answer questions about Tesla cars (Model S, 3, X, Y), technology (Autopilot, Full Self Driving), Tesla Energy products (Solar Roof, Powerwall), service, and company information.

If you don't know the answer or it's not Tesla-related, say "I'm sorry, I can only assist with Tesla-related queries."

Answer concisely and clearly.
"""

# Streamlit UI
st.set_page_config(page_title="Tesla Chatbot πŸš—βš‘", page_icon="πŸš—")

st.title("πŸš— Tesla Chatbot")
st.write("Ask me anything about Tesla cars, products, and services!")

user_input = st.text_input("You:", "")

if st.button("Ask"):
    if user_input.strip() == "":
        st.warning("Please type a question.")
    else:
        with st.spinner("Thinking..."):
            # Call OpenAI to get the response
            response = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",   # You can use gpt-4 if you have access
                messages=[
                    {"role": "system", "content": BASE_PROMPT},
                    {"role": "user", "content": user_input}
                ],
                temperature=0.5,
                max_tokens=300,
            )

            reply = response['choices'][0]['message']['content']
            st.success(reply)