{"id":13877978,"url":"https://github.com/gbaptista/ollama-ai","last_synced_at":"2025-04-04T14:06:16.845Z","repository":{"id":215829708,"uuid":"739889368","full_name":"gbaptista/ollama-ai","owner":"gbaptista","description":"A Ruby gem for interacting with Ollama's API that allows you to run open source AI LLMs (Large Language Models) locally.","archived":false,"fork":false,"pushed_at":"2024-07-21T11:13:36.000Z","size":126,"stargazers_count":226,"open_issues_count":3,"forks_count":8,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-03-17T21:49:09.403Z","etag":null,"topics":["ai","alpaca","bakllava","dolphin","llama","llama2","llava","llm","mistral","mistral-ai","mixtral","nano-bots","ollama","ollama-api","openorca","vicuna"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/ollama-ai","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gbaptista.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-06T21:12:18.000Z","updated_at":"2025-03-17T20:20:10.000Z","dependencies_parsed_at":"2024-01-13T13:27:18.902Z","dependency_job_id":"55e7aaab-1364-4c69-97d2-05164c93c348","html_url":"https://github.com/gbaptista/ollama-ai","commit_stats":{"total_commits":16,"total_committers":2,"mean_commits":8.0,"dds":0.0625,"last_synced_commit":"f60cc91853bde6d81e4e4e357ff5620a05be4049"},"previous_names":["gbaptista/ollama-ai"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbaptista%2Follama-ai","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbaptista%2Follama-ai/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbaptista%2Follama-ai/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbaptista%2Follama-ai/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gbaptista","download_url":"https://codeload.github.com/gbaptista/ollama-ai/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247190096,"owners_count":20898696,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","alpaca","bakllava","dolphin","llama","llama2","llava","llm","mistral","mistral-ai","mixtral","nano-bots","ollama","ollama-api","openorca","vicuna"],"created_at":"2024-08-06T08:01:36.558Z","updated_at":"2025-04-04T14:06:16.820Z","avatar_url":"https://github.com/gbaptista.png","language":"Ruby","funding_links":[],"categories":["Ruby","AI"],"sub_categories":[],"readme":"# Ollama AI\n\nA Ruby gem for interacting with [Ollama](https://ollama.ai)'s API that allows you to run open source AI LLMs (Large Language Models) locally.\n\n![The image presents a llama's head merged with a red ruby gemstone against a light beige background. The red facets form both the ruby and the contours of the llama, creating a clever visual fusion.](https://raw.githubusercontent.com/gbaptista/assets/main/ollama-ai/ollama-ai-canvas.png)\n\n\u003e _This Gem is designed to provide low-level access to Ollama, enabling people to build abstractions on top of it. If you are interested in more high-level abstractions or more user-friendly tools, you may want to consider [Nano Bots](https://github.com/icebaker/ruby-nano-bots) 💎 🤖._\n\n## TL;DR and Quick Start\n\n```ruby\ngem 'ollama-ai', '~\u003e 1.3.0'\n```\n\n```ruby\nrequire 'ollama-ai'\n\nclient = Ollama.new(\n  credentials: { address: 'http://localhost:11434' },\n  options: { server_sent_events: true }\n)\n\nresult = client.generate(\n  { model: 'llama2',\n    prompt: 'Hi!' }\n)\n```\n\nResult:\n```ruby\n[{ 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:34:02.088810408Z',\n   'response' =\u003e 'Hello',\n   'done' =\u003e false },\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:34:02.419045606Z',\n   'response' =\u003e '!',\n   'done' =\u003e false },\n # ..\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:34:07.680049831Z',\n   'response' =\u003e '?',\n   'done' =\u003e false },\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:34:07.872170352Z',\n   'response' =\u003e '',\n   'done' =\u003e true,\n   'context' =\u003e\n     [518, 25_580,\n      # ...\n      13_563, 29_973],\n   'total_duration' =\u003e 11_653_781_127,\n   'load_duration' =\u003e 1_186_200_439,\n   'prompt_eval_count' =\u003e 22,\n   'prompt_eval_duration' =\u003e 5_006_751_000,\n   'eval_count' =\u003e 25,\n   'eval_duration' =\u003e 5_453_058_000 }]\n```\n\n## Index\n\n- [TL;DR and Quick Start](#tldr-and-quick-start)\n- [Index](#index)\n- [Setup](#setup)\n  - [Installing](#installing)\n- [Usage](#usage)\n  - [Client](#client)\n    - [Bearer Authentication](#bearer-authentication)\n  - [Methods](#methods)\n    - [generate: Generate a completion](#generate-generate-a-completion)\n      - [Without Streaming Events](#without-streaming-events)\n      - [Receiving Stream Events](#receiving-stream-events)\n    - [chat: Generate a chat completion](#chat-generate-a-chat-completion)\n      - [Back-and-Forth Conversations](#back-and-forth-conversations)\n    - [embeddings: Generate Embeddings](#embeddings-generate-embeddings)\n    - [Models](#models)\n      - [create: Create a Model](#create-create-a-model)\n      - [tags: List Local Models](#tags-list-local-models)\n      - [show: Show Model Information](#show-show-model-information)\n      - [copy: Copy a Model](#copy-copy-a-model)\n      - [delete: Delete a Model](#delete-delete-a-model)\n      - [pull: Pull a Model](#pull-pull-a-model)\n      - [push: Push a Model](#push-push-a-model)\n  - [Modes](#modes)\n    - [Text](#text)\n    - [Image](#image)\n  - [Streaming and Server-Sent Events (SSE)](#streaming-and-server-sent-events-sse)\n    - [Server-Sent Events (SSE) Hang](#server-sent-events-sse-hang)\n  - [New Functionalities and APIs](#new-functionalities-and-apis)\n  - [Request Options](#request-options)\n    - [Adapter](#adapter)\n    - [Timeout](#timeout)\n  - [Error Handling](#error-handling)\n    - [Rescuing](#rescuing)\n    - [For Short](#for-short)\n    - [Errors](#errors)\n- [Development](#development)\n  - [Purpose](#purpose)\n  - [Publish to RubyGems](#publish-to-rubygems)\n  - [Updating the README](#updating-the-readme)\n- [Resources and References](#resources-and-references)\n- [Disclaimer](#disclaimer)\n\n## Setup\n\n### Installing\n\n```sh\ngem install ollama-ai -v 1.3.0\n```\n\n```sh\ngem 'ollama-ai', '~\u003e 1.3.0'\n```\n\n## Usage\n\n### Client\n\nCreate a new client:\n```ruby\nrequire 'ollama-ai'\n\nclient = Ollama.new(\n  credentials: { address: 'http://localhost:11434' },\n  options: { server_sent_events: true }\n)\n```\n\n#### Bearer Authentication\n\n```ruby\nrequire 'ollama-ai'\n\nclient = Ollama.new(\n  credentials: {\n    address: 'http://localhost:11434',\n    bearer_token: 'eyJhbG...Qssw5c'\n  },\n  options: { server_sent_events: true }\n)\n```\n\nRemember that hardcoding your credentials in code is unsafe. It's preferable to use environment variables:\n\n```ruby\nrequire 'ollama-ai'\n\nclient = Ollama.new(\n  credentials: {\n    address: 'http://localhost:11434',\n    bearer_token: ENV['OLLAMA_BEARER_TOKEN']\n  },\n  options: { server_sent_events: true }\n)\n```\n\n### Methods\n\n```ruby\nclient.generate\nclient.chat\nclient.embeddings\n\nclient.create\nclient.tags\nclient.show\nclient.copy\nclient.delete\nclient.pull\nclient.push\n```\n\n#### generate: Generate a completion\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion\n\n##### Without Streaming Events\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion\n\n```ruby\nresult = client.generate(\n  { model: 'llama2',\n    prompt: 'Hi!',\n    stream: false }\n)\n```\n\nResult:\n```ruby\n[{ 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:35:41.951371247Z',\n   'response' =\u003e \"Hi there! It's nice to meet you. How are you today?\",\n   'done' =\u003e true,\n   'context' =\u003e\n     [518, 25_580,\n      # ...\n      9826, 29_973],\n   'total_duration' =\u003e 6_981_097_576,\n   'load_duration' =\u003e 625_053,\n   'prompt_eval_count' =\u003e 22,\n   'prompt_eval_duration' =\u003e 4_075_171_000,\n   'eval_count' =\u003e 16,\n   'eval_duration' =\u003e 2_900_325_000 }]\n```\n\n##### Receiving Stream Events\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion\n\nEnsure that you have enabled [Server-Sent Events](#streaming-and-server-sent-events-sse) before using blocks for streaming. `stream: true` is not necessary, as `true` is the [default](https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion):\n\n```ruby\nclient.generate(\n  { model: 'llama2',\n    prompt: 'Hi!' }\n) do |event, raw|\n  puts event\nend\n```\n\nEvent:\n```ruby\n{ 'model' =\u003e 'llama2',\n  'created_at' =\u003e '2024-01-07T01:36:30.665245712Z',\n  'response' =\u003e 'Hello',\n  'done' =\u003e false }\n```\n\nYou can get all the receive events at once as an array:\n```ruby\nresult = client.generate(\n  { model: 'llama2',\n    prompt: 'Hi!' }\n)\n```\n\nResult:\n```ruby\n[{ 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:36:30.665245712Z',\n   'response' =\u003e 'Hello',\n   'done' =\u003e false },\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:36:30.927337136Z',\n   'response' =\u003e '!',\n   'done' =\u003e false },\n # ...\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:36:37.249416767Z',\n   'response' =\u003e '?',\n   'done' =\u003e false },\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:36:37.44041283Z',\n   'response' =\u003e '',\n   'done' =\u003e true,\n   'context' =\u003e\n     [518, 25_580,\n      # ...\n      13_563, 29_973],\n   'total_duration' =\u003e 10_551_395_645,\n   'load_duration' =\u003e 966_631,\n   'prompt_eval_count' =\u003e 22,\n   'prompt_eval_duration' =\u003e 4_034_990_000,\n   'eval_count' =\u003e 25,\n   'eval_duration' =\u003e 6_512_954_000 }]\n```\n\nYou can mix both as well:\n```ruby\nresult = client.generate(\n  { model: 'llama2',\n    prompt: 'Hi!' }\n) do |event, raw|\n  puts event\nend\n```\n\n#### chat: Generate a chat completion\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-chat-completion\n\n```ruby\nresult = client.chat(\n  { model: 'llama2',\n    messages: [\n      { role: 'user', content: 'Hi! My name is Purple.' }\n    ] }\n) do |event, raw|\n  puts event\nend\n```\n\nEvent:\n```ruby\n{ 'model' =\u003e 'llama2',\n  'created_at' =\u003e '2024-01-07T01:38:01.729897311Z',\n  'message' =\u003e { 'role' =\u003e 'assistant', 'content' =\u003e \"\\n\" },\n  'done' =\u003e false }\n```\n\nResult:\n```ruby\n[{ 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:38:01.729897311Z',\n   'message' =\u003e { 'role' =\u003e 'assistant', 'content' =\u003e \"\\n\" },\n   'done' =\u003e false },\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:38:02.081494506Z',\n   'message' =\u003e { 'role' =\u003e 'assistant', 'content' =\u003e '*' },\n   'done' =\u003e false },\n # ...\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:38:17.855905499Z',\n   'message' =\u003e { 'role' =\u003e 'assistant', 'content' =\u003e '?' },\n   'done' =\u003e false },\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:38:18.07331245Z',\n   'message' =\u003e { 'role' =\u003e 'assistant', 'content' =\u003e '' },\n   'done' =\u003e true,\n   'total_duration' =\u003e 22_494_544_502,\n   'load_duration' =\u003e 4_224_600,\n   'prompt_eval_count' =\u003e 28,\n   'prompt_eval_duration' =\u003e 6_496_583_000,\n   'eval_count' =\u003e 61,\n   'eval_duration' =\u003e 15_991_728_000 }]\n```\n\n##### Back-and-Forth Conversations\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-chat-completion\n\nTo maintain a back-and-forth conversation, you need to append the received responses and build a history for your requests:\n\n```ruby\nresult = client.chat(\n  { model: 'llama2',\n    messages: [\n      { role: 'user', content: 'Hi! My name is Purple.' },\n      { role: 'assistant',\n        content: 'Hi, Purple!' },\n      { role: 'user', content: \"What's my name?\" }\n    ] }\n) do |event, raw|\n  puts event\nend\n```\n\nEvent:\n\n```ruby\n{ 'model' =\u003e 'llama2',\n  'created_at' =\u003e '2024-01-07T01:40:07.352998498Z',\n  'message' =\u003e { 'role' =\u003e 'assistant', 'content' =\u003e ' Pur' },\n  'done' =\u003e false }\n```\n\nResult:\n```ruby\n[{ 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:40:06.562939469Z',\n   'message' =\u003e { 'role' =\u003e 'assistant', 'content' =\u003e 'Your' },\n   'done' =\u003e false },\n # ...\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:40:07.352998498Z',\n   'message' =\u003e { 'role' =\u003e 'assistant', 'content' =\u003e ' Pur' },\n   'done' =\u003e false },\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:40:07.545323584Z',\n   'message' =\u003e { 'role' =\u003e 'assistant', 'content' =\u003e 'ple' },\n   'done' =\u003e false },\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:40:07.77769408Z',\n   'message' =\u003e { 'role' =\u003e 'assistant', 'content' =\u003e '!' },\n   'done' =\u003e false },\n { 'model' =\u003e 'llama2',\n   'created_at' =\u003e '2024-01-07T01:40:07.974165849Z',\n   'message' =\u003e { 'role' =\u003e 'assistant', 'content' =\u003e '' },\n   'done' =\u003e true,\n   'total_duration' =\u003e 11_482_012_681,\n   'load_duration' =\u003e 4_246_882,\n   'prompt_eval_count' =\u003e 57,\n   'prompt_eval_duration' =\u003e 10_387_150_000,\n   'eval_count' =\u003e 6,\n   'eval_duration' =\u003e 1_089_249_000 }]\n```\n\n#### embeddings: Generate Embeddings\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-embeddings\n\n```ruby\nresult = client.embeddings(\n  { model: 'llama2',\n    prompt: 'Hi!' }\n)\n```\n\nResult:\n```ruby\n[{ 'embedding' =\u003e\n   [0.6970467567443848, -2.248202085494995,\n    # ...\n    -1.5994540452957153, -0.3464218080043793] }]\n```\n\n#### Models\n\n##### create: Create a Model\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#create-a-model\n\n```ruby\nresult = client.create(\n  { name: 'mario',\n    modelfile: \"FROM llama2\\nSYSTEM You are mario from Super Mario Bros.\" }\n) do |event, raw|\n  puts event\nend\n```\n\nEvent:\n```ruby\n{ 'status' =\u003e 'reading model metadata' }\n```\n\nResult:\n```ruby\n[{ 'status' =\u003e 'reading model metadata' },\n { 'status' =\u003e 'creating system layer' },\n { 'status' =\u003e\n   'using already created layer sha256:4eca7304a07a42c48887f159ef5ad82ed5a5bd30fe52db4aadae1dd938e26f70' },\n { 'status' =\u003e\n   'using already created layer sha256:876a8d805b60882d53fed3ded3123aede6a996bdde4a253de422cacd236e33d3' },\n { 'status' =\u003e\n   'using already created layer sha256:a47b02e00552cd7022ea700b1abf8c572bb26c9bc8c1a37e01b566f2344df5dc' },\n { 'status' =\u003e\n   'using already created layer sha256:f02dd72bb2423204352eabc5637b44d79d17f109fdb510a7c51455892aa2d216' },\n { 'status' =\u003e\n   'writing layer sha256:1741cf59ce26ff01ac614d31efc700e21e44dd96aed60a7c91ab3f47e440ef94' },\n { 'status' =\u003e\n   'writing layer sha256:e8bcbb2eebad88c2fa64bc32939162c064be96e70ff36aff566718fc9186b427' },\n { 'status' =\u003e 'writing manifest' },\n { 'status' =\u003e 'success' }]\n```\n\nAfter creation, you can use it:\n```ruby\nclient.generate(\n  { model: 'mario',\n    prompt: 'Hi! Who are you?' }\n) do |event, raw|\n  print event['response']\nend\n```\n\n\u003e _Woah! *adjusts sunglasses* It's-a me, Mario! *winks* You must be a new friend I've-a met here in the Mushroom Kingdom. *tips top hat* What brings you to this neck of the woods? Maybe you're looking for-a some help on your adventure? *nods* Just let me know, and I'll do my best to-a assist ya! 😃_\n\n##### tags: List Local Models\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#list-local-models\n\n```ruby\nresult = client.tags\n```\n\nResult:\n```ruby\n[{ 'models' =\u003e\n   [{ 'name' =\u003e 'llama2:latest',\n      'modified_at' =\u003e '2024-01-06T15:06:23.6349195-03:00',\n      'size' =\u003e 3_826_793_677,\n      'digest' =\u003e\n      '78e26419b4469263f75331927a00a0284ef6544c1975b826b15abdaef17bb962',\n      'details' =\u003e\n      { 'format' =\u003e 'gguf',\n        'family' =\u003e 'llama',\n        'families' =\u003e ['llama'],\n        'parameter_size' =\u003e '7B',\n        'quantization_level' =\u003e 'Q4_0' } },\n    { 'name' =\u003e 'mario:latest',\n      'modified_at' =\u003e '2024-01-06T22:41:59.495298101-03:00',\n      'size' =\u003e 3_826_793_787,\n      'digest' =\u003e\n      '291f46d2fa687dfaff45de96a8cb6e32707bc16ec1e1dfe8d65e9634c34c660c',\n      'details' =\u003e\n      { 'format' =\u003e 'gguf',\n        'family' =\u003e 'llama',\n        'families' =\u003e ['llama'],\n        'parameter_size' =\u003e '7B',\n        'quantization_level' =\u003e 'Q4_0' } }] }]\n```\n\n##### show: Show Model Information\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#show-model-information\n\n```ruby\nresult = client.show(\n  { name: 'llama2' }\n)\n```\n\nResult:\n```ruby\n[{ 'license' =\u003e\n     \"LLAMA 2 COMMUNITY LICENSE AGREEMENT\\t\\n\" \\\n     # ...\n     \"* Reporting violations of the Acceptable Use Policy or unlicensed uses of Llama...\" \\\n     \"\\n\",\n   'modelfile' =\u003e\n     \"# Modelfile generated by \\\"ollama show\\\"\\n\" \\\n     # ...\n     'PARAMETER stop \"\u003c\u003c/SYS\u003e\u003e\"',\n   'parameters' =\u003e\n     \"stop                           [INST]\\n\" \\\n     \"stop                           [/INST]\\n\" \\\n     \"stop                           \u003c\u003cSYS\u003e\u003e\\n\" \\\n     'stop                           \u003c\u003c/SYS\u003e\u003e',\n     'template' =\u003e\n     \"[INST] \u003c\u003cSYS\u003e\u003e{{ .System }}\u003c\u003c/SYS\u003e\u003e\\n\\n{{ .Prompt }} [/INST]\\n\",\n   'details' =\u003e\n     { 'format' =\u003e 'gguf',\n       'family' =\u003e 'llama',\n       'families' =\u003e ['llama'],\n       'parameter_size' =\u003e '7B',\n       'quantization_level' =\u003e 'Q4_0' } }]\n```\n\n##### copy: Copy a Model\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#copy-a-model\n\n```ruby\nresult = client.copy(\n  { source: 'llama2',\n    destination: 'llama2-backup' }\n)\n```\n\nResult:\n```ruby\ntrue\n```\n\nIf the source model does not exist:\n```ruby\nbegin\n  result = client.copy(\n    { source: 'purple',\n      destination: 'purple-backup' }\n  )\nrescue Ollama::Errors::OllamaError =\u003e error\n  puts error.class # Ollama::Errors::RequestError\n  puts error.message # 'the server responded with status 404'\n\n  puts error.payload\n  # { source: 'purple',\n  #   destination: 'purple-backup',\n  #   ...\n  # }\n\n  puts error.request.inspect\n  # #\u003cFaraday::ResourceNotFound response={:status=\u003e404, :headers...\nend\n```\n\n##### delete: Delete a Model\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#delete-a-model\n\n```ruby\nresult = client.delete(\n  { name: 'llama2' }\n)\n```\n\nResult:\n```ruby\ntrue\n```\n\nIf the model does not exist:\n```ruby\nbegin\n  result = client.delete(\n    { name: 'llama2' }\n  )\nrescue Ollama::Errors::OllamaError =\u003e error\n  puts error.class # Ollama::Errors::RequestError\n  puts error.message # 'the server responded with status 404'\n\n  puts error.payload\n  # { name: 'llama2',\n  #   ...\n  # }\n\n  puts error.request.inspect\n  # #\u003cFaraday::ResourceNotFound response={:status=\u003e404, :headers...\nend\n```\n\n##### pull: Pull a Model\n\nAPI Documentation: https://github.com/jmorganca/ollama/blob/main/docs/api.md#pull-a-model\n\n```ruby\nresult = client.pull(\n  { name: 'llama2' }\n) do |event, raw|\n  puts event\nend\n```\n\nEvent:\n```ruby\n{ 'status' =\u003e 'pulling manifest' }\n```\n\nResult:\n```ruby\n[{ 'status' =\u003e 'pulling manifest' },\n { 'status' =\u003e 'pulling 4eca7304a07a',\n   'digest' =\u003e\n   'sha256:4eca7304a07a42c48887f159ef5ad82ed5a5bd30fe52db4aadae1dd938e26f70',\n   'total' =\u003e 1_602_463_008,\n   'completed' =\u003e 1_602_463_008 },\n # ...\n { 'status' =\u003e 'verifying sha256 digest' },\n { 'status' =\u003e 'writing manifest' },\n { 'status' =\u003e 'removing any unused layers' },\n { 'status' =\u003e 'success' }]\n```\n\n##### push: Push a Model\n\nDocumentation: [API](https://github.com/jmorganca/ollama/blob/main/docs/api.md#push-a-model) and [_Publishing Your Model_](https://github.com/jmorganca/ollama/blob/main/docs/import.md#publishing-your-model-optional--early-alpha).\n\n\nYou need to create an account at https://ollama.ai and add your Public Key at https://ollama.ai/settings/keys.\n\nYour keys are located in `/usr/share/ollama/.ollama/`. You may need to copy them to your user directory:\n\n```sh\nsudo cp /usr/share/ollama/.ollama/id_ed25519 ~/.ollama/\nsudo cp /usr/share/ollama/.ollama/id_ed25519.pub ~/.ollama/\n```\n\nCopy your model to your user namespace:\n\n```ruby\nclient.copy(\n  { source: 'mario',\n    destination: 'your-user/mario' }\n)\n```\n\nAnd push it:\n\n```ruby\nresult = client.push(\n  { name: 'your-user/mario' }\n) do |event, raw|\n  puts event\nend\n```\n\nEvent:\n```ruby\n{ 'status' =\u003e 'retrieving manifest' }\n```\n\nResult:\n```ruby\n[{ 'status' =\u003e 'retrieving manifest' },\n { 'status' =\u003e 'pushing 4eca7304a07a',\n   'digest' =\u003e\n   'sha256:4eca7304a07a42c48887f159ef5ad82ed5a5bd30fe52db4aadae1dd938e26f70',\n   'total' =\u003e 1_602_463_008,\n   'completed' =\u003e 1_602_463_008 },\n # ...\n { 'status' =\u003e 'pushing e8bcbb2eebad',\n   'digest' =\u003e\n   'sha256:e8bcbb2eebad88c2fa64bc32939162c064be96e70ff36aff566718fc9186b427',\n   'total' =\u003e 555,\n   'completed' =\u003e 555 },\n { 'status' =\u003e 'pushing manifest' },\n { 'status' =\u003e 'success' }]\n```\n\n### Modes\n\n#### Text\n\nYou can use the [generate](#generate-generate-a-completion) or [chat](#chat-generate-a-chat-completion) methods for text.\n\n#### Image\n\n![A black and white image of an old piano. The piano is an upright model, with the keys on the right side of the image. The piano is sitting on a tiled floor. There is a small round object on the top of the piano.](https://raw.githubusercontent.com/gbaptista/assets/main/gemini-ai/piano.jpg)\n\n\u003e _Courtesy of [Unsplash](https://unsplash.com/photos/greyscale-photo-of-grand-piano-czPs0z3-Ggg)_\n\nYou need to choose a model that supports images, like [LLaVA](https://ollama.ai/library/llava) or [bakllava](https://ollama.ai/library/bakllava), and encode the image as [Base64](https://en.wikipedia.org/wiki/Base64).\n\nDepending on your hardware, some models that support images can be slow, so you may want to increase the client [timeout](#timeout):\n\n```ruby\nclient = Ollama.new(\n  credentials: { address: 'http://localhost:11434' },\n  options: {\n    server_sent_events: true,\n    connection: { request: { timeout: 120, read_timeout: 120 } } }\n)\n```\n\nUsing the `generate` method:\n\n```ruby\nrequire 'base64'\n\nclient.generate(\n  { model: 'llava',\n    prompt: 'Please describe this image.',\n    images: [Base64.strict_encode64(File.read('piano.jpg'))] }\n) do |event, raw|\n  print event['response']\nend\n```\n\nOutput:\n\u003e _The image is a black and white photo of an old piano, which appears to be in need of maintenance. A chair is situated right next to the piano. Apart from that, there are no other objects or people visible in the scene._\n\nUsing the `chat` method:\n```ruby\nrequire 'base64'\n\nresult = client.chat(\n  { model: 'llava',\n    messages: [\n      { role: 'user',\n        content: 'Please describe this image.',\n        images: [Base64.strict_encode64(File.read('piano.jpg'))] }\n    ] }\n) do |event, raw|\n  puts event\nend\n```\n\nOutput:\n\u003e _The image displays an old piano, sitting on a wooden floor with black keys. Next to the piano, there is another keyboard in the scene, possibly used for playing music._\n\u003e \n\u003e _On top of the piano, there are two mice placed in different locations within its frame. These mice might be meant for controlling the music being played or simply as decorative items. The overall atmosphere seems to be focused on artistic expression through this unique instrument._\n\n### Streaming and Server-Sent Events (SSE)\n\n[Server-Sent Events (SSE)](https://en.wikipedia.org/wiki/Server-sent_events) is a technology that allows certain endpoints to offer streaming capabilities, such as creating the impression that \"the model is typing along with you,\" rather than delivering the entire answer all at once.\n\nYou can set up the client to use Server-Sent Events (SSE) for all supported endpoints:\n```ruby\nclient = Ollama.new(\n  credentials: { address: 'http://localhost:11434' },\n  options: { server_sent_events: true }\n)\n```\n\nOr, you can decide on a request basis:\n```ruby\nresult = client.generate(\n  { model: 'llama2',\n    prompt: 'Hi!' },\n  server_sent_events: true\n) do |event, raw|\n  puts event\nend\n```\n\nWith Server-Sent Events (SSE) enabled, you can use a block to receive partial results via events. This feature is particularly useful for methods that offer streaming capabilities, such as `generate`: [Receiving Stream Events](#receiving-stream-events)\n\n#### Server-Sent Events (SSE) Hang\n\nMethod calls will _hang_ until the server-sent events finish, so even without providing a block, you can obtain the final results of the received events: [Receiving Stream Events](#receiving-stream-events)\n\n### New Functionalities and APIs\n\nOllama may launch a new endpoint that we haven't covered in the Gem yet. If that's the case, you may still be able to use it through the `request` method. For example, `generate` is just a wrapper for `api/generate`, which you can call directly like this:\n\n```ruby\nresult = client.request(\n  'api/generate',\n  { model: 'llama2',\n    prompt: 'Hi!' },\n  request_method: 'POST', server_sent_events: true\n)\n```\n\n### Request Options\n\n#### Adapter\n\nThe gem uses [Faraday](https://github.com/lostisland/faraday) with the [Typhoeus](https://github.com/typhoeus/typhoeus) adapter by default.\n\nYou can use a different adapter if you want:\n\n```ruby\nrequire 'faraday/net_http'\n\nclient = Ollama.new(\n  credentials: { address: 'http://localhost:11434' },\n  options: { connection: { adapter: :net_http } }\n)\n```\n\n#### Timeout\n\nYou can set the maximum number of seconds to wait for the request to complete with the `timeout` option:\n\n```ruby\nclient = Ollama.new(\n  credentials: { address: 'http://localhost:11434' },\n  options: { connection: { request: { timeout: 5 } } }\n)\n```\n\nYou can also have more fine-grained control over [Faraday's Request Options](https://lostisland.github.io/faraday/#/customization/request-options?id=request-options) if you prefer:\n\n```ruby\nclient = Ollama.new(\n  credentials: { address: 'http://localhost:11434' },\n  options: {\n    connection: {\n      request: {\n        timeout: 5,\n        open_timeout: 5,\n        read_timeout: 5,\n        write_timeout: 5\n      }\n    }\n  }\n)\n```\n\n### Error Handling\n\n#### Rescuing\n\n```ruby\nrequire 'ollama-ai'\n\nbegin\n  client.chat_completions(\n    { model: 'llama2',\n      prompt: 'Hi!' }\n  )\nrescue Ollama::Errors::OllamaError =\u003e error\n  puts error.class # Ollama::Errors::RequestError\n  puts error.message # 'the server responded with status 500'\n\n  puts error.payload\n  # { model: 'llama2',\n  #   prompt: 'Hi!',\n  #   ...\n  # }\n\n  puts error.request.inspect\n  # #\u003cFaraday::ServerError response={:status=\u003e500, :headers...\nend\n```\n\n#### For Short\n\n```ruby\nrequire 'ollama-ai/errors'\n\nbegin\n  client.chat_completions(\n    { model: 'llama2',\n      prompt: 'Hi!' }\n  )\nrescue OllamaError =\u003e error\n  puts error.class # Ollama::Errors::RequestError\nend\n```\n\n#### Errors\n\n```ruby\nOllamaError\n\nBlockWithoutServerSentEventsError\n\nRequestError\n```\n\n## Development\n\n```bash\nbundle\nrubocop -A\n\nbundle exec ruby spec/tasks/run-client.rb\nbundle exec ruby spec/tasks/test-encoding.rb\n```\n\n### Purpose\n\nThis Gem is designed to provide low-level access to Ollama, enabling people to build abstractions on top of it. If you are interested in more high-level abstractions or more user-friendly tools, you may want to consider [Nano Bots](https://github.com/icebaker/ruby-nano-bots) 💎 🤖.\n\n### Publish to RubyGems\n\n```bash\ngem build ollama-ai.gemspec\n\ngem signin\n\ngem push ollama-ai-1.3.0.gem\n```\n\n### Updating the README\n\nInstall [Babashka](https://babashka.org):\n\n```sh\ncurl -s https://raw.githubusercontent.com/babashka/babashka/master/install | sudo bash\n```\n\nUpdate the `template.md` file and then:\n\n```sh\nbb tasks/generate-readme.clj\n```\n\nTrick for automatically updating the `README.md` when `template.md` changes:\n\n```sh\nsudo pacman -S inotify-tools # Arch / Manjaro\nsudo apt-get install inotify-tools # Debian / Ubuntu / Raspberry Pi OS\nsudo dnf install inotify-tools # Fedora / CentOS / RHEL\n\nwhile inotifywait -e modify template.md; do bb tasks/generate-readme.clj; done\n```\n\nTrick for Markdown Live Preview:\n```sh\npip install -U markdown_live_preview\n\nmlp README.md -p 8076\n```\n\n## Resources and References\n\nThese resources and references may be useful throughout your learning process:\n\n- [Ollama Official Website](https://ollama.ai)\n- [Ollama GitHub](https://github.com/jmorganca/ollama)\n- [Ollama API Documentation](https://github.com/jmorganca/ollama/blob/main/docs/api.md)\n\n## Disclaimer\n\nThis is not an official Ollama project, nor is it affiliated with Ollama in any way.\n\nThis software is distributed under the [MIT License](https://github.com/gbaptista/ollama-ai/blob/main/LICENSE). This license includes a disclaimer of warranty. Moreover, the authors assume no responsibility for any damage or costs that may result from using this project. Use the Ollama AI Ruby Gem at your own risk.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgbaptista%2Follama-ai","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgbaptista%2Follama-ai","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgbaptista%2Follama-ai/lists"}