Ollama 发布 Python 和 JavaScript 库
Ollama releases Python and JavaScript Libraries

原始链接: https://ollama.ai/blog/python-javascript-libraries

2024 年 2 月,开发人员可以开始将他们的应用程序与 AI 语言模型 Ollama 集成。 该平台现在提供 Python 和 JavaScript 库,通过其广泛的功能集简化交互。 只需几行代码,用户就可以实现流行的功能,例如流式传输结果、使用预定义提示完成文本、通过在传统文本旁边输入图像来利用多模式功能,甚至根据独特要求创建自定义模型。 这些库遵循最佳编码实践,同时保持易于使用。 它们加入了不断增长的开发者工具阵容,可用于各种其他编程语言,如 Dart、Swift、C#、Java、PHP、Rust 等。 此外,Ollama 最近将其官方存储库转移到了一个名为“ollama”的新 GitHub 组织下,为全球更广泛的开发者社区的贡献打开了大门。 对于那些有兴趣进一步探索详细指南和示例的人,可以在 Github 上找到 Python 和 JavaScript 库。 (字数:96 个字)。

JavaScript 库中解决此问题的一种常见做法是定义命名空间或模块模式,并通过入口文件的根范围内的命名导出来导出函数。 这可以确保函数名称之间不会发生冲突,或者类型定义之间不会发生冲突。 此外,开发人员可以创建一个单独的文档作为使用示例,提供如何使用 ES6 模块以各种方式合并库的示例。 这些示例可以与安装说明一起包含在自述文件中,也可以根据存储库布局首选项放置在其他位置。 总的来说,目标是确保库的使用对于人们来说变得直观,无论他们是否导入特定的子模块,因为在解决冲突的目的之外通常不鼓励使用“as”。
相关文章

原文

Python & JavaScript Libraries

The initial versions of the Ollama Python and JavaScript libraries are now available:

Both libraries make it possible to integrate new and existing apps with Ollama in a few lines of code, and share the features and feel of the Ollama REST API.

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)

Use cases

Both libraries support Ollama’s full set of features. Here are some examples in 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')

More examples are available in the GitHub repositories for the Python and JavaScript libraries.

New GitHub handle

GitHub handle

These libraries, and the main Ollama repository now live in a new GitHub organization: ollama! Thank you to all the amazing community members who maintain libraries to interact with Ollama via Dart, Swift, C#, Java, PHP, Rust and more – a full list is available here – please don’t hesitate to make a pull request to add a library you’ve built or enjoy using.

Special thank you to Saul, the original author of ollama-js, and everyone else who has worked on community libraries to make Ollama more accessible from different programming languages.

联系我们 contact @ memedata.com