diff --git a/examples/async-chat.py b/examples/async-chat.py index 81a50d9..be10cfc 100644 --- a/examples/async-chat.py +++ b/examples/async-chat.py @@ -1,4 +1,5 @@ import asyncio + from ollama import AsyncClient diff --git a/examples/async-generate.py b/examples/async-generate.py index 0097af1..c3b601a 100644 --- a/examples/async-generate.py +++ b/examples/async-generate.py @@ -1,4 +1,5 @@ import asyncio + import ollama diff --git a/examples/async-structured-outputs.py b/examples/async-structured-outputs.py index b2c8dac..a33c4c9 100644 --- a/examples/async-structured-outputs.py +++ b/examples/async-structured-outputs.py @@ -1,6 +1,8 @@ +import asyncio + from pydantic import BaseModel + from ollama import AsyncClient -import asyncio # Define the schema for the response diff --git a/examples/async-tools.py b/examples/async-tools.py index 50d5568..5578229 100644 --- a/examples/async-tools.py +++ b/examples/async-tools.py @@ -1,6 +1,7 @@ import asyncio -from ollama import ChatResponse + import ollama +from ollama import ChatResponse def add_two_numbers(a: int, b: int) -> int: diff --git a/examples/chat-stream.py b/examples/chat-stream.py index cccab01..3aed84f 100644 --- a/examples/chat-stream.py +++ b/examples/chat-stream.py @@ -1,6 +1,5 @@ from ollama import chat - messages = [ { 'role': 'user', diff --git a/examples/chat-with-history.py b/examples/chat-with-history.py index 39775d6..2e0cc1d 100644 --- a/examples/chat-with-history.py +++ b/examples/chat-with-history.py @@ -1,6 +1,5 @@ from ollama import chat - messages = [ { 'role': 'user', diff --git a/examples/generate-stream.py b/examples/generate-stream.py index 10b7dc7..698a961 100644 --- a/examples/generate-stream.py +++ b/examples/generate-stream.py @@ -1,5 +1,4 @@ from ollama import generate - for part in 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 1a2311d..7a94de4 100644 --- a/examples/generate.py +++ b/examples/generate.py @@ -1,5 +1,4 @@ from ollama import generate - response = generate('llama3.2', 'Why is the sky blue?') print(response['response']) diff --git a/examples/list.py b/examples/list.py index 32d4525..00d6243 100644 --- a/examples/list.py +++ b/examples/list.py @@ -1,5 +1,4 @@ -from ollama import list -from ollama import ListResponse +from ollama import ListResponse, list response: ListResponse = list() diff --git a/examples/multimodal-chat.py b/examples/multimodal-chat.py index 8aff9f4..c1a1859 100644 --- a/examples/multimodal-chat.py +++ b/examples/multimodal-chat.py @@ -1,4 +1,5 @@ from ollama import chat + # from pathlib import Path # Pass in the path to the image diff --git a/examples/multimodal-generate.py b/examples/multimodal-generate.py index 44b3716..643e9aa 100644 --- a/examples/multimodal-generate.py +++ b/examples/multimodal-generate.py @@ -1,10 +1,10 @@ -import sys import random +import sys + import httpx from ollama import generate - latest = httpx.get('https://xkcd.com/info.0.json') latest.raise_for_status() diff --git a/examples/ps.py b/examples/ps.py index 34d5230..5f6965c 100644 --- a/examples/ps.py +++ b/examples/ps.py @@ -1,5 +1,4 @@ -from ollama import ps, pull, chat -from ollama import ProcessResponse +from ollama import ProcessResponse, chat, ps, pull # Ensure at least one model is loaded response = pull('llama3.2', stream=True) diff --git a/examples/pull.py b/examples/pull.py index e24f2e9..bd08c54 100644 --- a/examples/pull.py +++ b/examples/pull.py @@ -1,6 +1,6 @@ from tqdm import tqdm -from ollama import pull +from ollama import pull current_digest, bars = '', {} for progress in pull('llama3.2', stream=True): diff --git a/examples/structured-outputs-image.py b/examples/structured-outputs-image.py index dbcd45f..722a252 100644 --- a/examples/structured-outputs-image.py +++ b/examples/structured-outputs-image.py @@ -1,6 +1,8 @@ from pathlib import Path -from pydantic import BaseModel from typing import Literal + +from pydantic import BaseModel + from ollama import chat diff --git a/examples/structured-outputs.py b/examples/structured-outputs.py index cb28ccd..4c60d5f 100644 --- a/examples/structured-outputs.py +++ b/examples/structured-outputs.py @@ -1,6 +1,7 @@ -from ollama import chat from pydantic import BaseModel +from ollama import chat + # Define the schema for the response class FriendInfo(BaseModel): diff --git a/examples/tools.py b/examples/tools.py index 932355b..32a2012 100644 --- a/examples/tools.py +++ b/examples/tools.py @@ -1,5 +1,4 @@ -from ollama import chat -from ollama import ChatResponse +from ollama import ChatResponse, chat def add_two_numbers(a: int, b: int) -> int: diff --git a/ollama/__init__.py b/ollama/__init__.py index 2517b50..afe8ce7 100644 --- a/ollama/__init__.py +++ b/ollama/__init__.py @@ -1,40 +1,40 @@ -from ollama._client import Client, AsyncClient +from ollama._client import AsyncClient, Client from ollama._types import ( - Options, - Message, - Image, - Tool, - GenerateResponse, ChatResponse, - EmbedResponse, EmbeddingsResponse, - StatusResponse, - ProgressResponse, + EmbedResponse, + GenerateResponse, + Image, ListResponse, - ShowResponse, + Message, + Options, ProcessResponse, + ProgressResponse, RequestError, ResponseError, + ShowResponse, + StatusResponse, + Tool, ) __all__ = [ - 'Client', 'AsyncClient', - 'Options', - 'Message', - 'Image', - 'Tool', - 'GenerateResponse', 'ChatResponse', + 'Client', 'EmbedResponse', 'EmbeddingsResponse', - 'StatusResponse', - 'ProgressResponse', + 'GenerateResponse', + 'Image', 'ListResponse', - 'ShowResponse', + 'Message', + 'Options', 'ProcessResponse', + 'ProgressResponse', 'RequestError', 'ResponseError', + 'ShowResponse', + 'StatusResponse', + 'Tool', ] _client = Client() diff --git a/ollama/_client.py b/ollama/_client.py index 079eda7..9b2dd59 100644 --- a/ollama/_client.py +++ b/ollama/_client.py @@ -1,15 +1,17 @@ -import os +import ipaddress import json +import os import platform -import ipaddress +import sys import urllib.parse +from hashlib import sha256 from os import PathLike from pathlib import Path -from hashlib import sha256 - from typing import ( Any, Callable, + Dict, + List, Literal, Mapping, Optional, @@ -18,21 +20,16 @@ TypeVar, Union, overload, - Dict, - List, ) -import sys - from pydantic.json_schema import JsonSchemaValue - from ollama._utils import convert_function_to_tool if sys.version_info < (3, 9): - from typing import Iterator, AsyncIterator + from typing import AsyncIterator, Iterator else: - from collections.abc import Iterator, AsyncIterator + from collections.abc import AsyncIterator, Iterator from importlib import metadata @@ -46,13 +43,13 @@ from ollama._types import ( ChatRequest, ChatResponse, - CreateRequest, CopyRequest, + CreateRequest, DeleteRequest, - EmbedRequest, - EmbedResponse, EmbeddingsRequest, EmbeddingsResponse, + EmbedRequest, + EmbedResponse, GenerateRequest, GenerateResponse, Image, @@ -70,7 +67,6 @@ Tool, ) - T = TypeVar('T') diff --git a/ollama/_types.py b/ollama/_types.py index 995db14..d70a4ac 100644 --- a/ollama/_types.py +++ b/ollama/_types.py @@ -1,11 +1,8 @@ import json from base64 import b64decode, b64encode -from pathlib import Path from datetime import datetime -from typing import Any, Mapping, Optional, Union, Sequence, Dict, List - -from pydantic.json_schema import JsonSchemaValue -from typing_extensions import Annotated, Literal +from pathlib import Path +from typing import Any, Dict, List, Mapping, Optional, Sequence, Union from pydantic import ( BaseModel, @@ -14,6 +11,8 @@ Field, model_serializer, ) +from pydantic.json_schema import JsonSchemaValue +from typing_extensions import Annotated, Literal class SubscriptableBaseModel(BaseModel): diff --git a/ollama/_utils.py b/ollama/_utils.py index c0b67c9..2ea58ea 100644 --- a/ollama/_utils.py +++ b/ollama/_utils.py @@ -1,10 +1,12 @@ from __future__ import annotations -from collections import defaultdict + import inspect -from typing import Callable, Union import re +from collections import defaultdict +from typing import Callable, Union import pydantic + from ollama._types import Tool diff --git a/pyproject.toml b/pyproject.toml index bd4f6f2..ec94cf8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,10 +40,10 @@ select = [ "E", # pycodestyle errors "F", # pyflakes "B", # bugbear (likely bugs) + "I", # sort imports + "RUF022", # sort __all__ ] ignore = [ - "E111", # indentation not a multiple of four - "E114", # indentation not a multiple of four "E501", # line too long ] diff --git a/tests/test_client.py b/tests/test_client.py index eb18a19..67e87b9 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,14 +1,15 @@ import base64 -import os import json -from pydantic import ValidationError, BaseModel -import pytest +import os import tempfile from pathlib import Path + +import pytest +from pydantic import BaseModel, ValidationError from pytest_httpserver import HTTPServer, URIPattern from werkzeug.wrappers import Request, Response -from ollama._client import Client, AsyncClient, _copy_tools +from ollama._client import AsyncClient, Client, _copy_tools PNG_BASE64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgYGAAAAAEAAH2FzhVAAAAAElFTkSuQmCC' PNG_BYTES = base64.b64decode(PNG_BASE64) @@ -91,7 +92,7 @@ def generate(): @pytest.mark.parametrize('message_format', ('dict', 'pydantic_model')) @pytest.mark.parametrize('file_style', ('path', 'bytes')) def test_client_chat_images(httpserver: HTTPServer, message_format: str, file_style: str, tmp_path): - from ollama._types import Message, Image + from ollama._types import Image, Message httpserver.expect_ordered_request( '/api/chat', diff --git a/tests/test_type_serialization.py b/tests/test_type_serialization.py index 1ecbe08..f458cd2 100644 --- a/tests/test_type_serialization.py +++ b/tests/test_type_serialization.py @@ -1,9 +1,10 @@ +import tempfile from base64 import b64encode from pathlib import Path import pytest + from ollama._types import CreateRequest, Image -import tempfile def test_image_serialization_bytes(): diff --git a/tests/test_utils.py b/tests/test_utils.py index 9fb1e3b..cef7bf2 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -2,7 +2,6 @@ import sys from typing import Dict, List, Mapping, Sequence, Set, Tuple, Union - from ollama._utils import convert_function_to_tool @@ -118,7 +117,7 @@ def all_types( def test_function_docstring_parsing(): - from typing import List, Dict, Any + from typing import Any, Dict, List def func_with_complex_docs(x: int, y: List[str]) -> Dict[str, Any]: """