Ollama Python 和 JavaScript 库的初始版本现已推出

这两个库都可以通过几行代码将新的和现有的应用程序与 Ollama 集成,并共享 Ollama REST API 的 features 和 feel。

Ollama 是一款命令行工具,可在 macOS 和 Linux 上本地运行 Llama 2、Code Llama 和其他模型。目前适用于 macOS 和 Linux,并计划支持 Windows。

Ollama 目前支持近二十多个语言模型系列,每个模型系列都有许多可用的 "tags"。Tags 是模型的变体,这些模型使用不同的微调方法以不同的规模进行训练,并以不同的级别进行量化,以便在本地良好运行。量化级别越高,模型越精确,但运行速度越慢,所需的内存也越大。

Getting Started

Python

pip install ollama
import ollama
response = ollama.chat(model='llama2', messages=[
  {
    'role': 'user',
    'content': 'Why is the sky blue?',
  },
])
print(response['message']['content'])

JavaScript

npm install ollama
import ollama from 'ollama'

const response = await ollama.chat({
  model: 'llama2',
  messages: [{ role: 'user', content: 'Why is the sky blue?' }],
})
console.log(response.message.content)

用例

这两个库都支持 Ollama 的全套功能。以下是 Python 中的一些示例:

Streaming

for chunk in chat('mistral', messages=messages, stream=True):
  print(chunk['message']['content'], end='', flush=True)

Multi-modal 

with open('image.png', 'rb') as file:
  response = ollama.chat(
    model='llava',
    messages=[
      {
        'role': 'user',
        'content': 'What is strange about this image?',
        'images': [file.read()],
      },
    ],
  )
print(response['message']['content'])

Text Completion 

result = ollama.generate(
  model='stable-code',
  prompt='// A c function to reverse a string\n',
)
print(result['response'])

Creating custom models 

modelfile='''
FROM llama2
SYSTEM You are mario from super mario bros.
'''

ollama.create(model='example', modelfile=modelfile)

Custom client 

ollama = Client(host='my.ollama.host')

更多示例可查看 Python 和 JavaScript 库。

最后修改于 2024-04-30 16:46:26
上一篇