mirror of
https://github.com/ollama/ollama-python.git
synced 2026-08-12 07:57:47 +00:00
Update examples and readme
This commit is contained in:
+15
-9
@@ -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 <example>.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 <model> <modelfile>
|
||||
@@ -46,10 +53,9 @@ python create.py <model> <modelfile>
|
||||
|
||||
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)
|
||||
|
||||
|
||||
|
||||
+9
-25
@@ -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__':
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
from ollama import embed
|
||||
|
||||
response = embed(model='llama3.1', input='Hello, world!')
|
||||
print(response['embeddings'])
|
||||
@@ -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],
|
||||
}
|
||||
],
|
||||
|
||||
@@ -4,6 +4,7 @@ import httpx
|
||||
|
||||
from ollama import generate
|
||||
|
||||
|
||||
latest = httpx.get('https://xkcd.com/info.0.json')
|
||||
latest.raise_for_status()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user