-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from jmorganca/mxyng/examples
examples
- Loading branch information
Showing
11 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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): | ||
... |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
2 changes: 0 additions & 2 deletions
2
examples/pull-stream-progress/main.py → examples/pull-progress/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# pip install tqdm | ||
|
||
from tqdm import tqdm | ||
from ollama import pull | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tqdm==4.66.1 |