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

打印 上一主题 下一主题

主题 987|帖子 987|积分 2961




  • 你有没有思量过在当地运行开源LLM?
  • 你是否不得不手动下载庞大的模子文件?
  • 你是否积极构建当地模子的API?
  • 你是否尝试在当地管理多个模子?
我猜你确实思量过!这些都是繁重的体力劳动。
荣幸的是,这里有Ollama
Ollama是一款盛行的大模子管理工具,用于在当地摆设和运行大型语言模子 - https://ollama.ai/

现在,你不再必要担心这些贫苦了,一款单一的应用步伐可以解决全部这些题目。
你只必要访问他们的官方网站,下载应用步伐并举行安装。就这样!现在,你将拥有一个用于模子管理的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来启动它:
  1. $ ollama serve
复制代码
Ollama CLI
我们来看看Ollama的下令行接口。
拉取模子
下令 ollama pull 会为你自动下载模子文件。
  1. `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 运行模子并开启与指定模子的交互式对话。
  1. 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列出全部当地下载摆设的模子。
  1. 01coder@X8EF4F3X1O ollama-libraries-example % ollama list``NAME                    ID              SIZE    MODIFIED`      `llama2:7b-chat          fe938a131f40    3.8 GB  8 weeks ago`  `orca-mini:latest        2dbd9f439647    2.0 GB  25 hours ago` `phi:latest              e2fd6321a5fe    1.6 GB  29 hours ago` `tinyllama:latest        2644915ede35    637 MB  6 minutes ago
复制代码
Ollama HTTP API
当你启动Ollama时,它会提供一系列用于模子管理的API。请参考以下文档以获取完整的端点列表:
https://github.com/ollama/ollama/blob/main/docs/api.md
文本补全
Ollama默认监听在11434端口。
  1. curl http://localhost:11434/api/generate -d '{`  `"model": "orca-mini",`  `"prompt":"Explain the word distinct"``}'
复制代码
谈天补全
  1. 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
代码非常简朴易懂,这里分享一些关键要点:
获取模子列表
  1. import ollama``   ``model_list = ollama.list()
复制代码
与模子谈天
与OpenAI Python SDK相比,Ollama Python库的chat函数返回不同格式的数据。在流模式下,应该以如下方式剖析块:
  1.         `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 + "▌")`
复制代码

通过该示例应用,你应该看到类似如下界面。

好了,本日的分享就到这里。周末舒畅!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

忿忿的泥巴坨

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表