Deprecated: Creation of dynamic property Timber\Site::$pingback is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Site.php on line 180

Deprecated: Creation of dynamic property Timber\User::$name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/User.php on line 118

Deprecated: Creation of dynamic property Timber\Post::$has_disclaimer is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_lock is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_last is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_meta_box_present is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_send_notification is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_modify_title_and_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_heading is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskw is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_linkdex is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_content_score is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskeywords is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_keywordsynonyms is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_estimated-reading-time-minutes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_wordproof_timestamp is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$fonte is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_fonte is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$fonte_url is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_fonte_url is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_category is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_blz_affiliation_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_content_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_speciali is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_thumbnail_id is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_metadesc is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$response_body is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_indexnow_last_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_0e5d9e0cdcaed40cab752039fc7ad449 is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_time_0e5d9e0cdcaed40cab752039fc7ad449 is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_9b08c0ea7adde878771c2e1c3f142e75 is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_time_9b08c0ea7adde878771c2e1c3f142e75 is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_9a29dd548100adf164e603b65881efb7 is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_time_9a29dd548100adf164e603b65881efb7 is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_date_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$ping_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_password is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$to_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$pinged is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_content_filtered is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$guid is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$menu_order is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_mime_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_count is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$filter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71
Intelligenza artificiale nelle vostre applicazioni con le librerie Python e JavaScript di Ollama

Intelligenza artificiale nelle vostre applicazioni con le librerie Python e JavaScript di Ollama

Quali sono i vantaggi derivanti dall'utilizzo delle nuove librerie Python e JavaScript per interagire con il framework Ollama e i principali modelli generativi open source. L'intelligenza artificiale si lega a doppio filo con qualsiasi applicazione.

La disponibilità di un gran numero di modelli generativi, addestrati utilizzando volumi di testi immensi, permette di sfruttare questi strumenti per offrire funzionalità basate sull’intelligenza artificiale a utenti e clienti. D’altra parte i modelli open source per l’intelligenza artificiale non si contano più: spesso, le licenze con cui sono distribuiti non soltanto ne permettono l’utilizzo per scopi commerciali ma offrono la possibilità di successive ottimizzazioni e addestramenti ex-post.

Ollama è un framework open source che abbiamo presentato qualche tempo fa e che consente di eseguire modelli di intelligenza artificiale basati su LLM (Large Language Models) in ambito locale. Il vantaggio è che con Ollama è possibile mantenere tutti i dati in locale, senza condividerli sul cloud con soggetti terzi, e accedere a una serie di personalizzazioni che, diversamente, risulterebbero inaccessibili.

Librerie Python e JavaScript Ollama per integrare l’intelligenza artificiale nelle proprie applicazioni

Nell’articolo presentato in precedenza, abbiamo visto come usare Ollama in locale, scegliere il modello generativo preferito e inviare i prompt da elaborare.

Ollama supporta diversi modelli generativi, tra cui Llama 2, Mistral, Orca Mini e Vicuna. Sono tutti adatti per l’utilizzo in un ampio ventaglio di applicazioni: generazione automatica di testi e riassunti, gestione di conversazioni articolate, elaborazione di compiti complessi legati al linguaggio. Con Ollama gli utenti possono adattare il funzionamento dei modelli alle loro specifiche esigenze.

Le nuove librerie Python e JavaScript Ollama semplificano notevolmente l’integrazione dei modelli generativi all’interno di applicazioni nuove e preesistenti. Entrambe offrono supporto completo per le funzionalità di Ollama, consentendo agli sviluppatori di sfruttare appieno le capacità del framework open source nelle rispettive applicazioni.

Come usare Ollama con Python

Supponendo di aver sviluppato un’applicazione Python, è possibile arricchirla con funzionalità di intelligenza artificiale semplicemente installando la libreria Ollama e importandola nel sorgente. L’installazione è effettuabile usando il seguente comando

pip install ollama

Nell’esempio di codice Python che segue, si importa dapprima la libreria di Ollama quindi si imposta Llama2 come modello generativo da usare. La funzione ollama.chat include un messaggio che dichiara il ruolo dell’utente (user) e il contenuto della richiesta (il prompt):

import ollama
response = ollama.chat(model='llama2', messages=[
{
'role': 'user',
'content': 'Perché il cielo è azzurro?',
},
])
print(response['message']['content'])

Importare la libreria Ollama in un progetto JavaScript

La stessa cosa può essere effettuata con JavaScript, ricorrendo al gestore di pacchetti npm (Node Package Manager) per l’ambiente di runtime Node.js. Il comando npm install ollama consente di aggiungere la libreria, mentre il codice che segue consente di utilizzarla direttamente in un progetto JavaScript:

import ollama from 'ollama'

const response = await ollama.chat({
model: 'llama2',
messages: [{ role: 'user', content: 'Perché il cielo è azzurro?' }],
})
console.log(response.message.content)

Perché le nuove librerie Ollama supportano l’intero set di caratteristiche del framework

Gli sviluppatori di Ollama fanno notare che le librerie Python e JavaScript consentono di accedere, loro stesse, all’intero ventaglio di caratteristiche del framework.

Innanzi tutto, le applicazioni possono utilizzare lo scambio di dati in streaming con Ollama. Questo termine fa riferimento alla possibilità di trasmettere e ricevere i dati in modo incrementale, cosa utilissima quando si tratta di muovere grandi quantità di informazioni. Nel codice seguente, l’opzione stream=True esprime la volontà di ottenere i risultati in modo incrementale sotto forma di “chunk“:

for chunk in chat('mistral', messages=messages, stream=True):
  print(chunk['message']['content'], end='', flush=True)

Intelligenza artificiale multimodale

Con le librerie Python e JavaScript è tuttavia possibile avviare anche transazioni multimodali con i vari modelli generativi. Questo significa che il sistema è capace di trattare e rispondere a dati provenienti sotto forma di diverse modalità: non soltanto testi, ma ad esempio anche immagini. Nell’esempio seguente, il modello Llava riceve in input un’immagine, un testo e offre un output costruito previa analisi del contenuto del file trasmesso.

with open('image.png', 'rb') as file:
response = ollama.chat(
model='llava',
messages=[
{
'role': 'user',
'content': 'Cosa c'è di strano nell'immagine?',
'images': [file.read()],
},
],
)
print(response['message']['content'])

Autocompletamento e generazione automatica di codice

Le librerie Ollama consentono anche di beneficiare di funzionalità evolute per il completamento del codice: basta servirsi del modello generativo più adatto allo scopo (ad esempio stable-code).

Con apposito codice, è possibile chiedere all’intelligenza artificiale di completare una richiesta predisposta con un qualunque linguaggio di programmazione oppure richiedere esplicitamente di completare un’istruzione.

Creazione di modelli personalizzati

Mettendo sul piatto una sorta di gioco di ruolo, è anche possibile dialogare con il modello generativo impostando delle regole ben precise. Nell’esempio che segue, con una sintassi peraltro molto semplice, si chiede al modello Llama2 di impersonare Mario, protagonista del popolare videogioco progettato e prodotto da Nintendo:

modelfile='''
FROM llama2
SYSTEM Tu sei Mario, dal videogioco Super Mario Bros.
'''

ollama.create(model='example', modelfile=modelfile)

Credit immagine in apertura: iStock.com – Shutthiphong Chandaeng

Ti consigliamo anche

Link copiato negli appunti