diff --git a/README.md b/README.md index b6ab33b..2eaed3e 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,9 @@ pip install ollama ## Usage ```python -from ollama import chat -from ollama import ChatResponse +import ollama -response: ChatResponse = chat(model='llama3.2', messages=[ +response: ollama.ChatResponse = ollama.chat(model='llama3.2', messages=[ { 'role': 'user', 'content': 'Why is the sky blue?', @@ -38,12 +37,12 @@ See [_types.py](ollama/_types.py) for more information on the response types. Response streaming can be enabled by setting `stream=True`. ```python -from ollama import chat +import ollama -stream = chat( - model='llama3.2', - messages=[{'role': 'user', 'content': 'Why is the sky blue?'}], - stream=True, +stream = ollama.chat( + model='llama3.2', + messages=[{'role': 'user', 'content': 'Why is the sky blue?'}], + stream=True, ) for chunk in stream: @@ -56,8 +55,9 @@ A custom client can be created by instantiating `Client` or `AsyncClient` from ` All extra keyword arguments are passed into the [`httpx.Client`](https://www.python-httpx.org/api/#client). ```python -from ollama import Client -client = Client( +import ollama + +client = ollama.Client( host='http://localhost:11434', headers={'x-some-header': 'some-value'} ) @@ -75,11 +75,12 @@ The `AsyncClient` class is used to make asynchronous requests. It can be configu ```python import asyncio -from ollama import AsyncClient +import ollama + async def chat(): message = {'role': 'user', 'content': 'Why is the sky blue?'} - response = await AsyncClient().chat(model='llama3.2', messages=[message]) + response = await ollama.AsyncClient().chat(model='llama3.2', messages=[message]) asyncio.run(chat()) ``` @@ -88,11 +89,11 @@ Setting `stream=True` modifies functions to return a Python asynchronous generat ```python import asyncio -from ollama import AsyncClient +import ollama async def chat(): message = {'role': 'user', 'content': 'Why is the sky blue?'} - async for part in await AsyncClient().chat(model='llama3.2', messages=[message], stream=True): + async for part in await ollama.AsyncClient().chat(model='llama3.2', messages=[message], stream=True): print(part['message']['content'], end='', flush=True) asyncio.run(chat()) diff --git a/examples/async-chat.py b/examples/async-chat.py index be10cfc..f3b6f5f 100644 --- a/examples/async-chat.py +++ b/examples/async-chat.py @@ -1,6 +1,6 @@ import asyncio -from ollama import AsyncClient +import ollama async def main(): @@ -11,7 +11,7 @@ async def main(): }, ] - client = AsyncClient() + client = ollama.AsyncClient() response = await client.chat('llama3.2', messages=messages) print(response['message']['content']) diff --git a/examples/async-structured-outputs.py b/examples/async-structured-outputs.py index a33c4c9..02c515f 100644 --- a/examples/async-structured-outputs.py +++ b/examples/async-structured-outputs.py @@ -2,7 +2,7 @@ from pydantic import BaseModel -from ollama import AsyncClient +import ollama # Define the schema for the response @@ -17,7 +17,7 @@ class FriendList(BaseModel): async def main(): - client = AsyncClient() + client = ollama.AsyncClient() response = await client.chat( model='llama3.1:8b', messages=[{'role': 'user', 'content': 'I have two friends. The first is Ollama 22 years old busy saving the world, and the second is Alonso 23 years old and wants to hang out. Return a list of friends in JSON format'}], diff --git a/examples/async-tools.py b/examples/async-tools.py index 5578229..5982cee 100644 --- a/examples/async-tools.py +++ b/examples/async-tools.py @@ -1,7 +1,6 @@ import asyncio import ollama -from ollama import ChatResponse def add_two_numbers(a: int, b: int) -> int: @@ -54,7 +53,7 @@ def subtract_two_numbers(a: int, b: int) -> int: async def main(): client = ollama.AsyncClient() - response: ChatResponse = await client.chat( + response: ollama.ChatResponse = await client.chat( 'llama3.1', messages=messages, tools=[add_two_numbers, subtract_two_numbers_tool], diff --git a/examples/chat-stream.py b/examples/chat-stream.py index 3aed84f..1c0d2d6 100644 --- a/examples/chat-stream.py +++ b/examples/chat-stream.py @@ -1,4 +1,4 @@ -from ollama import chat +import ollama messages = [ { @@ -7,7 +7,7 @@ }, ] -for part in chat('llama3.2', messages=messages, stream=True): +for part in ollama.chat('llama3.2', messages=messages, stream=True): print(part['message']['content'], end='', flush=True) print() diff --git a/examples/chat-with-history.py b/examples/chat-with-history.py index 2e0cc1d..82320bd 100644 --- a/examples/chat-with-history.py +++ b/examples/chat-with-history.py @@ -1,4 +1,4 @@ -from ollama import chat +import ollama messages = [ { @@ -21,7 +21,7 @@ while True: user_input = input('Chat with history: ') - response = chat( + response = ollama.chat( 'llama3.2', messages=messages + [ diff --git a/examples/chat.py b/examples/chat.py index 2a30f8a..115ad51 100644 --- a/examples/chat.py +++ b/examples/chat.py @@ -1,4 +1,4 @@ -from ollama import chat +import ollama messages = [ { @@ -7,5 +7,5 @@ }, ] -response = chat('llama3.2', messages=messages) +response = ollama.chat('llama3.2', messages=messages) print(response['message']['content']) diff --git a/examples/create.py b/examples/create.py index 1c06f05..889a88b 100644 --- a/examples/create.py +++ b/examples/create.py @@ -1,6 +1,6 @@ import sys -from ollama import create +import ollama args = sys.argv[1:] if len(args) == 2: @@ -25,5 +25,5 @@ SYSTEM You are Mario from super mario bros, acting as an assistant. """ -for response in create(model=args[0], modelfile=modelfile, stream=True): +for response in ollama.create(model=args[0], modelfile=modelfile, stream=True): print(response['status']) diff --git a/examples/embed.py b/examples/embed.py index 5af145e..a53c8da 100644 --- a/examples/embed.py +++ b/examples/embed.py @@ -1,4 +1,4 @@ -from ollama import embed +import ollama -response = embed(model='llama3.2', input='Hello, world!') +response = ollama.embed(model='llama3.2', input='Hello, world!') print(response['embeddings']) diff --git a/examples/fill-in-middle.py b/examples/fill-in-middle.py index 0bd2e01..447ff0b 100644 --- a/examples/fill-in-middle.py +++ b/examples/fill-in-middle.py @@ -1,4 +1,4 @@ -from ollama import generate +import ollama prompt = '''def remove_non_ascii(s: str) -> str: """ ''' @@ -7,7 +7,7 @@ return result """ -response = generate( +response = ollama.generate( model='codellama:7b-code', prompt=prompt, suffix=suffix, diff --git a/examples/generate-stream.py b/examples/generate-stream.py index 698a961..4c29ec0 100644 --- a/examples/generate-stream.py +++ b/examples/generate-stream.py @@ -1,4 +1,4 @@ -from ollama import generate +import ollama -for part in generate('llama3.2', 'Why is the sky blue?', stream=True): +for part in ollama.generate('llama3.2', 'Why is the sky blue?', stream=True): print(part['response'], end='', flush=True) diff --git a/examples/generate.py b/examples/generate.py index 7a94de4..56903e3 100644 --- a/examples/generate.py +++ b/examples/generate.py @@ -1,4 +1,4 @@ -from ollama import generate +import ollama -response = generate('llama3.2', 'Why is the sky blue?') +response = ollama.generate('llama3.2', 'Why is the sky blue?') print(response['response']) diff --git a/examples/list.py b/examples/list.py index 00d6243..284b2e0 100644 --- a/examples/list.py +++ b/examples/list.py @@ -1,8 +1,6 @@ -from ollama import ListResponse, list +import ollama -response: ListResponse = list() - -for model in response.models: +for model in ollama.list().models: print('Name:', model.model) print(' Size (MB):', f'{(model.size.real / 1024 / 1024):.2f}') if model.details: diff --git a/examples/multimodal-chat.py b/examples/multimodal-chat.py index c1a1859..6d2fe30 100644 --- a/examples/multimodal-chat.py +++ b/examples/multimodal-chat.py @@ -1,4 +1,4 @@ -from ollama import chat +import ollama # from pathlib import Path @@ -10,7 +10,7 @@ # or the raw bytes # img = Path(path).read_bytes() -response = chat( +response = ollama.chat( model='llama3.2-vision', messages=[ { diff --git a/examples/multimodal-generate.py b/examples/multimodal-generate.py index 643e9aa..28ff3a3 100644 --- a/examples/multimodal-generate.py +++ b/examples/multimodal-generate.py @@ -3,7 +3,7 @@ import httpx -from ollama import generate +import ollama latest = httpx.get('https://xkcd.com/info.0.json') latest.raise_for_status() @@ -23,7 +23,7 @@ raw = httpx.get(comic.json().get('img')) raw.raise_for_status() -for response in generate('llava', 'explain this comic:', images=[raw.content], stream=True): +for response in ollama.generate('llava', 'explain this comic:', images=[raw.content], stream=True): print(response['response'], end='', flush=True) print() diff --git a/examples/ps.py b/examples/ps.py index 5f6965c..3871f74 100644 --- a/examples/ps.py +++ b/examples/ps.py @@ -1,9 +1,8 @@ -from ollama import ProcessResponse, chat, ps, pull +import ollama # Ensure at least one model is loaded -response = pull('llama3.2', stream=True) progress_states = set() -for progress in response: +for progress in ollama.pull('llama3.2', stream=True): if progress.get('status') in progress_states: continue progress_states.add(progress.get('status')) @@ -12,11 +11,9 @@ print('\n') print('Waiting for model to load... \n') -chat(model='llama3.2', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}]) +ollama.chat(model='llama3.2', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}]) - -response: ProcessResponse = ps() -for model in response.models: +for model in ollama.ps().models: print('Model: ', model.model) print(' Digest: ', model.digest) print(' Expires at: ', model.expires_at) diff --git a/examples/pull.py b/examples/pull.py index bd08c54..7e59c4c 100644 --- a/examples/pull.py +++ b/examples/pull.py @@ -1,9 +1,9 @@ from tqdm import tqdm -from ollama import pull +import ollama current_digest, bars = '', {} -for progress in pull('llama3.2', stream=True): +for progress in ollama.pull('llama3.2', stream=True): digest = progress.get('digest', '') if digest != current_digest and current_digest in bars: bars[current_digest].close() diff --git a/examples/structured-outputs-image.py b/examples/structured-outputs-image.py index 722a252..a7d4f56 100644 --- a/examples/structured-outputs-image.py +++ b/examples/structured-outputs-image.py @@ -3,7 +3,7 @@ from pydantic import BaseModel -from ollama import chat +import ollama # Define the schema for image objects @@ -32,7 +32,7 @@ class ImageDescription(BaseModel): raise FileNotFoundError(f'Image not found at: {path}') # Set up chat as usual -response = chat( +response = ollama.chat( model='llama3.2-vision', format=ImageDescription.model_json_schema(), # Pass in the schema for the response messages=[ diff --git a/examples/structured-outputs.py b/examples/structured-outputs.py index 4c60d5f..932e273 100644 --- a/examples/structured-outputs.py +++ b/examples/structured-outputs.py @@ -1,6 +1,6 @@ from pydantic import BaseModel -from ollama import chat +import ollama # Define the schema for the response @@ -15,7 +15,7 @@ class FriendList(BaseModel): # schema = {'type': 'object', 'properties': {'friends': {'type': 'array', 'items': {'type': 'object', 'properties': {'name': {'type': 'string'}, 'age': {'type': 'integer'}, 'is_available': {'type': 'boolean'}}, 'required': ['name', 'age', 'is_available']}}}, 'required': ['friends']} -response = chat( +response = ollama.chat( model='llama3.1:8b', messages=[{'role': 'user', 'content': 'I have two friends. The first is Ollama 22 years old busy saving the world, and the second is Alonso 23 years old and wants to hang out. Return a list of friends in JSON format'}], format=FriendList.model_json_schema(), # Use Pydantic to generate the schema or format=schema diff --git a/examples/tools.py b/examples/tools.py index 32a2012..bc000da 100644 --- a/examples/tools.py +++ b/examples/tools.py @@ -1,4 +1,4 @@ -from ollama import ChatResponse, chat +import ollama def add_two_numbers(a: int, b: int) -> int: @@ -47,7 +47,7 @@ def subtract_two_numbers(a: int, b: int) -> int: 'subtract_two_numbers': subtract_two_numbers, } -response: ChatResponse = chat( +response: ollama.ChatResponse = ollama.chat( 'llama3.1', messages=messages, tools=[add_two_numbers, subtract_two_numbers_tool], @@ -72,7 +72,7 @@ def subtract_two_numbers(a: int, b: int) -> int: messages.append({'role': 'tool', 'content': str(output), 'name': tool.function.name}) # Get final response from model with function outputs - final_response = chat('llama3.1', messages=messages) + final_response = ollama.chat('llama3.1', messages=messages) print('Final response:', final_response.message.content) else: