Skip to content

Commit

Permalink
Addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ParthSareen committed Nov 21, 2024
1 parent 6c472d2 commit 52543c2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions examples/async-tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,31 @@ 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',
messages=[{'role': 'user', 'content': prompt}],
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__':
Expand Down
1 change: 1 addition & 0 deletions examples/chat-with-history.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
20 changes: 11 additions & 9 deletions examples/ps.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')
23 changes: 11 additions & 12 deletions examples/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,26 @@ 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',
messages=[{'role': 'user', 'content': prompt}],
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')

0 comments on commit 52543c2

Please sign in to comment.