一起学习大语言模型-ollama-python使用及示例代码

安装

pip install ollama

访问llama3.2模型

from ollama import chat, ChatResponse, Client
# 创建客户端, url为ollama服务所在的地址和端口号
client = Client(
    host="http://localhost:11434"
)
# 消息体, chat接口支持多伦对话
msg_arr = []
# 用户的对话内容
msg_arr.append({
    "role": "user",
    "content": "中国的首都是哪?"
})

# 对话结果
resp = client.chat(model='llama3.2',          #type: ChatResponse
    messages=msg_arr
)

# 打印返回的对话内容
print(resp.message.content)


#输出: 中国的首都是北京。

访问模型并使用function_call功能

注意: 这里用llama3.2会导致不管你问什么问题都会返回函数调用,这个需要改模型模板,暂时不用这个方式, 换一个qwen2.5模型, 就可以解决这个问题。

示例代码如下:

from ollama import chat, ChatResponse, Client
# 创建客户端, url为ollama服务所在的地址和端口号
client = Client(
    host="http://localhost:11434"
)
# 消息体, chat接口支持多伦对话
msg_arr = []
# 添加系统提示词, 让回答问题更准确些
msg_arr.append({
    "role": "system",
    "content": "只回答用户问题相关内容"
})
# 用户的对话内容
msg_arr.append({
    "role": "user",
    "content": "北京今天天气如何?"
})

# 定义一个获取天气的函数
def get_weather(loc):
    return f"{loc}天气晴朗, 万里无云"

# 定义一个工具集
tool_arr = []

tool_arr.append({
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "use this function to answer user's question about weather",
            "parameters": {
                "type": "object",
                "properties": {
                    "loc": {
                        "type": "string",
                        "description": "the location to get the weather for"
                    }
                },
                "required": ["loc"]
            }
        }
})

if resp.message.tool_calls != None:
    for tool_call in resp.message.tool_calls:
        if tool_call.function.name == "get_weather":
            res = get_weather(tool_call.function.arguments["loc"])
            print(res)
else:
    print(resp.message.content)

# 询问“北京天气”, 输出: 北京天气晴朗, 万里无云
# 询问“中国首都”, 输出: 中国首都是北京

使用llama3.2-vision模型图像识别

这里需要将图片文件base64编码

示例代码如下:

from ollama import chat, ChatResponse, Client
import base64

# 对要识别的图片进行base64编码
def img_to_base64(img_path):
    with open(img_path, "rb") as f:
        encoded_base64_str = base64.b64encode(f.read()).decode("utf-8")
    return encoded_base64_str

# 创建客户端, url为ollama服务所在的地址和端口号
client = Client(
    host="http://localhost:11434"
)
# 消息体, chat接口支持多伦对话
msg_arr = []
# 添加系统提示词, 让回答问题更准确些
msg_arr.append({
    "role": "system",
    "content": "只回答用户问题相关内容"
})

img_base64 = img_to_base64("你的图片路径.jpg")

# 如果多张图片就在images列表中添加
msg_arr.append({
    "role": "user",
    "content": "图片中都有什么?",
    "images": [f"{img_base64}"]
})

resp = client.chat(model='llama3.2-vision',          #type: ChatResponse
    messages=msg_arr
)

print(resp.messages.content)

# 输出图片的内容

其他用法

generate接口

用于单次推理, 不适用于多轮对话

....
client.generate(model='llama3.2', prompt='Why is the sky blue?')

查看目标地址安装了多少个模型

res = client.list()
for m in res.models:
    print(m.model)

查看目标地址服务运行了多少个模型

client.ps()

生成文本嵌入式向量

当问题比较专业,需要检索知识库时, 一般需要用文本嵌入式向量来进行扩展知识的检索

如下代码是一个示例:

from ollama import chat, ChatResponse, Client
import base64
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np


client = Client(
    host="http://localhost:11434"
)

res = client.embed("qwen2.5", input="大狗是《鸡毛飞上天》里的角色。")
res2 = client.embed("qwen2.5", input="大狗是谁?")
print(cosine_similarity(np.array(res["embeddings"]), np.array(res2["embeddings"])))

# 输出 [[0.83929001]]

如果值在0.7-1.0之间, 意味着这俩个文本内容具有较高的相似性, 在对问题进行答案检索时候, 很可能这个就是答案。

还有其他一些方法, 可以参考一下官网文档: https://github.com/ollama/ollama-python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值