Microsoft AI103 完成第三部

上个星期在询问chatGPT, Claude, Gemini 我该拿什么AI文凭之后,他们个别都建议 Microsoft AI103。

我们可以自学之后才参加考试,所以无需上课费用,只需缴考试费用。

今天完成第三集,是hands on 做一个generative AI chat app, 用Visual Studio Code链接Azure

重新复习怎么在VS Code 开一个新的project

1. Create and open a folder
Open VS Code.
Go to File → Open Folder… (on Mac, Cmd + O).
Click New Folder, name it (for example ai103-project), and click Open.
If asked “Do you trust the authors?”, choose Yes, I trust the authors.

2. Open the terminal
Terminal → New Terminal (or Ctrl + `). It opens already inside your project folder.
git clone https://github.com/microsoftlearning/mslearn-ai-studio .

3. For a Python project (likely what you need for Azure AI tutorials)
Make sure Python is installed (python3 --version in the terminal).
Install the Python extension from the Extensions panel (Cmd + Shift + X).
Create a virtual environment:
python3 -m venv .venv
source .venv/bin/activate

Press Cmd + Shift + P, type Python: Select Interpreter, and choose the one in .venv.
Install packages as needed, for example:
pip install openai azure-ai-projects azure-identity

python chat-app.py 遇到的错误

A. DefaultAzureCredential failed to retrieve a token from the included credentials.
Attempted credentials:
EnvironmentCredential: EnvironmentCredential authentication unavailable.
Claude的建议:
1. Install the Azure CLI (using Homebrew): brew install azure-cli
2. az login
3. Run the app again python chat-app.py

B. AzureCliCredential: ERROR: AADSTS500011:
Claude最后找到:
1. “https://ai.azure.com/.default” 我写错成 /default.
2. 在 .env 里,我把model_deployment 的 gpt4.1写错成 gpt-4.1.

Claude AI 整理大纲:
这个单元主要用 OpenAI endpoint,下一单元做 agent 才会用 Foundry project endpoint。讲师也强调,微软想尽量少用 key,因为 key 到处放有安全风险。

两种聊天 API 的比较

Chat Completions(旧的)

  • 模型本身是无状态的,不会记得你。
  • 要有对话感,你要自己保存整个对话历史,每次请求都全部重新发送。
  • 系统提示也只是 messages 数组里的一条消息。
  • 对话越长,请求越大。
  • 读取回复要一层层取:choices[0].message.content。

Responses(新的)

  • 读取回复很简单:response.output_text。
  • 对话可以存在服务器端,你只需要传上一次回复的 ID(previous_response_id)。
  • 每次请求的大小固定:当前消息加一个 ID。
  • 系统提示直接用 instructions= 参数。