[PR #522] [MERGED] add a simple python client to access ollama #10196

Closed
opened 2026-04-12 22:54:11 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/522
Author: @pdevine
Created: 9/13/2023
Status: Merged
Merged: 9/14/2023
Merged by: @pdevine

Base: mainHead: python-bindings


📝 Commits (2)

  • 0ed358d add a simple python client to access ollama
  • cad4c59 comments

📊 Changes

1 file changed (+225 additions, -0 deletions)

View changed files

api/client.py (+225 -0)

📄 Description

These are some simple python bindings for interacting with the local Ollama server. Most of the functions should be pretty straight forward, and each of the streaming endpoints has a default way of handling the output but can be passed in a "callback" function to override the default.

The callback functions can be as simple as:

def my_callback(chunk):
        """
        Callback function to handle individual chunks of the streaming response.
   
        Parameters:
        - chunk (dict): The individual chunk of JSON data from the streaming response.
        """
   
        # Here, we are simply printing the entire chunk as a JSON string with indentation
        # for readability. In a real application, you would likely want to do something
        # more specific with the data in each chunk.
        print(json.dumps(chunk, indent=4))
   
        # If you want to specifically print the 'response' field, you can do so like this:
        # response_piece = chunk.get('response')
        # if response_piece:
        #     print(response_piece)

It's been a little while since I put together a python library, so I'm not sure if I hit all of the correct idioms here. To test it out, you can do something like:

>>> import sys
>>> sys.path.append("<path to git/ollama/api>")
>>> import client

To run something like generate:

>>> response, history = client.generate("llama2", "Is friendship like magic?")

This will return a string for the output and the context history which you can feed back in with:
>>> client.generate("llama2", "Friendship sure feels like magic", context=history)

To hook in the above callback, use:
>>> client.generate("llama2", "Is the universe a giant black hole?", callback=my_callback)




---

<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/522 **Author:** [@pdevine](https://github.com/pdevine) **Created:** 9/13/2023 **Status:** ✅ Merged **Merged:** 9/14/2023 **Merged by:** [@pdevine](https://github.com/pdevine) **Base:** `main` ← **Head:** `python-bindings` --- ### 📝 Commits (2) - [`0ed358d`](https://github.com/ollama/ollama/commit/0ed358d77f5f952c839dcd1e08bc1535d01020f7) add a simple python client to access ollama - [`cad4c59`](https://github.com/ollama/ollama/commit/cad4c59c2d94edd25bc9c3508f44f400905105e5) comments ### 📊 Changes **1 file changed** (+225 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `api/client.py` (+225 -0) </details> ### 📄 Description These are some simple python bindings for interacting with the local Ollama server. Most of the functions should be pretty straight forward, and each of the streaming endpoints has a default way of handling the output but can be passed in a "callback" function to override the default. The callback functions can be as simple as: ``` def my_callback(chunk): """ Callback function to handle individual chunks of the streaming response. Parameters: - chunk (dict): The individual chunk of JSON data from the streaming response. """ # Here, we are simply printing the entire chunk as a JSON string with indentation # for readability. In a real application, you would likely want to do something # more specific with the data in each chunk. print(json.dumps(chunk, indent=4)) # If you want to specifically print the 'response' field, you can do so like this: # response_piece = chunk.get('response') # if response_piece: # print(response_piece) ``` It's been a little while since I put together a python library, so I'm not sure if I hit all of the correct idioms here. To test it out, you can do something like: ``` >>> import sys >>> sys.path.append("<path to git/ollama/api>") >>> import client ``` To run something like generate: ``` >>> response, history = client.generate("llama2", "Is friendship like magic?") This will return a string for the output and the context history which you can feed back in with: >>> client.generate("llama2", "Friendship sure feels like magic", context=history) To hook in the above callback, use: >>> client.generate("llama2", "Is the universe a giant black hole?", callback=my_callback) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-12 22:54:11 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#10196