LLM 大语言模型入门:GPT、Claude 与本地部署(Ollama)

小飞兽 大模型/LLM 6 次阅读 2026-07-29

大语言模型(Large Language Model,LLM)是深度学习时代的标志性技术,能够理解和生成人类语言,在问答、写作、代码生成、知识推理等任务上表现出接近甚至超越人类的水平。

一、主流 LLM 模型概述

OpenAI 的 GPT-4/4o 系列、Google 的 Gemini、Anthropic 的 Claude 3/3.5 系列,以及 Meta 开源的 Llama 3 系列,各自在推理能力、长上下文、安全性、性价比上各有优势。

import openai
client = openai.OpenAI(api_key='sk-...')

response = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role': 'system', 'content': '你是一个专业的技术顾问'},
{'role': 'user', 'content': '用 Python 写一个快速排序算法'},
],
temperature=0.7,
max_tokens=2000,
)
print(response.choices[0].message.content)

import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
message = client.messages.create(
model='claude-3-5-sonnet-20241022',
max_tokens=2000,
messages=[{'role': 'user', 'content': '解释一下什么是 RAG 技术'}],
)
print(message.content[0].text)

二、Ollama 本地部署开源大模型

Ollama 是最流行的本地大模型运行框架,支持一键拉取和运行 Llama 3、Qwen、Mistral、Gemma 等开源模型,数据完全留在本地。

import ollama

response = ollama.chat(
model='llama3.2',
messages=[
{'role': 'system', 'content': '你是一个 Python 编程助手'},
{'role': 'user', 'content': '写一个函数判断字符串是否是回文'},
],
)
print(response['message']['content'])

stream = ollama.chat(
model='llama3.2',
messages=[{'role': 'user', 'content': '写一个 Python 生成斐波那契数列的代码'}],
stream=True,
)
for chunk in stream:
print(chunk['message']['content'], end='', flush=True)
print()

import requests
resp = requests.post(
'http://localhost:11434/api/chat',
json={'model': 'llama3.2', 'messages': [{'role': 'user', 'content': '你好'}], 'stream': False},
)
print(resp.json()['message']['content'])

三、模型选择指南

# 场景                    推荐模型

通用对话/写作 GPT-4o / Claude 3.5 Sonnet


中文任务 Qwen 2.5 / Kimi / 豆包


代码生成 GPT-4o / Claude 3.5 Sonnet


长文档分析 Claude 3.5(200K 上下文)/ Gemini 1.5


隐私敏感/离线 Ollama + Llama3.2 / Qwen2.5


低成本/高频简单任务 GPT-4o-mini / Claude 3 Haiku</code></pre><h2>四、常见问题</h2><p><strong>Q1:API 调用时遇到 Rate Limit(429)怎么办?</strong><br/>降低请求频率,实现指数退避重试。使用队列控制并发。</p><p><strong>Q2:Ollama 模型下载很慢怎么解决?</strong><br/>使用代理或科学上网工具。也可从 ModelScope、HuggingFace 手动下载 GGUF 格式模型文件。</p><p><strong>Q3:本地模型回答质量差怎么办?</strong><br/>选择更大参数的模型(如 7B/13B 而非 3B)。使用中文能力强的模型(如 Qwen2.5)。</p><h2>五、延伸阅读</h2><ul><li>OpenAI API 文档:<a href="https://platform.openai.com/docs/">https://platform.openai.com/docs/</a></li><li>Anthropic Claude 文档:<a href="https://docs.anthropic.com/">https://docs.anthropic.com/</a></li><li>Ollama 官方:<a href="https://ollama.com/">https://ollama.com/</a></li><li>开源模型排行:<a href="https://huggingface.co/models">HuggingFace Model Hub</a></li></ul>