前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >llama-cpp-python快速上手 - plus studio

llama-cpp-python快速上手 - plus studio

作者头像
plus sign
发布2024-02-29 08:14:24
4150
发布2024-02-29 08:14:24
举报
文章被收录于专栏:个人博客个人博客

llama-cpp-python快速上手

搭建环境

项目地址GitHub,有能力的话可以直接阅读原始文档。

首先按照文档,安装llama-cpp-python

代码语言:text
复制
pip install llama-cpp-python

接下来,你可能缺一些依赖,这一点在文档中没有涉及但是我整理了我缺少的依赖,依次运行即可。

代码语言:text
复制
pip install uvicorn
pip install anyio
pip install starlette
pip install fastapi
pip install pydantic_settings
pip install sse_starlette

高级API和低级API

高级API

高级 API 通过Llama类提供简单的托管接口。请将./models/7B/ggml-model.bin 换成你的模型的路径,下同。

代码语言:text
复制
from llama_cpp import Llama
llm = Llama(model_path="./models/7B/ggml-model.bin")
output = llm("Q: Name the planets in the solar system? A: ", max_tokens=32, stop=["Q:", "\n"], echo=True)
print(output)

返回值如下

代码语言:text
复制
{
  "id": "cmpl-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "object": "text_completion",
  "created": 1679561337,
  "model": "./models/7B/ggml-model.bin",
  "choices": [
    {
      "text": "Q: Name the planets in the solar system? A: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune and Pluto.",
      "index": 0,
      "logprobs": None,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 14,
    "completion_tokens": 28,
    "total_tokens": 42
  }
}

低级API

低级 API 直接ctypes绑定到llama.cpp. 整个低级 API 可以在llama_cpp/llama_cpp.py中找到,并直接镜像llama.h中的 C API 。

代码语言:text
复制
import llama_cpp
import ctypes
params = llama_cpp.llama_context_default_params()
# use bytes for char * params
ctx = llama_cpp.llama_init_from_file(b"./models/7b/ggml-model.bin", params)
max_tokens = params.n_ctx
# use ctypes arrays for array params
tokens = (llama_cpp.llama_token * int(max_tokens))()
n_tokens = llama_cpp.llama_tokenize(ctx, b"Q: Name the planets in the solar system? A: ", tokens, max_tokens, add_bos=llama_cpp.c_bool(True))
llama_cpp.llama_free(ctx)

搭建与openai接口兼容的服务器接口

llama-cpp-python提供一个 Web 服务器,旨在作为 OpenAI API 的直接替代品。

代码语言:text
复制
python3 -m llama_cpp.server --model models/7B/ggml-model.bin

你可以在上面的命令运行成功后访问文档

文档是全英的,想要对话接口的话我用python写了个示例

代码语言:text
复制
import requests
  
url = 'http://localhost:8000/v1/chat/completions'
headers = {
	'accept': 'application/json',
	'Content-Type': 'application/json'
}
data = {
	'messages': [
		{
		'content': 'You are a helpful assistant.',
		'role': 'system'
		},
		{
		'content': 'What is the capital of France?',
		'role': 'user'
		}
	]
}
  
response = requests.post(url, headers=headers, json=data)
print(response.json())
print(response.json()['choices'][0]['message']['content'])

如果你想自建一个接口,请在遵守相关法律法规的情况下,在自己的服务器上启动相关服务,并反向代理http://localhost:8000 地址。例如你反向代理到了https://example.com,那你的对话地址就是https://example.com/v1/chat/completions。当你想用gpt的时候就不用看openai的脸色了,直接部署一个自己的接口自己请求,或者调用openai库的时候apibase写自己的接口。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-7-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • llama-cpp-python快速上手
    • 搭建环境
      • 高级API和低级API
        • 高级API
        • 低级API
      • 搭建与openai接口兼容的服务器接口
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档