Build Your First AI Application Hands-On
Create a practical AI tool from scratch using Python and a large model API
本章学习要点
Understand the definition and market positioning of AI application development
Master the core tech stack for AI applications (API, SDK, frameworks)
Differentiate the four forms of AI applications (Chat, Embedding, Agent, End-to-End)
Learn about the career prospects and salary levels for AI application developers
Enough theory, let's build a real AI application. In this chapter, we'll use Python + the DeepSeek API to build a 'Smart Document Q&A Assistant' from scratch—after uploading a document, the AI can answer questions based on its content. This is one of the most practical and common forms of AI applications.
Environment Setup
Install Python
If you don't have a Python environment yet, we recommend using Anaconda (which includes many scientific computing libraries) or downloading and installing the latest version directly from python.org. During installation, check the 'Add to PATH' option.
Get an API Key
Using DeepSeek as an example: Visit platform.deepseek.com, register an account, and create an API Key on the API management page. DeepSeek provides a free quota for development and testing, which is sufficient for all exercises in this chapter. If you prefer using OpenAI, the process is similar—just visit platform.openai.com.
Install Dependencies
Run the following command in your terminal to install the necessary libraries: `pip install openai streamlit python-docx PyPDF2`. The openai library not only supports OpenAI but can also be used directly with most large language models that are compatible with the OpenAI API format (including DeepSeek).
Step 1: The Simplest API Call
Open Cursor or VS Code and create a new Python file. Write a basic API call to have the AI answer a question. The core code is only a few lines: create a client, set the model and messages, send the request, and print the result.
Here's a key concept—the **messages array**. It contains the complete context of the conversation: a system message defines the AI's role and behavioral guidelines, user messages are the user's input, and assistant messages are the AI's responses. By maintaining this array, you can implement multi-turn conversation memory.
Debugging Tips
If you encounter an error, 90% of the time it's due to: an incorrectly set API Key, network connection issues (accessing OpenAI from within China requires a proxy), or a misspelled model name. Having Cursor's AI help you debug is the fastest way.
Step 2: Add a System Prompt
The difference between a good AI application and a simple API call often lies in the system prompt. The system prompt defines the AI's identity, scope of capabilities, and behavioral guidelines.
**Practical Example**: Suppose you're building a foreign trade outreach email assistant. The system prompt should include: 'You are a professional foreign trade business expert, skilled in writing B2B outreach emails. Your writing style is concise and professional, avoiding excessive sales pitches. All emails must include the company's value proposition, industry-specific customization for the client, and a clear CTA. If the user does not provide enough information, you should proactively ask.'
Step 3: Build the Document Q&A Feature
Now for the core functionality—enabling the AI to answer questions based on uploaded documents. The basic idea is: read the document content → send the document content and the user's question together to the AI → the AI answers based on the document.
Document Reading
Use python-docx to read Word files and PyPDF2 to read PDF files. The core task is to extract the text from the document into a string. For short documents (within a few thousand words), you can put all the content directly into the prompt.
Handling Long Documents
When a document exceeds the model's context window (DeepSeek supports 64K tokens), you need to perform text chunking: split the long document into multiple small paragraphs and only send the paragraphs most relevant to the user's question to the AI. This is the basic principle of RAG (Retrieval-Augmented Generation). A simple version can use keyword matching, while an advanced version uses vector similarity search.
Step 4: Create an Interface with Streamlit
Command-line tools are only suitable for personal use. To let others use it, you need a web interface. Streamlit is the simplest Python web framework—just a few lines of code can create an interactive webpage.
Core components: `st.file_uploader` lets users upload documents, `st.text_input` lets users input questions, `st.chat_message` displays the conversation history. Run `streamlit run app.py` to see your application in the browser.
Deployment
Once it runs locally, you can deploy it so others can access it using the following methods: **Streamlit Cloud** (free, deploys directly from GitHub), **Railway** (a simple cloud deployment platform), **Alibaba Cloud Function Compute/Tencent Cloud Cloud Functions** (domestic solutions, pay-per-call).
Step 5: Iterate and Optimize
After the basic version is running, you can continue to optimize: add conversation history memory (remember context), support multiple file formats (Excel, Markdown, etc.), add streaming output (displaying text word by word like ChatGPT), and incorporate error handling and loading state indicators.
You can have the AI help you write code for each optimization step. Describe the feature you want in Cursor, and it will generate improvement suggestions based on your existing code.
Learning Advice
Don't try to make everything perfect at once. First, get the simplest version running, then iterate step by step. Add only one feature at a time, and ensure it works before adding the next. This incremental development approach is the most efficient in AI-assisted programming—the quality of code the AI generates for small-scale modifications is far higher than for generating a large project all at once.
实用建议
The key principle of AI application development is incremental iteration: first get the simplest version running (even if it's just an API call), then gradually add features. Add only one feature at a time, ensuring it runs before adding the next. The AI's code quality for small modifications is far superior to that for a large project all at once.
注意事项
Your API Key is the 'key' to your AI application and must never be hardcoded in your code or uploaded to GitHub. Use environment variables (.env file) to manage your API Key and exclude the .env file in .gitignore. A leaked API Key can lead to significant costs.
重要提醒
The difference between a good AI application and a simple API call often lies in the System Prompt. Spending time refining the System Prompt—defining a clear identity, scope of capabilities, and behavioral guidelines—is the most effective way to improve application quality.
RAG Document Q&A System Architecture
AI Application Development Five-Step Method
After completing your first AI application, the next chapter will take us into a more cutting-edge field—AI Agent development, enabling the AI not only to answer questions but also to autonomously execute complex tasks.
Previous Chapter
The Complete Picture of AI Application Development: From API Calls to Agent Building
Next Chapter
AI Agent Development: Enabling AI to Handle Complex Tasks Autonomously
Course Chapters
Finished? Mark as completed
Complete all chapters to earn your certificate
Want to unlock all course content?
Purchase the full learning pack for all chapters + certification guides + job templates
View Full Course