
Who this is for
- Developers of the first contact with the model API
- Students and technical bloggers who want to connect AI capabilities to the project
What problem does this article solve?
Don't know how to prepare the environment, read the API key, initiate a request, and get the available text results.
Key concepts
- API Key
- model name
- request parameter
- response object
- environmental variable
Walkthrough
- Create a virtual environment and install dependencies
- Configure OPENAI_API_KEY
- Write a minimal call script
- Added exception prompt and mock mode
Python code example
The companion demo is generated in:
cd examples/llm-guides/001_llm_api_quickstart
python demo.py
The example follows two principles:
- From
OPENAI_API_KEY, load the key at runtime to avoid hard-coding it. - Without an API key, the example runs in mock mode so you can follow the workflow.
The essential call looks like this:
import os
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
print("未配置 OPENAI_API_KEY,进入 mock 模式。")
else:
# 真实项目中在这里初始化 SDK 客户端并发起请求。
print("已读取 OPENAI_API_KEY,可以调用模型。")
Common pitfalls
- Publishing after a single successful demo run leaves out error handling, logging and human review.
- Including API keys, real file paths, account details or business data in published articles or code.
- Readers can struggle to reproduce an example if it is unclear which parts are mocked and which run in production.
- Citing an API or framework feature without checking its documentation can mislead readers when versions change.
Summary
- LLM Guide 01 walks through a first LLM API call from scratch, covering environment setup, reading the API key, sending the request, and extracting usable text from the response.
- Make the workflow reproducible, reversible and easy to review. One good generation tells you little about how it will hold up in use.
- Before publishing, check current documentation for model capabilities, API parameters and third-party frameworks.
CSDN tags
Artificial Intelligence, Large Model, Python, API, LLM
WeChat article summary
A minimal runnable example takes you through an entire LLM API call, from start to finish, the first time you integrate a large language model into a project.
Alternative title for Zhihu
- What should I pay attention to when calling the mockup API for the first time?
- How to run a LLM request from zero with Python?
- What concepts should you learn first?
References and further reading
- Check the OpenAI documentation for your model, SDK and account permissions.
- Python documentation on environment variables, virtual environments and exception handling.
- Consult the documentation for the tools used in the example, such as LangChain, LlamaIndex, Haystack or the vector database.