diff --git a/examples/async-chat-stream/README.md b/examples/async-chat-stream/README.md new file mode 100644 index 0000000..611295a --- /dev/null +++ b/examples/async-chat-stream/README.md @@ -0,0 +1,3 @@ +# async-chat-stream + +This example demonstrates how to create a conversation history using an asynchronous Ollama client and the chat endpoint. The streaming response is outputted to `stdout` as well as a TTS if enabled with `--speak` and available. Supported TTS are `say` on macOS and `espeak` on Linux. diff --git a/examples/async-chat-stream/main.py b/examples/async-chat-stream/main.py new file mode 100644 index 0000000..6504776 --- /dev/null +++ b/examples/async-chat-stream/main.py @@ -0,0 +1,59 @@ +import shutil +import asyncio +import argparse + +import ollama + + +async def speak(speaker, content): + if speaker: + p = await asyncio.create_subprocess_exec(speaker, content) + await p.communicate() + + +async def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--speak', default=False, action='store_true') + args = parser.parse_args() + + speaker = None + if not args.speak: + ... + elif say := shutil.which('say'): + speaker = say + elif (espeak := shutil.which('espeak')) or (espeak := shutil.which('espeak-ng')): + speaker = espeak + + client = ollama.AsyncClient() + + messages = [] + + while True: + if content_in := input('>>> '): + messages.append({'role': 'user', 'content': content_in}) + + content_out = '' + message = {'role': 'assistant', 'content': ''} + async for response in await client.chat(model='mistral', messages=messages, stream=True): + if response['done']: + messages.append(message) + + content = response['message']['content'] + print(content, end='', flush=True) + + content_out += content + if content in ['.', '!', '?', '\n']: + await speak(speaker, content_out) + content_out = '' + + message['content'] += content + + if content_out: + await speak(speaker, content_out) + print() + + +try: + asyncio.run(main()) +except (KeyboardInterrupt, EOFError): + ... diff --git a/examples/simple-chat-stream/main.py b/examples/chat-stream/main.py similarity index 100% rename from examples/simple-chat-stream/main.py rename to examples/chat-stream/main.py diff --git a/examples/simple-chat/main.py b/examples/chat/main.py similarity index 100% rename from examples/simple-chat/main.py rename to examples/chat/main.py diff --git a/examples/simple-fill-in-middle/main.py b/examples/fill-in-middle/main.py similarity index 100% rename from examples/simple-fill-in-middle/main.py rename to examples/fill-in-middle/main.py diff --git a/examples/simple-generate-stream/main.py b/examples/generate-stream/main.py similarity index 100% rename from examples/simple-generate-stream/main.py rename to examples/generate-stream/main.py diff --git a/examples/simple-generate/main.py b/examples/generate/main.py similarity index 100% rename from examples/simple-generate/main.py rename to examples/generate/main.py diff --git a/examples/simple-multimodal/main.py b/examples/multimodal/main.py similarity index 100% rename from examples/simple-multimodal/main.py rename to examples/multimodal/main.py diff --git a/examples/pull-progress/README.md b/examples/pull-progress/README.md new file mode 100644 index 0000000..8a44f60 --- /dev/null +++ b/examples/pull-progress/README.md @@ -0,0 +1,9 @@ +# pull-progress + +This example emulates `ollama pull` using the Python library and [`tqdm`](https://tqdm.github.io/). + +## Setup + +```shell +pip install -r requirements.txt +``` diff --git a/examples/pull-stream-progress/main.py b/examples/pull-progress/main.py similarity index 96% rename from examples/pull-stream-progress/main.py rename to examples/pull-progress/main.py index 02db1af..2be6415 100644 --- a/examples/pull-stream-progress/main.py +++ b/examples/pull-progress/main.py @@ -1,5 +1,3 @@ -# pip install tqdm - from tqdm import tqdm from ollama import pull diff --git a/examples/pull-progress/requirements.txt b/examples/pull-progress/requirements.txt new file mode 100644 index 0000000..ae3df91 --- /dev/null +++ b/examples/pull-progress/requirements.txt @@ -0,0 +1 @@ +tqdm==4.66.1