忿忿的泥巴坨 发表于 2024-12-3 05:53:58

Ollama | 当地摆设运行开源大模子

https://img-blog.csdnimg.cn/img_convert/ee6177ba4addbdb6690092c4edd21f6b.png


[*] 你有没有思量过在当地运行开源LLM?
[*] 你是否不得不手动下载庞大的模子文件?
[*] 你是否积极构建当地模子的API?
[*] 你是否尝试在当地管理多个模子?
我猜你确实思量过!这些都是繁重的体力劳动。
荣幸的是,这里有Ollama。
Ollama是一款盛行的大模子管理工具,用于在当地摆设和运行大型语言模子 - https://ollama.ai/
https://img-blog.csdnimg.cn/img_convert/371c99c8a97eaf96e3e7829f2a409ea6.png
现在,你不再必要担心这些贫苦了,一款单一的应用步伐可以解决全部这些题目。
你只必要访问他们的官方网站,下载应用步伐并举行安装。就这样!现在,你将拥有一个用于模子管理的CLI或GUI,包括拉取、移除、运行、创建自界说模子等功能。
Ollama支持哪些模子?
Ollama支持一大波主流的模子。你可以在 https://ollama.ai/library 上找到它所支持的模子列表。
现在的列表如下:


[*] llama2
[*] mistral
[*] llava
[*] mixtral
[*] starling-lm
[*] neural-chat
[*] codellama
[*] dolphin-mixtral
[*] mistral-openorca
[*] llama2-unsensored
[*] ……
运行Ollama
当你完成了应用步伐的安装,你可以通过运行Ollama的桌面应用步伐来启动它。大概,你也可以通过CLI来启动它:
$ ollama serve
Ollama CLI
我们来看看Ollama的下令行接口。
拉取模子
下令 ollama pull 会为你自动下载模子文件。
`01coder@X8EF4F3X1O ollama-libraries-example % ollama pull tinyllama``pulling manifest` `pulling manifest                                                   MB/s      4s                                                               ▏ 284 MB/637 MB   28 MB/s   12s``pulling 2af3b81862c6... 100% ▕████▏ 637 MB`                         `pulling af0ddbdaaa26... 100% ▕████▏   70 B`                         `pulling c8472cd9daed... 100% ▕████▏   31 B`                         `pulling fa956ab37b8c... 100% ▕████▏   98 B`                         `pulling 6331358be52a... 100% ▕████▏483 B`                         `verifying sha256 digest` `writing manifest` `removing any unused layers` `success`
运行模子
下令ollama run 运行模子并开启与指定模子的交互式对话。
01coder@X8EF4F3X1O ollama-libraries-example % ollama run orca-mini``>>> Explain the word distinct` `Distinct means separate or distinct from others, with no` `similarity or connection to others. It refers to something that` `is unique or different in a way that cannot be easily identified` `or compared with other things. In other words, something that is` `distinct is not like anything else that you might encounter.``   ``>>> What did I ask?` `You asked me to explain the word "distinct".``   ``>>> Send a message (/? for help)
查询当地摆设的模子
下令ollama list列出全部当地下载摆设的模子。
01coder@X8EF4F3X1O ollama-libraries-example % ollama list``NAME                  ID            SIZE    MODIFIED`      `llama2:7b-chat          fe938a131f40    3.8 GB8 weeks ago``orca-mini:latest      2dbd9f439647    2.0 GB25 hours ago` `phi:latest            e2fd6321a5fe    1.6 GB29 hours ago` `tinyllama:latest      2644915ede35    637 MB6 minutes ago
Ollama HTTP API
当你启动Ollama时,它会提供一系列用于模子管理的API。请参考以下文档以获取完整的端点列表:
https://github.com/ollama/ollama/blob/main/docs/api.md
文本补全
Ollama默认监听在11434端口。
curl http://localhost:11434/api/generate -d '{``"model": "orca-mini",``"prompt":"Explain the word distinct"``}'
谈天补全
curl http://localhost:11434/api/chat -d '{``"model": "orca-mini",``"messages": [`    `{ "role": "user", "content": "Explain the word distinct" }``]``}'
Python与Javascript开发包
近来,Ollama发布了Python和JavaScript库,答应开发人员以最小的工作量将Ollama集成到现有或新的应用步伐中。


[*] Python: https://github.com/ollama/ollama-python
[*] JavaScript: https://github.com/ollama/ollama-js
Python库用例 | 使用Streamlit应用与LLM谈天
Streamlit是一个非常适合快速开发AI应用步伐的开发框架。
我创建了一个简朴的项目,演示了如何使用Ollama Python库与Streamlit一起构建一个Web应用步伐,用户可以通过该应用与Ollama支持的任何模子举行谈天。请使用以下链接检察:
https://github.com/sugarforever/ollama-libraries-example/tree/main/python
这是基于一个使用OpenAI Python SDK的Streamlit教程创建的:
https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps
代码非常简朴易懂,这里分享一些关键要点:
获取模子列表
import ollama``   ``model_list = ollama.list()
与模子谈天
与OpenAI Python SDK相比,Ollama Python库的chat函数返回不同格式的数据。在流模式下,应该以如下方式剖析块:
      `full_response = ""`      `for chunk in ollama.chat(`            `model=st.session_state["model_name"],`            `messages=[`                `{"role": m["role"], "content": m["content"]}`                `for m in st.session_state.messages`            `],`            `stream=True,`      `):`            `if 'message' in chunk and 'content' in chunk['message']:`                `full_response += (chunk['message']['content'] or "")`                `message_placeholder.markdown(full_response + "▌")`
通过该示例应用,你应该看到类似如下界面。
https://img-blog.csdnimg.cn/img_convert/6c3aa940c7cff45214a5bbea543b5308.png
好了,本日的分享就到这里。周末舒畅!
页: [1]
查看完整版本: Ollama | 当地摆设运行开源大模子