1 环境使用腾讯云-轻量云服务器
2 安装ollama3, 打开https://ollama.com/download
执行上述命令后可以看到, 已安装并启动完成
3 现在执行命令ollama list
发现是空,需要加载想用的模型,比如ollama pull llama3
这是一个默认8b参数的模型,大小4.7G, 加载完之后就可以进行访问,访问方式分为streaming(默认值)和no streaming, 由于该服务器只能使用cpu,且配置极低,过程是相当慢。
streaming:
curl http://localhost:11434/api/chat -d '{
"model": "llama3",
"messages": [
{
"role": "user",
"content": "中国首都是哪里,只回答城市名称即可"
}
],
"stream": true
}'
{
"model": "llama3",
"created_at": "2024-05-06T11:29:18.143434047Z",
"message": {
"role": "assistant",
"content": "北京"
},
"done": false
} {
"model": "llama3",
"created_at": "2024-05-06T11:29:33.392988556Z",
"message": {
"role": "assistant",
"content": ""
},
"done": true,
"total_duration": 29951710613,
"load_duration": 4505881,
"prompt_eval_duration": 14395639000,
"eval_count": 2,
"eval_duration": 15390551000
}
no streaming:
curl http://localhost:11434/api/chat -d '{
"model": "llama3",
"messages": [
{
"role": "user",
"content": "中国首都是哪里,只回答城市名称即可"
}
],
"stream": false
}'
{
"model": "llama3",
"created_at": "2024-05-06T11:30:29.850547607Z",
"message": {
"role": "assistant",
"content": "北京"
},
"done": true,
"total_duration": 30074356729,
"load_duration": 4547609,
"prompt_eval_duration": 14470996000,
"eval_count": 2,
"eval_duration": 15445680000
}
4 generate与chat
可以发现请求的时候有/api/chat和/api/generate两种模式,它们什么区别呢?
- chat模式需要更强的上下文理解能力,以保持对话的连贯性,而generate模式则更侧重于根据给定的提示生成文本
- chat涉及多轮交互,适合构建可以进行来回对话的系统;generate通常是单次生成,不涉及多轮对话
- chat API可能需要设计为接收对话历史或上下文信息,而generate API则可能更简单,只接收生成文本所需的输入提示
curl http://localhost:11434/api/chat -d '{
"model": "llama3",
"messages": [
{
"role": "user",
"content": "为什么吃饭?"
},
{
"role": "assistant",
"content": "因为了饿了."
},
{
"role": "user",
"content": "不能喝水吗?"
}
]
}'
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。