From 6fa242894ff3a15061826bc0a13b142299479e10 Mon Sep 17 00:00:00 2001 From: ParthSareen Date: Wed, 20 Nov 2024 14:15:41 -0800 Subject: [PATCH] Update examples and readme --- examples/README.md | 24 ++++++++++++++--------- examples/async-list.py | 34 +++++++++------------------------ examples/embed.py | 4 ++++ examples/multimodal_chat.py | 18 +++++++++++------ examples/multimodal_generate.py | 1 + 5 files changed, 41 insertions(+), 40 deletions(-) create mode 100644 examples/embed.py diff --git a/examples/README.md b/examples/README.md index 73c61ad..96cbe97 100644 --- a/examples/README.md +++ b/examples/README.md @@ -2,7 +2,7 @@ Run the examples in this directory with: ```sh -# Navigate to examples directory +# Ensure you are in the examples directory cd examples/ # Run example @@ -14,30 +14,37 @@ python3 .py - [async-chat.py](async-chat.py) - [chat-stream.py](chat-stream.py) - Streamed outputs + ### Generate - Generate text with a model - [generate.py](generate.py) - [async-generate.py](async-generate.py) - [generate-stream.py](generate-stream.py) - Streamed outputs + ### Tools/Function Calling - Call a function with a model - [tools.py](tools.py) - Simple example of Tools/Function Calling - [async-tools.py](async-tools.py) -### Multimodal - Chat with a multimodal model + +### Multimodal with Image - Chat with a multimodal (image chat) model - [multimodal_chat.py](multimodal_chat.py) - [multimodal_generate.py](multimodal_generate.py) - ### Ollama List - List all downloaded models and their properties - [list.py](list.py) - [async-list.py](async-list.py) + +### Ollama ps - Show model status with CPU/GPU usage +- [ps.py](ps.py) + + ### Ollama Pull - Pull a model from Ollama Requirement: `pip install tqdm` - - [pull.py](pull.py) + ### Ollama Create - Create a model from a Modelfile ```python python create.py @@ -46,10 +53,9 @@ python create.py See [ollama/docs/modelfile.md](https://github.com/ollama/ollama/blob/main/docs/modelfile.md) for more information on the Modelfile format. + +### Ollama Embed - Generate embeddings with a model +- [embed.py](embed.py) + ### Fill in the middle - [fill-in-middle.py](fill-in-middle.py) - Given a prefix and suffix, fill in the middle - -### Ollama ps - Show model status with CPU/GPU usage -- [ps.py](ps.py) - - diff --git a/examples/async-list.py b/examples/async-list.py index 82d2fcd..1b69a94 100644 --- a/examples/async-list.py +++ b/examples/async-list.py @@ -6,31 +6,15 @@ async def main(): client = ollama.AsyncClient() response = await client.list() - models = response['models'] - models_data = [] - for model in models: - if model.get('details'): # Check if details exist - models_data.append( - ( - model.get('name', 'N/A'), - model.get('size', 0) / 1024 / 1024, # Convert to MB - model.get('details', {}).get('format', 'N/A'), - model.get('details', {}).get('family', 'N/A'), - model.get('details', {}).get('parameter_size', 'N/A'), - model.get('details', {}).get('quantization_level', 'N/A'), - ) - ) - - print(f'\n{len(models)} models found!') - print('\nDetailed model information:') - for model in models_data: - print(f'Name: {model[0]}') - print(f'Size (MB): {model[1]:.2f}') - print(f'Format: {model[2]}') - print(f'Family: {model[3]}') - print(f'Parameter Size: {model[4]}') - print(f'Quantization Level: {model[5]}') - print('-' * 50) + for model in response.models: + if model.details: + print(f'Name: {model.model}') + print(f'Size (MB): {(model.size.real / 1024 / 1024):.2f}') + print(f'Format: {model.details.format}') + print(f'Family: {model.details.family}') + print(f'Parameter Size: {model.details.parameter_size}') + print(f'Quantization Level: {model.details.quantization_level}') + print('-' * 50) if __name__ == '__main__': diff --git a/examples/embed.py b/examples/embed.py new file mode 100644 index 0000000..8e31d8c --- /dev/null +++ b/examples/embed.py @@ -0,0 +1,4 @@ +from ollama import embed + +response = embed(model='llama3.1', input='Hello, world!') +print(response['embeddings']) diff --git a/examples/multimodal_chat.py b/examples/multimodal_chat.py index f23e5ed..8aff9f4 100644 --- a/examples/multimodal_chat.py +++ b/examples/multimodal_chat.py @@ -1,14 +1,20 @@ -from ollama import Client +from ollama import chat +# from pathlib import Path -client = Client() -path = '' -# Passing in wrong path for image error sucks -response = client.chat( +# Pass in the path to the image +path = input('Please enter the path to the image: ') + +# You can also pass in base64 encoded image data +# img = base64.b64encode(Path(path).read_bytes()).decode() +# or the raw bytes +# img = Path(path).read_bytes() + +response = chat( model='llama3.2-vision', messages=[ { 'role': 'user', - 'content': 'What is in this image? Be concise. Respond with the structure {"focal": "...", "subject": "...", "background": "..."}', + 'content': 'What is in this image? Be concise.', 'images': [path], } ], diff --git a/examples/multimodal_generate.py b/examples/multimodal_generate.py index ad70139..44b3716 100644 --- a/examples/multimodal_generate.py +++ b/examples/multimodal_generate.py @@ -4,6 +4,7 @@ import httpx from ollama import generate + latest = httpx.get('https://xkcd.com/info.0.json') latest.raise_for_status()