diff --git a/README.md b/README.md index c39a91e..d62b533 100644 --- a/README.md +++ b/README.md @@ -54,9 +54,10 @@ for chunk in stream: ``` ## Custom client -A custom client can be created with the fields from the [httpx client](https://www.python-httpx.org/api/#client). +A custom client can be created by `Client` or `AsyncClient` from `ollama`. All kwargs are passed into the httpx client. +See accepted parameters for the [httpx client](https://www.python-httpx.org/api/#client) for more information. ```python from ollama import Client diff --git a/examples/async-tools.py b/examples/async-tools.py index 4440cb0..07b3c4a 100644 --- a/examples/async-tools.py +++ b/examples/async-tools.py @@ -44,8 +44,14 @@ def subtract_two_numbers(a: int, b: int) -> int: async def main(): client = ollama.AsyncClient() + prompt = 'What is three plus one?' - print(f'Prompt: {prompt}') + print('Prompt:', prompt) + + available_functions = { + 'add_two_numbers': add_two_numbers, + 'subtract_two_numbers': subtract_two_numbers, + } response: ChatResponse = await client.chat( 'llama3.1', @@ -53,22 +59,16 @@ async def main(): tools=[add_two_numbers, subtract_two_numbers_tool], ) - available_functions = { - 'add_two_numbers': add_two_numbers, - 'subtract_two_numbers': subtract_two_numbers, - } - if response.message.tool_calls: # There may be multiple tool calls in the response for tool in response.message.tool_calls: # Ensure the function is available, and then call it - if tool.function.name in available_functions: - print(f'Calling function {tool.function.name}') - print(f'Arguments: {tool.function.arguments}') - function_to_call = available_functions[tool.function.name] - print(f'Function output: {function_to_call(**tool.function.arguments)}') + if function_to_call := available_functions.get(tool.function.name): + print('Calling function:', tool.function.name) + print('Arguments:', tool.function.arguments) + print('Function output:', function_to_call(**tool.function.arguments)) else: - print(f'Function {tool.function.name} not found') + print('Function', tool.function.name, 'not found') if __name__ == '__main__': diff --git a/examples/chat-with-history.py b/examples/chat-with-history.py index 7787808..e98d15f 100644 --- a/examples/chat-with-history.py +++ b/examples/chat-with-history.py @@ -32,6 +32,7 @@ # Add the response to the messages to maintain the history messages.append( + {'role': 'user', 'content': user_input}, {'role': 'assistant', 'content': response.message.content}, ) print(response.message.content + '\n') diff --git a/examples/ps.py b/examples/ps.py index 7096c33..34d5230 100644 --- a/examples/ps.py +++ b/examples/ps.py @@ -1,4 +1,4 @@ -from ollama import ps, pull +from ollama import ps, pull, chat from ollama import ProcessResponse # Ensure at least one model is loaded @@ -12,14 +12,16 @@ print('\n') +print('Waiting for model to load... \n') +chat(model='llama3.2', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}]) + response: ProcessResponse = ps() for model in response.models: - print(f'Model: {model.model}') - print(f'Digest: {model.digest}') - print(f'Expires at: {model.expires_at}') - print(f'Size: {model.size}') - print(f'Size vram: {model.size_vram}') - print(f'Details: {model.details}') - - print('---' * 10) + print('Model: ', model.model) + print(' Digest: ', model.digest) + print(' Expires at: ', model.expires_at) + print(' Size: ', model.size) + print(' Size vram: ', model.size_vram) + print(' Details: ', model.details) + print('\n') diff --git a/examples/tools.py b/examples/tools.py index 00aeb43..6151cd9 100644 --- a/examples/tools.py +++ b/examples/tools.py @@ -41,7 +41,12 @@ def subtract_two_numbers(a: int, b: int) -> int: } prompt = 'What is three plus one?' -print(f'Prompt: {prompt}') +print('Prompt:', prompt) + +available_functions = { + 'add_two_numbers': add_two_numbers, + 'subtract_two_numbers': subtract_two_numbers, +} response: ChatResponse = chat( 'llama3.1', @@ -49,19 +54,13 @@ def subtract_two_numbers(a: int, b: int) -> int: tools=[add_two_numbers, subtract_two_numbers_tool], ) -available_functions = { - 'add_two_numbers': add_two_numbers, - 'subtract_two_numbers': subtract_two_numbers, -} - if response.message.tool_calls: # There may be multiple tool calls in the response for tool in response.message.tool_calls: # Ensure the function is available, and then call it - if tool.function.name in available_functions: - print(f'Calling function {tool.function.name}') - print(f'Arguments: {tool.function.arguments}') - function_to_call = available_functions[tool.function.name] - print(f'Function output: {function_to_call(**tool.function.arguments)}') + if function_to_call := available_functions.get(tool.function.name): + print('Calling function:', tool.function.name) + print('Arguments:', tool.function.arguments) + print('Function output:', function_to_call(**tool.function.arguments)) else: - print(f'Function {tool.function.name} not found') + print('Function', tool.function.name, 'not found')