2025-04-01 10:33:50 +02:00
from ollama import Client
client = Client (
2025-04-02 10:15:21 +02:00
host = ' https://ollama.molodetz.nl/ ' ,
2025-04-01 10:33:50 +02:00
headers = { ' x-some-header ' : ' some-value ' }
)
def times_two ( nr_1 : int ) - > int :
return nr_1 * 2
available_functions = {
' times_two ' : times_two
}
messages = [ ]
def chat_stream ( message ) :
if message :
messages . append ( { ' role ' : ' user ' , ' content ' : message } )
content = ' '
2025-04-01 10:56:11 +02:00
for response in client . chat ( model = ' qwen2.5-coder:0.5b ' , messages = messages , stream = True ) :
2025-04-01 10:33:50 +02:00
content + = response . message . content
2025-04-01 10:56:11 +02:00
print ( response . message . content , end = ' ' , flush = True )
2025-04-01 10:33:50 +02:00
messages . append ( { ' role ' : ' assistant ' , ' content ' : content } )
print ( " " )
2025-04-01 10:56:11 +02:00
def chat ( message , stream = False ) :
2025-04-01 10:33:50 +02:00
if stream :
return chat_stream ( message )
if message :
messages . append ( { ' role ' : ' user ' , ' content ' : message } )
response = client . chat ( model = ' qwen2.5:3b ' , messages = messages ,
tools = [ times_two ] )
if response . message . tool_calls :
for tool in response . message . tool_calls :
if function_to_call := available_functions . get ( tool . function . name ) :
print ( ' Calling function: ' , tool . function . name )
print ( ' Arguments: ' , tool . function . arguments )
output = function_to_call ( * * tool . function . arguments )
print ( ' Function output: ' , output )
else :
print ( ' Function ' , tool . function . name , ' not found ' )
if response . message . tool_calls :
messages . append ( response . message )
messages . append ( { ' role ' : ' tool ' , ' content ' : str ( output ) , ' name ' : tool . function . name } )
return chat ( None )
return response . message . content
while True :
chat_stream ( " A farmer and a sheep are standing on one side of a river. There is a boat with enough room for one human and one animal. How can the farmer get across the river with the sheep in the fewest number of trips? " )