当前位置:   article > 正文

Ollama 本地开源大模型聊天应用,非常详细手把手教会你_curl 调用ollama 聊天demo

curl 调用ollama 聊天demo

前言

如果您想在localhost部署并运行开源大模型,可以试试Ollama。本文我们将使用Ollama部署,并通过API的方式调用大模型。

安装

Ollama提供了python和js两种开发包,对前端开发者挺友好的,用它!

pip install ollama

npm install ollama

  • 1
  • 2
  • 3
  • 4

应用场景

  • 聊天接口

  • 多模态

模型

我们可以通过 [library (ollama.com)] 查看Ollama支持的模型清单,有gemmallama2mistralmixtral等,非常的丰富。

比如我们要使用的开源模型是llama2, 我们可以使用如下代码下载(首次)并运行模型

# 拉取模型
ollama pull llama2
# 运行模型
ollama run llama2

  • 1
  • 2
  • 3
  • 4
  • 5

接口

如果我们使用过openai的一些接口, 那么就了解文本补全、聊天、嵌入等。ollama提供了REST API来提供了请求接口。

  • 生成式接口
curl http://localhost:11434/api/generate -d '{
  "model": "llama2",
  "prompt":"Why is the sky blue?"
}'

  • 1
  • 2
  • 3
  • 4
  • 5
  • 聊天接口
curl http://localhost:11434/api/chat -d '{
  "model": "mistral",
  "messages": [
    { "role": "user", "content": "why is the sky blue?" }
  ]
}'

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 嵌入
curl http://localhost:11434/api/embeddings -d '{
  "model": "all-minilm",
  "prompt": "Here is an article about llamas..."
}'

  • 1
  • 2
  • 3
  • 4
  • 5

实战

我们将结合StreamlitOllama,开发一个聊天应用。

[Streamlit]是一款Web开发框架,适用于python快速完成一些大模型、数学科学计算的UI开发。

我们还会用到 [Build a ChatGPT-like App | Streamlit]代码快速构建类chatgpt应用。

# 引入streamlit UI库
import streamlit as st
# 引入 ollama
import ollama
# 获取ollama的模型列表
model_list = ollama.list()
# 设置默认模型名字为 llama2:7b-chat
if "model_name" not in st.session_state:
    st.session_state["model_name"] = "llama2:7b-chat"
# 初始化聊天信息数组
if "messages" not in st.session_state:
    st.session_state.messages = []
# 设置边栏
with st.sidebar:
    # 侧边栏的标题
    st.subheader("Settings")
    # 下拉框   选择模型, 默认选中llama2
    option = st.selectbox(
        'Select a model',
        [model['name'] for model in model_list['models']])
    st.write('You selected:', option)
    st.session_state["model_name"] = option
# 页面标题  与llama聊天
st.title(f"Chat with {st.session_state['model_name']}")
# 遍历聊天数组
for message in st.session_state.messages:
    # 根据角色
    with st.chat_message(message["role"]):
        # 输出内容
        st.markdown(message["content"])

if prompt := st.chat_input("What is up?"):
    
    st.session_state.messages.append({"role": "user", "content": prompt})
    
    with st.chat_message("user"):
        st.markdown(prompt)
    
    with st.chat_message("assistant"):
        # 大模型返回后就清空输入框
        message_placeholder = st.empty()
        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 + "▌")
        message_placeholder.markdown(full_response)
    st.session_state.messages.append({"role": "assistant", "content": full_response})


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

image.png

  • 拉取模型

ollama pull

image.png

除了llama2, 我们再拉取下orca-mini

  • 列出当前所有模型

ollama list

image.png

  • 运行streamlit

streamlit run app.py

image.png

总结

  • Ollama在本地部署开源大模型,真心方便且靠谱。 我在红米老爷机上运行了, 可以。
  • 结合streamlit 快速将Web搭建了出来。

如何学习AI大模型?

我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

在这里插入图片描述

第一阶段: 从大模型系统设计入手,讲解大模型的主要方法;

第二阶段: 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用;

第三阶段: 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统;

第四阶段: 大模型知识库应用开发以LangChain框架为例,构建物流行业咨询智能问答系统;

第五阶段: 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型;

第六阶段: 以SD多模态大模型为主,搭建了文生图小程序案例;

第七阶段: 以大模型平台应用与开发为主,通过星火大模型,文心大模型等成熟大模型构建大模型行业应用。

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/815338
推荐阅读
相关标签