Build Your Own AI Chat System with Python and DeepSeek API!
Do you want to have your own AI chat system? Do you want to quickly build a cool web application using Python and Streamlit? Do you want to experience the powerful features of the DeepSeek API?
This tutorial will guide you step by step to achieve all the above goals with simple code, easy to get started, come and explore together!
1. Preparation
-
1. Register for a DeepSeek account and obtain an API key: Visit the DeepSeek official website to register and obtain your API key. -
2. Install Python libraries: Use the pip command to install the following libraries:
-
• <span>pip install streamlit</span>
-
• <span>pip install requests</span>
2. Create a Streamlit Application
-
1. Create a new Python file, for example <span>app.py</span>
. -
2. Import necessary libraries:
import streamlit as st
import requests
-
3. Set the DeepSeek API key and request URL:
DEEPSEEK_API_KEY = "your_api_key"
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
-
4. Create the Streamlit application interface:
st.title("🤖 My AI Chat System")
# Initialize session_state to store the history of conversations
if "messages" not in st.session_state:
st.session_state.messages = []
# Display the history of conversations
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Create an input box for user questions
if prompt := st.chat_input("What do you want to say to me?"):
# Add user input to the history of conversations
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Call the DeepSeek API for conversation
response = requests.post(
DEEPSEEK_API_URL,
headers={
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
"Content-Type": "application/json",
},
json={
"model": "deepseek-chat", # Specify the model to use
"messages": st.session_state.messages,
},
)
# Parse the API response and display the result
if response.status_code == 200:
ai_response = response.json()["choices"][0]["message"]["content"]
# Add AI response to the history of conversations
st.session_state.messages.append({"role": "assistant", "content": ai_response})
with st.chat_message("assistant"):
st.markdown(ai_response)
else:
st.error("Request failed, please check your API key and network connection.")
3. Run the Application
-
1. Run the following command in the terminal to start the Streamlit application:
streamlit run app.py
-
2. Open your browser and visit <span>http://localhost:8501</span>
to experience your exclusive AI chat system!
4. Advanced Features
-
• Customize model parameters: Adjust parameters in the API request, such as <span>temperature</span>
and<span>max_tokens</span>
, to control the style and length of generated text. -
• Add more interactive elements: Use various components provided by Streamlit, such as buttons, dropdown menus, file uploads, etc., to enrich the functionality of your application. -
• Deploy the application to the cloud: Deploy your application to platforms like Streamlit Cloud, Heroku, etc., and share it with more people.
5. Code Analysis
-
• <span>st.session_state</span>
: Used to save data such as conversation history when the page refreshes. -
• <span>st.chat_message</span>
: Used to display messages from different roles in the chat interface, such as the user and AI assistant. -
• <span>st.chat_input</span>
: Used to create a chat input box for users to easily input questions.
6. Recommended Resources
-
• Streamlit Official Documentation: https://docs.streamlit.io/ -
• DeepSeek API Documentation: https://www.deepseek.com/zh/docs -
• Python Official Documentation: https://docs.python.org/en/3/
Conclusion
Congratulations! You have successfully built your own AI chat system using Python and Streamlit!
Act quickly, explore more possibilities, and create your own AI applications!